zookeeperProgrammers.html 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <meta content="Apache Forrest" name="Generator">
  6. <meta name="Forrest-version" content="0.9">
  7. <meta name="Forrest-skin-name" content="pelt">
  8. <title>ZooKeeper 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://zookeeper.apache.org/">ZooKeeper</a> &gt; <a href="http://zookeeper.apache.org/">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://zookeeper.apache.org/"><img class="logoImage" alt="ZooKeeper" src="images/zookeeper_small.gif" title="ZooKeeper: distributed coordination"></a>
  43. </div>
  44. <!--+
  45. |end Project Logo
  46. +-->
  47. <!--+
  48. |start Search
  49. +-->
  50. <div class="searchbox">
  51. <form action="http://www.google.com/search" method="get" class="roundtopsmall">
  52. <input value="zookeeper.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://zookeeper.apache.org/">Project</a>
  65. </li>
  66. <li>
  67. <a class="unselected" href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/">Wiki</a>
  68. </li>
  69. <li class="current">
  70. <a class="selected" href="index.html">ZooKeeper 3.4 Documentation</a>
  71. </li>
  72. </ul>
  73. <!--+
  74. |end Tabs
  75. +-->
  76. </div>
  77. </div>
  78. <div id="main">
  79. <div id="publishedStrip">
  80. <!--+
  81. |start Subtabs
  82. +-->
  83. <div id="level2tabs"></div>
  84. <!--+
  85. |end Endtabs
  86. +-->
  87. <script type="text/javascript"><!--
  88. document.write("Last Published: " + document.lastModified);
  89. // --></script>
  90. </div>
  91. <!--+
  92. |breadtrail
  93. +-->
  94. <div class="breadtrail">
  95. &nbsp;
  96. </div>
  97. <!--+
  98. |start Menu, mainarea
  99. +-->
  100. <!--+
  101. |start Menu
  102. +-->
  103. <div id="menu">
  104. <div onclick="SwitchMenu('menu_1.1', 'skin/')" id="menu_1.1Title" class="menutitle">Overview</div>
  105. <div id="menu_1.1" class="menuitemgroup">
  106. <div class="menuitem">
  107. <a href="index.html">Welcome</a>
  108. </div>
  109. <div class="menuitem">
  110. <a href="zookeeperOver.html">Overview</a>
  111. </div>
  112. <div class="menuitem">
  113. <a href="zookeeperStarted.html">Getting Started</a>
  114. </div>
  115. <div class="menuitem">
  116. <a href="releasenotes.html">Release Notes</a>
  117. </div>
  118. </div>
  119. <div onclick="SwitchMenu('menu_selected_1.2', 'skin/')" id="menu_selected_1.2Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">Developer</div>
  120. <div id="menu_selected_1.2" class="selectedmenuitemgroup" style="display: block;">
  121. <div class="menuitem">
  122. <a href="api/index.html">API Docs</a>
  123. </div>
  124. <div class="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 class="menuitem">
  143. <a href="zookeeperQuotas.html">Quota Guide</a>
  144. </div>
  145. <div class="menuitem">
  146. <a href="zookeeperJMX.html">JMX</a>
  147. </div>
  148. <div class="menuitem">
  149. <a href="zookeeperObservers.html">Observers Guide</a>
  150. </div>
  151. </div>
  152. <div onclick="SwitchMenu('menu_1.4', 'skin/')" id="menu_1.4Title" class="menutitle">Contributor</div>
  153. <div id="menu_1.4" class="menuitemgroup">
  154. <div class="menuitem">
  155. <a href="zookeeperInternals.html">ZooKeeper Internals</a>
  156. </div>
  157. </div>
  158. <div onclick="SwitchMenu('menu_1.5', 'skin/')" id="menu_1.5Title" class="menutitle">Miscellaneous</div>
  159. <div id="menu_1.5" class="menuitemgroup">
  160. <div class="menuitem">
  161. <a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER">Wiki</a>
  162. </div>
  163. <div class="menuitem">
  164. <a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/FAQ">FAQ</a>
  165. </div>
  166. <div class="menuitem">
  167. <a href="http://zookeeper.apache.org/mailing_lists.html">Mailing Lists</a>
  168. </div>
  169. </div>
  170. <div id="credit"></div>
  171. <div id="roundbottom">
  172. <img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
  173. <!--+
  174. |alternative credits
  175. +-->
  176. <div id="credit2"></div>
  177. </div>
  178. <!--+
  179. |end Menu
  180. +-->
  181. <!--+
  182. |start content
  183. +-->
  184. <div id="content">
  185. <div title="Portable Document Format" class="pdflink">
  186. <a class="dida" href="zookeeperProgrammers.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
  187. PDF</a>
  188. </div>
  189. <h1>ZooKeeper Programmer's Guide</h1>
  190. <h3>Developing Distributed Applications that use ZooKeeper</h3>
  191. <div id="front-matter">
  192. <div id="minitoc-area">
  193. <ul class="minitoc">
  194. <li>
  195. <a href="#_introduction">Introduction</a>
  196. </li>
  197. <li>
  198. <a href="#ch_zkDataModel">The ZooKeeper Data Model</a>
  199. <ul class="minitoc">
  200. <li>
  201. <a href="#sc_zkDataModel_znodes">ZNodes</a>
  202. <ul class="minitoc">
  203. <li>
  204. <a href="#sc_zkDataMode_watches">Watches</a>
  205. </li>
  206. <li>
  207. <a href="#Data+Access">Data Access</a>
  208. </li>
  209. <li>
  210. <a href="#Ephemeral+Nodes">Ephemeral Nodes</a>
  211. </li>
  212. <li>
  213. <a href="#Sequence+Nodes+--+Unique+Naming">Sequence Nodes -- Unique Naming</a>
  214. </li>
  215. </ul>
  216. </li>
  217. <li>
  218. <a href="#sc_timeInZk">Time in ZooKeeper</a>
  219. </li>
  220. <li>
  221. <a href="#sc_zkStatStructure">ZooKeeper Stat Structure</a>
  222. </li>
  223. </ul>
  224. </li>
  225. <li>
  226. <a href="#ch_zkSessions">ZooKeeper Sessions</a>
  227. </li>
  228. <li>
  229. <a href="#ch_zkWatches">ZooKeeper Watches</a>
  230. <ul class="minitoc">
  231. <li>
  232. <a href="#sc_WatchGuarantees">What ZooKeeper Guarantees about Watches</a>
  233. </li>
  234. <li>
  235. <a href="#sc_WatchRememberThese">Things to Remember about Watches</a>
  236. </li>
  237. </ul>
  238. </li>
  239. <li>
  240. <a href="#sc_ZooKeeperAccessControl">ZooKeeper access control using ACLs</a>
  241. <ul class="minitoc">
  242. <li>
  243. <a href="#sc_ACLPermissions">ACL Permissions</a>
  244. <ul class="minitoc">
  245. <li>
  246. <a href="#sc_BuiltinACLSchemes">Builtin ACL Schemes</a>
  247. </li>
  248. <li>
  249. <a href="#ZooKeeper+C+client+API">ZooKeeper C client API</a>
  250. </li>
  251. </ul>
  252. </li>
  253. </ul>
  254. </li>
  255. <li>
  256. <a href="#sc_ZooKeeperPluggableAuthentication">Pluggable ZooKeeper authentication</a>
  257. </li>
  258. <li>
  259. <a href="#ch_zkGuarantees">Consistency Guarantees</a>
  260. </li>
  261. <li>
  262. <a href="#ch_bindings">Bindings</a>
  263. <ul class="minitoc">
  264. <li>
  265. <a href="#Java+Binding">Java Binding</a>
  266. </li>
  267. <li>
  268. <a href="#C+Binding">C Binding</a>
  269. <ul class="minitoc">
  270. <li>
  271. <a href="#Installation">Installation</a>
  272. </li>
  273. <li>
  274. <a href="#Using+the+C+Client">Using the C Client</a>
  275. </li>
  276. </ul>
  277. </li>
  278. </ul>
  279. </li>
  280. <li>
  281. <a href="#ch_guideToZkOperations">Building Blocks: A Guide to ZooKeeper Operations</a>
  282. <ul class="minitoc">
  283. <li>
  284. <a href="#sc_errorsZk">Handling Errors</a>
  285. </li>
  286. <li>
  287. <a href="#sc_connectingToZk">Connecting to ZooKeeper</a>
  288. </li>
  289. <li>
  290. <a href="#sc_readOps">Read Operations</a>
  291. </li>
  292. <li>
  293. <a href="#sc_writeOps">Write Operations</a>
  294. </li>
  295. <li>
  296. <a href="#sc_handlingWatches">Handling Watches</a>
  297. </li>
  298. <li>
  299. <a href="#sc_miscOps">Miscelleaneous ZooKeeper Operations</a>
  300. </li>
  301. </ul>
  302. </li>
  303. <li>
  304. <a href="#ch_programStructureWithExample">Program Structure, with Simple Example</a>
  305. </li>
  306. <li>
  307. <a href="#ch_gotchas">Gotchas: Common Problems and Troubleshooting</a>
  308. </li>
  309. </ul>
  310. </div>
  311. </div>
  312. <a name="_introduction"></a>
  313. <h2 class="h3">Introduction</h2>
  314. <div class="section">
  315. <p>This document is a guide for developers wishing to create
  316. distributed applications that take advantage of ZooKeeper's coordination
  317. services. It contains conceptual and practical information.</p>
  318. <p>The first four sections of this guide present higher level
  319. discussions of various ZooKeeper concepts. These are necessary both for an
  320. understanding of how ZooKeeper works as well how to work with it. It does
  321. not contain source code, but it does assume a familiarity with the
  322. problems associated with distributed computing. The sections in this first
  323. group are:</p>
  324. <ul>
  325. <li>
  326. <p>
  327. <a href="#ch_zkDataModel">The ZooKeeper Data Model</a>
  328. </p>
  329. </li>
  330. <li>
  331. <p>
  332. <a href="#ch_zkSessions">ZooKeeper Sessions</a>
  333. </p>
  334. </li>
  335. <li>
  336. <p>
  337. <a href="#ch_zkWatches">ZooKeeper Watches</a>
  338. </p>
  339. </li>
  340. <li>
  341. <p>
  342. <a href="#ch_zkGuarantees">Consistency Guarantees</a>
  343. </p>
  344. </li>
  345. </ul>
  346. <p>The next four sections provide practical programming
  347. information. These are:</p>
  348. <ul>
  349. <li>
  350. <p>
  351. <a href="#ch_guideToZkOperations">Building Blocks: A Guide to ZooKeeper Operations</a>
  352. </p>
  353. </li>
  354. <li>
  355. <p>
  356. <a href="#ch_bindings">Bindings</a>
  357. </p>
  358. </li>
  359. <li>
  360. <p>
  361. <a href="#ch_programStructureWithExample">Program Structure, with Simple Example</a>
  362. <em>[tbd]</em>
  363. </p>
  364. </li>
  365. <li>
  366. <p>
  367. <a href="#ch_gotchas">Gotchas: Common Problems and Troubleshooting</a>
  368. </p>
  369. </li>
  370. </ul>
  371. <p>The book concludes with an <a href="#apx_linksToOtherInfo">appendix</a> containing links to other
  372. useful, ZooKeeper-related information.</p>
  373. <p>Most of information in this document is written to be accessible as
  374. stand-alone reference material. However, before starting your first
  375. ZooKeeper application, you should probably at least read the chaptes on
  376. the <a href="#ch_zkDataModel">ZooKeeper Data Model</a> and <a href="#ch_guideToZkOperations">ZooKeeper Basic Operations</a>. Also,
  377. the <a href="#ch_programStructureWithExample">Simple Programmming
  378. Example</a> <em>[tbd]</em> is helpful for understanding the basic
  379. structure of a ZooKeeper client application.</p>
  380. </div>
  381. <a name="ch_zkDataModel"></a>
  382. <h2 class="h3">The ZooKeeper Data Model</h2>
  383. <div class="section">
  384. <p>ZooKeeper has a hierarchal name space, much like a distributed file
  385. system. The only difference is that each node in the namespace can have
  386. data associated with it as well as children. It is like having a file
  387. system that allows a file to also be a directory. Paths to nodes are
  388. always expressed as canonical, absolute, slash-separated paths; there are
  389. no relative reference. Any unicode character can be used in a path subject
  390. to the following constraints:</p>
  391. <ul>
  392. <li>
  393. <p>The null character (\u0000) cannot be part of a path name. (This
  394. causes problems with the C binding.)</p>
  395. </li>
  396. <li>
  397. <p>The following characters can't be used because they don't
  398. display well, or render in confusing ways: \u0001 - \u001F and \u007F
  399. - \u009F.</p>
  400. </li>
  401. <li>
  402. <p>The following characters are not allowed: \ud800 - uF8FF,
  403. \uFFF0 - uFFFF, \uXFFFE - \uXFFFF (where X is a digit 1 - E), \uF0000 -
  404. \uFFFFF.</p>
  405. </li>
  406. <li>
  407. <p>The "." character can be used as part of another name, but "."
  408. and ".." cannot alone be used to indicate a node along a path,
  409. because ZooKeeper doesn't use relative paths. The following would be
  410. invalid: "/a/b/./c" or "/a/b/../c".</p>
  411. </li>
  412. <li>
  413. <p>The token "zookeeper" is reserved.</p>
  414. </li>
  415. </ul>
  416. <a name="sc_zkDataModel_znodes"></a>
  417. <h3 class="h4">ZNodes</h3>
  418. <p>Every node in a ZooKeeper tree is referred to as a
  419. <em>znode</em>. Znodes maintain a stat structure that
  420. includes version numbers for data changes, acl changes. The stat
  421. structure also has timestamps. The version number, together with the
  422. timestamp, allows ZooKeeper to validate the cache and to coordinate
  423. updates. Each time a znode's data changes, the version number increases.
  424. For instance, whenever a client retrieves data, it also receives the
  425. version of the data. And when a client performs an update or a delete,
  426. it must supply the version of the data of the znode it is changing. If
  427. the version it supplies doesn't match the actual version of the data,
  428. the update will fail. (This behavior can be overridden. For more
  429. information see... )<em>[tbd...]</em>
  430. </p>
  431. <div class="note">
  432. <div class="label">Note</div>
  433. <div class="content">
  434. <p>In distributed application engineering, the word
  435. <em>node</em> can refer to a generic host machine, a
  436. server, a member of an ensemble, a client process, etc. In the ZooKeeper
  437. documentation, <em>znodes</em> refer to the data nodes.
  438. <em>Servers</em> refer to machines that make up the
  439. ZooKeeper service; <em>quorum peers</em> refer to the
  440. servers that make up an ensemble; client refers to any host or process
  441. which uses a ZooKeeper service.</p>
  442. </div>
  443. </div>
  444. <p>Znodes are the main enitity that a programmer access. They have
  445. several characteristics that are worth mentioning here.</p>
  446. <a name="sc_zkDataMode_watches"></a>
  447. <h4>Watches</h4>
  448. <p>Clients can set watches on znodes. Changes to that znode trigger
  449. the watch and then clear the watch. When a watch triggers, ZooKeeper
  450. sends the client a notification. More information about watches can be
  451. found in the section
  452. <a href="#ch_zkWatches">ZooKeeper Watches</a>.</p>
  453. <a name="Data+Access"></a>
  454. <h4>Data Access</h4>
  455. <p>The data stored at each znode in a namespace is read and written
  456. atomically. Reads get all the data bytes associated with a znode and a
  457. write replaces all the data. Each node has an Access Control List
  458. (ACL) that restricts who can do what.</p>
  459. <p>ZooKeeper was not designed to be a general database or large
  460. object store. Instead, it manages coordination data. This data can
  461. come in the form of configuration, status information, rendezvous, etc.
  462. A common property of the various forms of coordination data is that
  463. they are relatively small: measured in kilobytes.
  464. The ZooKeeper client and the server implementations have sanity checks
  465. to ensure that znodes have less than 1M of data, but the data should
  466. be much less than that on average. Operating on relatively large data
  467. sizes will cause some operations to take much more time than others and
  468. will affect the latencies of some operations because of the extra time
  469. needed to move more data over the network and onto storage media. If
  470. large data storage is needed, the usually pattern of dealing with such
  471. data is to store it on a bulk storage system, such as NFS or HDFS, and
  472. store pointers to the storage locations in ZooKeeper.</p>
  473. <a name="Ephemeral+Nodes"></a>
  474. <h4>Ephemeral Nodes</h4>
  475. <p>ZooKeeper also has the notion of ephemeral nodes. These znodes
  476. exists as long as the session that created the znode is active. When
  477. the session ends the znode is deleted. Because of this behavior
  478. ephemeral znodes are not allowed to have children.</p>
  479. <a name="Sequence+Nodes+--+Unique+Naming"></a>
  480. <h4>Sequence Nodes -- Unique Naming</h4>
  481. <p>When creating a znode you can also request that
  482. ZooKeeper append a monotonically increasing counter to the end
  483. of path. This counter is unique to the parent znode. The
  484. counter has a format of %010d -- that is 10 digits with 0
  485. (zero) padding (the counter is formatted in this way to
  486. simplify sorting), i.e. "&lt;path&gt;0000000001". See
  487. <a href="recipes.html#sc_recipes_Queues">Queue
  488. Recipe</a> for an example use of this feature. Note: the
  489. counter used to store the next sequence number is a signed int
  490. (4bytes) maintained by the parent node, the counter will
  491. overflow when incremented beyond 2147483647 (resulting in a
  492. name "&lt;path&gt;-2147483647").</p>
  493. <a name="sc_timeInZk"></a>
  494. <h3 class="h4">Time in ZooKeeper</h3>
  495. <p>ZooKeeper tracks time multiple ways:</p>
  496. <ul>
  497. <li>
  498. <p>
  499. <strong>Zxid</strong>
  500. </p>
  501. <p>Every change to the ZooKeeper state receives a stamp in the
  502. form of a <em>zxid</em> (ZooKeeper Transaction Id).
  503. This exposes the total ordering of all changes to ZooKeeper. Each
  504. change will have a unique zxid and if zxid1 is smaller than zxid2
  505. then zxid1 happened before zxid2.</p>
  506. </li>
  507. <li>
  508. <p>
  509. <strong>Version numbers</strong>
  510. </p>
  511. <p>Every change to a node will cause an increase to one of the
  512. version numbers of that node. The three version numbers are version
  513. (number of changes to the data of a znode), cversion (number of
  514. changes to the children of a znode), and aversion (number of changes
  515. to the ACL of a znode).</p>
  516. </li>
  517. <li>
  518. <p>
  519. <strong>Ticks</strong>
  520. </p>
  521. <p>When using multi-server ZooKeeper, servers use ticks to define
  522. timing of events such as status uploads, session timeouts,
  523. connection timeouts between peers, etc. The tick time is only
  524. indirectly exposed through the minimum session timeout (2 times the
  525. tick time); if a client requests a session timeout less than the
  526. minimum session timeout, the server will tell the client that the
  527. session timeout is actually the minimum session timeout.</p>
  528. </li>
  529. <li>
  530. <p>
  531. <strong>Real time</strong>
  532. </p>
  533. <p>ZooKeeper doesn't use real time, or clock time, at all except
  534. to put timestamps into the stat structure on znode creation and
  535. znode modification.</p>
  536. </li>
  537. </ul>
  538. <a name="sc_zkStatStructure"></a>
  539. <h3 class="h4">ZooKeeper Stat Structure</h3>
  540. <p>The Stat structure for each znode in ZooKeeper is made up of the
  541. following fields:</p>
  542. <ul>
  543. <li>
  544. <p>
  545. <strong>czxid</strong>
  546. </p>
  547. <p>The zxid of the change that caused this znode to be
  548. created.</p>
  549. </li>
  550. <li>
  551. <p>
  552. <strong>mzxid</strong>
  553. </p>
  554. <p>The zxid of the change that last modified this znode.</p>
  555. </li>
  556. <li>
  557. <p>
  558. <strong>ctime</strong>
  559. </p>
  560. <p>The time in milliseconds from epoch when this znode was
  561. created.</p>
  562. </li>
  563. <li>
  564. <p>
  565. <strong>mtime</strong>
  566. </p>
  567. <p>The time in milliseconds from epoch when this znode was last
  568. modified.</p>
  569. </li>
  570. <li>
  571. <p>
  572. <strong>version</strong>
  573. </p>
  574. <p>The number of changes to the data of this znode.</p>
  575. </li>
  576. <li>
  577. <p>
  578. <strong>cversion</strong>
  579. </p>
  580. <p>The number of changes to the children of this znode.</p>
  581. </li>
  582. <li>
  583. <p>
  584. <strong>aversion</strong>
  585. </p>
  586. <p>The number of changes to the ACL of this znode.</p>
  587. </li>
  588. <li>
  589. <p>
  590. <strong>ephemeralOwner</strong>
  591. </p>
  592. <p>The session id of the owner of this znode if the znode is an
  593. ephemeral node. If it is not an ephemeral node, it will be
  594. zero.</p>
  595. </li>
  596. <li>
  597. <p>
  598. <strong>dataLength</strong>
  599. </p>
  600. <p>The length of the data field of this znode.</p>
  601. </li>
  602. <li>
  603. <p>
  604. <strong>numChildren</strong>
  605. </p>
  606. <p>The number of children of this znode.</p>
  607. </li>
  608. </ul>
  609. </div>
  610. <a name="ch_zkSessions"></a>
  611. <h2 class="h3">ZooKeeper Sessions</h2>
  612. <div class="section">
  613. <p>A ZooKeeper client establishes a session with the ZooKeeper
  614. service by creating a handle to the service using a language
  615. binding. Once created, the handle starts of in the CONNECTING state
  616. and the client library tries to connect to one of the servers that
  617. make up the ZooKeeper service at which point it switches to the
  618. CONNECTED state. During normal operation will be in one of these
  619. two states. If an unrecoverable error occurs, such as session
  620. expiration or authentication failure, or if the application explicitly
  621. closes the handle, the handle will move to the CLOSED state.
  622. The following figure shows the possible state transitions of a
  623. ZooKeeper client:</p>
  624. <img alt="" src="images/state_dia.jpg"><p>To create a client session the application code must provide
  625. a connection string containing a comma separated list of host:port pairs,
  626. each corresponding to a ZooKeeper server (e.g. "127.0.0.1:4545" or
  627. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"). The ZooKeeper
  628. client library will pick an arbitrary server and try to connect to
  629. it. If this connection fails, or if the client becomes
  630. disconnected from the server for any reason, the client will
  631. automatically try the next server in the list, until a connection
  632. is (re-)established.</p>
  633. <p>
  634. <strong>Added in 3.2.0</strong>: An
  635. optional "chroot" suffix may also be appended to the connection
  636. string. This will run the client commands while interpreting all
  637. paths relative to this root (similar to the unix chroot
  638. command). If used the example would look like:
  639. "127.0.0.1:4545/app/a" or
  640. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" where the
  641. client would be rooted at "/app/a" and all paths would be relative
  642. to this root - ie getting/setting/etc... "/foo/bar" would result
  643. in operations being run on "/app/a/foo/bar" (from the server
  644. perspective). This feature is particularly useful in multi-tenant
  645. environments where each user of a particular ZooKeeper service
  646. could be rooted differently. This makes re-use much simpler as
  647. each user can code his/her application as if it were rooted at
  648. "/", while actual location (say /app/a) could be determined at
  649. deployment time.</p>
  650. <p>When a client gets a handle to the ZooKeeper service,
  651. ZooKeeper creates a ZooKeeper session, represented as a 64-bit
  652. number, that it assigns to the client. If the client connects to a
  653. different ZooKeeper server, it will send the session id as a part
  654. of the connection handshake. As a security measure, the server
  655. creates a password for the session id that any ZooKeeper server
  656. can validate.The password is sent to the client with the session
  657. id when the client establishes the session. The client sends this
  658. password with the session id whenever it reestablishes the session
  659. with a new server.</p>
  660. <p>One of the parameters to the ZooKeeper client library call
  661. to create a ZooKeeper session is the session timeout in
  662. milliseconds. The client sends a requested timeout, the server
  663. responds with the timeout that it can give the client. The current
  664. implementation requires that the timeout be a minimum of 2 times
  665. the tickTime (as set in the server configuration) and a maximum of
  666. 20 times the tickTime. The ZooKeeper client API allows access to
  667. the negotiated timeout.</p>
  668. <p>When a client (session) becomes partitioned from the ZK
  669. serving cluster it will begin searching the list of servers that
  670. were specified during session creation. Eventually, when
  671. connectivity between the client and at least one of the servers is
  672. re-established, the session will either again transition to the
  673. "connected" state (if reconnected within the session timeout
  674. value) or it will transition to the "expired" state (if
  675. reconnected after the session timeout). It is not advisable to
  676. create a new session object (a new ZooKeeper.class or zookeeper
  677. handle in the c binding) for disconnection. The ZK client library
  678. will handle reconnect for you. In particular we have heuristics
  679. built into the client library to handle things like "herd effect",
  680. etc... Only create a new session when you are notified of session
  681. expiration (mandatory).</p>
  682. <p>Session expiration is managed by the ZooKeeper cluster
  683. itself, not by the client. When the ZK client establishes a
  684. session with the cluster it provides a "timeout" value detailed
  685. above. This value is used by the cluster to determine when the
  686. client's session expires. Expirations happens when the cluster
  687. does not hear from the client within the specified session timeout
  688. period (i.e. no heartbeat). At session expiration the cluster will
  689. delete any/all ephemeral nodes owned by that session and
  690. immediately notify any/all connected clients of the change (anyone
  691. watching those znodes). At this point the client of the expired
  692. session is still disconnected from the cluster, it will not be
  693. notified of the session expiration until/unless it is able to
  694. re-establish a connection to the cluster. The client will stay in
  695. disconnected state until the TCP connection is re-established with
  696. the cluster, at which point the watcher of the expired session
  697. will receive the "session expired" notification.</p>
  698. <p>Example state transitions for an expired session as seen by
  699. the expired session's watcher:</p>
  700. <ol>
  701. <li>
  702. <p>'connected' : session is established and client
  703. is communicating with cluster (client/server communication is
  704. operating properly)</p>
  705. </li>
  706. <li>
  707. <p>.... client is partitioned from the
  708. cluster</p>
  709. </li>
  710. <li>
  711. <p>'disconnected' : client has lost connectivity
  712. with the cluster</p>
  713. </li>
  714. <li>
  715. <p>.... time elapses, after 'timeout' period the
  716. cluster expires the session, nothing is seen by client as it is
  717. disconnected from cluster</p>
  718. </li>
  719. <li>
  720. <p>.... time elapses, the client regains network
  721. level connectivity with the cluster</p>
  722. </li>
  723. <li>
  724. <p>'expired' : eventually the client reconnects to
  725. the cluster, it is then notified of the
  726. expiration</p>
  727. </li>
  728. </ol>
  729. <p>Another parameter to the ZooKeeper session establishment
  730. call is the default watcher. Watchers are notified when any state
  731. change occurs in the client. For example if the client loses
  732. connectivity to the server the client will be notified, or if the
  733. client's session expires, etc... This watcher should consider the
  734. initial state to be disconnected (i.e. before any state changes
  735. events are sent to the watcher by the client lib). In the case of
  736. a new connection, the first event sent to the watcher is typically
  737. the session connection event.</p>
  738. <p>The session is kept alive by requests sent by the client. If
  739. the session is idle for a period of time that would timeout the
  740. session, the client will send a PING request to keep the session
  741. alive. This PING request not only allows the ZooKeeper server to
  742. know that the client is still active, but it also allows the
  743. client to verify that its connection to the ZooKeeper server is
  744. still active. The timing of the PING is conservative enough to
  745. ensure reasonable time to detect a dead connection and reconnect
  746. to a new server.</p>
  747. <p>
  748. Once a connection to the server is successfully established
  749. (connected) there are basically two cases where the client lib generates
  750. connectionloss (the result code in c binding, exception in Java -- see
  751. the API documentation for binding specific details) when either a synchronous or
  752. asynchronous operation is performed and one of the following holds:
  753. </p>
  754. <ol>
  755. <li>
  756. <p>The application calls an operation on a session that is no
  757. longer alive/valid</p>
  758. </li>
  759. <li>
  760. <p>The ZooKeeper client disconnects from a server when there
  761. are pending operations to that server, i.e., there is a pending asynchronous call.
  762. </p>
  763. </li>
  764. </ol>
  765. <p>
  766. <strong>Added in 3.2.0 -- SessionMovedException</strong>. There is an internal
  767. exception that is generally not seen by clients called the SessionMovedException.
  768. This exception occurs because a request was received on a connection for a session
  769. which has been reestablished on a different server. The normal cause of this error is
  770. a client that sends a request to a server, but the network packet gets delayed, so
  771. the client times out and connects to a new server. When the delayed packet arrives at
  772. the first server, the old server detects that the session has moved, and closes the
  773. client connection. Clients normally do not see this error since they do not read
  774. from those old connections. (Old connections are usually closed.) One situation in which this
  775. condition can be seen is when two clients try to reestablish the same connection using
  776. a saved session id and password. One of the clients will reestablish the connection
  777. and the second client will be disconnected (causing the pair to attempt to re-establish
  778. its connection/session indefinitely).</p>
  779. <p>
  780. <strong>Updating the list of servers</strong>. We allow a client to
  781. update the connection string by providing a new comma separated list of host:port pairs,
  782. each corresponding to a ZooKeeper server. The function invokes a probabilistic load-balancing
  783. algorithm which may cause the client to disconnect from its current host with the goal
  784. to achieve expected uniform number of connections per server in the new list.
  785. In case the current host to which the client is connected is not in the new list
  786. this call will always cause the connection to be dropped. Otherwise, the decision
  787. is based on whether the number of servers has increased or decreased and by how much.
  788. </p>
  789. <p>
  790. For example, if the previous connection string contained 3 hosts and now the list contains
  791. these 3 hosts and 2 more hosts, 40% of clients connected to each of the 3 hosts will
  792. move to one of the new hosts in order to balance the load. The algorithm will cause the client
  793. to drop its connection to the current host to which it is connected with probability 0.4 and in this
  794. case cause the client to connect to one of the 2 new hosts, chosen at random.
  795. </p>
  796. <p>
  797. Another example -- suppose we have 5 hosts and now update the list to remove 2 of the hosts,
  798. the clients connected to the 3 remaining hosts will stay connected, whereas all clients connected
  799. to the 2 removed hosts will need to move to one of the 3 hosts, chosen at random. If the connection
  800. is dropped, the client moves to a special mode where he chooses a new server to connect to using the
  801. probabilistic algorithm, and not just round robin.
  802. </p>
  803. <p>
  804. In the first example, each client decides to disconnect with probability 0.4 but once the decision is
  805. made, it will try to connect to a random new server and only if it cannot connect to any of the new
  806. servers will it try to connect to the old ones. After finding a server, or trying all servers in the
  807. new list and failing to connect, the client moves back to the normal mode of operation where it picks
  808. an arbitrary server from the connectString and attempt to connect to it. If that fails, is will continue
  809. trying different random servers in round robin. (see above the algorithm used to initially choose a server)
  810. </p>
  811. </div>
  812. <a name="ch_zkWatches"></a>
  813. <h2 class="h3">ZooKeeper Watches</h2>
  814. <div class="section">
  815. <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
  816. side effect. Here is ZooKeeper's definition of a watch: a watch event is
  817. one-time trigger, sent to the client that set the watch, which occurs when
  818. the data for which the watch was set changes. There are three key points
  819. to consider in this definition of a watch:</p>
  820. <ul>
  821. <li>
  822. <p>
  823. <strong>One-time trigger</strong>
  824. </p>
  825. <p>One watch event will be sent to the client when the data has changed.
  826. For example, if a client does a getData("/znode1", true) and later the
  827. data for /znode1 is changed or deleted, the client will get a watch
  828. event for /znode1. If /znode1 changes again, no watch event will be
  829. sent unless the client has done another read that sets a new
  830. watch.</p>
  831. </li>
  832. <li>
  833. <p>
  834. <strong>Sent to the client</strong>
  835. </p>
  836. <p>This implies that an event is on the way to the client, but may
  837. not reach the client before the successful return code to the change
  838. operation reaches the client that initiated the change. Watches are
  839. sent asynchronously to watchers. ZooKeeper provides an ordering
  840. guarantee: a client will never see a change for which it has set a
  841. watch until it first sees the watch event. Network delays or other
  842. factors may cause different clients to see watches and return codes
  843. from updates at different times. The key point is that everything seen
  844. by the different clients will have a consistent order.</p>
  845. </li>
  846. <li>
  847. <p>
  848. <strong>The data for which the watch was
  849. set</strong>
  850. </p>
  851. <p>This refers to the different ways a node can change. It
  852. helps to think of ZooKeeper as maintaining two lists of
  853. watches: data watches and child watches. getData() and
  854. exists() set data watches. getChildren() sets child
  855. watches. Alternatively, it may help to think of watches being
  856. set according to the kind of data returned. getData() and
  857. exists() return information about the data of the node,
  858. whereas getChildren() returns a list of children. Thus,
  859. setData() will trigger data watches for the znode being set
  860. (assuming the set is successful). A successful create() will
  861. trigger a data watch for the znode being created and a child
  862. watch for the parent znode. A successful delete() will trigger
  863. both a data watch and a child watch (since there can be no
  864. more children) for a znode being deleted as well as a child
  865. watch for the parent znode.</p>
  866. </li>
  867. </ul>
  868. <p>Watches are maintained locally at the ZooKeeper server to which the
  869. client is connected. This allows watches to be lightweight to set,
  870. maintain, and dispatch. When a client connects to a new server, the watch
  871. will be triggered for any session events. Watches will not be received
  872. while disconnected from a server. When a client reconnects, any previously
  873. registered watches will be reregistered and triggered if needed. In
  874. general this all occurs transparently. There is one case where a watch
  875. may be missed: a watch for the existence of a znode not yet created will
  876. be missed if the znode is created and deleted while disconnected.</p>
  877. <a name="sc_WatchGuarantees"></a>
  878. <h3 class="h4">What ZooKeeper Guarantees about Watches</h3>
  879. <p>With regard to watches, ZooKeeper maintains these
  880. guarantees:</p>
  881. <ul>
  882. <li>
  883. <p>Watches are ordered with respect to other events, other
  884. watches, and asynchronous replies. The ZooKeeper client libraries
  885. ensures that everything is dispatched in order.</p>
  886. </li>
  887. </ul>
  888. <ul>
  889. <li>
  890. <p>A client will see a watch event for a znode it is watching
  891. before seeing the new data that corresponds to that znode.</p>
  892. </li>
  893. </ul>
  894. <ul>
  895. <li>
  896. <p>The order of watch events from ZooKeeper corresponds to the
  897. order of the updates as seen by the ZooKeeper service.</p>
  898. </li>
  899. </ul>
  900. <a name="sc_WatchRememberThese"></a>
  901. <h3 class="h4">Things to Remember about Watches</h3>
  902. <ul>
  903. <li>
  904. <p>Watches are one time triggers; if you get a watch event and
  905. you want to get notified of future changes, you must set another
  906. watch.</p>
  907. </li>
  908. </ul>
  909. <ul>
  910. <li>
  911. <p>Because watches are one time triggers and there is latency
  912. between getting the event and sending a new request to get a watch
  913. you cannot reliably see every change that happens to a node in
  914. ZooKeeper. Be prepared to handle the case where the znode changes
  915. multiple times between getting the event and setting the watch
  916. again. (You may not care, but at least realize it may
  917. happen.)</p>
  918. </li>
  919. </ul>
  920. <ul>
  921. <li>
  922. <p>A watch object, or function/context pair, will only be
  923. triggered once for a given notification. For example, if the same
  924. watch object is registered for an exists and a getData call for the
  925. same file and that file is then deleted, the watch object would
  926. only be invoked once with the deletion notification for the file.
  927. </p>
  928. </li>
  929. </ul>
  930. <ul>
  931. <li>
  932. <p>When you disconnect from a server (for example, when the
  933. server fails), you will not get any watches until the connection
  934. is reestablished. For this reason session events are sent to all
  935. outstanding watch handlers. Use session events to go into a safe
  936. mode: you will not be receiving events while disconnected, so your
  937. process should act conservatively in that mode.</p>
  938. </li>
  939. </ul>
  940. </div>
  941. <a name="sc_ZooKeeperAccessControl"></a>
  942. <h2 class="h3">ZooKeeper access control using ACLs</h2>
  943. <div class="section">
  944. <p>ZooKeeper uses ACLs to control access to its znodes (the
  945. data nodes of a ZooKeeper data tree). The ACL implementation is
  946. quite similar to UNIX file access permissions: it employs
  947. permission bits to allow/disallow various operations against a
  948. node and the scope to which the bits apply. Unlike standard UNIX
  949. permissions, a ZooKeeper node is not limited by the three standard
  950. scopes for user (owner of the file), group, and world
  951. (other). ZooKeeper does not have a notion of an owner of a
  952. znode. Instead, an ACL specifies sets of ids and permissions that
  953. are associated with those ids.</p>
  954. <p>Note also that an ACL pertains only to a specific znode. In
  955. particular it does not apply to children. For example, if
  956. <em>/app</em> is only readable by ip:172.16.16.1 and
  957. <em>/app/status</em> is world readable, anyone will
  958. be able to read <em>/app/status</em>; ACLs are not
  959. recursive.</p>
  960. <p>ZooKeeper supports pluggable authentication schemes. Ids are
  961. specified using the form <em>scheme:id</em>,
  962. where <em>scheme</em> is a the authentication scheme
  963. that the id corresponds to. For
  964. example, <em>ip:172.16.16.1</em> is an id for a
  965. host with the address <em>172.16.16.1</em>.</p>
  966. <p>When a client connects to ZooKeeper and authenticates
  967. itself, ZooKeeper associates all the ids that correspond to a
  968. client with the clients connection. These ids are checked against
  969. the ACLs of znodes when a clients tries to access a node. ACLs are
  970. made up of pairs of <em>(scheme:expression,
  971. perms)</em>. The format of
  972. the <em>expression</em> is specific to the scheme. For
  973. example, the pair <em>(ip:19.22.0.0/16, READ)</em>
  974. gives the <em>READ</em> permission to any clients with
  975. an IP address that starts with 19.22.</p>
  976. <a name="sc_ACLPermissions"></a>
  977. <h3 class="h4">ACL Permissions</h3>
  978. <p>ZooKeeper supports the following permissions:</p>
  979. <ul>
  980. <li>
  981. <p>
  982. <strong>CREATE</strong>: you can create a child node</p>
  983. </li>
  984. <li>
  985. <p>
  986. <strong>READ</strong>: you can get data from a node and list its children.</p>
  987. </li>
  988. <li>
  989. <p>
  990. <strong>WRITE</strong>: you can set data for a node</p>
  991. </li>
  992. <li>
  993. <p>
  994. <strong>DELETE</strong>: you can delete a child node</p>
  995. </li>
  996. <li>
  997. <p>
  998. <strong>ADMIN</strong>: you can set permissions</p>
  999. </li>
  1000. </ul>
  1001. <p>The <em>CREATE</em>
  1002. and <em>DELETE</em> permissions have been broken out
  1003. of the <em>WRITE</em> permission for finer grained
  1004. access controls. The cases for <em>CREATE</em>
  1005. and <em>DELETE</em> are the following:</p>
  1006. <p>You want A to be able to do a set on a ZooKeeper node, but
  1007. not be able to <em>CREATE</em>
  1008. or <em>DELETE</em> children.</p>
  1009. <p>
  1010. <em>CREATE</em>
  1011. without <em>DELETE</em>: clients create requests by
  1012. creating ZooKeeper nodes in a parent directory. You want all
  1013. clients to be able to add, but only request processor can
  1014. delete. (This is kind of like the APPEND permission for
  1015. files.)</p>
  1016. <p>Also, the <em>ADMIN</em> permission is there
  1017. since ZooKeeper doesn&rsquo;t have a notion of file owner. In some
  1018. sense the <em>ADMIN</em> permission designates the
  1019. entity as the owner. ZooKeeper doesn&rsquo;t support the LOOKUP
  1020. permission (execute permission bit on directories to allow you
  1021. to LOOKUP even though you can't list the directory). Everyone
  1022. implicitly has LOOKUP permission. This allows you to stat a
  1023. node, but nothing more. (The problem is, if you want to call
  1024. zoo_exists() on a node that doesn't exist, there is no
  1025. permission to check.)</p>
  1026. <a name="sc_BuiltinACLSchemes"></a>
  1027. <h4>Builtin ACL Schemes</h4>
  1028. <p>ZooKeeeper has the following built in schemes:</p>
  1029. <ul>
  1030. <li>
  1031. <p>
  1032. <strong>world</strong> has a
  1033. single id, <em>anyone</em>, that represents
  1034. anyone.</p>
  1035. </li>
  1036. <li>
  1037. <p>
  1038. <strong>auth</strong> doesn't
  1039. use any id, represents any authenticated
  1040. user.</p>
  1041. </li>
  1042. <li>
  1043. <p>
  1044. <strong>digest</strong> uses
  1045. a <em>username:password</em> string to generate
  1046. MD5 hash which is then used as an ACL ID
  1047. identity. Authentication is done by sending
  1048. the <em>username:password</em> in clear text. When
  1049. used in the ACL the expression will be
  1050. the <em>username:base64</em>
  1051. encoded <em>SHA1</em>
  1052. password <em>digest</em>.</p>
  1053. </li>
  1054. <li>
  1055. <p>
  1056. <strong>ip</strong> uses the
  1057. client host IP as an ACL ID identity. The ACL expression is of
  1058. the form <em>addr/bits</em> where the most
  1059. significant <em>bits</em>
  1060. of <em>addr</em> are matched against the most
  1061. significant <em>bits</em> of the client host
  1062. IP.</p>
  1063. </li>
  1064. </ul>
  1065. <a name="ZooKeeper+C+client+API"></a>
  1066. <h4>ZooKeeper C client API</h4>
  1067. <p>The following constants are provided by the ZooKeeper C
  1068. library:</p>
  1069. <ul>
  1070. <li>
  1071. <p>
  1072. <em>const</em> <em>int</em> ZOO_PERM_READ; //can read node&rsquo;s value and list its children</p>
  1073. </li>
  1074. <li>
  1075. <p>
  1076. <em>const</em> <em>int</em> ZOO_PERM_WRITE;// can set the node&rsquo;s value</p>
  1077. </li>
  1078. <li>
  1079. <p>
  1080. <em>const</em> <em>int</em> ZOO_PERM_CREATE; //can create children</p>
  1081. </li>
  1082. <li>
  1083. <p>
  1084. <em>const</em> <em>int</em> ZOO_PERM_DELETE;// can delete children</p>
  1085. </li>
  1086. <li>
  1087. <p>
  1088. <em>const</em> <em>int</em> ZOO_PERM_ADMIN; //can execute set_acl()</p>
  1089. </li>
  1090. <li>
  1091. <p>
  1092. <em>const</em> <em>int</em> ZOO_PERM_ALL;// all of the above flags OR&rsquo;d together</p>
  1093. </li>
  1094. </ul>
  1095. <p>The following are the standard ACL IDs:</p>
  1096. <ul>
  1097. <li>
  1098. <p>
  1099. <em>struct</em> Id ZOO_ANYONE_ID_UNSAFE; //(&lsquo;world&rsquo;,&rsquo;anyone&rsquo;)</p>
  1100. </li>
  1101. <li>
  1102. <p>
  1103. <em>struct</em> Id ZOO_AUTH_IDS;// (&lsquo;auth&rsquo;,&rsquo;&rsquo;)</p>
  1104. </li>
  1105. </ul>
  1106. <p>ZOO_AUTH_IDS empty identity string should be interpreted as &ldquo;the identity of the creator&rdquo;.</p>
  1107. <p>ZooKeeper client comes with three standard ACLs:</p>
  1108. <ul>
  1109. <li>
  1110. <p>
  1111. <em>struct</em> ACL_vector ZOO_OPEN_ACL_UNSAFE; //(ZOO_PERM_ALL,ZOO_ANYONE_ID_UNSAFE)</p>
  1112. </li>
  1113. <li>
  1114. <p>
  1115. <em>struct</em> ACL_vector ZOO_READ_ACL_UNSAFE;// (ZOO_PERM_READ, ZOO_ANYONE_ID_UNSAFE)</p>
  1116. </li>
  1117. <li>
  1118. <p>
  1119. <em>struct</em> ACL_vector ZOO_CREATOR_ALL_ACL; //(ZOO_PERM_ALL,ZOO_AUTH_IDS)</p>
  1120. </li>
  1121. </ul>
  1122. <p>The ZOO_OPEN_ACL_UNSAFE is completely open free for all
  1123. ACL: any application can execute any operation on the node and
  1124. can create, list and delete its children. The
  1125. ZOO_READ_ACL_UNSAFE is read-only access for any
  1126. application. CREATE_ALL_ACL grants all permissions to the
  1127. creator of the node. The creator must have been authenticated by
  1128. the server (for example, using &ldquo;<em>digest</em>&rdquo;
  1129. scheme) before it can create nodes with this ACL.</p>
  1130. <p>The following ZooKeeper operations deal with ACLs:</p>
  1131. <ul>
  1132. <li>
  1133. <p>
  1134. <em>int</em> <em>zoo_add_auth</em>
  1135. (zhandle_t *zh,<em>const</em> <em>char</em>*
  1136. scheme,<em>const</em> <em>char</em>*
  1137. cert, <em>int</em> certLen, void_completion_t
  1138. completion, <em>const</em> <em>void</em>
  1139. *data);</p>
  1140. </li>
  1141. </ul>
  1142. <p>The application uses the zoo_add_auth function to
  1143. authenticate itself to the server. The function can be called
  1144. multiple times if the application wants to authenticate using
  1145. different schemes and/or identities.</p>
  1146. <ul>
  1147. <li>
  1148. <p>
  1149. <em>int</em> <em>zoo_create</em>
  1150. (zhandle_t *zh, <em>const</em> <em>char</em>
  1151. *path, <em>const</em> <em>char</em>
  1152. *value,<em>int</em>
  1153. valuelen, <em>const</em> <em>struct</em>
  1154. ACL_vector *acl, <em>int</em>
  1155. flags,<em>char</em>
  1156. *realpath, <em>int</em>
  1157. max_realpath_len);</p>
  1158. </li>
  1159. </ul>
  1160. <p>zoo_create(...) operation creates a new node. The acl
  1161. parameter is a list of ACLs associated with the node. The parent
  1162. node must have the CREATE permission bit set.</p>
  1163. <ul>
  1164. <li>
  1165. <p>
  1166. <em>int</em> <em>zoo_get_acl</em>
  1167. (zhandle_t *zh, <em>const</em> <em>char</em>
  1168. *path,<em>struct</em> ACL_vector
  1169. *acl, <em>struct</em> Stat *stat);</p>
  1170. </li>
  1171. </ul>
  1172. <p>This operation returns a node&rsquo;s ACL info.</p>
  1173. <ul>
  1174. <li>
  1175. <p>
  1176. <em>int</em> <em>zoo_set_acl</em>
  1177. (zhandle_t *zh, <em>const</em> <em>char</em>
  1178. *path, <em>int</em>
  1179. version,<em>const</em> <em>struct</em>
  1180. ACL_vector *acl);</p>
  1181. </li>
  1182. </ul>
  1183. <p>This function replaces node&rsquo;s ACL list with a new one. The
  1184. node must have the ADMIN permission set.</p>
  1185. <p>Here is a sample code that makes use of the above APIs to
  1186. authenticate itself using the &ldquo;<em>foo</em>&rdquo; scheme
  1187. and create an ephemeral node &ldquo;/xyz&rdquo; with create-only
  1188. permissions.</p>
  1189. <div class="note">
  1190. <div class="label">Note</div>
  1191. <div class="content">
  1192. <p>This is a very simple example which is intended to show
  1193. how to interact with ZooKeeper ACLs
  1194. specifically. See <span class="codefrag filename">.../trunk/src/c/src/cli.c</span>
  1195. for an example of a proper C client implementation</p>
  1196. </div>
  1197. </div>
  1198. <pre class="code">
  1199. #include &lt;string.h&gt;
  1200. #include &lt;errno.h&gt;
  1201. #include "zookeeper.h"
  1202. static zhandle_t *zh;
  1203. /**
  1204. * In this example this method gets the cert for your
  1205. * environment -- you must provide
  1206. */
  1207. char *foo_get_cert_once(char* id) { return 0; }
  1208. /** Watcher function -- empty for this example, not something you should
  1209. * do in real code */
  1210. void watcher(zhandle_t *zzh, int type, int state, const char *path,
  1211. void *watcherCtx) {}
  1212. int main(int argc, char argv) {
  1213. char buffer[512];
  1214. char p[2048];
  1215. char *cert=0;
  1216. char appId[64];
  1217. strcpy(appId, "example.foo_test");
  1218. cert = foo_get_cert_once(appId);
  1219. if(cert!=0) {
  1220. fprintf(stderr,
  1221. "Certificate for appid [%s] is [%s]\n",appId,cert);
  1222. strncpy(p,cert, sizeof(p)-1);
  1223. free(cert);
  1224. } else {
  1225. fprintf(stderr, "Certificate for appid [%s] not found\n",appId);
  1226. strcpy(p, "dummy");
  1227. }
  1228. zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
  1229. zh = zookeeper_init("localhost:3181", watcher, 10000, 0, 0, 0);
  1230. if (!zh) {
  1231. return errno;
  1232. }
  1233. if(zoo_add_auth(zh,"foo",p,strlen(p),0,0)!=ZOK)
  1234. return 2;
  1235. struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_CREATE, ZOO_AUTH_IDS}};
  1236. struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL};
  1237. int rc = zoo_create(zh,"/xyz","value", 5, &amp;CREATE_ONLY, ZOO_EPHEMERAL,
  1238. buffer, sizeof(buffer)-1);
  1239. /** this operation will fail with a ZNOAUTH error */
  1240. int buflen= sizeof(buffer);
  1241. struct Stat stat;
  1242. rc = zoo_get(zh, "/xyz", 0, buffer, &amp;buflen, &amp;stat);
  1243. if (rc) {
  1244. fprintf(stderr, "Error %d for %s\n", rc, __LINE__);
  1245. }
  1246. zookeeper_close(zh);
  1247. return 0;
  1248. }
  1249. </pre>
  1250. </div>
  1251. <a name="sc_ZooKeeperPluggableAuthentication"></a>
  1252. <h2 class="h3">Pluggable ZooKeeper authentication</h2>
  1253. <div class="section">
  1254. <p>ZooKeeper runs in a variety of different environments with
  1255. various different authentication schemes, so it has a completely
  1256. pluggable authentication framework. Even the builtin authentication
  1257. schemes use the pluggable authentication framework.</p>
  1258. <p>To understand how the authentication framework works, first you must
  1259. understand the two main authentication operations. The framework
  1260. first must authenticate the client. This is usually done as soon as
  1261. the client connects to a server and consists of validating information
  1262. sent from or gathered about a client and associating it with the connection.
  1263. The second operation handled by the framework is finding the entries in an
  1264. ACL that correspond to client. ACL entries are &lt;<em>idspec,
  1265. permissions</em>&gt; pairs. The <em>idspec</em> may be
  1266. a simple string match against the authentication information associated
  1267. with the connection or it may be a expression that is evaluated against that
  1268. information. It is up to the implementation of the authentication plugin
  1269. to do the match. Here is the interface that an authentication plugin must
  1270. implement:</p>
  1271. <pre class="code">
  1272. public interface AuthenticationProvider {
  1273. String getScheme();
  1274. KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte authData[]);
  1275. boolean isValid(String id);
  1276. boolean matches(String id, String aclExpr);
  1277. boolean isAuthenticated();
  1278. }
  1279. </pre>
  1280. <p>The first method <em>getScheme</em> returns the string
  1281. that identifies the plugin. Because we support multiple methods of authentication,
  1282. an authentication credential or an <em>idspec</em> will always be
  1283. prefixed with <em>scheme:</em>. The ZooKeeper server uses the scheme
  1284. returned by the authentication plugin to determine which ids the scheme
  1285. applies to.</p>
  1286. <p>
  1287. <em>handleAuthentication</em> is called when a client
  1288. sends authentication information to be associated with a connection. The
  1289. client specifies the scheme to which the information corresponds. The
  1290. ZooKeeper server passes the information to the authentication plugin whose
  1291. <em>getScheme</em> matches the scheme passed by the client. The
  1292. implementor of <em>handleAuthentication</em> will usually return
  1293. an error if it determines that the information is bad, or it will associate information
  1294. with the connection using <em>cnxn.getAuthInfo().add(new Id(getScheme(), data))</em>.
  1295. </p>
  1296. <p>The authentication plugin is involved in both setting and using ACLs. When an
  1297. ACL is set for a znode, the ZooKeeper server will pass the id part of the entry to
  1298. the <em>isValid(String id)</em> method. It is up to the plugin to verify
  1299. that the id has a correct form. For example, <em>ip:172.16.0.0/16</em>
  1300. is a valid id, but <em>ip:host.com</em> is not. If the new ACL includes
  1301. an "auth" entry, <em>isAuthenticated</em> is used to see if the
  1302. authentication information for this scheme that is assocatied with the connection
  1303. should be added to the ACL. Some schemes
  1304. should not be included in auth. For example, the IP address of the client is not
  1305. considered as an id that should be added to the ACL if auth is specified.</p>
  1306. <p>ZooKeeper invokes
  1307. <em>matches(String id, String aclExpr)</em> when checking an ACL. It
  1308. needs to match authentication information of the client against the relevant ACL
  1309. entries. To find the entries which apply to the client, the ZooKeeper server will
  1310. find the scheme of each entry and if there is authentication information
  1311. from that client for that scheme, <em>matches(String id, String aclExpr)</em>
  1312. will be called with <em>id</em> set to the authentication information
  1313. that was previously added to the connection by <em>handleAuthentication</em> and
  1314. <em>aclExpr</em> set to the id of the ACL entry. The authentication plugin
  1315. uses its own logic and matching scheme to determine if <em>id</em> is included
  1316. in <em>aclExpr</em>.
  1317. </p>
  1318. <p>There are two built in authentication plugins: <em>ip</em> and
  1319. <em>digest</em>. Additional plugins can adding using system properties. At
  1320. startup the ZooKeeper server will look for system properties that start with
  1321. "zookeeper.authProvider." and interpret the value of those properties as the class name
  1322. of an authentication plugin. These properties can be set using the
  1323. <em>-Dzookeeeper.authProvider.X=com.f.MyAuth</em> or adding entries such as
  1324. the following in the server configuration file:</p>
  1325. <pre class="code">
  1326. authProvider.1=com.f.MyAuth
  1327. authProvider.2=com.f.MyAuth2
  1328. </pre>
  1329. <p>Care should be taking to ensure that the suffix on the property is unique. If there are
  1330. duplicates such as <em>-Dzookeeeper.authProvider.X=com.f.MyAuth -Dzookeeper.authProvider.X=com.f.MyAuth2</em>,
  1331. only one will be used. Also all servers must have the same plugins defined, otherwise clients using
  1332. the authentication schemes provided by the plugins will have problems connecting to some servers.
  1333. </p>
  1334. </div>
  1335. <a name="ch_zkGuarantees"></a>
  1336. <h2 class="h3">Consistency Guarantees</h2>
  1337. <div class="section">
  1338. <p>ZooKeeper is a high performance, scalable service. Both reads and
  1339. write operations are designed to be fast, though reads are faster than
  1340. writes. The reason for this is that in the case of reads, ZooKeeper can
  1341. serve older data, which in turn is due to ZooKeeper's consistency
  1342. guarantees:</p>
  1343. <dl>
  1344. <dt>
  1345. <term>Sequential Consistency</term>
  1346. </dt>
  1347. <dd>
  1348. <p>Updates from a client will be applied in the order that they
  1349. were sent.</p>
  1350. </dd>
  1351. <dt>
  1352. <term>Atomicity</term>
  1353. </dt>
  1354. <dd>
  1355. <p>Updates either succeed or fail -- there are no partial
  1356. results.</p>
  1357. </dd>
  1358. <dt>
  1359. <term>Single System Image</term>
  1360. </dt>
  1361. <dd>
  1362. <p>A client will see the same view of the service regardless of
  1363. the server that it connects to.</p>
  1364. </dd>
  1365. <dt>
  1366. <term>Reliability</term>
  1367. </dt>
  1368. <dd>
  1369. <p>Once an update has been applied, it will persist from that
  1370. time forward until a client overwrites the update. This guarantee
  1371. has two corollaries:</p>
  1372. <ol>
  1373. <li>
  1374. <p>If a client gets a successful return code, the update will
  1375. have been applied. On some failures (communication errors,
  1376. timeouts, etc) the client will not know if the update has
  1377. applied or not. We take steps to minimize the failures, but the
  1378. guarantee is only present with successful return codes.
  1379. (This is called the <em>monotonicity condition</em> in Paxos.)</p>
  1380. </li>
  1381. <li>
  1382. <p>Any updates that are seen by the client, through a read
  1383. request or successful update, will never be rolled back when
  1384. recovering from server failures.</p>
  1385. </li>
  1386. </ol>
  1387. </dd>
  1388. <dt>
  1389. <term>Timeliness</term>
  1390. </dt>
  1391. <dd>
  1392. <p>The clients view of the system is guaranteed to be up-to-date
  1393. within a certain time bound (on the order of tens of seconds).
  1394. Either system changes will be seen by a client within this bound, or
  1395. the client will detect a service outage.</p>
  1396. </dd>
  1397. </dl>
  1398. <p>Using these consistency guarantees it is easy to build higher level
  1399. functions such as leader election, barriers, queues, and read/write
  1400. revocable locks solely at the ZooKeeper client (no additions needed to
  1401. ZooKeeper). See <a href="recipes.html">Recipes and Solutions</a>
  1402. for more details.</p>
  1403. <div class="note">
  1404. <div class="label">Note</div>
  1405. <div class="content">
  1406. <p>Sometimes developers mistakenly assume one other guarantee that
  1407. ZooKeeper does <em>not</em> in fact make. This is:</p>
  1408. <dl>
  1409. <dt>
  1410. <term>Simultaneously Consistent Cross-Client Views</term>
  1411. </dt>
  1412. <dd>
  1413. <p>ZooKeeper does not guarantee that at every instance in
  1414. time, two different clients will have identical views of
  1415. ZooKeeper data. Due to factors like network delays, one client
  1416. may perform an update before another client gets notified of the
  1417. change. Consider the scenario of two clients, A and B. If client
  1418. A sets the value of a znode /a from 0 to 1, then tells client B
  1419. to read /a, client B may read the old value of 0, depending on
  1420. which server it is connected to. If it
  1421. is important that Client A and Client B read the same value,
  1422. Client B should should call the <strong>sync()</strong> method from the ZooKeeper API
  1423. method before it performs its read.</p>
  1424. <p>So, ZooKeeper by itself doesn't guarantee that changes occur
  1425. synchronously across all servers, but ZooKeeper
  1426. primitives can be used to construct higher level functions that
  1427. provide useful client synchronization. (For more information,
  1428. see the <a href="recipes.html">ZooKeeper Recipes</a>.
  1429. <em>[tbd:..]</em>).</p>
  1430. </dd>
  1431. </dl>
  1432. </div>
  1433. </div>
  1434. </div>
  1435. <a name="ch_bindings"></a>
  1436. <h2 class="h3">Bindings</h2>
  1437. <div class="section">
  1438. <p>The ZooKeeper client libraries come in two languages: Java and C.
  1439. The following sections describe these.</p>
  1440. <a name="Java+Binding"></a>
  1441. <h3 class="h4">Java Binding</h3>
  1442. <p>There are two packages that make up the ZooKeeper Java binding:
  1443. <strong>org.apache.zookeeper</strong> and <strong>org.apache.zookeeper.data</strong>. The rest of the
  1444. packages that make up ZooKeeper are used internally or are part of the
  1445. server implementation. The <strong>org.apache.zookeeper.data</strong> package is made up of
  1446. generated classes that are used simply as containers.</p>
  1447. <p>The main class used by a ZooKeeper Java client is the <strong>ZooKeeper</strong> class. Its two constructors differ only
  1448. by an optional session id and password. ZooKeeper supports session
  1449. recovery accross instances of a process. A Java program may save its
  1450. session id and password to stable storage, restart, and recover the
  1451. session that was used by the earlier instance of the program.</p>
  1452. <p>When a ZooKeeper object is created, two threads are created as
  1453. well: an IO thread and an event thread. All IO happens on the IO thread
  1454. (using Java NIO). All event callbacks happen on the event thread.
  1455. Session maintenance such as reconnecting to ZooKeeper servers and
  1456. maintaining heartbeat is done on the IO thread. Responses for
  1457. synchronous methods are also processed in the IO thread. All responses
  1458. to asynchronous methods and watch events are processed on the event
  1459. thread. There are a few things to notice that result from this
  1460. design:</p>
  1461. <ul>
  1462. <li>
  1463. <p>All completions for asynchronous calls and watcher callbacks
  1464. will be made in order, one at a time. The caller can do any
  1465. processing they wish, but no other callbacks will be processed
  1466. during that time.</p>
  1467. </li>
  1468. <li>
  1469. <p>Callbacks do not block the processing of the IO thread or the
  1470. processing of the synchronous calls.</p>
  1471. </li>
  1472. <li>
  1473. <p>Synchronous calls may not return in the correct order. For
  1474. example, assume a client does the following processing: issues an
  1475. asynchronous read of node <strong>/a</strong> with
  1476. <em>watch</em> set to true, and then in the completion
  1477. callback of the read it does a synchronous read of <strong>/a</strong>. (Maybe not good practice, but not illegal
  1478. either, and it makes for a simple example.)</p>
  1479. <p>Note that if there is a change to <strong>/a</strong> between the asynchronous read and the
  1480. synchronous read, the client library will receive the watch event
  1481. saying <strong>/a</strong> changed before the
  1482. response for the synchronous read, but because the completion
  1483. callback is blocking the event queue, the synchronous read will
  1484. return with the new value of <strong>/a</strong>
  1485. before the watch event is processed.</p>
  1486. </li>
  1487. </ul>
  1488. <p>Finally, the rules associated with shutdown are straightforward:
  1489. once a ZooKeeper object is closed or receives a fatal event
  1490. (SESSION_EXPIRED and AUTH_FAILED), the ZooKeeper object becomes invalid.
  1491. On a close, the two threads shut down and any further access on zookeeper
  1492. handle is undefined behavior and should be avoided. </p>
  1493. <a name="C+Binding"></a>
  1494. <h3 class="h4">C Binding</h3>
  1495. <p>The C binding has a single-threaded and multi-threaded library.
  1496. The multi-threaded library is easiest to use and is most similar to the
  1497. Java API. This library will create an IO thread and an event dispatch
  1498. thread for handling connection maintenance and callbacks. The
  1499. single-threaded library allows ZooKeeper to be used in event driven
  1500. applications by exposing the event loop used in the multi-threaded
  1501. library.</p>
  1502. <p>The package includes two shared libraries: zookeeper_st and
  1503. zookeeper_mt. The former only provides the asynchronous APIs and
  1504. callbacks for integrating into the application's event loop. The only
  1505. reason this library exists is to support the platforms were a
  1506. <em>pthread</em> library is not available or is unstable
  1507. (i.e. FreeBSD 4.x). In all other cases, application developers should
  1508. link with zookeeper_mt, as it includes support for both Sync and Async
  1509. API.</p>
  1510. <a name="Installation"></a>
  1511. <h4>Installation</h4>
  1512. <p>If you're building the client from a check-out from the Apache
  1513. repository, follow the steps outlined below. If you're building from a
  1514. project source package downloaded from apache, skip to step <strong>3</strong>.</p>
  1515. <ol>
  1516. <li>
  1517. <p>Run <span class="codefrag command">ant compile_jute</span> from the ZooKeeper
  1518. top level directory (<span class="codefrag filename">.../trunk</span>).
  1519. This will create a directory named "generated" under
  1520. <span class="codefrag filename">.../trunk/src/c</span>.</p>
  1521. </li>
  1522. <li>
  1523. <p>Change directory to the<span class="codefrag filename">.../trunk/src/c</span>
  1524. 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.
  1525. Skip to step<strong> 4</strong>.</p>
  1526. </li>
  1527. <li>
  1528. <p>If you are building from a project source package,
  1529. unzip/untar the source tarball and cd to the<span class="codefrag filename">
  1530. zookeeper-x.x.x/src/c</span> directory.</p>
  1531. </li>
  1532. <li>
  1533. <p>Run <span class="codefrag command">./configure &lt;your-options&gt;</span> to
  1534. generate the makefile. Here are some of options the <strong>configure</strong> utility supports that can be
  1535. useful in this step:</p>
  1536. <ul>
  1537. <li>
  1538. <p>
  1539. <span class="codefrag command">--enable-debug</span>
  1540. </p>
  1541. <p>Enables optimization and enables debug info compiler
  1542. options. (Disabled by default.)</p>
  1543. </li>
  1544. <li>
  1545. <p>
  1546. <span class="codefrag command">--without-syncapi </span>
  1547. </p>
  1548. <p>Disables Sync API support; zookeeper_mt library won't be
  1549. built. (Enabled by default.)</p>
  1550. </li>
  1551. <li>
  1552. <p>
  1553. <span class="codefrag command">--disable-static </span>
  1554. </p>
  1555. <p>Do not build static libraries. (Enabled by
  1556. default.)</p>
  1557. </li>
  1558. <li>
  1559. <p>
  1560. <span class="codefrag command">--disable-shared</span>
  1561. </p>
  1562. <p>Do not build shared libraries. (Enabled by
  1563. default.)</p>
  1564. </li>
  1565. </ul>
  1566. <div class="note">
  1567. <div class="label">Note</div>
  1568. <div class="content">
  1569. <p>See INSTALL for general information about running
  1570. <strong>configure</strong>.</p>
  1571. </div>
  1572. </div>
  1573. </li>
  1574. <li>
  1575. <p>Run <span class="codefrag command">make</span> or <span class="codefrag command">make
  1576. install</span> to build the libraries and install them.</p>
  1577. </li>
  1578. <li>
  1579. <p>To generate doxygen documentation for the ZooKeeper API, run
  1580. <span class="codefrag command">make doxygen-doc</span>. All documentation will be
  1581. placed in a new subfolder named docs. By default, this command
  1582. only generates HTML. For information on other document formats,
  1583. run <span class="codefrag command">./configure --help</span>
  1584. </p>
  1585. </li>
  1586. </ol>
  1587. <a name="Using+the+C+Client"></a>
  1588. <h4>Using the C Client</h4>
  1589. <p>You can test your client by running a ZooKeeper server (see
  1590. instructions on the project wiki page on how to run it) and connecting
  1591. to it using one of the cli applications that were built as part of the
  1592. installation procedure. cli_mt (multithreaded, built against
  1593. zookeeper_mt library) is shown in this example, but you could also use
  1594. cli_st (singlethreaded, built against zookeeper_st library):</p>
  1595. <p>
  1596. <span class="codefrag command">$ cli_mt zookeeper_host:9876</span>
  1597. </p>
  1598. <p>This is a client application that gives you a shell for
  1599. executing simple ZooKeeper commands. Once successfully started
  1600. and connected to the server it displays a shell prompt. You
  1601. can now enter ZooKeeper commands. For example, to create a
  1602. node:</p>
  1603. <p>
  1604. <span class="codefrag command">&gt; create /my_new_node</span>
  1605. </p>
  1606. <p>To verify that the node's been created:</p>
  1607. <p>
  1608. <span class="codefrag command">&gt; ls /</span>
  1609. </p>
  1610. <p>You should see a list of node who are children of the root node
  1611. "/".</p>
  1612. <p>In order to be able to use the ZooKeeper API in your application
  1613. you have to remember to</p>
  1614. <ol>
  1615. <li>
  1616. <p>Include ZooKeeper header: #include
  1617. &lt;zookeeper/zookeeper.h&gt;</p>
  1618. </li>
  1619. <li>
  1620. <p>If you are building a multithreaded client, compile with
  1621. -DTHREADED compiler flag to enable the multi-threaded version of
  1622. the library, and then link against against the
  1623. <em>zookeeper_mt</em> library. If you are building a
  1624. single-threaded client, do not compile with -DTHREADED, and be
  1625. sure to link against the<em> zookeeper_st
  1626. </em>library.</p>
  1627. </li>
  1628. </ol>
  1629. <p>Refer to <a href="#ch_programStructureWithExample">Program Structure, with Simple Example</a>
  1630. for examples of usage in Java and C.
  1631. <em>[tbd]</em>
  1632. </p>
  1633. </div>
  1634. <a name="ch_guideToZkOperations"></a>
  1635. <h2 class="h3">Building Blocks: A Guide to ZooKeeper Operations</h2>
  1636. <div class="section">
  1637. <p>This section surveys all the operations a developer can perform
  1638. against a ZooKeeper server. It is lower level information than the earlier
  1639. concepts chapters in this manual, but higher level than the ZooKeeper API
  1640. Reference. It covers these topics:</p>
  1641. <ul>
  1642. <li>
  1643. <p>
  1644. <a href="#sc_connectingToZk">Connecting to ZooKeeper</a>
  1645. </p>
  1646. </li>
  1647. </ul>
  1648. <a name="sc_errorsZk"></a>
  1649. <h3 class="h4">Handling Errors</h3>
  1650. <p>Both the Java and C client bindings may report errors. The Java client binding does so by throwing KeeperException, calling code() on the exception will return the specific error code. The C client binding returns an error code as defined in the enum ZOO_ERRORS. API callbacks indicate result code for both language bindings. See the API documentation (javadoc for Java, doxygen for C) for full details on the possible errors and their meaning.</p>
  1651. <a name="sc_connectingToZk"></a>
  1652. <h3 class="h4">Connecting to ZooKeeper</h3>
  1653. <p></p>
  1654. <a name="sc_readOps"></a>
  1655. <h3 class="h4">Read Operations</h3>
  1656. <p></p>
  1657. <a name="sc_writeOps"></a>
  1658. <h3 class="h4">Write Operations</h3>
  1659. <p></p>
  1660. <a name="sc_handlingWatches"></a>
  1661. <h3 class="h4">Handling Watches</h3>
  1662. <p></p>
  1663. <a name="sc_miscOps"></a>
  1664. <h3 class="h4">Miscelleaneous ZooKeeper Operations</h3>
  1665. <p></p>
  1666. </div>
  1667. <a name="ch_programStructureWithExample"></a>
  1668. <h2 class="h3">Program Structure, with Simple Example</h2>
  1669. <div class="section">
  1670. <p>
  1671. <em>[tbd]</em>
  1672. </p>
  1673. </div>
  1674. <a name="ch_gotchas"></a>
  1675. <h2 class="h3">Gotchas: Common Problems and Troubleshooting</h2>
  1676. <div class="section">
  1677. <p>So now you know ZooKeeper. It's fast, simple, your application
  1678. works, but wait ... something's wrong. Here are some pitfalls that
  1679. ZooKeeper users fall into:</p>
  1680. <ol>
  1681. <li>
  1682. <p>If you are using watches, you must look for the connected watch
  1683. event. When a ZooKeeper client disconnects from a server, you will
  1684. not receive notification of changes until reconnected. If you are
  1685. watching for a znode to come into existance, you will miss the event
  1686. if the znode is created and deleted while you are disconnected.</p>
  1687. </li>
  1688. <li>
  1689. <p>You must test ZooKeeper server failures. The ZooKeeper service
  1690. can survive failures as long as a majority of servers are active. The
  1691. question to ask is: can your application handle it? In the real world
  1692. a client's connection to ZooKeeper can break. (ZooKeeper server
  1693. failures and network partitions are common reasons for connection
  1694. loss.) The ZooKeeper client library takes care of recovering your
  1695. connection and letting you know what happened, but you must make sure
  1696. that you recover your state and any outstanding requests that failed.
  1697. Find out if you got it right in the test lab, not in production - test
  1698. with a ZooKeeper service made up of a several of servers and subject
  1699. them to reboots.</p>
  1700. </li>
  1701. <li>
  1702. <p>The list of ZooKeeper servers used by the client must match the
  1703. list of ZooKeeper servers that each ZooKeeper server has. Things can
  1704. work, although not optimally, if the client list is a subset of the
  1705. real list of ZooKeeper servers, but not if the client lists ZooKeeper
  1706. servers not in the ZooKeeper cluster.</p>
  1707. </li>
  1708. <li>
  1709. <p>Be careful where you put that transaction log. The most
  1710. performance-critical part of ZooKeeper is the transaction log.
  1711. ZooKeeper must sync transactions to media before it returns a
  1712. response. A dedicated transaction log device is key to consistent good
  1713. performance. Putting the log on a busy device will adversely effect
  1714. performance. If you only have one storage device, put trace files on
  1715. NFS and increase the snapshotCount; it doesn't eliminate the problem,
  1716. but it can mitigate it.</p>
  1717. </li>
  1718. <li>
  1719. <p>Set your Java max heap size correctly. It is very important to
  1720. <em>avoid swapping.</em> Going to disk unnecessarily will
  1721. almost certainly degrade your performance unacceptably. Remember, in
  1722. ZooKeeper, everything is ordered, so if one request hits the disk, all
  1723. other queued requests hit the disk.</p>
  1724. <p>To avoid swapping, try to set the heapsize to the amount of
  1725. physical memory you have, minus the amount needed by the OS and cache.
  1726. The best way to determine an optimal heap size for your configurations
  1727. is to <em>run load tests</em>. If for some reason you
  1728. can't, be conservative in your estimates and choose a number well
  1729. below the limit that would cause your machine to swap. For example, on
  1730. a 4G machine, a 3G heap is a conservative estimate to start
  1731. with.</p>
  1732. </li>
  1733. </ol>
  1734. </div>
  1735. <a name="apx_linksToOtherInfo"></a>
  1736. <appendix id="apx_linksToOtherInfo">
  1737. <title>Links to Other Information</title>
  1738. <p>Outside the formal documentation, there're several other sources of
  1739. information for ZooKeeper developers.</p>
  1740. <dl>
  1741. <dt>
  1742. <term>ZooKeeper Whitepaper <em>[tbd: find url]</em>
  1743. </term>
  1744. </dt>
  1745. <dd>
  1746. <p>The definitive discussion of ZooKeeper design and performance,
  1747. by Yahoo! Research</p>
  1748. </dd>
  1749. <dt>
  1750. <term>API Reference <em>[tbd: find url]</em>
  1751. </term>
  1752. </dt>
  1753. <dd>
  1754. <p>The complete reference to the ZooKeeper API</p>
  1755. </dd>
  1756. <dt>
  1757. <term>
  1758. <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/zookeeper.m4v">ZooKeeper
  1759. Talk at the Hadoup Summit 2008</a>
  1760. </term>
  1761. </dt>
  1762. <dd>
  1763. <p>A video introduction to ZooKeeper, by Benjamin Reed of Yahoo!
  1764. Research</p>
  1765. </dd>
  1766. <dt>
  1767. <term>
  1768. <a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/Tutorial">Barrier and
  1769. Queue Tutorial</a>
  1770. </term>
  1771. </dt>
  1772. <dd>
  1773. <p>The excellent Java tutorial by Flavio Junqueira, implementing
  1774. simple barriers and producer-consumer queues using ZooKeeper.</p>
  1775. </dd>
  1776. <dt>
  1777. <term>
  1778. <a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/ZooKeeperArticles">ZooKeeper
  1779. - A Reliable, Scalable Distributed Coordination System</a>
  1780. </term>
  1781. </dt>
  1782. <dd>
  1783. <p>An article by Todd Hoff (07/15/2008)</p>
  1784. </dd>
  1785. <dt>
  1786. <term>
  1787. <a href="recipes.html">ZooKeeper Recipes</a>
  1788. </term>
  1789. </dt>
  1790. <dd>
  1791. <p>Pseudo-level discussion of the implementation of various
  1792. synchronization solutions with ZooKeeper: Event Handles, Queues,
  1793. Locks, and Two-phase Commits.</p>
  1794. </dd>
  1795. <dt>
  1796. <term>
  1797. <em>[tbd]</em>
  1798. </term>
  1799. </dt>
  1800. <dd>
  1801. <p>Any other good sources anyone can think of...</p>
  1802. </dd>
  1803. </dl>
  1804. </appendix>
  1805. <p align="right">
  1806. <font size="-2"></font>
  1807. </p>
  1808. </div>
  1809. <!--+
  1810. |end content
  1811. +-->
  1812. <div class="clearboth">&nbsp;</div>
  1813. </div>
  1814. <div id="footer">
  1815. <!--+
  1816. |start bottomstrip
  1817. +-->
  1818. <div class="lastmodified">
  1819. <script type="text/javascript"><!--
  1820. document.write("Last Published: " + document.lastModified);
  1821. // --></script>
  1822. </div>
  1823. <div class="copyright">
  1824. Copyright &copy;
  1825. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  1826. </div>
  1827. <!--+
  1828. |end bottomstrip
  1829. +-->
  1830. </div>
  1831. </body>
  1832. </html>