zookeeperProgrammers.html 57 KB

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