TestMulti.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. int rc;
  48. time_t expires = time(0) + seconds;
  49. time_t timeLeft = seconds;
  50. fd_set rfds, wfds, efds;
  51. FD_ZERO(&rfds);
  52. FD_ZERO(&wfds);
  53. FD_ZERO(&efds);
  54. while(timeLeft >= 0) {
  55. zookeeper_interest(zh, &fd, &interest, &tv);
  56. if (fd != -1) {
  57. if (interest&ZOOKEEPER_READ) {
  58. FD_SET(fd, &rfds);
  59. } else {
  60. FD_CLR(fd, &rfds);
  61. }
  62. if (interest&ZOOKEEPER_WRITE) {
  63. FD_SET(fd, &wfds);
  64. } else {
  65. FD_CLR(fd, &wfds);
  66. }
  67. } else {
  68. fd = 0;
  69. }
  70. FD_SET(0, &rfds);
  71. if (tv.tv_sec > timeLeft) {
  72. tv.tv_sec = timeLeft;
  73. }
  74. rc = select(fd+1, &rfds, &wfds, &efds, &tv);
  75. timeLeft = expires - time(0);
  76. events = 0;
  77. if (FD_ISSET(fd, &rfds)) {
  78. events |= ZOOKEEPER_READ;
  79. }
  80. if (FD_ISSET(fd, &wfds)) {
  81. events |= ZOOKEEPER_WRITE;
  82. }
  83. zookeeper_process(zh, events);
  84. }
  85. }
  86. #endif
  87. typedef struct evt {
  88. string path;
  89. int type;
  90. } evt_t;
  91. typedef struct watchCtx {
  92. private:
  93. list<evt_t> events;
  94. watchCtx(const watchCtx&);
  95. watchCtx& operator=(const watchCtx&);
  96. public:
  97. bool connected;
  98. zhandle_t *zh;
  99. Mutex mutex;
  100. watchCtx() {
  101. connected = false;
  102. zh = 0;
  103. }
  104. ~watchCtx() {
  105. if (zh) {
  106. zookeeper_close(zh);
  107. zh = 0;
  108. }
  109. }
  110. evt_t getEvent() {
  111. evt_t evt;
  112. mutex.acquire();
  113. CPPUNIT_ASSERT( events.size() > 0);
  114. evt = events.front();
  115. events.pop_front();
  116. mutex.release();
  117. return evt;
  118. }
  119. int countEvents() {
  120. int count;
  121. mutex.acquire();
  122. count = events.size();
  123. mutex.release();
  124. return count;
  125. }
  126. void putEvent(evt_t evt) {
  127. mutex.acquire();
  128. events.push_back(evt);
  129. mutex.release();
  130. }
  131. bool waitForConnected(zhandle_t *zh) {
  132. time_t expires = time(0) + 10;
  133. while(!connected && time(0) < expires) {
  134. yield(zh, 1);
  135. }
  136. return connected;
  137. }
  138. bool waitForDisconnected(zhandle_t *zh) {
  139. time_t expires = time(0) + 15;
  140. while(connected && time(0) < expires) {
  141. yield(zh, 1);
  142. }
  143. return !connected;
  144. }
  145. } watchctx_t;
  146. #ifdef THREADED
  147. class Zookeeper_multi : public CPPUNIT_NS::TestFixture
  148. {
  149. CPPUNIT_TEST_SUITE(Zookeeper_multi);
  150. //FIXME: None of these tests pass in single-threaded mode. It seems to be a
  151. //flaw in the test suite setup.
  152. CPPUNIT_TEST(testCreate);
  153. CPPUNIT_TEST(testCreateDelete);
  154. CPPUNIT_TEST(testInvalidVersion);
  155. CPPUNIT_TEST(testNestedCreate);
  156. CPPUNIT_TEST(testSetData);
  157. CPPUNIT_TEST(testUpdateConflict);
  158. CPPUNIT_TEST(testDeleteUpdateConflict);
  159. CPPUNIT_TEST(testAsyncMulti);
  160. CPPUNIT_TEST(testMultiFail);
  161. CPPUNIT_TEST(testCheck);
  162. CPPUNIT_TEST(testWatch);
  163. CPPUNIT_TEST(testSequentialNodeCreateInAsyncMulti);
  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 waitForMultiCompletion(int seconds) {
  220. time_t expires = time(0) + seconds;
  221. while(count == 0 && time(0) < expires) {
  222. sleep(1);
  223. }
  224. count--;
  225. }
  226. static void resetCounter() {
  227. count = 0;
  228. }
  229. /**
  230. * Test basic multi-op create functionality
  231. */
  232. void testCreate() {
  233. int rc;
  234. watchctx_t ctx;
  235. zhandle_t *zk = createClient(&ctx);
  236. int sz = 512;
  237. char p1[sz];
  238. char p2[sz];
  239. char p3[sz];
  240. p1[0] = p2[0] = p3[0] = '\0';
  241. int nops = 3 ;
  242. zoo_op_t ops[nops];
  243. zoo_op_result_t results[nops];
  244. zoo_create_op_init(&ops[0], "/multi1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  245. zoo_create_op_init(&ops[1], "/multi1/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
  246. zoo_create_op_init(&ops[2], "/multi1/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz);
  247. rc = zoo_multi(zk, nops, ops, results);
  248. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  249. CPPUNIT_ASSERT(strcmp(p1, "/multi1") == 0);
  250. CPPUNIT_ASSERT(strcmp(p2, "/multi1/a") == 0);
  251. CPPUNIT_ASSERT(strcmp(p3, "/multi1/b") == 0);
  252. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  253. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err);
  254. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2].err);
  255. }
  256. /**
  257. * Test create followed by delete
  258. */
  259. void testCreateDelete() {
  260. int rc;
  261. watchctx_t ctx;
  262. zhandle_t *zk = createClient(&ctx);
  263. int sz = 512;
  264. char p1[sz];
  265. p1[0] = '\0';
  266. int nops = 2 ;
  267. zoo_op_t ops[nops];
  268. zoo_op_result_t results[nops];
  269. zoo_create_op_init(&ops[0], "/multi2", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  270. zoo_delete_op_init(&ops[1], "/multi2", 0);
  271. rc = zoo_multi(zk, nops, ops, results);
  272. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  273. // '/multi2' should have been deleted
  274. rc = zoo_exists(zk, "/multi2", 0, NULL);
  275. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  276. }
  277. /**
  278. * Test invalid versions
  279. */
  280. void testInvalidVersion() {
  281. int rc;
  282. watchctx_t ctx;
  283. zhandle_t *zk = createClient(&ctx);
  284. int nops = 4;
  285. zoo_op_t ops[nops];
  286. zoo_op_result_t results[nops];
  287. zoo_create_op_init(&ops[0], "/multi3", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  288. zoo_delete_op_init(&ops[1], "/multi3", 1);
  289. zoo_create_op_init(&ops[2], "/multi3", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  290. zoo_create_op_init(&ops[3], "/multi3/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  291. rc = zoo_multi(zk, nops, ops, results);
  292. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, rc);
  293. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  294. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, results[1].err);
  295. CPPUNIT_ASSERT_EQUAL((int)ZRUNTIMEINCONSISTENCY, results[2].err);
  296. CPPUNIT_ASSERT_EQUAL((int)ZRUNTIMEINCONSISTENCY, results[3].err);
  297. }
  298. /**
  299. * Test nested creates that rely on state in earlier op in multi
  300. */
  301. void testNestedCreate() {
  302. int rc;
  303. watchctx_t ctx;
  304. zhandle_t *zk = createClient(&ctx);
  305. int sz = 512;
  306. char p1[sz];
  307. p1[0] = '\0';
  308. int nops = 6;
  309. zoo_op_t ops[nops];
  310. zoo_op_result_t results[nops];
  311. /* Create */
  312. zoo_create_op_init(&ops[0], "/multi4", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  313. zoo_create_op_init(&ops[1], "/multi4/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  314. zoo_create_op_init(&ops[2], "/multi4/a/1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  315. /* Delete */
  316. zoo_delete_op_init(&ops[3], "/multi4/a/1", 0);
  317. zoo_delete_op_init(&ops[4], "/multi4/a", 0);
  318. zoo_delete_op_init(&ops[5], "/multi4", 0);
  319. rc = zoo_multi(zk, nops, ops, results);
  320. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  321. // Verify tree deleted
  322. rc = zoo_exists(zk, "/multi4/a/1", 0, NULL);
  323. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  324. rc = zoo_exists(zk, "/multi4/a", 0, NULL);
  325. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  326. rc = zoo_exists(zk, "/multi4", 0, NULL);
  327. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  328. }
  329. /**
  330. * Test setdata functionality
  331. */
  332. void testSetData() {
  333. int rc;
  334. watchctx_t ctx;
  335. zhandle_t *zk = createClient(&ctx);
  336. int sz = 512;
  337. struct Stat s1;
  338. char buf[sz];
  339. int blen = sz ;
  340. char p1[sz], p2[sz];
  341. int nops = 2;
  342. zoo_op_t ops[nops];
  343. zoo_op_result_t results[nops];
  344. zoo_create_op_init(&ops[0], "/multi5", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  345. zoo_create_op_init(&ops[1], "/multi5/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
  346. rc = zoo_multi(zk, nops, ops, results);
  347. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  348. yield(zk, 5);
  349. zoo_op_t setdata_ops[nops];
  350. zoo_op_result_t setdata_results[nops];
  351. zoo_set_op_init(&setdata_ops[0], "/multi5", "1", 1, 0, &s1);
  352. zoo_set_op_init(&setdata_ops[1], "/multi5/a", "2", 1, 0, &s1);
  353. rc = zoo_multi(zk, nops, setdata_ops, setdata_results);
  354. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  355. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  356. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err);
  357. memset(buf, '\0', blen);
  358. rc = zoo_get(zk, "/multi5", 0, buf, &blen, &s1);
  359. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  360. CPPUNIT_ASSERT_EQUAL(1, blen);
  361. CPPUNIT_ASSERT(strcmp("1", buf) == 0);
  362. memset(buf, '\0', blen);
  363. rc = zoo_get(zk, "/multi5/a", 0, buf, &blen, &s1);
  364. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  365. CPPUNIT_ASSERT_EQUAL(1, blen);
  366. CPPUNIT_ASSERT(strcmp("2", buf) == 0);
  367. }
  368. /**
  369. * Test update conflicts
  370. */
  371. void testUpdateConflict() {
  372. int rc;
  373. watchctx_t ctx;
  374. zhandle_t *zk = createClient(&ctx);
  375. int sz = 512;
  376. char buf[sz];
  377. int blen = sz;
  378. char p1[sz];
  379. p1[0] = '\0';
  380. struct Stat s1;
  381. int nops = 3;
  382. zoo_op_t ops[nops];
  383. zoo_op_result_t results[nops];
  384. zoo_create_op_init(&ops[0], "/multi6", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  385. zoo_set_op_init(&ops[1], "/multi6", "X", 1, 0, &s1);
  386. zoo_set_op_init(&ops[2], "/multi6", "Y", 1, 0, &s1);
  387. rc = zoo_multi(zk, nops, ops, results);
  388. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, rc);
  389. //Updating version solves conflict -- order matters
  390. ops[2].set_op.version = 1;
  391. rc = zoo_multi(zk, nops, ops, results);
  392. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  393. memset(buf, 0, sz);
  394. rc = zoo_get(zk, "/multi6", 0, buf, &blen, &s1);
  395. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  396. CPPUNIT_ASSERT_EQUAL(blen, 1);
  397. CPPUNIT_ASSERT(strncmp(buf, "Y", 1) == 0);
  398. }
  399. /**
  400. * Test delete-update conflicts
  401. */
  402. void testDeleteUpdateConflict() {
  403. int rc;
  404. watchctx_t ctx;
  405. zhandle_t *zk = createClient(&ctx);
  406. int sz = 512;
  407. char buf[sz];
  408. int blen;
  409. char p1[sz];
  410. p1[0] = '\0';
  411. struct Stat stat;
  412. int nops = 3;
  413. zoo_op_t ops[nops];
  414. zoo_op_result_t results[nops];
  415. zoo_create_op_init(&ops[0], "/multi7", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  416. zoo_delete_op_init(&ops[1], "/multi7", 0);
  417. zoo_set_op_init(&ops[2], "/multi7", "Y", 1, 0, &stat);
  418. rc = zoo_multi(zk, nops, ops, results);
  419. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  420. // '/multi' should never have been created as entire op should fail
  421. rc = zoo_exists(zk, "/multi7", 0, NULL);
  422. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  423. }
  424. void testAsyncMulti() {
  425. int rc;
  426. watchctx_t ctx;
  427. zhandle_t *zk = createClient(&ctx);
  428. int sz = 512;
  429. char p1[sz], p2[sz], p3[sz];
  430. p1[0] = '\0';
  431. p2[0] = '\0';
  432. p3[0] = '\0';
  433. int nops = 3;
  434. zoo_op_t ops[nops];
  435. zoo_op_result_t results[nops];
  436. zoo_create_op_init(&ops[0], "/multi8", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  437. zoo_create_op_init(&ops[1], "/multi8/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
  438. zoo_create_op_init(&ops[2], "/multi8/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz);
  439. rc = zoo_amulti(zk, nops, ops, results, multi_completion_fn, 0);
  440. waitForMultiCompletion(10);
  441. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  442. CPPUNIT_ASSERT(strcmp(p1, "/multi8") == 0);
  443. CPPUNIT_ASSERT(strcmp(p2, "/multi8/a") == 0);
  444. CPPUNIT_ASSERT(strcmp(p3, "/multi8/b") == 0);
  445. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  446. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err);
  447. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2].err);
  448. }
  449. void testMultiFail() {
  450. int rc;
  451. watchctx_t ctx;
  452. zhandle_t *zk = createClient(&ctx);
  453. int sz = 512;
  454. char p1[sz], p2[sz], p3[sz];
  455. p1[0] = '\0';
  456. p2[0] = '\0';
  457. p3[0] = '\0';
  458. int nops = 3;
  459. zoo_op_t ops[nops];
  460. zoo_op_result_t results[nops];
  461. zoo_create_op_init(&ops[0], "/multi9", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  462. zoo_create_op_init(&ops[1], "/multi9", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz);
  463. zoo_create_op_init(&ops[2], "/multi9/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz);
  464. rc = zoo_multi(zk, nops, ops, results);
  465. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, rc);
  466. }
  467. /**
  468. * Test basic multi-op check functionality
  469. */
  470. void testCheck() {
  471. int rc;
  472. watchctx_t ctx;
  473. zhandle_t *zk = createClient(&ctx);
  474. int sz = 512;
  475. char p1[sz];
  476. p1[0] = '\0';
  477. struct Stat s1;
  478. rc = zoo_create(zk, "/multi0", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  479. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  480. // Conditionally create /multi0/a' only if '/multi0' at version 0
  481. int nops = 2;
  482. zoo_op_t ops[nops];
  483. zoo_op_result_t results[nops];
  484. zoo_check_op_init(&ops[0], "/multi0", 0);
  485. zoo_create_op_init(&ops[1], "/multi0/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  486. rc = zoo_multi(zk, nops, ops, results);
  487. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  488. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  489. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err);
  490. // '/multi0/a' should have been created as it passed version check
  491. rc = zoo_exists(zk, "/multi0/a", 0, NULL);
  492. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  493. // Only create '/multi0/b' if '/multi0' at version 10 (which it's not)
  494. zoo_op_t ops2[nops];
  495. zoo_check_op_init(&ops2[0], "/multi0", 10);
  496. zoo_create_op_init(&ops2[1], "/multi0/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz);
  497. rc = zoo_multi(zk, nops, ops2, results);
  498. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, rc);
  499. CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, results[0].err);
  500. CPPUNIT_ASSERT_EQUAL((int)ZRUNTIMEINCONSISTENCY, results[1].err);
  501. // '/multi0/b' should NOT have been created
  502. rc = zoo_exists(zk, "/multi0/b", 0, NULL);
  503. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  504. }
  505. /**
  506. * Do a multi op inside a watch callback context.
  507. */
  508. static void doMultiInWatch(zhandle_t *zk, int type, int state, const char *path, void *ctx) {
  509. int rc;
  510. int sz = 512;
  511. char p1[sz];
  512. p1[0] = '\0';
  513. struct Stat s1;
  514. int nops = 1;
  515. zoo_op_t ops[nops];
  516. zoo_op_result_t results[nops];
  517. zoo_set_op_init(&ops[0], "/multiwatch", "1", 1, -1, NULL);
  518. rc = zoo_multi(zk, nops, ops, results);
  519. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  520. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err);
  521. memset(p1, '\0', sz);
  522. rc = zoo_get(zk, "/multiwatch", 0, p1, &sz, &s1);
  523. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  524. CPPUNIT_ASSERT_EQUAL(1, sz);
  525. CPPUNIT_ASSERT(strcmp("1", p1) == 0);
  526. count++;
  527. }
  528. /**
  529. * Test multi-op called from a watch
  530. */
  531. void testWatch() {
  532. int rc;
  533. watchctx_t ctx;
  534. zhandle_t *zk = createClient(&ctx);
  535. int sz = 512;
  536. char p1[sz];
  537. p1[0] = '\0';
  538. rc = zoo_create(zk, "/multiwatch", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  539. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  540. // create a watch on node '/multiwatch'
  541. rc = zoo_wget(zk, "/multiwatch", doMultiInWatch, &ctx, p1, &sz, NULL);
  542. // setdata on node '/multiwatch' this should trip the watch
  543. rc = zoo_set(zk, "/multiwatch", NULL, -1, -1);
  544. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  545. // wait for multi completion in doMultiInWatch
  546. waitForMultiCompletion(5);
  547. }
  548. /**
  549. * ZOOKEEPER-1624: PendingChanges of create sequential node request didn't
  550. * get rollbacked correctly when multi-op failed. This caused
  551. * create sequential node request in subsequent multi-op to failed because
  552. * sequential node name generation is incorrect.
  553. *
  554. * The check is to make sure that each request in multi-op failed with
  555. * the correct reason.
  556. */
  557. void testSequentialNodeCreateInAsyncMulti() {
  558. int rc;
  559. watchctx_t ctx;
  560. zhandle_t *zk = createClient(&ctx);
  561. int iteration = 4;
  562. int nops = 2;
  563. zoo_op_result_t results[iteration][nops];
  564. zoo_op_t ops[nops];
  565. zoo_create_op_init(&ops[0], "/node-", "", 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, NULL, 0);
  566. zoo_create_op_init(&ops[1], "/dup", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
  567. for (int i = 0; i < iteration ; ++i) {
  568. rc = zoo_amulti(zk, nops, ops, results[i], multi_completion_fn_no_assert, 0);
  569. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  570. }
  571. waitForMultiCompletion(10);
  572. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0][0].err);
  573. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1][0].err);
  574. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2][0].err);
  575. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[3][0].err);
  576. CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0][1].err);
  577. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[1][1].err);
  578. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[2][1].err);
  579. CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[3][1].err);
  580. resetCounter();
  581. }
  582. };
  583. volatile int Zookeeper_multi::count;
  584. const char Zookeeper_multi::hostPorts[] = "127.0.0.1:22181";
  585. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_multi);
  586. #endif