zookeeperProgrammers.html 69 KB

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