Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions zookeeper-client/zookeeper-client-c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ set(test_sources
tests/TestClientRetry.cc
tests/TestOperations.cc
tests/TestMulti.cc
tests/TestMultiFailure.cc
tests/TestWatchers.cc
tests/TestClient.cc
tests/ZooKeeperQuorumServer.cc
Expand Down
1 change: 1 addition & 0 deletions zookeeper-client/zookeeper-client-c/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ TEST_SOURCES = \
tests/TestClientRetry.cc \
tests/TestOperations.cc \
tests/TestMulti.cc \
tests/TestMultiFailure.cc \
tests/TestWatchers.cc \
tests/TestClient.cc \
tests/ZooKeeperQuorumServer.cc \
Expand Down
15 changes: 11 additions & 4 deletions zookeeper-client/zookeeper-client-c/include/zookeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -1815,9 +1815,14 @@ ZOOAPI int zoo_aset_acl(zhandle_t *zh, const char *path, int version,
* ops supported by a multi op (see \ref zoo_acreate, \ref zoo_adelete, \ref zoo_aset).
* \param data the data that will be passed to the completion routine when
* the function completes.
* \return the return code for the function call. This can be any of the
* values that can be returned by the ops supported by a multi op (see
* \ref zoo_acreate, \ref zoo_adelete, \ref zoo_aset).
* \return ZOK on success or one of the following errcodes on failure:
* ZBADARGUMENTS - invalid input parameters
* ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE
* ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
* ZUNIMPLEMENTED - an operation type is not supported in a multi
* ZSYSTEMERROR - out of memory
* If the call fails, every result slot holds the return code and the
* completion is not called.
*/
ZOOAPI int zoo_amulti(zhandle_t *zh, int count, const zoo_op_t *ops,
zoo_op_result_t *results, void_completion_t, const void *data);
Expand Down Expand Up @@ -2583,7 +2588,9 @@ ZOOAPI int zoo_set_acl(zhandle_t *zh, const char *path, int version,
* \param results an array to hold the results of the operations
* \return the return code for the function call. This can be any of the
* values that can be returned by the ops supported by a multi op (see
* \ref zoo_acreate, \ref zoo_adelete, \ref zoo_aset).
* \ref zoo_acreate, \ref zoo_adelete, \ref zoo_aset), or one of the
* call errors of \ref zoo_amulti. If the multi fails as a whole, for
* example on connection loss, every result slot holds an error code.
*/
ZOOAPI int zoo_multi(zhandle_t *zh, int count, const zoo_op_t *ops, zoo_op_result_t *results);

Expand Down
112 changes: 103 additions & 9 deletions zookeeper-client/zookeeper-client-c/src/zookeeper.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ static const char* format_endpoint_info(const struct sockaddr_storage* ep);
/* deserialize forward declarations */
static void deserialize_response(zhandle_t *zh, int type, int xid, int failed, int rc, completion_list_t *cptr, struct iarchive *ia);
static int deserialize_multi(zhandle_t *zh, int xid, completion_list_t *cptr, struct iarchive *ia);
static void cleanup_failed_multi(zhandle_t *zh, int xid, int rc, completion_list_t *cptr);

/* completion routine forward declarations */
static int add_completion(zhandle_t *zh, int xid, int completion_type,
Expand Down Expand Up @@ -2035,6 +2036,13 @@ void free_completions(zhandle_t *zh,int callCompletion,int reason)
struct sync_completion
*sc = (struct sync_completion*)cptr->data;
sc->rc = reason;
/*
* The caller reads the result array of a multi after the
* wait ends. Fill every slot with the failure code first.
*/
if (cptr->c.type == COMPLETION_MULTI) {
cleanup_failed_multi(zh, cptr->xid, reason, cptr);
}
notify_sync_completion(sc);
zh->outstanding_sync--;
destroy_completion_entry(cptr);
Expand Down Expand Up @@ -3306,16 +3314,47 @@ static void cleanup_failed_multi(zhandle_t *zh, int xid, int rc, completion_list
}
}

/*
* Deliver a Create2Response to a string completion. The server answers
* container creates in a multi with a path and a stat. The sub-operation
* result has no place for the stat, so this function discards it.
*/
static void deserialize_create2_to_string(zhandle_t *zh, int rc,
completion_list_t *cptr, struct iarchive *ia)
{
struct Create2Response res;
const char *client_path;
memset(&res, 0, sizeof(res));
deserialize_Create2Response(ia, "reply", &res);
client_path = sub_string(zh, res.path);
cptr->c.string_result(rc, client_path, cptr->data);
free_duplicate_path(client_path, res.path);
deallocate_Create2Response(&res);
}

/*
* Decode the sub-results of a multi reply. The completion type of each
* sub-operation selects its record format. Two wire types are exceptions.
* An error result has an ErrorResponse, and a container create has a
* Create2Response. Every sub-operation gets a result. This is also true
* when the reply does not match the request.
*/
static int deserialize_multi(zhandle_t *zh, int xid, completion_list_t *cptr, struct iarchive *ia)
{
int rc = 0;
completion_head_t *clist = &cptr->c.clist;
struct MultiHeader mhdr = {0, 0, 0};
assert(clist);
deserialize_MultiHeader(ia, "multiheader", &mhdr);
if (deserialize_MultiHeader(ia, "multiheader", &mhdr) < 0) {
mhdr.done = 1;
}
while (!mhdr.done) {
completion_list_t *entry = dequeue_completion_nolock(clist);
assert(entry);
if (entry == NULL) {
LOG_ERROR(LOGCALLBACK(zh),
"Multi reply xid=%#x has more results than operations", xid);
return ZMARSHALLINGERROR;
}

if (mhdr.type == -1) {
struct ErrorResponse er;
Expand All @@ -3326,14 +3365,27 @@ static int deserialize_multi(zhandle_t *zh, int xid, completion_list_t *cptr, st
}
}

deserialize_response(zh, entry->c.type, xid, mhdr.type == -1, mhdr.err, entry, ia);
deserialize_MultiHeader(ia, "multiheader", &mhdr);
if (mhdr.type == ZOO_CREATE2_OP && entry->c.type == COMPLETION_STRING) {
deserialize_create2_to_string(zh, mhdr.err, entry, ia);
} else {
deserialize_response(zh, entry->c.type, xid, mhdr.type == -1, mhdr.err, entry, ia);
}
if (deserialize_MultiHeader(ia, "multiheader", &mhdr) < 0) {
mhdr.done = 1;
}
//While deserializing the response we must destroy completion entry for each operation in
//the zoo_multi transaction. Otherwise this results in memory leak when client invokes zoo_multi
//operation.
destroy_completion_entry(entry);
}

if (clist->head != NULL) {
LOG_ERROR(LOGCALLBACK(zh),
"Multi reply xid=%#x has fewer results than operations", xid);
cleanup_failed_multi(zh, xid, ZMARSHALLINGERROR, cptr);
rc = ZMARSHALLINGERROR;
}

return rc;
}

Expand Down Expand Up @@ -3880,7 +3932,11 @@ static int do_add_completion(zhandle_t *zh, const void *dc,
if (!c)
return ZSYSTEMERROR;
lock_completion_list(&zh->sent_requests);
if (zh->close_requested != 1) {
/*
* Refuse new requests on a closing or unrecoverable handle. No later
* teardown drains the queue, so a synchronous caller waits forever.
*/
if (zh->close_requested != 1 && !is_unrecoverable(zh)) {
queue_completion_nolock(&zh->sent_requests, c, add_to_front);
if (dc == SYNCHRONOUS_MARKER) {
zh->outstanding_sync++;
Expand Down Expand Up @@ -4830,6 +4886,23 @@ static void op_result_stat_completion(int err, const struct Stat *stat, const vo
}
}

/*
* Fail every result slot of a multi that zoo_amulti() does not submit.
* No reply arrives for such a multi, so the slots get the return code.
*/
static void fail_multi_results(int count, zoo_op_result_t *results, int rc)
{
int i;
if (results == NULL) {
return;
}
for (i = 0; i < count; i++) {
results[i].err = rc;
results[i].value = NULL;
results[i].stat = NULL;
}
}

static int CheckVersionRequest_init(zhandle_t *zh, struct CheckVersionRequest *req,
const char *path, int version)
{
Expand All @@ -4856,6 +4929,11 @@ int zoo_amulti(zhandle_t *zh, int count, const zoo_op_t *ops,
int rc = serialize_RequestHeader(oa, "header", &h);

int index = 0;
if (count > 0 && (ops == NULL || results == NULL)) {
close_buffer_oarchive(&oa, 1);
fail_multi_results(count, results, ZBADARGUMENTS);
return ZBADARGUMENTS;
}
for (index=0; index < count; index++) {
const zoo_op_t *op = ops+index;
zoo_op_result_t *result = results+index;
Expand Down Expand Up @@ -4973,10 +5051,18 @@ int zoo_amulti(zhandle_t *zh, int count, const zoo_op_t *ops,
LOG_DEBUG(LOGCALLBACK(zh), "Sending multi request xid=%#x with %d subrequests to %s",
h.xid, index, zoo_get_current_server(zh));

if (rc == ZUNIMPLEMENTED) {
return rc;
/*
* A failed call-time check returns its own code. A serializer error
* becomes ZMARSHALLINGERROR.
*/
if (rc != ZUNIMPLEMENTED && rc != ZBADARGUMENTS && rc != ZINVALIDSTATE
&& rc != ZSYSTEMERROR) {
rc = nonblocking_send(zh, rc);
}
return nonblocking_send(zh, rc);
if (rc != ZOK) {
fail_multi_results(count, results, rc);
}
return rc;
}

typedef union WatchesRequest WatchesRequest;
Expand Down Expand Up @@ -5528,7 +5614,15 @@ static void process_sync_completion(zhandle_t *zh,
case COMPLETION_VOID:
break;
case COMPLETION_MULTI:
sc->rc = deserialize_multi(zh, cptr->xid, cptr, ia);
/*
* A reply with an error in its header has no multi body. Fail
* every sub-operation with the header error.
*/
if (sc->rc == 0) {
sc->rc = deserialize_multi(zh, cptr->xid, cptr, ia);
} else {
cleanup_failed_multi(zh, cptr->xid, sc->rc, cptr);
}
break;
default:
LOG_DEBUG(LOGCALLBACK(zh), "Unsupported completion type=%d", cptr->c.type);
Expand Down
43 changes: 43 additions & 0 deletions zookeeper-client/zookeeper-client-c/tests/TestMulti.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class Zookeeper_multi : public CPPUNIT_NS::TestFixture
//flaw in the test suite setup.
CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testCreateDelete);
CPPUNIT_TEST(testCreateContainer);
CPPUNIT_TEST(testInvalidVersion);
CPPUNIT_TEST(testNestedCreate);
CPPUNIT_TEST(testSetData);
Expand Down Expand Up @@ -329,6 +330,48 @@ class Zookeeper_multi : public CPPUNIT_NS::TestFixture
CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
}

/**
* Test container create followed by other operations. The server
* answers a container create with a path and a stat. The client must
* decode the sub-results that follow it correctly.
*/
void testCreateContainer() {
int rc;
watchctx_t ctx;
zhandle_t *zk = createClient(&ctx);
int sz = 512;
char p1[sz];
char p2[sz];
p1[0] = p2[0] = '\0';
struct Stat s1;
memset(&s1, 0, sizeof(s1));
int nops = 4;
zoo_op_t ops[nops];
zoo_op_result_t results[nops];

zoo_create_op_init(&ops[0], "/multiContainer", "", 0,
&ZOO_OPEN_ACL_UNSAFE, ZOO_CONTAINER, p1, sz);
zoo_create_op_init(&ops[1], "/multiContainer/a", "", 0,
&ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
zoo_set_op_init(&ops[2], "/multiContainer/a", "x", 1, 0, &s1);
zoo_check_op_init(&ops[3], "/multiContainer", 0);

rc = zoo_multi(zk, nops, ops, results);
CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
for (int i = 0; i < nops; i++) {
CPPUNIT_ASSERT_EQUAL((int)ZOK, results[i].err);
}
CPPUNIT_ASSERT(strcmp(p1, "/multiContainer") == 0);
CPPUNIT_ASSERT(strcmp(p2, "/multiContainer/a") == 0);
CPPUNIT_ASSERT_EQUAL(1, s1.version);
CPPUNIT_ASSERT_EQUAL(1, s1.dataLength);

zoo_delete_op_init(&ops[0], "/multiContainer/a", -1);
zoo_delete_op_init(&ops[1], "/multiContainer", -1);
rc = zoo_multi(zk, 2, ops, results);
CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
}

/**
* Test invalid versions
*/
Expand Down
Loading