recipes.html 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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_recipes_errorHandlingNote">Important Note About Error Handling</a>
  213. </li>
  214. <li>
  215. <a href="#sc_outOfTheBox">Out of the Box Applications: Name Service, Configuration, Group
  216. Membership</a>
  217. </li>
  218. <li>
  219. <a href="#sc_recipes_eventHandles">Barriers</a>
  220. <ul class="minitoc">
  221. <li>
  222. <a href="#sc_doubleBarriers">Double Barriers</a>
  223. </li>
  224. </ul>
  225. </li>
  226. <li>
  227. <a href="#sc_recipes_Queues">Queues</a>
  228. <ul class="minitoc">
  229. <li>
  230. <a href="#sc_recipes_priorityQueues">Priority Queues</a>
  231. </li>
  232. </ul>
  233. </li>
  234. <li>
  235. <a href="#sc_recipes_Locks">Locks</a>
  236. <ul class="minitoc">
  237. <li>
  238. <a href="#sc_recipes_GuidNote">Recoverable Errors and the GUID</a>
  239. </li>
  240. <li>
  241. <a href="#Shared+Locks">Shared Locks</a>
  242. </li>
  243. <li>
  244. <a href="#sc_recoverableSharedLocks">Recoverable Shared Locks</a>
  245. </li>
  246. </ul>
  247. </li>
  248. <li>
  249. <a href="#sc_recipes_twoPhasedCommit">Two-phased Commit</a>
  250. </li>
  251. <li>
  252. <a href="#sc_leaderElection">Leader Election</a>
  253. </li>
  254. </ul>
  255. </li>
  256. </ul>
  257. </div>
  258. </div>
  259. <a name="ch_recipes"></a>
  260. <h2 class="h3">A Guide to Creating Higher-level Constructs with ZooKeeper</h2>
  261. <div class="section">
  262. <p>In this article, you'll find guidelines for using
  263. ZooKeeper to implement higher order functions. All of them are conventions
  264. implemented at the client and do not require special support from
  265. ZooKeeper. Hopfully the community will capture these conventions in client-side libraries
  266. to ease their use and to encourage standardization.</p>
  267. <p>One of the most interesting things about ZooKeeper is that even
  268. though ZooKeeper uses <em>asynchronous</em> notifications, you
  269. can use it to build <em>synchronous</em> consistency
  270. primitives, such as queues and locks. As you will see, this is possible
  271. because ZooKeeper imposes an overall order on updates, and has mechanisms
  272. to expose this ordering.</p>
  273. <p>Note that the recipes below attempt to employ best practices. In
  274. particular, they avoid polling, timers or anything else that would result
  275. in a "herd effect", causing bursts of traffic and limiting
  276. scalability.</p>
  277. <p>There are many useful functions that can be imagined that aren't
  278. included here - revocable read-write priority locks, as just one example.
  279. And some of the constructs mentioned here - locks, in particular -
  280. illustrate certain points, even though you may find other constructs, such
  281. as event handles or queues, a more practical means of performing the same
  282. function. In general, the examples in this section are designed to
  283. stimulate thought.</p>
  284. <a name="sc_recipes_errorHandlingNote"></a>
  285. <h3 class="h4">Important Note About Error Handling</h3>
  286. <p>When implementing the recipes you must handle recoverable exceptions
  287. (see the <a href="http://wiki.apache.org/hadoop/ZooKeeper/FAQ">FAQ</a>). In
  288. particular, several of the recipes employ sequential ephemeral
  289. nodes. When creating a sequential ephemeral node there is an error case in
  290. which the create() succeeds on the server but the server crashes before
  291. returning the name of the node to the client. When the client reconnects its
  292. session is still valid and, thus, the node is not removed. The implication is
  293. that it is difficult for the client to know if its node was created or not. The
  294. recipes below include measures to handle this.</p>
  295. <a name="sc_outOfTheBox"></a>
  296. <h3 class="h4">Out of the Box Applications: Name Service, Configuration, Group
  297. Membership</h3>
  298. <p>Name service and configuration are two of the primary applications
  299. of ZooKeeper. These two functions are provided directly by the ZooKeeper
  300. API.</p>
  301. <p>Another function directly provided by ZooKeeper is <em>group
  302. membership</em>. The group is represented by a node. Members of the
  303. group create ephemeral nodes under the group node. Nodes of the members
  304. that fail abnormally will be removed automatically when ZooKeeper detects
  305. the failure.</p>
  306. <a name="sc_recipes_eventHandles"></a>
  307. <h3 class="h4">Barriers</h3>
  308. <p>Distributed systems use <em>barriers</em>
  309. to block processing of a set of nodes until a condition is met
  310. at which time all the nodes are allowed to proceed. Barriers are
  311. implemented in ZooKeeper by designating a barrier node. The
  312. barrier is in place if the barrier node exists. Here's the
  313. pseudo code:</p>
  314. <ol>
  315. <li>
  316. <p>Client calls the ZooKeeper API's <strong>exists()</strong> function on the barrier node, with
  317. <em>watch</em> set to true.</p>
  318. </li>
  319. <li>
  320. <p>If <strong>exists()</strong> returns false, the
  321. barrier is gone and the client proceeds</p>
  322. </li>
  323. <li>
  324. <p>Else, if <strong>exists()</strong> returns true,
  325. the clients wait for a watch event from ZooKeeper for the barrier
  326. node.</p>
  327. </li>
  328. <li>
  329. <p>When the watch event is triggered, the client reissues the
  330. <strong>exists( )</strong> call, again waiting until
  331. the barrier node is removed.</p>
  332. </li>
  333. </ol>
  334. <a name="sc_doubleBarriers"></a>
  335. <h4>Double Barriers</h4>
  336. <p>Double barriers enable clients to synchronize the beginning and
  337. the end of a computation. When enough processes have joined the barrier,
  338. processes start their computation and leave the barrier once they have
  339. finished. This recipe shows how to use a ZooKeeper node as a
  340. barrier.</p>
  341. <p>The pseudo code in this recipe represents the barrier node as
  342. <em>b</em>. Every client process <em>p</em>
  343. registers with the barrier node on entry and unregisters when it is
  344. ready to leave. A node registers with the barrier node via the <strong>Enter</strong> procedure below, it waits until
  345. <em>x</em> client process register before proceeding with
  346. the computation. (The <em>x</em> here is up to you to
  347. determine for your system.)</p>
  348. <table class="ForrestTable" cellspacing="1" cellpadding="4">
  349. <tr>
  350. <td><strong>Enter</strong></td>
  351. <td><strong>Leave</strong></td>
  352. </tr>
  353. <tr>
  354. <td>
  355. <ol>
  356. <li>
  357. <p>Create a name <em><em>n</em> =
  358. <em>b</em>+&ldquo;/&rdquo;+<em>p</em></em>
  359. </p>
  360. </li>
  361. <li>
  362. <p>Set watch: <strong>exists(<em>b</em> + &lsquo;&lsquo;/ready&rsquo;&rsquo;,
  363. true)</strong>
  364. </p>
  365. </li>
  366. <li>
  367. <p>Create child: <strong>create(
  368. <em>n</em>, EPHEMERAL)</strong>
  369. </p>
  370. </li>
  371. <li>
  372. <p>
  373. <strong>L = getChildren(b,
  374. false)</strong>
  375. </p>
  376. </li>
  377. <li>
  378. <p>if fewer children in L than<em>
  379. x</em>, wait for watch event</p>
  380. </li>
  381. <li>
  382. <p>else <strong>create(b + &lsquo;&lsquo;/ready&rsquo;&rsquo;,
  383. REGULAR)</strong>
  384. </p>
  385. </li>
  386. </ol>
  387. </td>
  388. <td>
  389. <ol>
  390. <li>
  391. <p>
  392. <strong>L = getChildren(b,
  393. false)</strong>
  394. </p>
  395. </li>
  396. <li>
  397. <p>if no children, exit</p>
  398. </li>
  399. <li>
  400. <p>if <em>p</em> is only process node in
  401. L, delete(n) and exit</p>
  402. </li>
  403. <li>
  404. <p>if <em>p</em> is the lowest process
  405. node in L, wait on highest process node in P</p>
  406. </li>
  407. <li>
  408. <p>else <strong>delete(<em>n</em>) </strong>if
  409. still exists and wait on lowest process node in L</p>
  410. </li>
  411. <li>
  412. <p>goto 1</p>
  413. </li>
  414. </ol>
  415. </td>
  416. </tr>
  417. </table>
  418. <p>On entering, all processes watch on a ready node and
  419. create an ephemeral node as a child of the barrier node. Each process
  420. but the last enters the barrier and waits for the ready node to appear
  421. at line 5. The process that creates the xth node, the last process, will
  422. see x nodes in the list of children and create the ready node, waking up
  423. the other processes. Note that waiting processes wake up only when it is
  424. time to exit, so waiting is efficient.
  425. </p>
  426. <p>On exit, you can't use a flag such as <em>ready</em>
  427. because you are watching for process nodes to go away. By using
  428. ephemeral nodes, processes that fail after the barrier has been entered
  429. do not prevent correct processes from finishing. When processes are
  430. ready to leave, they need to delete their process nodes and wait for all
  431. other processes to do the same.</p>
  432. <p>Processes exit when there are no process nodes left as children of
  433. <em>b</em>. However, as an efficiency, you can use the
  434. lowest process node as the ready flag. All other processes that are
  435. ready to exit watch for the lowest existing process node to go away, and
  436. the owner of the lowest process watches for any other process node
  437. (picking the highest for simplicity) to go away. This means that only a
  438. single process wakes up on each node deletion except for the last node,
  439. which wakes up everyone when it is removed.</p>
  440. <a name="sc_recipes_Queues"></a>
  441. <h3 class="h4">Queues</h3>
  442. <p>Distributed queues are a common data structure. To implement a
  443. distributed queue in ZooKeeper, first designate a znode to hold the queue,
  444. the queue node. The distributed clients put something into the queue by
  445. calling create() with a pathname ending in "queue-", with the
  446. <em>sequence</em> and <em>ephemeral</em> flags in
  447. the create() call set to true. Because the <em>sequence</em>
  448. flag is set, the new pathnames will have the form
  449. _path-to-queue-node_/queue-X, where X is a monotonic increasing number. A
  450. client that wants to be removed from the queue calls ZooKeeper's <strong>getChildren( )</strong> function, with
  451. <em>watch</em> set to true on the queue node, and begins
  452. processing nodes with the lowest number. The client does not need to issue
  453. another <strong>getChildren( )</strong> until it exhausts
  454. the list obtained from the first <strong>getChildren(
  455. )</strong> call. If there are are no children in the queue node, the
  456. reader waits for a watch notification to check the queue again.</p>
  457. <div class="note">
  458. <div class="label">Note</div>
  459. <div class="content">
  460. <p>There now exists a Queue implementation in ZooKeeper
  461. recipes directory. This is distributed with the release --
  462. src/recipes/queue directory of the release artifact.
  463. </p>
  464. </div>
  465. </div>
  466. <a name="sc_recipes_priorityQueues"></a>
  467. <h4>Priority Queues</h4>
  468. <p>To implement a priority queue, you need only make two simple
  469. changes to the generic <a href="#sc_recipes_Queues">queue
  470. recipe</a> . First, to add to a queue, the pathname ends with
  471. "queue-YY" where YY is the priority of the element with lower numbers
  472. representing higher priority (just like UNIX). Second, when removing
  473. from the queue, a client uses an up-to-date children list meaning that
  474. the client will invalidate previously obtained children lists if a watch
  475. notification triggers for the queue node.</p>
  476. <a name="sc_recipes_Locks"></a>
  477. <h3 class="h4">Locks</h3>
  478. <p>Fully distributed locks that are globally synchronous, meaning at
  479. any snapshot in time no two clients think they hold the same lock. These
  480. can be implemented using ZooKeeeper. As with priority queues, first define
  481. a lock node.</p>
  482. <div class="note">
  483. <div class="label">Note</div>
  484. <div class="content">
  485. <p>There now exists a Lock implementation in ZooKeeper
  486. recipes directory. This is distributed with the release --
  487. src/recipes/lock directory of the release artifact.
  488. </p>
  489. </div>
  490. </div>
  491. <p>Clients wishing to obtain a lock do the following:</p>
  492. <ol>
  493. <li>
  494. <p>Call <strong>create( )</strong> with a pathname
  495. of "_locknode_/guid-lock-" and the <em>sequence</em> and
  496. <em>ephemeral</em> flags set. The <em>guid</em>
  497. is needed in case the create() result is missed. See the note below.</p>
  498. </li>
  499. <li>
  500. <p>Call <strong>getChildren( )</strong> on the lock
  501. node <em>without</em> setting the watch flag (this is
  502. important to avoid the herd effect).</p>
  503. </li>
  504. <li>
  505. <p>If the pathname created in step <strong>1</strong> has the lowest sequence number suffix, the
  506. client has the lock and the client exits the protocol.</p>
  507. </li>
  508. <li>
  509. <p>The client calls <strong>exists( )</strong> with
  510. the watch flag set on the path in the lock directory with the next
  511. lowest sequence number.</p>
  512. </li>
  513. <li>
  514. <p>if <strong>exists( )</strong> returns false, go
  515. to step <strong>2</strong>. Otherwise, wait for a
  516. notification for the pathname from the previous step before going to
  517. step <strong>2</strong>.</p>
  518. </li>
  519. </ol>
  520. <p>The unlock protocol is very simple: clients wishing to release a
  521. lock simply delete the node they created in step 1.</p>
  522. <p>Here are a few things to notice:</p>
  523. <ul>
  524. <li>
  525. <p>The removal of a node will only cause one client to wake up
  526. since each node is watched by exactly one client. In this way, you
  527. avoid the herd effect.</p>
  528. </li>
  529. </ul>
  530. <ul>
  531. <li>
  532. <p>There is no polling or timeouts.</p>
  533. </li>
  534. </ul>
  535. <ul>
  536. <li>
  537. <p>Because of the way you implement locking, it is easy to see the
  538. amount of lock contention, break locks, debug locking problems,
  539. etc.</p>
  540. </li>
  541. </ul>
  542. <a name="sc_recipes_GuidNote"></a>
  543. <h4>Recoverable Errors and the GUID</h4>
  544. <ul>
  545. <li>
  546. <p>If a recoverable error occurs calling <strong>create()</strong> the
  547. client should call <strong>getChildren()</strong> and check for a node
  548. containing the <em>guid</em> used in the path name.
  549. This handles the case (noted <a href="#sc_recipes_errorHandlingNote">above</a>) of
  550. the create() succeeding on the server but the server crashing before returning the name
  551. of the new node.</p>
  552. </li>
  553. </ul>
  554. <a name="Shared+Locks"></a>
  555. <h4>Shared Locks</h4>
  556. <p>You can implement shared locks by with a few changes to the lock
  557. protocol:</p>
  558. <table class="ForrestTable" cellspacing="1" cellpadding="4">
  559. <tr>
  560. <td><strong>Obtaining a read
  561. lock:</strong></td>
  562. <td><strong>Obtaining a write
  563. lock:</strong></td>
  564. </tr>
  565. <tr>
  566. <td>
  567. <ol>
  568. <li>
  569. <p>Call <strong>create( )</strong> to
  570. create a node with pathname
  571. "<span class="codefrag filename">guid-/read-</span>". This is the
  572. lock node use later in the protocol. Make sure to set both
  573. the <em>sequence</em> and
  574. <em>ephemeral</em> flags.</p>
  575. </li>
  576. <li>
  577. <p>Call <strong>getChildren( )</strong>
  578. on the lock node <em>without</em> setting the
  579. <em>watch</em> flag - this is important, as it
  580. avoids the herd effect.</p>
  581. </li>
  582. <li>
  583. <p>If there are no children with a pathname starting
  584. with "<span class="codefrag filename">write-</span>" and having a lower
  585. sequence number than the node created in step <strong>1</strong>, the client has the lock and can
  586. exit the protocol. </p>
  587. </li>
  588. <li>
  589. <p>Otherwise, call <strong>exists(
  590. )</strong>, with <em>watch</em> flag, set on
  591. the node in lock directory with pathname staring with
  592. "<span class="codefrag filename">write-</span>" having the next lowest
  593. sequence number.</p>
  594. </li>
  595. <li>
  596. <p>If <strong>exists( )</strong>
  597. returns <em>false</em>, goto step <strong>2</strong>.</p>
  598. </li>
  599. <li>
  600. <p>Otherwise, wait for a notification for the pathname
  601. from the previous step before going to step <strong>2</strong>
  602. </p>
  603. </li>
  604. </ol>
  605. </td>
  606. <td>
  607. <ol>
  608. <li>
  609. <p>Call <strong>create( )</strong> to
  610. create a node with pathname
  611. "<span class="codefrag filename">guid-/write-</span>". This is the
  612. lock node spoken of later in the protocol. Make sure to
  613. set both <em>sequence</em> and
  614. <em>ephemeral</em> flags.</p>
  615. </li>
  616. <li>
  617. <p>Call <strong>getChildren( )
  618. </strong> on the lock node <em>without</em>
  619. setting the <em>watch</em> flag - this is
  620. important, as it avoids the herd effect.</p>
  621. </li>
  622. <li>
  623. <p>If there are no children with a lower sequence
  624. number than the node created in step <strong>1</strong>, the client has the lock and the
  625. client exits the protocol.</p>
  626. </li>
  627. <li>
  628. <p>Call <strong>exists( ),</strong>
  629. with <em>watch</em> flag set, on the node with
  630. the pathname that has the next lowest sequence
  631. number.</p>
  632. </li>
  633. <li>
  634. <p>If <strong>exists( )</strong>
  635. returns <em>false</em>, goto step <strong>2</strong>. Otherwise, wait for a
  636. notification for the pathname from the previous step
  637. before going to step <strong>2</strong>.</p>
  638. </li>
  639. </ol>
  640. </td>
  641. </tr>
  642. </table>
  643. <p>Notes:</p>
  644. <ul>
  645. <li>
  646. <p>It might appear that this recipe creates a herd effect:
  647. when there is a large group of clients waiting for a read
  648. lock, and all getting notified more or less simultaneously
  649. when the "<span class="codefrag filename">write-</span>" node with the lowest
  650. sequence number is deleted. In fact. that's valid behavior:
  651. as all those waiting reader clients should be released since
  652. they have the lock. The herd effect refers to releasing a
  653. "herd" when in fact only a single or a small number of
  654. machines can proceed.</p>
  655. </li>
  656. </ul>
  657. <ul>
  658. <li>
  659. <p>See the <a href="#sc_recipes_GuidNote">note for Locks</a> on how to use the guid in the node.</p>
  660. </li>
  661. </ul>
  662. <a name="sc_recoverableSharedLocks"></a>
  663. <h4>Recoverable Shared Locks</h4>
  664. <p>With minor modifications to the Shared Lock protocol, you make
  665. shared locks revocable by modifying the shared lock protocol:</p>
  666. <p>In step <strong>1</strong>, of both obtain reader
  667. and writer lock protocols, call <strong>getData(
  668. )</strong> with <em>watch</em> set, immediately after the
  669. call to <strong>create( )</strong>. If the client
  670. subsequently receives notification for the node it created in step
  671. <strong>1</strong>, it does another <strong>getData( )</strong> on that node, with
  672. <em>watch</em> set and looks for the string "unlock", which
  673. signals to the client that it must release the lock. This is because,
  674. according to this shared lock protocol, you can request the client with
  675. the lock give up the lock by calling <strong>setData()
  676. </strong> on the lock node, writing "unlock" to that node.</p>
  677. <p>Note that this protocol requires the lock holder to consent to
  678. releasing the lock. Such consent is important, especially if the lock
  679. holder needs to do some processing before releasing the lock. Of course
  680. you can always implement <em>Revocable Shared Locks with Freaking
  681. Laser Beams</em> by stipulating in your protocol that the revoker
  682. is allowed to delete the lock node if after some length of time the lock
  683. isn't deleted by the lock holder.</p>
  684. <a name="sc_recipes_twoPhasedCommit"></a>
  685. <h3 class="h4">Two-phased Commit</h3>
  686. <p>A two-phase commit protocol is an algorithm that lets all clients in
  687. a distributed system agree either to commit a transaction or abort.</p>
  688. <p>In ZooKeeper, you can implement a two-phased commit by having a
  689. coordinator create a transaction node, say "/app/Tx", and one child node
  690. per participating site, say "/app/Tx/s_i". When coordinator creates the
  691. child node, it leaves the content undefined. Once each site involved in
  692. the transaction receives the transaction from the coordinator, the site
  693. reads each child node and sets a watch. Each site then processes the query
  694. and votes "commit" or "abort" by writing to its respective node. Once the
  695. write completes, the other sites are notified, and as soon as all sites
  696. have all votes, they can decide either "abort" or "commit". Note that a
  697. node can decide "abort" earlier if some site votes for "abort".</p>
  698. <p>An interesting aspect of this implementation is that the only role
  699. of the coordinator is to decide upon the group of sites, to create the
  700. ZooKeeper nodes, and to propagate the transaction to the corresponding
  701. sites. In fact, even propagating the transaction can be done through
  702. ZooKeeper by writing it in the transaction node.</p>
  703. <p>There are two important drawbacks of the approach described above.
  704. One is the message complexity, which is O(n&sup2;). The second is the
  705. impossibility of detecting failures of sites through ephemeral nodes. To
  706. detect the failure of a site using ephemeral nodes, it is necessary that
  707. the site create the node.</p>
  708. <p>To solve the first problem, you can have only the coordinator
  709. notified of changes to the transaction nodes, and then notify the sites
  710. once coordinator reaches a decision. Note that this approach is scalable,
  711. but it's is slower too, as it requires all communication to go through the
  712. coordinator.</p>
  713. <p>To address the second problem, you can have the coordinator
  714. propagate the transaction to the sites, and have each site creating its
  715. own ephemeral node.</p>
  716. <a name="sc_leaderElection"></a>
  717. <h3 class="h4">Leader Election</h3>
  718. <p>A simple way of doing leader election with ZooKeeper is to use the
  719. <strong>SEQUENCE|EPHEMERAL</strong> flags when creating
  720. znodes that represent "proposals" of clients. The idea is to have a znode,
  721. say "/election", such that each znode creates a child znode "/election/guid-n_"
  722. with both flags SEQUENCE|EPHEMERAL. With the sequence flag, ZooKeeper
  723. automatically appends a sequence number that is greater that any one
  724. previously appended to a child of "/election". The process that created
  725. the znode with the smallest appended sequence number is the leader.
  726. </p>
  727. <p>That's not all, though. It is important to watch for failures of the
  728. leader, so that a new client arises as the new leader in the case the
  729. current leader fails. A trivial solution is to have all application
  730. processes watching upon the current smallest znode, and checking if they
  731. are the new leader when the smallest znode goes away (note that the
  732. smallest znode will go away if the leader fails because the node is
  733. ephemeral). But this causes a herd effect: upon of failure of the current
  734. leader, all other processes receive a notification, and execute
  735. getChildren on "/election" to obtain the current list of children of
  736. "/election". If the number of clients is large, it causes a spike on the
  737. number of operations that ZooKeeper servers have to process. To avoid the
  738. herd effect, it is sufficient to watch for the next znode down on the
  739. sequence of znodes. If a client receives a notification that the znode it
  740. is watching is gone, then it becomes the new leader in the case that there
  741. is no smaller znode. Note that this avoids the herd effect by not having
  742. all clients watching the same znode. </p>
  743. <p>Here's the pseudo code:</p>
  744. <p>Let ELECTION be a path of choice of the application. To volunteer to
  745. be a leader: </p>
  746. <ol>
  747. <li>
  748. <p>Create znode z with path "ELECTION/guid-n_" with both SEQUENCE and
  749. EPHEMERAL flags;</p>
  750. </li>
  751. <li>
  752. <p>Let C be the children of "ELECTION", and i be the sequence
  753. number of z;</p>
  754. </li>
  755. <li>
  756. <p>Watch for changes on "ELECTION/guid-n_j", where j is the smallest
  757. sequence number such that j &lt; i and n_j is a znode in C;</p>
  758. </li>
  759. </ol>
  760. <p>Upon receiving a notification of znode deletion: </p>
  761. <ol>
  762. <li>
  763. <p>Let C be the new set of children of ELECTION; </p>
  764. </li>
  765. <li>
  766. <p>If z is the smallest node in C, then execute leader
  767. procedure;</p>
  768. </li>
  769. <li>
  770. <p>Otherwise, watch for changes on "ELECTION/guid-n_j", where j is the
  771. smallest sequence number such that j &lt; i and n_j is a znode in C;
  772. </p>
  773. </li>
  774. </ol>
  775. <p>Notes:</p>
  776. <ul>
  777. <li>
  778. <p>Note that the znode having no preceding znode on the list of
  779. children does not imply that the creator of this znode is aware that it is
  780. the current leader. Applications may consider creating a separate to znode
  781. to acknowledge that the leader has executed the leader procedure. </p>
  782. </li>
  783. </ul>
  784. <ul>
  785. <li>
  786. <p>See the <a href="#sc_recipes_GuidNote">note for Locks</a> on how to use the guid in the node.</p>
  787. </li>
  788. </ul>
  789. </div>
  790. <p align="right">
  791. <font size="-2"></font>
  792. </p>
  793. </div>
  794. <!--+
  795. |end content
  796. +-->
  797. <div class="clearboth">&nbsp;</div>
  798. </div>
  799. <div id="footer">
  800. <!--+
  801. |start bottomstrip
  802. +-->
  803. <div class="lastmodified">
  804. <script type="text/javascript"><!--
  805. document.write("Last Published: " + document.lastModified);
  806. // --></script>
  807. </div>
  808. <div class="copyright">
  809. Copyright &copy;
  810. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  811. </div>
  812. <!--+
  813. |end bottomstrip
  814. +-->
  815. </div>
  816. </body>
  817. </html>