zookeeperProgrammers.html 72 KB

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