TestMulti.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <cppunit/extensions/HelperMacros.h>
  19. #include "CppAssertHelper.h"
  20. #include <signal.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <sys/select.h>
  24. #include "CollectionUtil.h"
  25. #include "ThreadingUtil.h"
  26. using namespace Util;
  27. #include "Vector.h"
  28. using namespace std;
  29. #include <cstring>
  30. #include <list>
  31. #include <zookeeper.h>
  32. #include <errno.h>
  33. #include <recordio.h>
  34. #include "Util.h"
  35. #ifdef THREADED
  36. static void yield(zhandle_t *zh, int i)
  37. {
  38. sleep(i);
  39. }
  40. #else
  41. static void yield(zhandle_t *zh, int seconds)
  42. {
  43. int fd;
  44. int interest;
  45. int events;
  46. struct timeval tv;
  47. time_t expires = time(0) + seconds;
  48. time_t timeLeft = seconds;
  49. fd_set rfds, wfds, efds;
  50. FD_ZERO(&rfds);
  51. FD_ZERO(&wfds);
  52. FD_ZERO(&efds);
  53. while(timeLeft >= 0) {
  54. zookeeper_interest(zh, &fd, &interest, &tv);
  55. if (fd != -1) {
  56. if (interest&ZOOKEEPER_READ) {
  57. FD_SET(fd, &rfds);
  58. } else {
  59. FD_CLR(fd, &rfds);
  60. }
  61. if (interest&ZOOKEEPER_WRITE) {
  62. FD_SET(fd, &wfds);
  63. } else {
  64. FD_CLR(fd, &wfds);
  65. }
  66. } else {
  67. fd = 0;
  68. }
  69. FD_SET(0, &rfds);
  70. if (tv.tv_sec > timeLeft) {
  71. tv.tv_sec = timeLeft;
  72. }
  73. select(fd+1, &rfds, &wfds, &efds, &tv);
  74. timeLeft = expires - time(0);
  75. events = 0;
  76. if (FD_ISSET(fd, &rfds)) {
  77. events |= ZOOKEEPER_READ;
  78. }
  79. if (FD_ISSET(fd, &wfds)) {
  80. events |= ZOOKEEPER_WRITE;
  81. }
  82. zookeeper_process(zh, events);
  83. }
  84. }
  85. #endif
  86. typedef struct evt {
  87. string path;
  88. int type;
  89. } evt_t;
  90. typedef struct watchCtx {
  91. private:
  92. list<evt_t> events;
  93. watchCtx(const watchCtx&);
  94. watchCtx& operator=(const watchCtx&);
  95. public:
  96. bool connected;
  97. zhandle_t *zh;
  98. Mutex mutex;
  99. watchCtx() {
  100. connected = false;
  101. zh = 0;
  102. }
  103. ~watchCtx() {
  104. if (zh) {
  105. zookeeper_close(zh);
  106. zh = 0;
  107. }
  108. }
  109. evt_t getEvent() {
  110. evt_t evt;
  111. mutex.acquire();
  112. CPPUNIT_ASSERT( events.size() > 0);
  113. evt = events.front();
  114. events.pop_front();
  115. mutex.release();
  116. return evt;
  117. }
  118. int countEvents() {
  119. int count;
  120. mutex.acquire();
  121. count = events.size();
  122. mutex.release();
  123. return count;
  124. }
  125. void putEvent(evt_t evt) {
  126. mutex.acquire();
  127. events.push_back(evt);
  128. mutex.release();
  129. }
  130. bool waitForConnected(zhandle_t *zh) {
  131. time_t expires = time(0) + 10;
  132. while(!connected && time(0) < expires) {
  133. yield(zh, 1);
  134. }
  135. return connected;
  136. }
  137. bool waitForDisconnected(zhandle_t *zh) {
  138. time_t expires = time(0) + 15;
  139. while(connected && time(0) < expires) {
  140. yield(zh, 1);
  141. }
  142. return !connected;
  143. }
  144. } watchctx_t;
  145. #ifdef THREADED
  146. class Zookeeper_multi : public CPPUNIT_NS::TestFixture
  147. {
  148. CPPUNIT_TEST_SUITE(Zookeeper_multi);
  149. //FIXME: None of these tests pass in single-threaded mode. It seems to be a
  150. //flaw in the test suite setup.
  151. CPPUNIT_TEST(testCreate);
  152. CPPUNIT_TEST(testCreateDelete);
  153. CPPUNIT_TEST(testInvalidVersion);
  154. CPPUNIT_TEST(testNestedCreate);
  155. CPPUNIT_TEST(testSetData);
  156. CPPUNIT_TEST(testUpdateConflict);
  157. CPPUNIT_TEST(testDeleteUpdateConflict);
  158. CPPUNIT_TEST(testAsyncMulti);
  159. CPPUNIT_TEST(testMultiFail);
  160. CPPUNIT_TEST(testCheck);
  161. CPPUNIT_TEST(testWatch);
  162. CPPUNIT_TEST(testSequentialNodeCreateInAsyncMulti);
  163. CPPUNIT_TEST(testBigAsyncMulti);
  164. CPPUNIT_TEST_SUITE_END();
  165. static void watcher(zhandle_t *, int type, int state, const char *path,void*v){
  166. watchctx_t *ctx = (watchctx_t*)v;
  167. if (state == ZOO_CONNECTED_STATE) {
  168. ctx->connected = true;
  169. } else {
  170. ctx->connected = false;
  171. }
  172. if (type != ZOO_SESSION_EVENT) {
  173. evt_t evt;
  174. evt.path = path;
  175. evt.type = type;
  176. ctx->putEvent(evt);
  177. }
  178. }
  179. static const char hostPorts[];
  180. const char *getHostPorts() {
  181. return hostPorts;
  182. }
  183. zhandle_t *createClient(watchctx_t *ctx) {
  184. return createClient(hostPorts, ctx);
  185. }
  186. zhandle_t *createClient(const char *hp, watchctx_t *ctx) {
  187. zhandle_t *zk = zookeeper_init(hp, watcher, 10000, 0, ctx, 0);
  188. ctx->zh = zk;
  189. CPPUNIT_ASSERT_EQUAL(true, ctx->waitForConnected(zk));
  190. return zk;
  191. }
  192. FILE *logfile;
  193. public:
  194. Zookeeper_multi() {
  195. logfile = openlogfile("Zookeeper_multi");
  196. }
  197. ~Zookeeper_multi() {
  198. if (logfile) {
  199. fflush(logfile);
  200. fclose(logfile);
  201. logfile = 0;
  202. }
  203. }
  204. void setUp()
  205. {
  206. zoo_set_log_stream(logfile);
  207. }
  208. void tearDown()
  209. {
  210. }
  211. static volatile int count;
  212. static void multi_completion_fn(int rc, const void *data) {
  213. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  214. count++;
  215. }
  216. static void multi_completion_fn_no_assert(int rc, const void *data) {
  217. count++;
  218. }
  219. static void multi_completion_fn_rc(int rc, const void *data) {
  220. count++;
  221. *((int*) data) = rc;
  222. }
  223. static void create_completion_fn_rc(int rc, const char* value, const void *data) {
  224. count++;
  225. *((int*) data) = rc;
  226. }
  227. static void waitForMultiCompletion(int seconds) {
  228. time_t expires = time(0) + seconds;
  229. while(count == 0 && time(0) < expires) {
  230. sleep(1);
  231. }
  232. count--;
  233. }
  234. static void resetCounter() {
  235. count = 0;
  236. }
  237. /**
  238. * Test basic multi-op create functionality
  239. */
  240. void testCreate() {
  241. int rc;
  242. watchctx_t ctx;
  243. zhandle_t *zk = createClient(&ctx);
  244. int sz = 512;
  245. char p1[sz];
  246. char p2[sz];
  247. char p3[sz];
  248. p1[0] = p2[0] = p3[0] = '\0';
  249. int nops = 3 ;
  250. zoo_op_t ops[nops];
  251. zoo_op_result_t results[nops];
  252. zoo_create_op_init(&ops[0], "/multi1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  253. zoo_create_op_init(&ops[1], "/multi1/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
  254. zoo_create_op_init(&ops[2], "/multi1/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz);
  255. rc = zoo_multi(zk, nops, ops, results);
  256. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  257. CPPUNIT_ASSERT(strcmp(p1, "/multi1") == 0);
  258. CPPUNIT_ASSERT(strcmp(p2, "/multi1/a") == 0);
  259. CPPUNIT_ASSERT(strcmp(p3, "/multi1/b") == 0);
  260. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  261. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err);
  262. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2].err);
  263. }
  264. /**
  265. * Test create followed by delete
  266. */
  267. void testCreateDelete() {
  268. int rc;
  269. watchctx_t ctx;
  270. zhandle_t *zk = createClient(&ctx);
  271. int sz = 512;
  272. char p1[sz];
  273. p1[0] = '\0';
  274. int nops = 2 ;
  275. zoo_op_t ops[nops];
  276. zoo_op_result_t results[nops];
  277. zoo_create_op_init(&ops[0], "/multi2", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  278. zoo_delete_op_init(&ops[1], "/multi2", 0);
  279. rc = zoo_multi(zk, nops, ops, results);
  280. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  281. // '/multi2' should have been deleted
  282. rc = zoo_exists(zk, "/multi2", 0, NULL);
  283. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  284. }
  285. /**
  286. * Test invalid versions
  287. */
  288. void testInvalidVersion() {
  289. int rc;
  290. watchctx_t ctx;
  291. zhandle_t *zk = createClient(&ctx);
  292. int nops = 4;
  293. zoo_op_t ops[nops];
  294. zoo_op_result_t results[nops];
  295. zoo_create_op_init(&ops[0], "/multi3", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  296. zoo_delete_op_init(&ops[1], "/multi3", 1);
  297. zoo_create_op_init(&ops[2], "/multi3", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  298. zoo_create_op_init(&ops[3], "/multi3/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  299. rc = zoo_multi(zk, nops, ops, results);
  300. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, rc);
  301. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  302. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, results[1].err);
  303. CPPUNIT_ASSERT_EQUAL((int)ZRUNTIMEINCONSISTENCY, results[2].err);
  304. CPPUNIT_ASSERT_EQUAL((int)ZRUNTIMEINCONSISTENCY, results[3].err);
  305. }
  306. /**
  307. * Test nested creates that rely on state in earlier op in multi
  308. */
  309. void testNestedCreate() {
  310. int rc;
  311. watchctx_t ctx;
  312. zhandle_t *zk = createClient(&ctx);
  313. int sz = 512;
  314. char p1[sz];
  315. p1[0] = '\0';
  316. int nops = 6;
  317. zoo_op_t ops[nops];
  318. zoo_op_result_t results[nops];
  319. /* Create */
  320. zoo_create_op_init(&ops[0], "/multi4", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  321. zoo_create_op_init(&ops[1], "/multi4/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  322. zoo_create_op_init(&ops[2], "/multi4/a/1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  323. /* Delete */
  324. zoo_delete_op_init(&ops[3], "/multi4/a/1", 0);
  325. zoo_delete_op_init(&ops[4], "/multi4/a", 0);
  326. zoo_delete_op_init(&ops[5], "/multi4", 0);
  327. rc = zoo_multi(zk, nops, ops, results);
  328. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  329. // Verify tree deleted
  330. rc = zoo_exists(zk, "/multi4/a/1", 0, NULL);
  331. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  332. rc = zoo_exists(zk, "/multi4/a", 0, NULL);
  333. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  334. rc = zoo_exists(zk, "/multi4", 0, NULL);
  335. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  336. }
  337. /**
  338. * Test setdata functionality
  339. */
  340. void testSetData() {
  341. int rc;
  342. watchctx_t ctx;
  343. zhandle_t *zk = createClient(&ctx);
  344. int sz = 512;
  345. struct Stat s1;
  346. char buf[sz];
  347. int blen = sz ;
  348. char p1[sz], p2[sz];
  349. int nops = 2;
  350. zoo_op_t ops[nops];
  351. zoo_op_result_t results[nops];
  352. zoo_create_op_init(&ops[0], "/multi5", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  353. zoo_create_op_init(&ops[1], "/multi5/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
  354. rc = zoo_multi(zk, nops, ops, results);
  355. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  356. yield(zk, 5);
  357. zoo_op_t setdata_ops[nops];
  358. zoo_op_result_t setdata_results[nops];
  359. zoo_set_op_init(&setdata_ops[0], "/multi5", "1", 1, 0, &s1);
  360. zoo_set_op_init(&setdata_ops[1], "/multi5/a", "2", 1, 0, &s1);
  361. rc = zoo_multi(zk, nops, setdata_ops, setdata_results);
  362. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  363. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  364. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err);
  365. memset(buf, '\0', blen);
  366. rc = zoo_get(zk, "/multi5", 0, buf, &blen, &s1);
  367. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  368. CPPUNIT_ASSERT_EQUAL(1, blen);
  369. CPPUNIT_ASSERT(strcmp("1", buf) == 0);
  370. memset(buf, '\0', blen);
  371. rc = zoo_get(zk, "/multi5/a", 0, buf, &blen, &s1);
  372. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  373. CPPUNIT_ASSERT_EQUAL(1, blen);
  374. CPPUNIT_ASSERT(strcmp("2", buf) == 0);
  375. }
  376. /**
  377. * Test update conflicts
  378. */
  379. void testUpdateConflict() {
  380. int rc;
  381. watchctx_t ctx;
  382. zhandle_t *zk = createClient(&ctx);
  383. int sz = 512;
  384. char buf[sz];
  385. int blen = sz;
  386. char p1[sz];
  387. p1[0] = '\0';
  388. struct Stat s1;
  389. int nops = 3;
  390. zoo_op_t ops[nops];
  391. zoo_op_result_t results[nops];
  392. zoo_create_op_init(&ops[0], "/multi6", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  393. zoo_set_op_init(&ops[1], "/multi6", "X", 1, 0, &s1);
  394. zoo_set_op_init(&ops[2], "/multi6", "Y", 1, 0, &s1);
  395. rc = zoo_multi(zk, nops, ops, results);
  396. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, rc);
  397. //Updating version solves conflict -- order matters
  398. ops[2].set_op.version = 1;
  399. rc = zoo_multi(zk, nops, ops, results);
  400. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  401. memset(buf, 0, sz);
  402. rc = zoo_get(zk, "/multi6", 0, buf, &blen, &s1);
  403. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  404. CPPUNIT_ASSERT_EQUAL(blen, 1);
  405. CPPUNIT_ASSERT(strncmp(buf, "Y", 1) == 0);
  406. }
  407. /**
  408. * Test delete-update conflicts
  409. */
  410. void testDeleteUpdateConflict() {
  411. int rc;
  412. watchctx_t ctx;
  413. zhandle_t *zk = createClient(&ctx);
  414. int sz = 512;
  415. char buf[sz];
  416. int blen;
  417. char p1[sz];
  418. p1[0] = '\0';
  419. struct Stat stat;
  420. int nops = 3;
  421. zoo_op_t ops[nops];
  422. zoo_op_result_t results[nops];
  423. zoo_create_op_init(&ops[0], "/multi7", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  424. zoo_delete_op_init(&ops[1], "/multi7", 0);
  425. zoo_set_op_init(&ops[2], "/multi7", "Y", 1, 0, &stat);
  426. rc = zoo_multi(zk, nops, ops, results);
  427. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  428. // '/multi' should never have been created as entire op should fail
  429. rc = zoo_exists(zk, "/multi7", 0, NULL);
  430. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  431. }
  432. void testAsyncMulti() {
  433. int rc;
  434. watchctx_t ctx;
  435. zhandle_t *zk = createClient(&ctx);
  436. int sz = 512;
  437. char p1[sz], p2[sz], p3[sz];
  438. p1[0] = '\0';
  439. p2[0] = '\0';
  440. p3[0] = '\0';
  441. int nops = 3;
  442. zoo_op_t ops[nops];
  443. zoo_op_result_t results[nops];
  444. zoo_create_op_init(&ops[0], "/multi8", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  445. zoo_create_op_init(&ops[1], "/multi8/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
  446. zoo_create_op_init(&ops[2], "/multi8/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz);
  447. rc = zoo_amulti(zk, nops, ops, results, multi_completion_fn, 0);
  448. waitForMultiCompletion(10);
  449. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  450. CPPUNIT_ASSERT(strcmp(p1, "/multi8") == 0);
  451. CPPUNIT_ASSERT(strcmp(p2, "/multi8/a") == 0);
  452. CPPUNIT_ASSERT(strcmp(p3, "/multi8/b") == 0);
  453. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  454. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err);
  455. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2].err);
  456. }
  457. void testMultiFail() {
  458. int rc;
  459. watchctx_t ctx;
  460. zhandle_t *zk = createClient(&ctx);
  461. int sz = 512;
  462. char p1[sz], p2[sz], p3[sz];
  463. p1[0] = '\0';
  464. p2[0] = '\0';
  465. p3[0] = '\0';
  466. int nops = 3;
  467. zoo_op_t ops[nops];
  468. zoo_op_result_t results[nops];
  469. zoo_create_op_init(&ops[0], "/multi9", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  470. zoo_create_op_init(&ops[1], "/multi9", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
  471. zoo_create_op_init(&ops[2], "/multi9/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz);
  472. rc = zoo_multi(zk, nops, ops, results);
  473. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, rc);
  474. }
  475. /**
  476. * Test basic multi-op check functionality
  477. */
  478. void testCheck() {
  479. int rc;
  480. watchctx_t ctx;
  481. zhandle_t *zk = createClient(&ctx);
  482. int sz = 512;
  483. char p1[sz];
  484. p1[0] = '\0';
  485. struct Stat s1;
  486. rc = zoo_create(zk, "/multi0", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  487. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  488. // Conditionally create /multi0/a' only if '/multi0' at version 0
  489. int nops = 2;
  490. zoo_op_t ops[nops];
  491. zoo_op_result_t results[nops];
  492. zoo_check_op_init(&ops[0], "/multi0", 0);
  493. zoo_create_op_init(&ops[1], "/multi0/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  494. rc = zoo_multi(zk, nops, ops, results);
  495. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  496. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  497. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err);
  498. // '/multi0/a' should have been created as it passed version check
  499. rc = zoo_exists(zk, "/multi0/a", 0, NULL);
  500. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  501. // Only create '/multi0/b' if '/multi0' at version 10 (which it's not)
  502. zoo_op_t ops2[nops];
  503. zoo_check_op_init(&ops2[0], "/multi0", 10);
  504. zoo_create_op_init(&ops2[1], "/multi0/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  505. rc = zoo_multi(zk, nops, ops2, results);
  506. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, rc);
  507. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, results[0].err);
  508. CPPUNIT_ASSERT_EQUAL((int)ZRUNTIMEINCONSISTENCY, results[1].err);
  509. // '/multi0/b' should NOT have been created
  510. rc = zoo_exists(zk, "/multi0/b", 0, NULL);
  511. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  512. }
  513. /**
  514. * Do a multi op inside a watch callback context.
  515. */
  516. static void doMultiInWatch(zhandle_t *zk, int type, int state, const char *path, void *ctx) {
  517. int rc;
  518. int sz = 512;
  519. char p1[sz];
  520. p1[0] = '\0';
  521. struct Stat s1;
  522. int nops = 1;
  523. zoo_op_t ops[nops];
  524. zoo_op_result_t results[nops];
  525. zoo_set_op_init(&ops[0], "/multiwatch", "1", 1, -1, NULL);
  526. rc = zoo_multi(zk, nops, ops, results);
  527. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  528. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  529. memset(p1, '\0', sz);
  530. rc = zoo_get(zk, "/multiwatch", 0, p1, &sz, &s1);
  531. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  532. CPPUNIT_ASSERT_EQUAL(1, sz);
  533. CPPUNIT_ASSERT(strcmp("1", p1) == 0);
  534. count++;
  535. }
  536. /**
  537. * Test multi-op called from a watch
  538. */
  539. void testWatch() {
  540. int rc;
  541. watchctx_t ctx;
  542. zhandle_t *zk = createClient(&ctx);
  543. int sz = 512;
  544. char p1[sz];
  545. p1[0] = '\0';
  546. rc = zoo_create(zk, "/multiwatch", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  547. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  548. // create a watch on node '/multiwatch'
  549. rc = zoo_wget(zk, "/multiwatch", doMultiInWatch, &ctx, p1, &sz, NULL);
  550. // setdata on node '/multiwatch' this should trip the watch
  551. rc = zoo_set(zk, "/multiwatch", NULL, -1, -1);
  552. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  553. // wait for multi completion in doMultiInWatch
  554. waitForMultiCompletion(5);
  555. }
  556. /**
  557. * ZOOKEEPER-1636: If request is too large, the server will cut the
  558. * connection without sending response packet. The client will try to
  559. * process completion on multi request and eventually cause SIGSEGV
  560. */
  561. void testBigAsyncMulti() {
  562. int rc;
  563. int callback_rc = (int) ZOK;
  564. watchctx_t ctx;
  565. zhandle_t *zk = createClient(&ctx);
  566. // The request should be more than 1MB which exceeds the default
  567. // jute.maxbuffer and causes the server to drop client connection
  568. const int iteration = 500;
  569. const int type_count = 3;
  570. const int nops = iteration * type_count;
  571. char buff[1024];
  572. zoo_op_result_t results[nops];
  573. zoo_op_t ops[nops];
  574. struct Stat* s[nops];
  575. int index = 0;
  576. // Test that we deliver error to 3 types of sub-request
  577. for (int i = 0; i < iteration; ++i) {
  578. zoo_set_op_init(&ops[index++], "/x", buff, sizeof(buff), -1, s[i]);
  579. zoo_create_op_init(&ops[index++], "/x", buff, sizeof(buff),
  580. &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, NULL, 0);
  581. zoo_delete_op_init(&ops[index++], "/x", -1);
  582. }
  583. rc = zoo_amulti(zk, nops, ops, results, multi_completion_fn_rc,
  584. + &callback_rc);
  585. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  586. waitForMultiCompletion(10);
  587. // With the bug, we will get SIGSEGV before reaching this point
  588. CPPUNIT_ASSERT_EQUAL((int) ZCONNECTIONLOSS, callback_rc);
  589. // Make sure that all sub-request completions get processed
  590. for (int i = 0; i < nops; ++i) {
  591. CPPUNIT_ASSERT_EQUAL((int) ZCONNECTIONLOSS, results[i].err);
  592. }
  593. // The handle should be able to recover itself.
  594. ctx.waitForConnected(zk);
  595. // Try to submit another async request to see if it get processed
  596. // correctly
  597. rc = zoo_acreate(zk, "/target", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0,
  598. create_completion_fn_rc, &callback_rc);
  599. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  600. waitForMultiCompletion(10);
  601. CPPUNIT_ASSERT_EQUAL((int) ZOK, callback_rc);
  602. }
  603. /**
  604. * ZOOKEEPER-1624: PendingChanges of create sequential node request didn't
  605. * get rollbacked correctly when multi-op failed. This caused
  606. * create sequential node request in subsequent multi-op to failed because
  607. * sequential node name generation is incorrect.
  608. *
  609. * The check is to make sure that each request in multi-op failed with
  610. * the correct reason.
  611. */
  612. void testSequentialNodeCreateInAsyncMulti() {
  613. int rc;
  614. watchctx_t ctx;
  615. zhandle_t *zk = createClient(&ctx);
  616. int iteration = 4;
  617. int nops = 2;
  618. zoo_op_result_t results[iteration][nops];
  619. zoo_op_t ops[nops];
  620. zoo_create_op_init(&ops[0], "/node-", "", 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, NULL, 0);
  621. zoo_create_op_init(&ops[1], "/dup", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  622. for (int i = 0; i < iteration ; ++i) {
  623. rc = zoo_amulti(zk, nops, ops, results[i], multi_completion_fn_no_assert, 0);
  624. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  625. }
  626. waitForMultiCompletion(10);
  627. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0][0].err);
  628. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1][0].err);
  629. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2][0].err);
  630. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[3][0].err);
  631. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0][1].err);
  632. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[1][1].err);
  633. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[2][1].err);
  634. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[3][1].err);
  635. resetCounter();
  636. }
  637. };
  638. volatile int Zookeeper_multi::count;
  639. const char Zookeeper_multi::hostPorts[] = "127.0.0.1:22181";
  640. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_multi);
  641. #endif