zookeeperProgrammers.html 76 KB

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