recipes.html 30 KB

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