TestMulti.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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 p1[sz];
  416. p1[0] = '\0';
  417. struct Stat stat;
  418. int nops = 3;
  419. zoo_op_t ops[nops];
  420. zoo_op_result_t results[nops];
  421. zoo_create_op_init(&ops[0], "/multi7", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  422. zoo_delete_op_init(&ops[1], "/multi7", 0);
  423. zoo_set_op_init(&ops[2], "/multi7", "Y", 1, 0, &stat);
  424. rc = zoo_multi(zk, nops, ops, results);
  425. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  426. // '/multi' should never have been created as entire op should fail
  427. rc = zoo_exists(zk, "/multi7", 0, NULL);
  428. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  429. }
  430. void testAsyncMulti() {
  431. int rc;
  432. watchctx_t ctx;
  433. zhandle_t *zk = createClient(&ctx);
  434. int sz = 512;
  435. char p1[sz], p2[sz], p3[sz];
  436. p1[0] = '\0';
  437. p2[0] = '\0';
  438. p3[0] = '\0';
  439. int nops = 3;
  440. zoo_op_t ops[nops];
  441. zoo_op_result_t results[nops];
  442. zoo_create_op_init(&ops[0], "/multi8", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  443. zoo_create_op_init(&ops[1], "/multi8/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
  444. zoo_create_op_init(&ops[2], "/multi8/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz);
  445. rc = zoo_amulti(zk, nops, ops, results, multi_completion_fn, 0);
  446. waitForMultiCompletion(10);
  447. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  448. CPPUNIT_ASSERT(strcmp(p1, "/multi8") == 0);
  449. CPPUNIT_ASSERT(strcmp(p2, "/multi8/a") == 0);
  450. CPPUNIT_ASSERT(strcmp(p3, "/multi8/b") == 0);
  451. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  452. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err);
  453. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2].err);
  454. }
  455. void testMultiFail() {
  456. int rc;
  457. watchctx_t ctx;
  458. zhandle_t *zk = createClient(&ctx);
  459. int sz = 512;
  460. char p1[sz], p2[sz], p3[sz];
  461. p1[0] = '\0';
  462. p2[0] = '\0';
  463. p3[0] = '\0';
  464. int nops = 3;
  465. zoo_op_t ops[nops];
  466. zoo_op_result_t results[nops];
  467. zoo_create_op_init(&ops[0], "/multi9", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  468. zoo_create_op_init(&ops[1], "/multi9", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
  469. zoo_create_op_init(&ops[2], "/multi9/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz);
  470. rc = zoo_multi(zk, nops, ops, results);
  471. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, rc);
  472. }
  473. /**
  474. * Test basic multi-op check functionality
  475. */
  476. void testCheck() {
  477. int rc;
  478. watchctx_t ctx;
  479. zhandle_t *zk = createClient(&ctx);
  480. int sz = 512;
  481. char p1[sz];
  482. p1[0] = '\0';
  483. rc = zoo_create(zk, "/multi0", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  484. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  485. // Conditionally create /multi0/a' only if '/multi0' at version 0
  486. int nops = 2;
  487. zoo_op_t ops[nops];
  488. zoo_op_result_t results[nops];
  489. zoo_check_op_init(&ops[0], "/multi0", 0);
  490. zoo_create_op_init(&ops[1], "/multi0/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  491. rc = zoo_multi(zk, nops, ops, results);
  492. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  493. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  494. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err);
  495. // '/multi0/a' should have been created as it passed version check
  496. rc = zoo_exists(zk, "/multi0/a", 0, NULL);
  497. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  498. // Only create '/multi0/b' if '/multi0' at version 10 (which it's not)
  499. zoo_op_t ops2[nops];
  500. zoo_check_op_init(&ops2[0], "/multi0", 10);
  501. zoo_create_op_init(&ops2[1], "/multi0/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  502. rc = zoo_multi(zk, nops, ops2, results);
  503. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, rc);
  504. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, results[0].err);
  505. CPPUNIT_ASSERT_EQUAL((int)ZRUNTIMEINCONSISTENCY, results[1].err);
  506. // '/multi0/b' should NOT have been created
  507. rc = zoo_exists(zk, "/multi0/b", 0, NULL);
  508. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  509. }
  510. /**
  511. * Do a multi op inside a watch callback context.
  512. */
  513. static void doMultiInWatch(zhandle_t *zk, int type, int state, const char *path, void *ctx) {
  514. int rc;
  515. int sz = 512;
  516. char p1[sz];
  517. p1[0] = '\0';
  518. struct Stat s1;
  519. int nops = 1;
  520. zoo_op_t ops[nops];
  521. zoo_op_result_t results[nops];
  522. zoo_set_op_init(&ops[0], "/multiwatch", "1", 1, -1, NULL);
  523. rc = zoo_multi(zk, nops, ops, results);
  524. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  525. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  526. memset(p1, '\0', sz);
  527. rc = zoo_get(zk, "/multiwatch", 0, p1, &sz, &s1);
  528. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  529. CPPUNIT_ASSERT_EQUAL(1, sz);
  530. CPPUNIT_ASSERT(strcmp("1", p1) == 0);
  531. count++;
  532. }
  533. /**
  534. * Test multi-op called from a watch
  535. */
  536. void testWatch() {
  537. int rc;
  538. watchctx_t ctx;
  539. zhandle_t *zk = createClient(&ctx);
  540. int sz = 512;
  541. char p1[sz];
  542. p1[0] = '\0';
  543. rc = zoo_create(zk, "/multiwatch", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  544. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  545. // create a watch on node '/multiwatch'
  546. rc = zoo_wget(zk, "/multiwatch", doMultiInWatch, &ctx, p1, &sz, NULL);
  547. // setdata on node '/multiwatch' this should trip the watch
  548. rc = zoo_set(zk, "/multiwatch", NULL, -1, -1);
  549. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  550. // wait for multi completion in doMultiInWatch
  551. waitForMultiCompletion(5);
  552. }
  553. /**
  554. * ZOOKEEPER-1636: If request is too large, the server will cut the
  555. * connection without sending response packet. The client will try to
  556. * process completion on multi request and eventually cause SIGSEGV
  557. */
  558. void testBigAsyncMulti() {
  559. int rc;
  560. int callback_rc = (int) ZOK;
  561. watchctx_t ctx;
  562. zhandle_t *zk = createClient(&ctx);
  563. // The request should be more than 1MB which exceeds the default
  564. // jute.maxbuffer and causes the server to drop client connection
  565. const int iteration = 500;
  566. const int type_count = 3;
  567. const int nops = iteration * type_count;
  568. char buff[1024];
  569. zoo_op_result_t results[nops];
  570. zoo_op_t ops[nops];
  571. struct Stat* s[nops];
  572. int index = 0;
  573. // Test that we deliver error to 3 types of sub-request
  574. for (int i = 0; i < iteration; ++i) {
  575. zoo_set_op_init(&ops[index++], "/x", buff, sizeof(buff), -1, s[i]);
  576. zoo_create_op_init(&ops[index++], "/x", buff, sizeof(buff),
  577. &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, NULL, 0);
  578. zoo_delete_op_init(&ops[index++], "/x", -1);
  579. }
  580. rc = zoo_amulti(zk, nops, ops, results, multi_completion_fn_rc,
  581. + &callback_rc);
  582. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  583. waitForMultiCompletion(10);
  584. // With the bug, we will get SIGSEGV before reaching this point
  585. CPPUNIT_ASSERT_EQUAL((int) ZCONNECTIONLOSS, callback_rc);
  586. // Make sure that all sub-request completions get processed
  587. for (int i = 0; i < nops; ++i) {
  588. CPPUNIT_ASSERT_EQUAL((int) ZCONNECTIONLOSS, results[i].err);
  589. }
  590. // The handle should be able to recover itself.
  591. ctx.waitForConnected(zk);
  592. // Try to submit another async request to see if it get processed
  593. // correctly
  594. rc = zoo_acreate(zk, "/target", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0,
  595. create_completion_fn_rc, &callback_rc);
  596. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  597. waitForMultiCompletion(10);
  598. CPPUNIT_ASSERT_EQUAL((int) ZOK, callback_rc);
  599. }
  600. /**
  601. * ZOOKEEPER-1624: PendingChanges of create sequential node request didn't
  602. * get rollbacked correctly when multi-op failed. This caused
  603. * create sequential node request in subsequent multi-op to failed because
  604. * sequential node name generation is incorrect.
  605. *
  606. * The check is to make sure that each request in multi-op failed with
  607. * the correct reason.
  608. */
  609. void testSequentialNodeCreateInAsyncMulti() {
  610. int rc;
  611. watchctx_t ctx;
  612. zhandle_t *zk = createClient(&ctx);
  613. int iteration = 4;
  614. int nops = 2;
  615. zoo_op_result_t results[iteration][nops];
  616. zoo_op_t ops[nops];
  617. zoo_create_op_init(&ops[0], "/node-", "", 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, NULL, 0);
  618. zoo_create_op_init(&ops[1], "/dup", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  619. for (int i = 0; i < iteration ; ++i) {
  620. rc = zoo_amulti(zk, nops, ops, results[i], multi_completion_fn_no_assert, 0);
  621. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  622. }
  623. waitForMultiCompletion(10);
  624. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0][0].err);
  625. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1][0].err);
  626. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2][0].err);
  627. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[3][0].err);
  628. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0][1].err);
  629. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[1][1].err);
  630. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[2][1].err);
  631. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[3][1].err);
  632. resetCounter();
  633. }
  634. };
  635. volatile int Zookeeper_multi::count;
  636. const char Zookeeper_multi::hostPorts[] = "127.0.0.1:22181";
  637. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_multi);
  638. #endif