recipes.html 30 KB

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