zookeeperProgrammers.html 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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 Programmer's Guide</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 3.1 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="menupage">
  125. <div class="menupagetitle">Programmer's Guide</div>
  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="menuitem">
  134. <a href="recipes.html">Recipes</a>
  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">Miscellaneous</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>
  161. <div id="credit"></div>
  162. <div id="roundbottom">
  163. <img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
  164. <!--+
  165. |alternative credits
  166. +-->
  167. <div id="credit2"></div>
  168. </div>
  169. <!--+
  170. |end Menu
  171. +-->
  172. <!--+
  173. |start content
  174. +-->
  175. <div id="content">
  176. <div title="Portable Document Format" class="pdflink">
  177. <a class="dida" href="zookeeperProgrammers.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
  178. PDF</a>
  179. </div>
  180. <h1>ZooKeeper Programmer's Guide</h1>
  181. <h3>Developing Distributed Applications that use ZooKeeper</h3>
  182. <div id="minitoc-area">
  183. <ul class="minitoc">
  184. <li>
  185. <a href="#_introduction">Introduction</a>
  186. </li>
  187. <li>
  188. <a href="#ch_zkDataModel">The ZooKeeper Data Model</a>
  189. <ul class="minitoc">
  190. <li>
  191. <a href="#sc_zkDataModel_znodes">ZNodes</a>
  192. <ul class="minitoc">
  193. <li>
  194. <a href="#sc_zkDataMode_watches">Watches</a>
  195. </li>
  196. <li>
  197. <a href="#Data+Access">Data Access</a>
  198. </li>
  199. <li>
  200. <a href="#Ephemeral+Nodes">Ephemeral Nodes</a>
  201. </li>
  202. <li>
  203. <a href="#Sequence+Nodes+--+Unique+Naming">Sequence Nodes -- Unique Naming</a>
  204. </li>
  205. </ul>
  206. </li>
  207. <li>
  208. <a href="#sc_timeInZk">Time in ZooKeeper</a>
  209. </li>
  210. <li>
  211. <a href="#sc_zkStatStructure">ZooKeeper Stat Structure</a>
  212. </li>
  213. </ul>
  214. </li>
  215. <li>
  216. <a href="#ch_zkSessions">ZooKeeper Sessions</a>
  217. </li>
  218. <li>
  219. <a href="#ch_zkWatches">ZooKeeper Watches</a>
  220. <ul class="minitoc">
  221. <li>
  222. <a href="#sc_WatchGuarantees">What ZooKeeper Guarantees about Watches</a>
  223. </li>
  224. <li>
  225. <a href="#sc_WatchRememberThese">Things to Remember about Watches</a>
  226. </li>
  227. </ul>
  228. </li>
  229. <li>
  230. <a href="#sc_ZooKeeperAccessControl">ZooKeeper access control using ACLs</a>
  231. <ul class="minitoc">
  232. <li>
  233. <a href="#sc_ACLPermissions">ACL Permissions</a>
  234. <ul class="minitoc">
  235. <li>
  236. <a href="#sc_BuiltinACLSchemes">Builtin ACL Schemes</a>
  237. </li>
  238. <li>
  239. <a href="#ZooKeeper+C+client+API">ZooKeeper C client API</a>
  240. </li>
  241. </ul>
  242. </li>
  243. </ul>
  244. </li>
  245. <li>
  246. <a href="#ch_zkGuarantees">Consistency Guarantees</a>
  247. </li>
  248. <li>
  249. <a href="#ch_bindings">Bindings</a>
  250. <ul class="minitoc">
  251. <li>
  252. <a href="#Java+Binding">Java Binding</a>
  253. </li>
  254. <li>
  255. <a href="#C+Binding">C Binding</a>
  256. <ul class="minitoc">
  257. <li>
  258. <a href="#Installation">Installation</a>
  259. </li>
  260. <li>
  261. <a href="#Using+the+C+Client">Using the C Client</a>
  262. </li>
  263. </ul>
  264. </li>
  265. </ul>
  266. </li>
  267. <li>
  268. <a href="#ch_guideToZkOperations">Building Blocks: A Guide to ZooKeeper Operations</a>
  269. <ul class="minitoc">
  270. <li>
  271. <a href="#sc_connectingToZk">Connecting to ZooKeeper</a>
  272. </li>
  273. <li>
  274. <a href="#sc_readOps">Read Operations</a>
  275. </li>
  276. <li>
  277. <a href="#sc_writeOps">Write Operations</a>
  278. </li>
  279. <li>
  280. <a href="#sc_handlingWatches">Handling Watches</a>
  281. </li>
  282. <li>
  283. <a href="#sc_miscOps">Miscelleaneous ZooKeeper Operations</a>
  284. </li>
  285. </ul>
  286. </li>
  287. <li>
  288. <a href="#ch_programStructureWithExample">Program Structure, with Simple Example</a>
  289. </li>
  290. <li>
  291. <a href="#ch_gotchas">Gotchas: Common Problems and Troubleshooting</a>
  292. </li>
  293. </ul>
  294. </div>
  295. <a name="N1000B"></a><a name="_introduction"></a>
  296. <h2 class="h3">Introduction</h2>
  297. <div class="section">
  298. <p>This document is a guide for developers wishing to create
  299. distributed applications that take advantage of ZooKeeper's coordination
  300. services. It contains conceptual and practical information.</p>
  301. <p>The first four sections of this guide present higher level
  302. discussions of various ZooKeeper concepts. These are necessary both for an
  303. understanding of how ZooKeeper works as well how to work with it. It does
  304. not contain source code, but it does assume a familiarity with the
  305. problems associated with distributed computing. The sections in this first
  306. group are:</p>
  307. <ul>
  308. <li>
  309. <p>
  310. <a href="#ch_zkDataModel">The ZooKeeper Data Model</a>
  311. </p>
  312. </li>
  313. <li>
  314. <p>
  315. <a href="#ch_zkSessions">ZooKeeper Sessions</a>
  316. </p>
  317. </li>
  318. <li>
  319. <p>
  320. <a href="#ch_zkWatches">ZooKeeper Watches</a>
  321. </p>
  322. </li>
  323. <li>
  324. <p>
  325. <a href="#ch_zkGuarantees">Consistency Guarantees</a>
  326. </p>
  327. </li>
  328. </ul>
  329. <p>The next four sections of this provided practical programming
  330. information. These are:</p>
  331. <ul>
  332. <li>
  333. <p>
  334. <a href="#ch_guideToZkOperations">Building Blocks: A Guide to ZooKeeper Operations</a>
  335. </p>
  336. </li>
  337. <li>
  338. <p>
  339. <a href="#ch_bindings">Bindings</a>
  340. </p>
  341. </li>
  342. <li>
  343. <p>
  344. <a href="#ch_programStructureWithExample">Program Structure, with Simple Example</a>
  345. <em>[tbd]</em>
  346. </p>
  347. </li>
  348. <li>
  349. <p>
  350. <a href="#ch_gotchas">Gotchas: Common Problems and Troubleshooting</a>
  351. </p>
  352. </li>
  353. </ul>
  354. <p>The book concludes with an <a href="#apx_linksToOtherInfo">appendix</a> containing links to other
  355. useful, ZooKeeper-related information.</p>
  356. <p>Most of information in this document is written to be accessible as
  357. stand-alone reference material. However, before starting your first
  358. ZooKeeper application, you should probably at least read the chaptes on
  359. the <a href="#ch_zkDataModel">ZooKeeper Data Model</a> and <a href="#ch_guideToZkOperations">ZooKeeper Basic Operations</a>. Also,
  360. the <a href="#ch_programStructureWithExample">Simple Programmming
  361. Example</a> <em>[tbd]</em> is helpful for understand the basic
  362. structure of a ZooKeeper client application.</p>
  363. </div>
  364. <a name="N1007D"></a><a name="ch_zkDataModel"></a>
  365. <h2 class="h3">The ZooKeeper Data Model</h2>
  366. <div class="section">
  367. <p>ZooKeeper has a hierarchal name space, much like a distributed file
  368. system. The only difference is that each node in the namespace can have
  369. data associated with it as well as children. It is like having a file
  370. system that allows a file to also be a directory. Paths to nodes are
  371. always expressed as canonical, absolute, slash-separated paths; there are
  372. no relative reference. Any unicode character can be used in a path subject
  373. to the following constraints:</p>
  374. <ul>
  375. <li>
  376. <p>The null character (\u0000) cannot be part of a path name. (This
  377. causes problems with the C binding.)</p>
  378. </li>
  379. <li>
  380. <p>The following characters can't be used because they don't
  381. display well, or render in confusing ways: \u0001 - \u0019 and \u007F
  382. - \u009F.</p>
  383. </li>
  384. <li>
  385. <p>The following characters are not allowed: \ud800 -uF8FFF,
  386. \uFFF0-uFFFF, \uXFFFE - \uXFFFF (where X is a digit 1 - E), \uF0000 -
  387. \uFFFFF.</p>
  388. </li>
  389. <li>
  390. <p>The "." character can be used as part of another name, but "."
  391. and ".." cannot alone be used to indicate a node along a path,
  392. because ZooKeeper doesn't use relative paths. The following would be
  393. invalid: "/a/b/./c" or "/a/b/../c".</p>
  394. </li>
  395. <li>
  396. <p>The token "zookeeper" is reserved.</p>
  397. </li>
  398. </ul>
  399. <a name="N100A7"></a><a name="sc_zkDataModel_znodes"></a>
  400. <h3 class="h4">ZNodes</h3>
  401. <p>Every node in a ZooKeeper tree is refered to as a
  402. <em>znode</em>. Znodes maintain a stat structure that
  403. includes version numbers for data changes, acl changes. The stat
  404. structure also has timestamps. The version number, together with the
  405. timestamp allow ZooKeeper to validate the cache and to coordinate
  406. updates. Each time a znode's data changes, the version number increases.
  407. For instance, whenever a client retrieves data, it also receives the
  408. version of the data. And when a client performs an update or a delete,
  409. it must supply the version of the data of the znode it is changing. If
  410. the version it supplies doesn't match the actual version of the data,
  411. the update will fail. (This behavior can be overridden. For more
  412. information see... )<em>[tbd...]</em>
  413. </p>
  414. <div class="note">
  415. <div class="label">Note</div>
  416. <div class="content">
  417. <p>In distributed application engineering, the word
  418. <em>node</em> can refer to a generic host machine, a
  419. server, a member of an ensemble, a client process, etc. In the ZooKeeper
  420. documentatin, <em>znodes</em> refer to the data nodes.
  421. <em>Servers</em> to refer to machines that make up the
  422. ZooKeeper service; <em>quorum peers</em> refer to the
  423. servers that make up an ensemble; client refers to any host or process
  424. which uses a ZooKeeper service.</p>
  425. </div>
  426. </div>
  427. <p>Znodes are the main enitity that a programmer access. They have
  428. several characteristics that are worth mentioning here.</p>
  429. <a name="N100CA"></a><a name="sc_zkDataMode_watches"></a>
  430. <h4>Watches</h4>
  431. <p>Clients can set watches on znodes. Changes to that znode trigger
  432. the watch and then clear the watch. When a watch triggers, ZooKeeper
  433. sends the client a notification. More information about watches can be
  434. found in the section
  435. <a href="#ch_zkWatches">ZooKeeper Watches</a>.</p>
  436. <a name="N100D8"></a><a name="Data+Access"></a>
  437. <h4>Data Access</h4>
  438. <p>The data stored at each znode in a namespace is read and written
  439. atomically. Reads get all the data bytes associated with a znode and a
  440. write replaces all the data. Each node has an Access Control List
  441. (ACL) that restricts who can do what.</p>
  442. <a name="N100E2"></a><a name="Ephemeral+Nodes"></a>
  443. <h4>Ephemeral Nodes</h4>
  444. <p>ZooKeeper also has the notion of ephemeral nodes. These znodes
  445. exists as long as the session that created the znode is active. When
  446. the session ends the znode is deleted. Because of this behavior
  447. ephemeral znodes are not allowed to have children.</p>
  448. <a name="N100EC"></a><a name="Sequence+Nodes+--+Unique+Naming"></a>
  449. <h4>Sequence Nodes -- Unique Naming</h4>
  450. <p>When creating a znode you can also request that
  451. ZooKeeper append a monotonicly increasing counter to the end
  452. of path. This counter is unique to the parent znode. The
  453. counter has a format of %010d -- that is 10 digits with 0
  454. (zero) padding (the counter is formatted in this way to
  455. simplify sorting), i.e. "&lt;path&gt;0000000001". See
  456. <a href="recipes.html#sc_recipes_Queues">Queue
  457. Recipe</a> for an example use of this feature. Note: the
  458. counter used to store the next sequence number is a signed int
  459. (4bytes) maintained by the parent node, the counter will
  460. overflow when incremented beyond 2147483647 (resulting in a
  461. name "&lt;path&gt;-2147483647").</p>
  462. <a name="N100FB"></a><a name="sc_timeInZk"></a>
  463. <h3 class="h4">Time in ZooKeeper</h3>
  464. <p>ZooKeeper tracks time multiple ways:</p>
  465. <ul>
  466. <li>
  467. <p>
  468. <strong>Zxid</strong>
  469. </p>
  470. <p>Every change to the ZooKeeper state receives a stamp in the
  471. form of a <em>zxid</em> (ZooKeeper Transaction Id).
  472. This exposes the total ordering of all changes to ZooKeeper. Each
  473. change will have a unique zxid and if zxid1 is smaller than zxid2
  474. then zxid1 happened before zxid2.</p>
  475. </li>
  476. <li>
  477. <p>
  478. <strong>Version numbers</strong>
  479. </p>
  480. <p>Every change to a a node will cause an increase to one of the
  481. version numbers of that node. The three version numbers are version
  482. (number of changes to the data of a znode), cversion (number of
  483. changes to the children of a znode), and aversion (number of changes
  484. to the ACL of a znode).</p>
  485. </li>
  486. <li>
  487. <p>
  488. <strong>Ticks</strong>
  489. </p>
  490. <p>When using multi-server ZooKeeper, servers use ticks to define
  491. timing of events such as status uploads, session timeouts,
  492. connection timeouts between peers, etc. The tick time is only
  493. indirectly exposed through the minimum session timeout (2 times the
  494. tick time); if a client requests a session timeout less than the
  495. minimum session timeout, the server will tell the client that the
  496. session timeout is actually the minimum session timeout.</p>
  497. </li>
  498. <li>
  499. <p>
  500. <strong>Real time</strong>
  501. </p>
  502. <p>ZooKeeper doesn't use real time, or clock time, at all except
  503. to put timestamps into the stat structure on znode creation and
  504. znode modification.</p>
  505. </li>
  506. </ul>
  507. <a name="N10133"></a><a name="sc_zkStatStructure"></a>
  508. <h3 class="h4">ZooKeeper Stat Structure</h3>
  509. <p>The Stat structure for each znode in ZooKeeper is made up of the
  510. following fields:</p>
  511. <ul>
  512. <li>
  513. <p>
  514. <strong>czxid</strong>
  515. </p>
  516. <p>The zxid of the change that caused this znode to be
  517. created.</p>
  518. </li>
  519. <li>
  520. <p>
  521. <strong>mzxid</strong>
  522. </p>
  523. <p>The zxid of the change that last modified this znode.</p>
  524. </li>
  525. <li>
  526. <p>
  527. <strong>ctime</strong>
  528. </p>
  529. <p>The time in milliseconds from epoch when this znode was
  530. created.</p>
  531. </li>
  532. <li>
  533. <p>
  534. <strong>mtime</strong>
  535. </p>
  536. <p>The time in milliseconds from epoch when this znode was last
  537. modified.</p>
  538. </li>
  539. <li>
  540. <p>
  541. <strong>version</strong>
  542. </p>
  543. <p>The number of changes to the data of this znode.</p>
  544. </li>
  545. <li>
  546. <p>
  547. <strong>cversion</strong>
  548. </p>
  549. <p>The number of changes to the children of this znode.</p>
  550. </li>
  551. <li>
  552. <p>
  553. <strong>aversion</strong>
  554. </p>
  555. <p>The number of changes to the ACL of this znode.</p>
  556. </li>
  557. <li>
  558. <p>
  559. <strong>ephemeralOwner</strong>
  560. </p>
  561. <p>The session id of the owner of this znode if the znode is an
  562. ephemeral node. If it is not an ephemeral node, it will be
  563. zero.</p>
  564. </li>
  565. <li>
  566. <p>
  567. <strong>dataLength</strong>
  568. </p>
  569. <p>The length of the data field of this znode.</p>
  570. </li>
  571. <li>
  572. <p>
  573. <strong>numChildren</strong>
  574. </p>
  575. <p>The number of children of this znode.</p>
  576. </li>
  577. </ul>
  578. </div>
  579. <a name="N101A5"></a><a name="ch_zkSessions"></a>
  580. <h2 class="h3">ZooKeeper Sessions</h2>
  581. <div class="section">
  582. <p>When a client gets a handle to the ZooKeeper service, ZooKeeper
  583. creates a ZooKeeper session, represented as a 64-bit number, that it
  584. assigns to the client. If the client connects to a different ZooKeeper
  585. server, it will send the session id as a part of the connection handshake.
  586. As a security measure, the server creates a password for the session id
  587. that any ZooKeeper server can validate.The password is sent to the client with the session id when the
  588. client establishes the session. The client sends this password with the
  589. session id whenever it reestablishes the session with a new server.</p>
  590. <p>One of the parameters to the ZooKeeper client library call to create
  591. a ZooKeeper session is the session timeout in milliseconds. The client
  592. sends a requested timeout, the server responds with the timeout that it
  593. can give the client. The current implementation requires that the timeout
  594. be between 2 times the tickTime (as set in the server configuration) and
  595. 60 seconds.</p>
  596. <p>The session is kept alive by requests sent by the client. If the
  597. session is idle for a period of time that would timeout the session, the
  598. client will send a PING request to keep the session alive. This PING
  599. request not only allows the ZooKeeper server to know that the client is
  600. still active, but it also allows the client to verify that its connection
  601. to the ZooKeeper server is still active. The timing of the PING is
  602. conservative enough to ensure reasonable time to detect a dead connection
  603. and reconnect to a new server.</p>
  604. </div>
  605. <a name="N101B5"></a><a name="ch_zkWatches"></a>
  606. <h2 class="h3">ZooKeeper Watches</h2>
  607. <div class="section">
  608. <p>All of the read operations in ZooKeeper - <strong>getData()</strong>, <strong>getChildren()</strong>, and <strong>exists()</strong> - have the option of setting a watch as a
  609. side effect. Here is ZooKeeper's definition of a watch: a watch event is
  610. one-time trigger, sent to the client that set the watch, which occurs when
  611. the data for which the watch was set changes. There are three key points
  612. to consider in this definition of a watch:</p>
  613. <ul>
  614. <li>
  615. <p>
  616. <strong>One-time trigger</strong>
  617. </p>
  618. <p>One watch event will be sent to the client the data has changed.
  619. For example, if a client does a getData("/znode1", true) and later the
  620. data for /znode1 is changed or deleted, the client will get a watch
  621. event for /znode1. If /znode1 changes again, no watch event will be
  622. sent unless the client has done another read that sets a new
  623. watch.</p>
  624. </li>
  625. <li>
  626. <p>
  627. <strong>Sent to the client</strong>
  628. </p>
  629. <p>This implies that an event is on the way to the client, but may
  630. not reach the client before the successful return code to the change
  631. operation reaches the client that initiated the change. Watches are
  632. sent asynchronously to watchers. ZooKeeper provides an ordering
  633. guarantee: a client will never see a change for which it has set a
  634. watch until it first sees the watch event. Network delays or other
  635. factors may cause different clients to see watches and return codes
  636. from updates at different times. The key point is that everything seen
  637. by the different clients will have a consistent order.</p>
  638. </li>
  639. <li>
  640. <p>
  641. <strong>The data for which the watch was
  642. set</strong>
  643. </p>
  644. <p>This refers to the different ways a node can change. ZooKeeper
  645. maintains two lists of watches: data watches and child watches.
  646. getData() and exists() set data watches. getChildren() sets child
  647. watches. Thus, setData() will trigger data watches for the znode being
  648. set (assuming the set is successful). A successful create() will
  649. trigger a data watch for the znode being created and a child watch for
  650. the parent znode. A successful delete() will trigger both a data watch
  651. and a child watch (since there can be no more children) for a znode
  652. being deleted as well as a child watch for the parent znode.</p>
  653. </li>
  654. </ul>
  655. <p>Watches are maintained locally at the ZooKeeper server to which the
  656. client is connected. This allows watches to be light weight to set,
  657. maintain, and dispatch. When a client connects to a new server, the watch
  658. will be triggered for any session events. Watches will not be received
  659. while disconnected from a server. When a client reconnects, any previously
  660. registered watches will be reregistered and triggered if needed. In
  661. general this all occurs transparently. There is one case where a watch
  662. may be missed: a watch for the existance of a znode not yet created will
  663. be missed if the znode is created and deleted while disconnected.</p>
  664. <a name="N101EB"></a><a name="sc_WatchGuarantees"></a>
  665. <h3 class="h4">What ZooKeeper Guarantees about Watches</h3>
  666. <p>With regard to watches, ZooKeeper maintains these
  667. guarantees:</p>
  668. <ul>
  669. <li>
  670. <p>Watches are ordered with respect to other events, other
  671. watches, and asynchronous replies. The ZooKeeper client libraries
  672. ensures that everything is dispatched in order.</p>
  673. </li>
  674. </ul>
  675. <ul>
  676. <li>
  677. <p>A client will see a watch event for a znode it is watching
  678. before seeing the new data that corresponds to that znode.</p>
  679. </li>
  680. </ul>
  681. <ul>
  682. <li>
  683. <p>The order of watch events from ZooKeeper corresponds to the
  684. order of the updates as seen by the ZooKeeper service.</p>
  685. </li>
  686. </ul>
  687. <a name="N10210"></a><a name="sc_WatchRememberThese"></a>
  688. <h3 class="h4">Things to Remember about Watches</h3>
  689. <ul>
  690. <li>
  691. <p>Watches are one time triggers; if you get a watch event and
  692. you want to get notified of future changes, you must set another
  693. watch.</p>
  694. </li>
  695. </ul>
  696. <ul>
  697. <li>
  698. <p>Because watches are one time triggers and there is latency
  699. between getting the event and sending a new request to get a watch
  700. you cannot reliably see every change that happens to a node in
  701. ZooKeeper. Be prepared to handle the case where the znode changes
  702. multiple times between getting the event and setting the watch
  703. again. (You may not care, but at least realize it may
  704. happen.)</p>
  705. </li>
  706. </ul>
  707. <ul>
  708. <li>
  709. <p>A watch object, or function/context pair, will only be
  710. triggered once for a given notification. For example, if the same
  711. watch object is registered for an exists and a getData call for the
  712. same file and that file is then deleted, the watch object would
  713. only be invoked once with the deletion notification for the file.
  714. </p>
  715. </li>
  716. </ul>
  717. <ul>
  718. <li>
  719. <p>When you disconnect from a server (for example, when the
  720. server fails), you will not get any watches until the connection
  721. is reestablished. For this reason session events are sent to all
  722. outstanding watch handlers. Use session events to go into a safe
  723. mode: you will not be receiving events while disconnected, so your
  724. process should act conservatively in that mode.</p>
  725. </li>
  726. </ul>
  727. </div>
  728. <a name="N1023C"></a><a name="sc_ZooKeeperAccessControl"></a>
  729. <h2 class="h3">ZooKeeper access control using ACLs</h2>
  730. <div class="section">
  731. <p>ZooKeeper uses ACLs to control access to its znodes (the
  732. data nodes of a ZooKeeper data tree). The ACL implementation is
  733. quite similar to UNIX file access permissions: it employs
  734. permission bits to allow/disallow various operations against a
  735. node and the scope to which the bits apply. Unlike standard UNIX
  736. permissions, a ZooKeeper node is not limited by the three standard
  737. scopes for user (owner of the file), group, and world
  738. (other). ZooKeeper does not have a notion of an owner of a
  739. znode. Instead, an ACL specifies sets of ids and permissions that
  740. are associated with those ids.</p>
  741. <p>ZooKeeper supports pluggable authentication schemes. Ids are
  742. specified using the form <em>scheme:id</em>,
  743. where <em>scheme</em> is a the authentication scheme
  744. that the id corresponds to. For
  745. example, <em>host:host1.corp.com</em> is an id for a
  746. host named <em>host1.corp.com</em>.</p>
  747. <p>When a client connects to ZooKeeper and authenticates
  748. itself, ZooKeeper associates all the ids that correspond to a
  749. client with the clients connection. These ids are checked against
  750. the ACLs of znodes when a clients tries to access a node. ACLs are
  751. made up of pairs of <em>(scheme:expression,
  752. perms)</em>. The format of
  753. the <em>expression</em> is specific to the scheme. For
  754. example, the pair <em>(ip:19.22.0.0/16, READ)</em>
  755. gives the <em>READ</em> permission to any clients with
  756. an IP address that starts with 19.22.</p>
  757. <a name="N10263"></a><a name="sc_ACLPermissions"></a>
  758. <h3 class="h4">ACL Permissions</h3>
  759. <p>ZooKeeper supports the following permissions:</p>
  760. <ul>
  761. <li>
  762. <p>
  763. <strong>CREATE</strong>: you can create a child node</p>
  764. </li>
  765. <li>
  766. <p>
  767. <strong>READ</strong>: you can get data from a node and list its children.</p>
  768. </li>
  769. <li>
  770. <p>
  771. <strong>WRITE</strong>: you can set data for a node</p>
  772. </li>
  773. <li>
  774. <p>
  775. <strong>DELETE</strong>: you can delete a child node</p>
  776. </li>
  777. <li>
  778. <p>
  779. <strong>ADMIN</strong>: you can set permissions</p>
  780. </li>
  781. </ul>
  782. <p>The <em>CREATE</em>
  783. and <em>DELETE</em> permissions have been broken out
  784. of the <em>WRITE</em> permission for finer grained
  785. access controls. The cases for <em>CREATE</em>
  786. and <em>DELETE</em> are the following:</p>
  787. <p>You want A to be able to do a set on a ZooKeeper node, but
  788. not be able to <em>CREATE</em>
  789. or <em>DELETE</em> children.</p>
  790. <p>
  791. <em>CREATE</em>
  792. without <em>DELETE</em>: clients create requests by
  793. creating ZooKeeper nodes in a parent directory. You want all
  794. clients to be able to add, but only request processor can
  795. delete. (This is kind of like the APPEND permission for
  796. files.)</p>
  797. <p>Also, the <em>ADMIN</em> permission is there
  798. since ZooKeeper doesn&rsquo;t have a notion of file owner. In some
  799. sense the <em>ADMIN</em> permission designates the
  800. entity as the owner. ZooKeeper doesn&rsquo;t support the LOOKUP
  801. permission (execute permission bit on directories to allow you
  802. to LOOKUP even though you can't list the directory). Everyone
  803. implicitly has LOOKUP permission. This allows you to stat a
  804. node, but nothing more. (The problem is, if you want to call
  805. zoo_exists() on a node that doesn't exist, there is no
  806. permission to check.)</p>
  807. <a name="N102B9"></a><a name="sc_BuiltinACLSchemes"></a>
  808. <h4>Builtin ACL Schemes</h4>
  809. <p>ZooKeeeper has the following built in schemes:</p>
  810. <ul>
  811. <li>
  812. <p>
  813. <strong>world</strong> has a
  814. single id, <em>anyone</em>, that represents
  815. anyone.</p>
  816. </li>
  817. <li>
  818. <p>
  819. <strong>auth</strong> doesn't
  820. use any id, represents any authenticated
  821. user.</p>
  822. </li>
  823. <li>
  824. <p>
  825. <strong>digest</strong> uses
  826. a <em>username:password</em> string to generate
  827. MD5 hash which is then used as an ACL ID
  828. identity. Authentication is done by sending
  829. the <em>username:password</em> in clear text. When
  830. used in the ACL the expression will be
  831. the <em>username:base64</em>
  832. encoded <em>SHA1</em>
  833. password <em>digest</em>.</p>
  834. </li>
  835. <li>
  836. <p>
  837. <strong>host</strong> uses the
  838. client host name as an ACL ID identity. The ACL expression is
  839. a hostname suffix. For example, the ACL
  840. expression <em>host:corp.com</em> matches the
  841. ids <em>host:host1.corp.com</em>
  842. and <em>host:host2.corp.com</em>, but
  843. not <em>host:host1.store.com</em>.</p>
  844. </li>
  845. <li>
  846. <p>
  847. <strong>ip</strong> uses the
  848. client host IP as an ACL ID identity. The ACL expression is of
  849. the form <em>addr/bits</em> where the most
  850. significant <em>bits</em>
  851. of <em>addr</em> are matched against the most
  852. significant <em>bits</em> of the client host
  853. IP.</p>
  854. </li>
  855. </ul>
  856. <a name="N1030F"></a><a name="ZooKeeper+C+client+API"></a>
  857. <h4>ZooKeeper C client API</h4>
  858. <p>The following constants are provided by the ZooKeeper C
  859. library:</p>
  860. <ul>
  861. <li>
  862. <p>
  863. <em>const</em> <em>int</em> ZOO_PERM_READ; //can read node&rsquo;s value and list its children</p>
  864. </li>
  865. <li>
  866. <p>
  867. <em>const</em> <em>int</em> ZOO_PERM_WRITE;// can set the node&rsquo;s value</p>
  868. </li>
  869. <li>
  870. <p>
  871. <em>const</em> <em>int</em> ZOO_PERM_CREATE; //can create children</p>
  872. </li>
  873. <li>
  874. <p>
  875. <em>const</em> <em>int</em> ZOO_PERM_DELETE;// can delete children</p>
  876. </li>
  877. <li>
  878. <p>
  879. <em>const</em> <em>int</em> ZOO_PERM_ADMIN; //can execute set_acl()</p>
  880. </li>
  881. <li>
  882. <p>
  883. <em>const</em> <em>int</em> ZOO_PERM_ALL;// all of the above flags OR&rsquo;d together</p>
  884. </li>
  885. </ul>
  886. <p>The following are the standard ACL IDs:</p>
  887. <ul>
  888. <li>
  889. <p>
  890. <em>struct</em> Id ZOO_ANYONE_ID_UNSAFE; //(&lsquo;world&rsquo;,&rsquo;anyone&rsquo;)</p>
  891. </li>
  892. <li>
  893. <p>
  894. <em>struct</em> Id ZOO_AUTH_IDS;// (&lsquo;auth&rsquo;,&rsquo;&rsquo;)</p>
  895. </li>
  896. </ul>
  897. <p>ZOO_AUTH_IDS empty identity string should be interpreted as &ldquo;the identity of the creator&rdquo;.</p>
  898. <p>ZooKeeper client comes with three standard ACLs:</p>
  899. <ul>
  900. <li>
  901. <p>
  902. <em>struct</em> ACL_vector ZOO_OPEN_ACL_UNSAFE; //(ZOO_PERM_ALL,ZOO_ANYONE_ID_UNSAFE)</p>
  903. </li>
  904. <li>
  905. <p>
  906. <em>struct</em> ACL_vector ZOO_READ_ACL_UNSAFE;// (ZOO_PERM_READ, ZOO_ANYONE_ID_UNSAFE)</p>
  907. </li>
  908. <li>
  909. <p>
  910. <em>struct</em> ACL_vector ZOO_CREATOR_ALL_ACL; //(ZOO_PERM_ALL,ZOO_AUTH_IDS)</p>
  911. </li>
  912. </ul>
  913. <p>The ZOO_OPEN_ACL_UNSAFE is completely open free for all
  914. ACL: any application can execute any operation on the node and
  915. can create, list and delete its children. The
  916. ZOO_READ_ACL_UNSAFE is read-only access for any
  917. application. CREATE_ALL_ACL grants all permissions to the
  918. creator of the node. The creator must have been authenticated by
  919. the server (for example, using &ldquo;<em>digest</em>&rdquo;
  920. scheme) before it can create nodes with this ACL.</p>
  921. <p>The following ZooKeeper operations deal with ACLs:</p>
  922. <ul>
  923. <li>
  924. <p>
  925. <em>int</em> <em>zoo_add_auth</em>
  926. (zhandle_t *zh,<em>const</em> <em>char</em>*
  927. scheme,<em>const</em> <em>char</em>*
  928. cert, <em>int</em> certLen, void_completion_t
  929. completion, <em>const</em> <em>void</em>
  930. *data);</p>
  931. </li>
  932. </ul>
  933. <p>The application uses the zoo_add_auth function to
  934. authenticate itself to the server. The function can be called
  935. multiple times if the application wants to authenticate using
  936. different schemes and/or identities.</p>
  937. <ul>
  938. <li>
  939. <p>
  940. <em>int</em> <em>zoo_create</em>
  941. (zhandle_t *zh, <em>const</em> <em>char</em>
  942. *path, <em>const</em> <em>char</em>
  943. *value,<em>int</em>
  944. valuelen, <em>const</em> <em>struct</em>
  945. ACL_vector *acl, <em>int</em>
  946. flags,<em>char</em>
  947. *realpath, <em>int</em>
  948. max_realpath_len);</p>
  949. </li>
  950. </ul>
  951. <p>zoo_create(...) operation creates a new node. The acl
  952. parameter is a list of ACLs associated with the node. The parent
  953. node must have the CREATE permission bit set.</p>
  954. <ul>
  955. <li>
  956. <p>
  957. <em>int</em> <em>zoo_get_acl</em>
  958. (zhandle_t *zh, <em>const</em> <em>char</em>
  959. *path,<em>struct</em> ACL_vector
  960. *acl, <em>struct</em> Stat *stat);</p>
  961. </li>
  962. </ul>
  963. <p>This operation returns a node&rsquo;s ACL info.</p>
  964. <ul>
  965. <li>
  966. <p>
  967. <em>int</em> <em>zoo_set_acl</em>
  968. (zhandle_t *zh, <em>const</em> <em>char</em>
  969. *path, <em>int</em>
  970. version,<em>const</em> <em>struct</em>
  971. ACL_vector *acl);</p>
  972. </li>
  973. </ul>
  974. <p>This function replaces node&rsquo;s ACL list with a new one. The
  975. node must have the ADMIN permission set.</p>
  976. <p>Here is a sample code that makes use of the above APIs to
  977. authenticate itself using the &ldquo;<em>foo</em>&rdquo; scheme
  978. and create an ephemeral node &ldquo;/xyz&rdquo; with create-only
  979. permissions.</p>
  980. <div class="note">
  981. <div class="label">Note</div>
  982. <div class="content">
  983. <p>This is a very simple example which is intended to show
  984. how to interact with ZooKeeper ACLs
  985. specifically. See <span class="codefrag filename">.../trunk/src/c/src/cli.c</span>
  986. for an example of a proper C client implementation</p>
  987. </div>
  988. </div>
  989. <pre class="code">
  990. #include &lt;string.h&gt;
  991. #include &lt;errno.h&gt;
  992. #include "zookeeper.h"
  993. static zhandle_t *zh;
  994. /**
  995. * In this example this method gets the cert for your
  996. * environment -- you must provide
  997. */
  998. char *foo_get_cert_once(char* id) { return 0; }
  999. /** Watcher function -- empty for this example, not something you should
  1000. * do in real code */
  1001. void watcher(zhandle_t *zzh, int type, int state, const char *path,
  1002. void *watcherCtx) {}
  1003. int main(int argc, char argv) {
  1004. char buffer[512];
  1005. char p[2048];
  1006. char *cert=0;
  1007. char appId[64];
  1008. strcpy(appId, "example.foo_test");
  1009. cert = foo_get_cert_once(appId);
  1010. if(cert!=0) {
  1011. fprintf(stderr,
  1012. "Certificate for appid [%s] is [%s]\n",appId,cert);
  1013. strncpy(p,cert, sizeof(p)-1);
  1014. free(cert);
  1015. } else {
  1016. fprintf(stderr, "Certificate for appid [%s] not found\n",appId);
  1017. strcpy(p, "dummy");
  1018. }
  1019. zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
  1020. zh = zookeeper_init("localhost:3181", watcher, 10000, 0, 0, 0);
  1021. if (!zh) {
  1022. return errno;
  1023. }
  1024. if(zoo_add_auth(zh,"foo",p,strlen(p),0,0)!=ZOK)
  1025. return 2;
  1026. struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_CREATE, ZOO_AUTH_IDS}};
  1027. struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL};
  1028. int rc = zoo_create(zh,"/xyz","value", 5, &amp;CREATE_ONLY, ZOO_EPHEMERAL,
  1029. buffer, sizeof(buffer)-1);
  1030. /** this operation will fail with a ZNOAUTH error */
  1031. int buflen= sizeof(buffer);
  1032. struct Stat stat;
  1033. rc = zoo_get(zh, "/xyz", 0, buffer, &amp;buflen, &amp;stat);
  1034. if (rc) {
  1035. fprintf(stderr, "Error %d for %s\n", rc, __LINE__);
  1036. }
  1037. zookeeper_close(zh);
  1038. return 0;
  1039. }
  1040. </pre>
  1041. </div>
  1042. <a name="N10426"></a><a name="ch_zkGuarantees"></a>
  1043. <h2 class="h3">Consistency Guarantees</h2>
  1044. <div class="section">
  1045. <p>ZooKeeper is a high performance, scalable service. Both reads and
  1046. write operations are designed to be fast, though reads are faster than
  1047. writes. The reason for this is that in the case of reads, ZooKeeper can
  1048. serve older data, which in turn is due to ZooKeeper's consistency
  1049. guarantees:</p>
  1050. <dl>
  1051. <dt>
  1052. <term>Sequential Consistency</term>
  1053. </dt>
  1054. <dd>
  1055. <p>Updates from a client will be applied in the order that they
  1056. were sent.</p>
  1057. </dd>
  1058. <dt>
  1059. <term>Atomicity</term>
  1060. </dt>
  1061. <dd>
  1062. <p>Updates either succeed or fail -- there are no partial
  1063. results.</p>
  1064. </dd>
  1065. <dt>
  1066. <term>Single System Image</term>
  1067. </dt>
  1068. <dd>
  1069. <p>A client will see the same view of the service regardless of
  1070. the server that it connects to.</p>
  1071. </dd>
  1072. <dt>
  1073. <term>Reliability</term>
  1074. </dt>
  1075. <dd>
  1076. <p>Once an update has been applied, it will persist from that
  1077. time forward until a client overwrites the update. This guarantee
  1078. has two corollaries:</p>
  1079. <ol>
  1080. <li>
  1081. <p>If a client gets a successful return code, the update will
  1082. have been applied. On some failures (communication errors,
  1083. timeouts, etc) the client will not know if the update has
  1084. applied or not. We take steps to minimize the failures, but the
  1085. only guarantee is only present with successful return codes.
  1086. (This is called the <em>monotonicity condition</em> in Paxos.)</p>
  1087. </li>
  1088. <li>
  1089. <p>Any updates that are seen by the client, through a read
  1090. request or successful update, will never be rolled back when
  1091. recovering from server failures.</p>
  1092. </li>
  1093. </ol>
  1094. </dd>
  1095. <dt>
  1096. <term>Timeliness</term>
  1097. </dt>
  1098. <dd>
  1099. <p>The clients view of the system is guaranteed to be up-to-date
  1100. within a certain time bound. (On the order of tens of seconds.)
  1101. Either system changes will be seen by a client within this bound, or
  1102. the client will detect a service outage.</p>
  1103. </dd>
  1104. </dl>
  1105. <p>Using these consistency guarantees it is easy to build higher level
  1106. functions such as leader election, barriers, queues, and read/write
  1107. revocable locks solely at the ZooKeeper client (no additions needed to
  1108. ZooKeeper). See <a href="recipes.html">Recipes and Solutions</a>
  1109. for more details.</p>
  1110. <div class="note">
  1111. <div class="label">Note</div>
  1112. <div class="content">
  1113. <p>Sometimes developers mistakenly assume one other guarantee that
  1114. ZooKeeper does <em>not</em> in fact make. This is:</p>
  1115. <dl>
  1116. <dt>
  1117. <term>Simultaneously Conistent Cross-Client Views</term>
  1118. </dt>
  1119. <dd>
  1120. <p>ZooKeeper does not guarantee that at every instance in
  1121. time, two different clients will have identical views of
  1122. ZooKeeper data. Due to factors like network delays, one client
  1123. may perform an update before another client gets notified of the
  1124. change. Consider the scenario of two clients, A and B. If client
  1125. A sets the value of a znode /a from 0 to 1, then tells client B
  1126. to read /a, client B may read the old value of 0, depending on
  1127. which server it is connected to. If it
  1128. is important that Client A and Client B read the same value,
  1129. Client B should should call the <strong>sync()</strong> method from the ZooKeeper API
  1130. method before it performs its read.</p>
  1131. <p>So, ZooKeeper by itself doesn't guarantee that changes occur
  1132. synchronously across all servers, but ZooKeeper
  1133. primitives can be used to construct higher level functions that
  1134. provide useful client synchronization. (For more information,
  1135. see the <a href="recipes.html">ZooKeeper Recipes</a>.
  1136. <em>[tbd:..]</em>).</p>
  1137. </dd>
  1138. </dl>
  1139. </div>
  1140. </div>
  1141. </div>
  1142. <a name="N1048D"></a><a name="ch_bindings"></a>
  1143. <h2 class="h3">Bindings</h2>
  1144. <div class="section">
  1145. <p>The ZooKeeper client libraries come in two languages: Java and C.
  1146. The following sections describe these.</p>
  1147. <a name="N10496"></a><a name="Java+Binding"></a>
  1148. <h3 class="h4">Java Binding</h3>
  1149. <p>There are two packages that make up the ZooKeeper Java binding:
  1150. <strong>org.apache.zookeeper</strong> and <strong>org.apache.zookeeper.data</strong>. The rest of the
  1151. packages that make up ZooKeeper are used internally or are part of the
  1152. server implementation. The <strong>org.apache.zookeeper.data</strong> package is made up of
  1153. generated classes that are used simply as containers.</p>
  1154. <p>The main class used by a ZooKeeper Java client is the <strong>ZooKeeper</strong> class. Its two constructors differ only
  1155. by an optional session id and password. ZooKeeper supports session
  1156. recovery accross instances of a process. A Java program may save its
  1157. session id and password to stable storage, restart, and recover the
  1158. session that was used by the earlier instance of the program.</p>
  1159. <p>When a ZooKeeper object is created, two threads are created as
  1160. well: an IO thread and an event thread. All IO happens on the IO thread
  1161. (using Java NIO). All event callbacks happen on the event thread.
  1162. Session maintenance such as reconnecting to ZooKeeper servers and
  1163. maintaining heartbeat is done on the IO thread. Responses for
  1164. synchronous methods are also processed in the IO thread. All responses
  1165. to asynchronous methods and watch events are processed on the event
  1166. thread. There are a few things to notice that result from this
  1167. design:</p>
  1168. <ul>
  1169. <li>
  1170. <p>All completions for asynchronous calls and watcher callbacks
  1171. will be made in order, one at a time. The caller can do any
  1172. processing they wish, but no other callbacks will be processed
  1173. during that time.</p>
  1174. </li>
  1175. <li>
  1176. <p>Callbacks do not block the processing of the IO thread or the
  1177. processing of the synchronous calls.</p>
  1178. </li>
  1179. <li>
  1180. <p>Synchronous calls may not return in the correct order. For
  1181. example, assume a client does the following processing: issues an
  1182. asynchronous read of node <strong>/a</strong> with
  1183. <em>watch</em> set to true, and then in the completion
  1184. callback of the read it does a synchronous read of <strong>/a</strong>. (Maybe not good practice, but not illegal
  1185. either, and it makes for a simple example.)</p>
  1186. <p>Note that if there is a change to <strong>/a</strong> between the asynchronous read and the
  1187. synchronous read, the client library will receive the watch event
  1188. saying <strong>/a</strong> changed before the
  1189. response for the synchronous read, but because the completion
  1190. callback is blocking the event queue, the synchronous read will
  1191. return with the new value of <strong>/a</strong>
  1192. before the watch event is processed.</p>
  1193. </li>
  1194. </ul>
  1195. <p>Finally, the rules associated with shutdown are straightforward:
  1196. once a ZooKeeper object is closed or receives a fatal event
  1197. (SESSION_EXPIRED and AUTH_FAILED), the ZooKeeper object becomes invalid,
  1198. the two threads shut down, and any further ZooKeeper calls throw
  1199. errors.</p>
  1200. <a name="N104DF"></a><a name="C+Binding"></a>
  1201. <h3 class="h4">C Binding</h3>
  1202. <p>The C binding has a single-threaded and multi-threaded library.
  1203. The multi-threaded library is easiest to use and is most similar to the
  1204. Java API. This library will create an IO thread and an event dispatch
  1205. thread for handling connection maintenance and callbacks. The
  1206. single-threaded library allows ZooKeeper to be used in event driven
  1207. applications by exposing the event loop used in the multi-threaded
  1208. library.</p>
  1209. <p>The package includes two shared libraries: zookeeper_st and
  1210. zookeeper_mt. The former only provides the asynchronous APIs and
  1211. callbacks for integrating into the application's event loop. The only
  1212. reason this library exists is to support the platforms were a
  1213. <em>pthread</em> library is not available or is unstable
  1214. (i.e. FreeBSD 4.x). In all other cases, application developers should
  1215. link with zookeeper_mt, as it includes support for both Sync and Async
  1216. API.</p>
  1217. <a name="N104EE"></a><a name="Installation"></a>
  1218. <h4>Installation</h4>
  1219. <p>If you're building the client from a check-out from the Apache
  1220. repository, follow the steps outlined below. If you're building from a
  1221. project source package downloaded from apache, skip to step <strong>3</strong>.</p>
  1222. <ol>
  1223. <li>
  1224. <p>Run <span class="codefrag command">ant compile_jute</span> from the ZooKeeper
  1225. top level directory (<span class="codefrag filename">.../trunk</span>).
  1226. This will create a directory named "generated" under
  1227. <span class="codefrag filename">.../trunk/src/c</span>.</p>
  1228. </li>
  1229. <li>
  1230. <p>Change directory to the<span class="codefrag filename">.../trunk/src/c</span>
  1231. and run <span class="codefrag command">autoreconf -if</span> to bootstrap <strong>autoconf</strong>, <strong>automake</strong> and <strong>libtool</strong>. Make sure you have <strong>autoconf version 2.59</strong> or greater installed.
  1232. Skip to step<strong> 4</strong>.</p>
  1233. </li>
  1234. <li>
  1235. <p>If you are building from a project source package,
  1236. unzip/untar the source tarball and cd to the<span class="codefrag filename">
  1237. zookeeper-x.x.x/src/c</span> directory.</p>
  1238. </li>
  1239. <li>
  1240. <p>Run <span class="codefrag command">./configure &lt;your-options&gt;</span> to
  1241. generate the makefile. Here are some of options the <strong>configure</strong> utility supports that can be
  1242. useful in this step:</p>
  1243. <ul>
  1244. <li>
  1245. <p>
  1246. <span class="codefrag command">--enable-debug</span>
  1247. </p>
  1248. <p>Enables optimization and enables debug info compiler
  1249. options. (Disabled by default.)</p>
  1250. </li>
  1251. <li>
  1252. <p>
  1253. <span class="codefrag command">--without-syncapi </span>
  1254. </p>
  1255. <p>Disables Sync API support; zookeeper_mt library won't be
  1256. built. (Enabled by default.)</p>
  1257. </li>
  1258. <li>
  1259. <p>
  1260. <span class="codefrag command">--disable-static </span>
  1261. </p>
  1262. <p>Do not build static libraries. (Enabled by
  1263. default.)</p>
  1264. </li>
  1265. <li>
  1266. <p>
  1267. <span class="codefrag command">--disable-shared</span>
  1268. </p>
  1269. <p>Do not build shared libraries. (Enabled by
  1270. default.)</p>
  1271. </li>
  1272. </ul>
  1273. <div class="note">
  1274. <div class="label">Note</div>
  1275. <div class="content">
  1276. <p>See INSTALL for general information about running
  1277. <strong>configure</strong>.</p>
  1278. </div>
  1279. </div>
  1280. </li>
  1281. <li>
  1282. <p>Run <span class="codefrag command">make</span> or <span class="codefrag command">make
  1283. install</span> to build the libraries and install them.</p>
  1284. </li>
  1285. <li>
  1286. <p>To generate doxygen documentation for the ZooKeeper API, run
  1287. <span class="codefrag command">make doxygen-doc</span>. All documentation will be
  1288. placed in a new subfolder named docs. By default, this command
  1289. only generates HTML. For information on other document formats,
  1290. run <span class="codefrag command">./configure --help</span>
  1291. </p>
  1292. </li>
  1293. </ol>
  1294. <a name="N10597"></a><a name="Using+the+C+Client"></a>
  1295. <h4>Using the C Client</h4>
  1296. <p>You can test your client by running a ZooKeeper server (see
  1297. instructions on the project wiki page on how to run it) and connecting
  1298. to it using one of the cli applications that were built as part of the
  1299. installation procedure. cli_mt (multithreaded, built against
  1300. zookeeper_mt library) is shown in this example, but you could also use
  1301. cli_st (singlethreaded, built against zookeeper_st library):</p>
  1302. <p>
  1303. <span class="codefrag command">$ cli_mt zookeeper_host:9876</span>
  1304. </p>
  1305. <p>This is a client application that gives you a shell for
  1306. executing simple ZooKeeper commands. Once successfully started
  1307. and connected to the server it displays a shell prompt. You
  1308. can now enter ZooKeeper commands. For example, to create a
  1309. node:</p>
  1310. <p>
  1311. <span class="codefrag command">&gt; create /my_new_node</span>
  1312. </p>
  1313. <p>To verify that the node's been created:</p>
  1314. <p>
  1315. <span class="codefrag command">&gt; ls /</span>
  1316. </p>
  1317. <p>You should see a list of node who are children of the root node
  1318. "/".</p>
  1319. <p>In order to be able to use the ZooKeeper API in your application
  1320. you have to remember to</p>
  1321. <ol>
  1322. <li>
  1323. <p>Include ZooKeeper header: #include
  1324. &lt;zookeeper/zookeeper.h</p>
  1325. </li>
  1326. <li>
  1327. <p>If you are building a multithreaded client, compile with
  1328. -DTHREADED compiler flag to enable the multi-threaded version of
  1329. the library, and then link against against the
  1330. <em>zookeeper_mt</em> library. If you are building a
  1331. single-threaded client, do not compile with -DTHREADED, and be
  1332. sure to link against the<em> zookeeper_st
  1333. </em>library.</p>
  1334. </li>
  1335. </ol>
  1336. <p>Refer to <a href="#ch_programStructureWithExample">Program Structure, with Simple Example</a>
  1337. for examples of usage in Java and C.
  1338. <em>[tbd]</em>
  1339. </p>
  1340. </div>
  1341. <a name="N105DD"></a><a name="ch_guideToZkOperations"></a>
  1342. <h2 class="h3">Building Blocks: A Guide to ZooKeeper Operations</h2>
  1343. <div class="section">
  1344. <p>This section surveys all the operations a developer can perform
  1345. against a ZooKeeper server. It is lower level information than the earlier
  1346. concepts chapters in this manual, but higher level than the ZooKeeper API
  1347. Reference. It covers these topics:</p>
  1348. <ul>
  1349. <li>
  1350. <p>
  1351. <a href="#sc_connectingToZk">Connecting to ZooKeeper</a>
  1352. </p>
  1353. </li>
  1354. </ul>
  1355. <a name="N105F1"></a><a name="sc_connectingToZk"></a>
  1356. <h3 class="h4">Connecting to ZooKeeper</h3>
  1357. <p></p>
  1358. <a name="N105FA"></a><a name="sc_readOps"></a>
  1359. <h3 class="h4">Read Operations</h3>
  1360. <p></p>
  1361. <a name="N10603"></a><a name="sc_writeOps"></a>
  1362. <h3 class="h4">Write Operations</h3>
  1363. <p></p>
  1364. <a name="N1060C"></a><a name="sc_handlingWatches"></a>
  1365. <h3 class="h4">Handling Watches</h3>
  1366. <p></p>
  1367. <a name="N10615"></a><a name="sc_miscOps"></a>
  1368. <h3 class="h4">Miscelleaneous ZooKeeper Operations</h3>
  1369. <p></p>
  1370. </div>
  1371. <a name="N1061F"></a><a name="ch_programStructureWithExample"></a>
  1372. <h2 class="h3">Program Structure, with Simple Example</h2>
  1373. <div class="section">
  1374. <p>
  1375. <em>[tbd]</em>
  1376. </p>
  1377. </div>
  1378. <a name="N1062A"></a><a name="ch_gotchas"></a>
  1379. <h2 class="h3">Gotchas: Common Problems and Troubleshooting</h2>
  1380. <div class="section">
  1381. <p>So now you know ZooKeeper. It's fast, simple, your application
  1382. works, but wait ... something's wrong. Here are some pitfalls that
  1383. ZooKeeper users fall into:</p>
  1384. <ol>
  1385. <li>
  1386. <p>If you are using watches, you must look for the connected watch
  1387. event. When a ZooKeeper client disconnects from a server, you will
  1388. not receive notification of changes until reconnected. If you are
  1389. watching for a znode to come into existance, you will miss the event
  1390. if the znode is created and deleted while you are disconnected.</p>
  1391. </li>
  1392. <li>
  1393. <p>You must test ZooKeeper server failures. The ZooKeeper service
  1394. can survive failures as long as a majority of servers are active. The
  1395. question to ask is: can your application handle it? In the real world
  1396. a client's connection to ZooKeeper can break. (ZooKeeper server
  1397. failures and network partitions are common reasons for connection
  1398. loss.) The ZooKeeper client library takes care of recovering your
  1399. connection and letting you know what happened, but you must make sure
  1400. that you recover your state and any outstanding requests that failed.
  1401. Find out if you got it right in the test lab, not in production - test
  1402. with a ZooKeeper service made up of a several of servers and subject
  1403. them to reboots.</p>
  1404. </li>
  1405. <li>
  1406. <p>The list of ZooKeeper servers used by the client must match the
  1407. list of ZooKeeper servers that each ZooKeeper server has. Things can
  1408. work, although not optimally, if the client list is a subset of the
  1409. real list of ZooKeeper servers, but not if the client lists ZooKeeper
  1410. servers not in the ZooKeeper cluster.</p>
  1411. </li>
  1412. <li>
  1413. <p>Be careful where you put that transaction log. The most
  1414. performance-critical part of ZooKeeper is the transaction log.
  1415. ZooKeeper must sync transactions to media before it returns a
  1416. response. A dedicated transaction log device is key to consistent good
  1417. performance. Putting the log on a busy device will adversely effect
  1418. performance. If you only have one storage device, put trace files on
  1419. NFS and increase the snapshotCount; it doesn't eliminate the problem,
  1420. but it can mitigate it.</p>
  1421. </li>
  1422. <li>
  1423. <p>Set your Java max heap size correctly. It is very important to
  1424. <em>avoid swapping.</em> Going to disk unnecessarily will
  1425. almost certainly degrade your performance unacceptably. Remember, in
  1426. ZooKeeper, everything is ordered, so if one request hits the disk, all
  1427. other queued requests hit the disk.</p>
  1428. <p>To avoid swapping, try to set the heapsize to the amount of
  1429. physical memory you have, minus the amount needed by the OS and cache.
  1430. The best way to determine an optimal heap size for your configurations
  1431. is to <em>run load tests</em>. If for some reason you
  1432. can't, be conservative in your estimates and choose a number well
  1433. below the limit that would cause your machine to swap. For example, on
  1434. a 4G machine, a 3G heap is a conservative estimate to start
  1435. with.</p>
  1436. </li>
  1437. </ol>
  1438. </div>
  1439. <a name="apx_linksToOtherInfo"></a>
  1440. <appendix id="apx_linksToOtherInfo">
  1441. <title>Links to Other Information</title>
  1442. <p>Outside the formal documentation, there're several other sources of
  1443. information for ZooKeeper developers.</p>
  1444. <dl>
  1445. <dt>
  1446. <term>ZooKeeper Whitepaper <em>[tbd: find url]</em>
  1447. </term>
  1448. </dt>
  1449. <dd>
  1450. <p>The definitive discussion of ZooKeeper design and performance,
  1451. by Yahoo! Research</p>
  1452. </dd>
  1453. <dt>
  1454. <term>API Reference <em>[tbd: find url]</em>
  1455. </term>
  1456. </dt>
  1457. <dd>
  1458. <p>The complete reference to the ZooKeeper API</p>
  1459. </dd>
  1460. <dt>
  1461. <term>
  1462. <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/zookeeper.m4v">ZooKeeper
  1463. Talk at the Hadoup Summit 2008</a>
  1464. </term>
  1465. </dt>
  1466. <dd>
  1467. <p>A video introduction to ZooKeeper, by Benjamin Reed of Yahoo!
  1468. Research</p>
  1469. </dd>
  1470. <dt>
  1471. <term>
  1472. <a href="http://wiki.apache.org/hadoop/ZooKeeper/Tutorial">Barrier and
  1473. Queue Tutorial</a>
  1474. </term>
  1475. </dt>
  1476. <dd>
  1477. <p>The excellent Java tutorial by Flavio Junqueira, implementing
  1478. simple barriers and producer-consumer queues using ZooKeeper.</p>
  1479. </dd>
  1480. <dt>
  1481. <term>
  1482. <a href="http://wiki.apache.org/hadoop/ZooKeeper/ZooKeeperArticles">ZooKeeper
  1483. - A Reliable, Scalable Distributed Coordination System</a>
  1484. </term>
  1485. </dt>
  1486. <dd>
  1487. <p>An article by Todd Hoff (07/15/2008)</p>
  1488. </dd>
  1489. <dt>
  1490. <term>
  1491. <a href="recipes.html">ZooKeeper Recipes</a>
  1492. </term>
  1493. </dt>
  1494. <dd>
  1495. <p>Pseudo-level discussion of the implementation of various
  1496. synchronization solutions with ZooKeeper: Event Handles, Queues,
  1497. Locks, and Two-phase Commits.</p>
  1498. </dd>
  1499. <dt>
  1500. <term>
  1501. <em>[tbd]</em>
  1502. </term>
  1503. </dt>
  1504. <dd>
  1505. <p>Any other good sources anyone can think of...</p>
  1506. </dd>
  1507. </dl>
  1508. </appendix>
  1509. <p align="right">
  1510. <font size="-2"></font>
  1511. </p>
  1512. </div>
  1513. <!--+
  1514. |end content
  1515. +-->
  1516. <div class="clearboth">&nbsp;</div>
  1517. </div>
  1518. <div id="footer">
  1519. <!--+
  1520. |start bottomstrip
  1521. +-->
  1522. <div class="lastmodified">
  1523. <script type="text/javascript"><!--
  1524. document.write("Last Published: " + document.lastModified);
  1525. // --></script>
  1526. </div>
  1527. <div class="copyright">
  1528. Copyright &copy;
  1529. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  1530. </div>
  1531. <!--+
  1532. |end bottomstrip
  1533. +-->
  1534. </div>
  1535. </body>
  1536. </html>