zookeeperProgrammers.html 76 KB

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