zookeeperProgrammers.html 57 KB

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