recipes.html 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <meta content="Apache Forrest" name="Generator">
  6. <meta name="Forrest-version" content="0.9">
  7. <meta name="Forrest-skin-name" content="pelt">
  8. <title>ZooKeeper Recipes and Solutions</title>
  9. <link type="text/css" href="skin/basic.css" rel="stylesheet">
  10. <link media="screen" type="text/css" href="skin/screen.css" rel="stylesheet">
  11. <link media="print" type="text/css" href="skin/print.css" rel="stylesheet">
  12. <link type="text/css" href="skin/profile.css" rel="stylesheet">
  13. <script src="skin/getBlank.js" language="javascript" type="text/javascript"></script><script src="skin/getMenu.js" language="javascript" type="text/javascript"></script><script src="skin/fontsize.js" language="javascript" type="text/javascript"></script>
  14. <link rel="shortcut icon" href="images/favicon.ico">
  15. </head>
  16. <body onload="init()">
  17. <script type="text/javascript">ndeSetTextSize();</script>
  18. <div id="top">
  19. <!--+
  20. |breadtrail
  21. +-->
  22. <div class="breadtrail">
  23. <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://hadoop.apache.org/">Hadoop</a> &gt; <a href="http://hadoop.apache.org/zookeeper/">ZooKeeper</a><script src="skin/breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
  24. </div>
  25. <!--+
  26. |header
  27. +-->
  28. <div class="header">
  29. <!--+
  30. |start group logo
  31. +-->
  32. <div class="grouplogo">
  33. <a href="http://hadoop.apache.org/"><img class="logoImage" alt="Hadoop" src="images/hadoop-logo.jpg" title="Apache Hadoop"></a>
  34. </div>
  35. <!--+
  36. |end group logo
  37. +-->
  38. <!--+
  39. |start Project Logo
  40. +-->
  41. <div class="projectlogo">
  42. <a href="http://hadoop.apache.org/zookeeper/"><img class="logoImage" alt="ZooKeeper" src="images/zookeeper_small.gif" title="ZooKeeper: distributed coordination"></a>
  43. </div>
  44. <!--+
  45. |end Project Logo
  46. +-->
  47. <!--+
  48. |start Search
  49. +-->
  50. <div class="searchbox">
  51. <form action="http://www.google.com/search" method="get" class="roundtopsmall">
  52. <input value="hadoop.apache.org" name="sitesearch" type="hidden"><input onFocus="getBlank (this, 'Search the site with google');" size="25" name="q" id="query" type="text" value="Search the site with google">&nbsp;
  53. <input name="Search" value="Search" type="submit">
  54. </form>
  55. </div>
  56. <!--+
  57. |end search
  58. +-->
  59. <!--+
  60. |start Tabs
  61. +-->
  62. <ul id="tabs">
  63. <li>
  64. <a class="unselected" href="http://hadoop.apache.org/zookeeper/">Project</a>
  65. </li>
  66. <li>
  67. <a class="unselected" href="http://wiki.apache.org/hadoop/ZooKeeper">Wiki</a>
  68. </li>
  69. <li class="current">
  70. <a class="selected" href="index.html">ZooKeeper 3.4 Documentation</a>
  71. </li>
  72. </ul>
  73. <!--+
  74. |end Tabs
  75. +-->
  76. </div>
  77. </div>
  78. <div id="main">
  79. <div id="publishedStrip">
  80. <!--+
  81. |start Subtabs
  82. +-->
  83. <div id="level2tabs"></div>
  84. <!--+
  85. |end Endtabs
  86. +-->
  87. <script type="text/javascript"><!--
  88. document.write("Last Published: " + document.lastModified);
  89. // --></script>
  90. </div>
  91. <!--+
  92. |breadtrail
  93. +-->
  94. <div class="breadtrail">
  95. &nbsp;
  96. </div>
  97. <!--+
  98. |start Menu, mainarea
  99. +-->
  100. <!--+
  101. |start Menu
  102. +-->
  103. <div id="menu">
  104. <div onclick="SwitchMenu('menu_1.1', 'skin/')" id="menu_1.1Title" class="menutitle">Overview</div>
  105. <div id="menu_1.1" class="menuitemgroup">
  106. <div class="menuitem">
  107. <a href="index.html">Welcome</a>
  108. </div>
  109. <div class="menuitem">
  110. <a href="zookeeperOver.html">Overview</a>
  111. </div>
  112. <div class="menuitem">
  113. <a href="zookeeperStarted.html">Getting Started</a>
  114. </div>
  115. <div class="menuitem">
  116. <a href="releasenotes.html">Release Notes</a>
  117. </div>
  118. </div>
  119. <div onclick="SwitchMenu('menu_selected_1.2', 'skin/')" id="menu_selected_1.2Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">Developer</div>
  120. <div id="menu_selected_1.2" class="selectedmenuitemgroup" style="display: block;">
  121. <div class="menuitem">
  122. <a href="api/index.html">API Docs</a>
  123. </div>
  124. <div class="menuitem">
  125. <a href="zookeeperProgrammers.html">Programmer's Guide</a>
  126. </div>
  127. <div class="menuitem">
  128. <a href="javaExample.html">Java Example</a>
  129. </div>
  130. <div class="menuitem">
  131. <a href="zookeeperTutorial.html">Barrier and Queue Tutorial</a>
  132. </div>
  133. <div class="menupage">
  134. <div class="menupagetitle">Recipes</div>
  135. </div>
  136. </div>
  137. <div onclick="SwitchMenu('menu_1.3', 'skin/')" id="menu_1.3Title" class="menutitle">BookKeeper</div>
  138. <div id="menu_1.3" class="menuitemgroup">
  139. <div class="menuitem">
  140. <a href="bookkeeperStarted.html">Getting started</a>
  141. </div>
  142. <div class="menuitem">
  143. <a href="bookkeeperOverview.html">Overview</a>
  144. </div>
  145. <div class="menuitem">
  146. <a href="bookkeeperConfig.html">Setup guide</a>
  147. </div>
  148. <div class="menuitem">
  149. <a href="bookkeeperProgrammer.html">Programmer's guide</a>
  150. </div>
  151. </div>
  152. <div onclick="SwitchMenu('menu_1.4', 'skin/')" id="menu_1.4Title" class="menutitle">Admin &amp; Ops</div>
  153. <div id="menu_1.4" class="menuitemgroup">
  154. <div class="menuitem">
  155. <a href="zookeeperAdmin.html">Administrator's Guide</a>
  156. </div>
  157. <div class="menuitem">
  158. <a href="zookeeperQuotas.html">Quota Guide</a>
  159. </div>
  160. <div class="menuitem">
  161. <a href="zookeeperJMX.html">JMX</a>
  162. </div>
  163. <div class="menuitem">
  164. <a href="zookeeperObservers.html">Observers Guide</a>
  165. </div>
  166. </div>
  167. <div onclick="SwitchMenu('menu_1.5', 'skin/')" id="menu_1.5Title" class="menutitle">Contributor</div>
  168. <div id="menu_1.5" class="menuitemgroup">
  169. <div class="menuitem">
  170. <a href="zookeeperInternals.html">ZooKeeper Internals</a>
  171. </div>
  172. </div>
  173. <div onclick="SwitchMenu('menu_1.6', 'skin/')" id="menu_1.6Title" class="menutitle">Miscellaneous</div>
  174. <div id="menu_1.6" class="menuitemgroup">
  175. <div class="menuitem">
  176. <a href="http://wiki.apache.org/hadoop/ZooKeeper">Wiki</a>
  177. </div>
  178. <div class="menuitem">
  179. <a href="http://wiki.apache.org/hadoop/ZooKeeper/FAQ">FAQ</a>
  180. </div>
  181. <div class="menuitem">
  182. <a href="http://hadoop.apache.org/zookeeper/mailing_lists.html">Mailing Lists</a>
  183. </div>
  184. </div>
  185. <div id="credit"></div>
  186. <div id="roundbottom">
  187. <img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
  188. <!--+
  189. |alternative credits
  190. +-->
  191. <div id="credit2"></div>
  192. </div>
  193. <!--+
  194. |end Menu
  195. +-->
  196. <!--+
  197. |start content
  198. +-->
  199. <div id="content">
  200. <div title="Portable Document Format" class="pdflink">
  201. <a class="dida" href="recipes.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
  202. PDF</a>
  203. </div>
  204. <h1>ZooKeeper Recipes and Solutions</h1>
  205. <div id="front-matter">
  206. <div id="minitoc-area">
  207. <ul class="minitoc">
  208. <li>
  209. <a href="#ch_recipes">A Guide to Creating Higher-level Constructs with ZooKeeper</a>
  210. <ul class="minitoc">
  211. <li>
  212. <a href="#sc_outOfTheBox">Out of the Box Applications: Name Service, Configuration, Group
  213. Membership</a>
  214. </li>
  215. <li>
  216. <a href="#sc_recipes_eventHandles">Barriers</a>
  217. <ul class="minitoc">
  218. <li>
  219. <a href="#sc_doubleBarriers">Double Barriers</a>
  220. </li>
  221. </ul>
  222. </li>
  223. <li>
  224. <a href="#sc_recipes_Queues">Queues</a>
  225. <ul class="minitoc">
  226. <li>
  227. <a href="#sc_recipes_priorityQueues">Priority Queues</a>
  228. </li>
  229. </ul>
  230. </li>
  231. <li>
  232. <a href="#sc_recipes_Locks">Locks</a>
  233. <ul class="minitoc">
  234. <li>
  235. <a href="#Shared+Locks">Shared Locks</a>
  236. </li>
  237. <li>
  238. <a href="#sc_recoverableSharedLocks">Recoverable Shared Locks</a>
  239. </li>
  240. </ul>
  241. </li>
  242. <li>
  243. <a href="#sc_recipes_twoPhasedCommit">Two-phased Commit</a>
  244. </li>
  245. <li>
  246. <a href="#sc_leaderElection">Leader Election</a>
  247. </li>
  248. </ul>
  249. </li>
  250. </ul>
  251. </div>
  252. </div>
  253. <a name="ch_recipes"></a>
  254. <h2 class="h3">A Guide to Creating Higher-level Constructs with ZooKeeper</h2>
  255. <div class="section">
  256. <p>In this article, you'll find guidelines for using
  257. ZooKeeper to implement higher order functions. All of them are conventions
  258. implemented at the client and do not require special support from
  259. ZooKeeper. Hopfully the community will capture these conventions in client-side libraries
  260. to ease their use and to encourage standardization.</p>
  261. <p>One of the most interesting things about ZooKeeper is that even
  262. though ZooKeeper uses <em>asynchronous</em> notifications, you
  263. can use it to build <em>synchronous</em> consistency
  264. primitives, such as queues and locks. As you will see, this is possible
  265. because ZooKeeper imposes an overall order on updates, and has mechanisms
  266. to expose this ordering.</p>
  267. <p>Note that the recipes below attempt to employ best practices. In
  268. particular, they avoid polling, timers or anything else that would result
  269. in a "herd effect", causing bursts of traffic and limiting
  270. scalability.</p>
  271. <p>There are many useful functions that can be imagined that aren't
  272. included here - revocable read-write priority locks, as just one example.
  273. And some of the constructs mentioned here - locks, in particular -
  274. illustrate certain points, even though you may find other constructs, such
  275. as event handles or queues, a more practical means of performing the same
  276. function. In general, the examples in this section are designed to
  277. stimulate thought.</p>
  278. <a name="sc_outOfTheBox"></a>
  279. <h3 class="h4">Out of the Box Applications: Name Service, Configuration, Group
  280. Membership</h3>
  281. <p>Name service and configuration are two of the primary applications
  282. of ZooKeeper. These two functions are provided directly by the ZooKeeper
  283. API.</p>
  284. <p>Another function directly provided by ZooKeeper is <em>group
  285. membership</em>. The group is represented by a node. Members of the
  286. group create ephemeral nodes under the group node. Nodes of the members
  287. that fail abnormally will be removed automatically when ZooKeeper detects
  288. the failure.</p>
  289. <a name="sc_recipes_eventHandles"></a>
  290. <h3 class="h4">Barriers</h3>
  291. <p>Distributed systems use <em>barriers</em>
  292. to block processing of a set of nodes until a condition is met
  293. at which time all the nodes are allowed to proceed. Barriers are
  294. implemented in ZooKeeper by designating a barrier node. The
  295. barrier is in place if the barrier node exists. Here's the
  296. pseudo code:</p>
  297. <ol>
  298. <li>
  299. <p>Client calls the ZooKeeper API's <strong>exists()</strong> function on the barrier node, with
  300. <em>watch</em> set to true.</p>
  301. </li>
  302. <li>
  303. <p>If <strong>exists()</strong> returns false, the
  304. barrier is gone and the client proceeds</p>
  305. </li>
  306. <li>
  307. <p>Else, if <strong>exists()</strong> returns true,
  308. the clients wait for a watch event from ZooKeeper for the barrier
  309. node.</p>
  310. </li>
  311. <li>
  312. <p>When the watch event is triggered, the client reissues the
  313. <strong>exists( )</strong> call, again waiting until
  314. the barrier node is removed.</p>
  315. </li>
  316. </ol>
  317. <a name="sc_doubleBarriers"></a>
  318. <h4>Double Barriers</h4>
  319. <p>Double barriers enable clients to synchronize the beginning and
  320. the end of a computation. When enough processes have joined the barrier,
  321. processes start their computation and leave the barrier once they have
  322. finished. This recipe shows how to use a ZooKeeper node as a
  323. barrier.</p>
  324. <p>The pseudo code in this recipe represents the barrier node as
  325. <em>b</em>. Every client process <em>p</em>
  326. registers with the barrier node on entry and unregisters when it is
  327. ready to leave. A node registers with the barrier node via the <strong>Enter</strong> procedure below, it waits until
  328. <em>x</em> client process register before proceeding with
  329. the computation. (The <em>x</em> here is up to you to
  330. determine for your system.)</p>
  331. <table class="ForrestTable" cellspacing="1" cellpadding="4">
  332. <tr>
  333. <td><strong>Enter</strong></td>
  334. <td><strong>Leave</strong></td>
  335. </tr>
  336. <tr>
  337. <td>
  338. <ol>
  339. <li>
  340. <p>Create a name <em><em>n</em> =
  341. <em>b</em>+&ldquo;/&rdquo;+<em>p</em></em>
  342. </p>
  343. </li>
  344. <li>
  345. <p>Set watch: <strong>exists(<em>b</em> + &lsquo;&lsquo;/ready&rsquo;&rsquo;,
  346. true)</strong>
  347. </p>
  348. </li>
  349. <li>
  350. <p>Create child: <strong>create(
  351. <em>n</em>, EPHEMERAL)</strong>
  352. </p>
  353. </li>
  354. <li>
  355. <p>
  356. <strong>L = getChildren(b,
  357. false)</strong>
  358. </p>
  359. </li>
  360. <li>
  361. <p>if fewer children in L than<em>
  362. x</em>, wait for watch event</p>
  363. </li>
  364. <li>
  365. <p>else <strong>create(b + &lsquo;&lsquo;/ready&rsquo;&rsquo;,
  366. REGULAR)</strong>
  367. </p>
  368. </li>
  369. </ol>
  370. </td>
  371. <td>
  372. <ol>
  373. <li>
  374. <p>
  375. <strong>L = getChildren(b,
  376. false)</strong>
  377. </p>
  378. </li>
  379. <li>
  380. <p>if no children, exit</p>
  381. </li>
  382. <li>
  383. <p>if <em>p</em> is only process node in
  384. L, delete(n) and exit</p>
  385. </li>
  386. <li>
  387. <p>if <em>p</em> is the lowest process
  388. node in L, wait on highest process node in P</p>
  389. </li>
  390. <li>
  391. <p>else <strong>delete(<em>n</em>) </strong>if
  392. still exists and wait on lowest process node in L</p>
  393. </li>
  394. <li>
  395. <p>goto 1</p>
  396. </li>
  397. </ol>
  398. </td>
  399. </tr>
  400. </table>
  401. <p>On entering, all processes watch on a ready node and
  402. create an ephemeral node as a child of the barrier node. Each process
  403. but the last enters the barrier and waits for the ready node to appear
  404. at line 5. The process that creates the xth node, the last process, will
  405. see x nodes in the list of children and create the ready node, waking up
  406. the other processes. Note that waiting processes wake up only when it is
  407. time to exit, so waiting is efficient.
  408. </p>
  409. <p>On exit, you can't use a flag such as <em>ready</em>
  410. because you are watching for process nodes to go away. By using
  411. ephemeral nodes, processes that fail after the barrier has been entered
  412. do not prevent correct processes from finishing. When processes are
  413. ready to leave, they need to delete their process nodes and wait for all
  414. other processes to do the same.</p>
  415. <p>Processes exit when there are no process nodes left as children of
  416. <em>b</em>. However, as an efficiency, you can use the
  417. lowest process node as the ready flag. All other processes that are
  418. ready to exit watch for the lowest existing process node to go away, and
  419. the owner of the lowest process watches for any other process node
  420. (picking the highest for simplicity) to go away. This means that only a
  421. single process wakes up on each node deletion except for the last node,
  422. which wakes up everyone when it is removed.</p>
  423. <a name="sc_recipes_Queues"></a>
  424. <h3 class="h4">Queues</h3>
  425. <p>Distributed queues are a common data structure. To implement a
  426. distributed queue in ZooKeeper, first designate a znode to hold the queue,
  427. the queue node. The distributed clients put something into the queue by
  428. calling create() with a pathname ending in "queue-", with the
  429. <em>sequence</em> and <em>ephemeral</em> flags in
  430. the create() call set to true. Because the <em>sequence</em>
  431. flag is set, the new pathnames will have the form
  432. _path-to-queue-node_/queue-X, where X is a monotonic increasing number. A
  433. client that wants to be removed from the queue calls ZooKeeper's <strong>getChildren( )</strong> function, with
  434. <em>watch</em> set to true on the queue node, and begins
  435. processing nodes with the lowest number. The client does not need to issue
  436. another <strong>getChildren( )</strong> until it exhausts
  437. the list obtained from the first <strong>getChildren(
  438. )</strong> call. If there are are no children in the queue node, the
  439. reader waits for a watch notification to check the queue again.</p>
  440. <div class="note">
  441. <div class="label">Note</div>
  442. <div class="content">
  443. <p>There now exists a Queue implementation in ZooKeeper
  444. recipes directory. This is distributed with the release --
  445. src/recipes/queue directory of the release artifact.
  446. </p>
  447. </div>
  448. </div>
  449. <a name="sc_recipes_priorityQueues"></a>
  450. <h4>Priority Queues</h4>
  451. <p>To implement a priority queue, you need only make two simple
  452. changes to the generic <a href="#sc_recipes_Queues">queue
  453. recipe</a> . First, to add to a queue, the pathname ends with
  454. "queue-YY" where YY is the priority of the element with lower numbers
  455. representing higher priority (just like UNIX). Second, when removing
  456. from the queue, a client uses an up-to-date children list meaning that
  457. the client will invalidate previously obtained children lists if a watch
  458. notification triggers for the queue node.</p>
  459. <a name="sc_recipes_Locks"></a>
  460. <h3 class="h4">Locks</h3>
  461. <p>Fully distributed locks that are globally synchronous, meaning at
  462. any snapshot in time no two clients think they hold the same lock. These
  463. can be implemented using ZooKeeeper. As with priority queues, first define
  464. a lock node.</p>
  465. <div class="note">
  466. <div class="label">Note</div>
  467. <div class="content">
  468. <p>There now exists a Lock implementation in ZooKeeper
  469. recipes directory. This is distributed with the release --
  470. src/recipes/lock directory of the release artifact.
  471. </p>
  472. </div>
  473. </div>
  474. <p>Clients wishing to obtain a lock do the following:</p>
  475. <ol>
  476. <li>
  477. <p>Call <strong>create( )</strong> with a pathname
  478. of "_locknode_/lock-" and the <em>sequence</em> and
  479. <em>ephemeral</em> flags set.</p>
  480. </li>
  481. <li>
  482. <p>Call <strong>getChildren( )</strong> on the lock
  483. node <em>without</em> setting the watch flag (this is
  484. important to avoid the herd effect).</p>
  485. </li>
  486. <li>
  487. <p>If the pathname created in step <strong>1</strong> has the lowest sequence number suffix, the
  488. client has the lock and the client exits the protocol.</p>
  489. </li>
  490. <li>
  491. <p>The client calls <strong>exists( )</strong> with
  492. the watch flag set on the path in the lock directory with the next
  493. lowest sequence number.</p>
  494. </li>
  495. <li>
  496. <p>if <strong>exists( )</strong> returns false, go
  497. to step <strong>2</strong>. Otherwise, wait for a
  498. notification for the pathname from the previous step before going to
  499. step <strong>2</strong>.</p>
  500. </li>
  501. </ol>
  502. <p>The unlock protocol is very simple: clients wishing to release a
  503. lock simply delete the node they created in step 1.</p>
  504. <p>Here are a few things to notice:</p>
  505. <ul>
  506. <li>
  507. <p>The removal of a node will only cause one client to wake up
  508. since each node is watched by exactly one client. In this way, you
  509. avoid the herd effect.</p>
  510. </li>
  511. </ul>
  512. <ul>
  513. <li>
  514. <p>There is no polling or timeouts.</p>
  515. </li>
  516. </ul>
  517. <ul>
  518. <li>
  519. <p>Because of the way you implement locking, it is easy to see the
  520. amount of lock contention, break locks, debug locking problems,
  521. etc.</p>
  522. </li>
  523. </ul>
  524. <a name="Shared+Locks"></a>
  525. <h4>Shared Locks</h4>
  526. <p>You can implement shared locks by with a few changes to the lock
  527. protocol:</p>
  528. <table class="ForrestTable" cellspacing="1" cellpadding="4">
  529. <tr>
  530. <td><strong>Obtaining a read
  531. lock:</strong></td>
  532. <td><strong>Obtaining a write
  533. lock:</strong></td>
  534. </tr>
  535. <tr>
  536. <td>
  537. <ol>
  538. <li>
  539. <p>Call <strong>create( )</strong> to
  540. create a node with pathname
  541. "<span class="codefrag filename">_locknode_/read-</span>". This is the
  542. lock node use later in the protocol. Make sure to set both
  543. the <em>sequence</em> and
  544. <em>ephemeral</em> flags.</p>
  545. </li>
  546. <li>
  547. <p>Call <strong>getChildren( )</strong>
  548. on the lock node <em>without</em> setting the
  549. <em>watch</em> flag - this is important, as it
  550. avoids the herd effect.</p>
  551. </li>
  552. <li>
  553. <p>If there are no children with a pathname starting
  554. with "<span class="codefrag filename">write-</span>" and having a lower
  555. sequence number than the node created in step <strong>1</strong>, the client has the lock and can
  556. exit the protocol. </p>
  557. </li>
  558. <li>
  559. <p>Otherwise, call <strong>exists(
  560. )</strong>, with <em>watch</em> flag, set on
  561. the node in lock directory with pathname staring with
  562. "<span class="codefrag filename">write-</span>" having the next lowest
  563. sequence number.</p>
  564. </li>
  565. <li>
  566. <p>If <strong>exists( )</strong>
  567. returns <em>false</em>, goto step <strong>2</strong>.</p>
  568. </li>
  569. <li>
  570. <p>Otherwise, wait for a notification for the pathname
  571. from the previous step before going to step <strong>2</strong>
  572. </p>
  573. </li>
  574. </ol>
  575. </td>
  576. <td>
  577. <ol>
  578. <li>
  579. <p>Call <strong>create( )</strong> to
  580. create a node with pathname
  581. "<span class="codefrag filename">_locknode_/write-</span>". This is the
  582. lock node spoken of later in the protocol. Make sure to
  583. set both <em>sequence</em> and
  584. <em>ephemeral</em> flags.</p>
  585. </li>
  586. <li>
  587. <p>Call <strong>getChildren( )
  588. </strong> on the lock node <em>without</em>
  589. setting the <em>watch</em> flag - this is
  590. important, as it avoids the herd effect.</p>
  591. </li>
  592. <li>
  593. <p>If there are no children with a lower sequence
  594. number than the node created in step <strong>1</strong>, the client has the lock and the
  595. client exits the protocol.</p>
  596. </li>
  597. <li>
  598. <p>Call <strong>exists( ),</strong>
  599. with <em>watch</em> flag set, on the node with
  600. the pathname that has the next lowest sequence
  601. number.</p>
  602. </li>
  603. <li>
  604. <p>If <strong>exists( )</strong>
  605. returns <em>false</em>, goto step <strong>2</strong>. Otherwise, wait for a
  606. notification for the pathname from the previous step
  607. before going to step <strong>2</strong>.</p>
  608. </li>
  609. </ol>
  610. </td>
  611. </tr>
  612. </table>
  613. <div class="note">
  614. <div class="label">Note</div>
  615. <div class="content">
  616. <p>It might appear that this recipe creates a herd effect:
  617. when there is a large group of clients waiting for a read
  618. lock, and all getting notified more or less simultaneously
  619. when the "<span class="codefrag filename">write-</span>" node with the lowest
  620. sequence number is deleted. In fact. that's valid behavior:
  621. as all those waiting reader clients should be released since
  622. they have the lock. The herd effect refers to releasing a
  623. "herd" when in fact only a single or a small number of
  624. machines can proceed.
  625. </p>
  626. </div>
  627. </div>
  628. <a name="sc_recoverableSharedLocks"></a>
  629. <h4>Recoverable Shared Locks</h4>
  630. <p>With minor modifications to the Shared Lock protocol, you make
  631. shared locks revocable by modifying the shared lock protocol:</p>
  632. <p>In step <strong>1</strong>, of both obtain reader
  633. and writer lock protocols, call <strong>getData(
  634. )</strong> with <em>watch</em> set, immediately after the
  635. call to <strong>create( )</strong>. If the client
  636. subsequently receives notification for the node it created in step
  637. <strong>1</strong>, it does another <strong>getData( )</strong> on that node, with
  638. <em>watch</em> set and looks for the string "unlock", which
  639. signals to the client that it must release the lock. This is because,
  640. according to this shared lock protocol, you can request the client with
  641. the lock give up the lock by calling <strong>setData()
  642. </strong> on the lock node, writing "unlock" to that node.</p>
  643. <p>Note that this protocol requires the lock holder to consent to
  644. releasing the lock. Such consent is important, especially if the lock
  645. holder needs to do some processing before releasing the lock. Of course
  646. you can always implement <em>Revocable Shared Locks with Freaking
  647. Laser Beams</em> by stipulating in your protocol that the revoker
  648. is allowed to delete the lock node if after some length of time the lock
  649. isn't deleted by the lock holder.</p>
  650. <a name="sc_recipes_twoPhasedCommit"></a>
  651. <h3 class="h4">Two-phased Commit</h3>
  652. <p>A two-phase commit protocol is an algorithm that lets all clients in
  653. a distributed system agree either to commit a transaction or abort.</p>
  654. <p>In ZooKeeper, you can implement a two-phased commit by having a
  655. coordinator create a transaction node, say "/app/Tx", and one child node
  656. per participating site, say "/app/Tx/s_i". When coordinator creates the
  657. child node, it leaves the content undefined. Once each site involved in
  658. the transaction receives the transaction from the coordinator, the site
  659. reads each child node and sets a watch. Each site then processes the query
  660. and votes "commit" or "abort" by writing to its respective node. Once the
  661. write completes, the other sites are notified, and as soon as all sites
  662. have all votes, they can decide either "abort" or "commit". Note that a
  663. node can decide "abort" earlier if some site votes for "abort".</p>
  664. <p>An interesting aspect of this implementation is that the only role
  665. of the coordinator is to decide upon the group of sites, to create the
  666. ZooKeeper nodes, and to propagate the transaction to the corresponding
  667. sites. In fact, even propagating the transaction can be done through
  668. ZooKeeper by writing it in the transaction node.</p>
  669. <p>There are two important drawbacks of the approach described above.
  670. One is the message complexity, which is O(n&sup2;). The second is the
  671. impossibility of detecting failures of sites through ephemeral nodes. To
  672. detect the failure of a site using ephemeral nodes, it is necessary that
  673. the site create the node.</p>
  674. <p>To solve the first problem, you can have only the coordinator
  675. notified of changes to the transaction nodes, and then notify the sites
  676. once coordinator reaches a decision. Note that this approach is scalable,
  677. but it's is slower too, as it requires all communication to go through the
  678. coordinator.</p>
  679. <p>To address the second problem, you can have the coordinator
  680. propagate the transaction to the sites, and have each site creating its
  681. own ephemeral node.</p>
  682. <a name="sc_leaderElection"></a>
  683. <h3 class="h4">Leader Election</h3>
  684. <p>A simple way of doing leader election with ZooKeeper is to use the
  685. <strong>SEQUENCE|EPHEMERAL</strong> flags when creating
  686. znodes that represent "proposals" of clients. The idea is to have a znode,
  687. say "/election", such that each znode creates a child znode "/election/n_"
  688. with both flags SEQUENCE|EPHEMERAL. With the sequence flag, ZooKeeper
  689. automatically appends a sequence number that is greater that any one
  690. previously appended to a child of "/election". The process that created
  691. the znode with the smallest appended sequence number is the leader.
  692. </p>
  693. <p>That's not all, though. It is important to watch for failures of the
  694. leader, so that a new client arises as the new leader in the case the
  695. current leader fails. A trivial solution is to have all application
  696. processes watching upon the current smallest znode, and checking if they
  697. are the new leader when the smallest znode goes away (note that the
  698. smallest znode will go away if the leader fails because the node is
  699. ephemeral). But this causes a herd effect: upon of failure of the current
  700. leader, all other processes receive a notification, and execute
  701. getChildren on "/election" to obtain the current list of children of
  702. "/election". If the number of clients is large, it causes a spike on the
  703. number of operations that ZooKeeper servers have to process. To avoid the
  704. herd effect, it is sufficient to watch for the next znode down on the
  705. sequence of znodes. If a client receives a notification that the znode it
  706. is watching is gone, then it becomes the new leader in the case that there
  707. is no smaller znode. Note that this avoids the herd effect by not having
  708. all clients watching the same znode. </p>
  709. <p>Here's the pseudo code:</p>
  710. <p>Let ELECTION be a path of choice of the application. To volunteer to
  711. be a leader: </p>
  712. <ol>
  713. <li>
  714. <p>Create znode z with path "ELECTION/n_" with both SEQUENCE and
  715. EPHEMERAL flags;</p>
  716. </li>
  717. <li>
  718. <p>Let C be the children of "ELECTION", and i be the sequence
  719. number of z;</p>
  720. </li>
  721. <li>
  722. <p>Watch for changes on "ELECTION/n_j", where j is the smallest
  723. sequence number such that j &lt; i and n_j is a znode in C;</p>
  724. </li>
  725. </ol>
  726. <p>Upon receiving a notification of znode deletion: </p>
  727. <ol>
  728. <li>
  729. <p>Let C be the new set of children of ELECTION; </p>
  730. </li>
  731. <li>
  732. <p>If z is the smallest node in C, then execute leader
  733. procedure;</p>
  734. </li>
  735. <li>
  736. <p>Otherwise, watch for changes on "ELECTION/n_j", where j is the
  737. smallest sequence number such that j &lt; i and n_j is a znode in C;
  738. </p>
  739. </li>
  740. </ol>
  741. <p>Note that the znode having no preceding znode on the list of
  742. children does not imply that the creator of this znode is aware that it is
  743. the current leader. Applications may consider creating a separate to znode
  744. to acknowledge that the leader has executed the leader procedure. </p>
  745. </div>
  746. <p align="right">
  747. <font size="-2"></font>
  748. </p>
  749. </div>
  750. <!--+
  751. |end content
  752. +-->
  753. <div class="clearboth">&nbsp;</div>
  754. </div>
  755. <div id="footer">
  756. <!--+
  757. |start bottomstrip
  758. +-->
  759. <div class="lastmodified">
  760. <script type="text/javascript"><!--
  761. document.write("Last Published: " + document.lastModified);
  762. // --></script>
  763. </div>
  764. <div class="copyright">
  765. Copyright &copy;
  766. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  767. </div>
  768. <!--+
  769. |end bottomstrip
  770. +-->
  771. </div>
  772. </body>
  773. </html>