zookeeperProgrammers.html 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  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+Client">Using the 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 quorums, 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 a quorum; 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. It also means if a client connects to a different
  655. server, the new server is not going to know about its watches. So, when a
  656. client gets a disconnect event, it must consider that an implicit trigger
  657. of all watches. When a client reconnects to a new server, the client
  658. should re-set any watches that it is still interested in.</p>
  659. <a name="N101E9"></a><a name="sc_WatchGuarantees"></a>
  660. <h3 class="h4">What ZooKeeper Guarantees about Watches</h3>
  661. <p>With regard to watches, ZooKeeper maintains these
  662. guarantees:</p>
  663. <ul>
  664. <li>
  665. <p>Watches are ordered with respect to other events, other
  666. watches, and asynchronous replies. The ZooKeeper client libraries
  667. ensures that everything is dispatched in order.</p>
  668. </li>
  669. </ul>
  670. <ul>
  671. <li>
  672. <p>A client will see a watch event for a znode it is watching
  673. before seeing the new data that corresponds to that znode.</p>
  674. </li>
  675. </ul>
  676. <ul>
  677. <li>
  678. <p>The order of watch events from ZooKeeper corresponds to the
  679. order of the updates as seen by the ZooKeeper service.</p>
  680. </li>
  681. </ul>
  682. <a name="N1020E"></a><a name="sc_WatchRememberThese"></a>
  683. <h3 class="h4">Things to Remember about Watches</h3>
  684. <ul>
  685. <li>
  686. <p>Watches are one time triggers; if you get a watch event and
  687. you want to get notified of future changes, you must set another
  688. watch.</p>
  689. </li>
  690. </ul>
  691. <ul>
  692. <li>
  693. <p>Because watches are one time triggers and there is latency
  694. between getting the event and sending a new request to get a watch
  695. you cannot reliably see every change that happens to a node in
  696. ZooKeeper. Be prepared to handle the case where the znode changes
  697. multiple times between getting the event and setting the watch
  698. again. (You may not care, but at least realize it may
  699. happen.)</p>
  700. </li>
  701. </ul>
  702. <ul>
  703. <li>
  704. <p>When you disconnect from a server (for example, when the
  705. server fails), all of the watches you have registered are lost, so
  706. you should treat this case as if all your watches were
  707. triggered.</p>
  708. </li>
  709. </ul>
  710. </div>
  711. <a name="N10231"></a><a name="sc_ZooKeeperAccessControl"></a>
  712. <h2 class="h3">ZooKeeper access control using ACLs</h2>
  713. <div class="section">
  714. <p>ZooKeeper uses ACLs to control access to its znodes (the data nodes of a ZooKeeper data tree). The ACL implementation is quite similar to UNIX file access permissions: it employs permission bits to allow/disallow various operations against a node and the scope to which the bits apply. Unlike standard UNIX permissions, a ZooKeeper node is not limited by the three standard scopes for user (owner of the file), group, and world (other). ZooKeeper does not have a notion of an owner of a znode. Instead, an ACL specifies sets of ids and permissions that are associated with those ids.</p>
  715. <p>ZooKeeper supports pluggable authentication schemes. Ids are specified using the form <em>scheme:id</em>, where <em>scheme</em> is a the authentication scheme that the id corresponds to. For example, <em>host:host1.corp.com</em> is an id for a host named <em>host1.corp.com</em>.</p>
  716. <p>When a client connects to ZooKeeper and authenticates itself, ZooKeeper associates all the ids that correspond to a client with the clients connection. These ids are checked against the ACLs of znodes when a clients tries to access a node. ACLs are made up of pairs of <em>(scheme:expression, perms)</em>. The format of the <em>expression</em> is specific to the scheme. For example, the pair <em>(ip:19.22.0.0/16, READ)</em> gives the <em>READ</em> permission to any clients with an IP address that starts with 19.22.</p>
  717. <a name="N10258"></a><a name="sc_ACLPermissions"></a>
  718. <h3 class="h4">ACL Permissions</h3>
  719. <p>Zookeeper supports the following permissions:</p>
  720. <ul>
  721. <li>
  722. <p>
  723. <strong>CREATE</strong>: you can create a child node</p>
  724. </li>
  725. <li>
  726. <p>
  727. <strong>READ</strong>: you can get data from a node and list its children.</p>
  728. </li>
  729. <li>
  730. <p>
  731. <strong>WRITE</strong>: you can set data for a node</p>
  732. </li>
  733. <li>
  734. <p>
  735. <strong>DELETE</strong>: you can delete a child node</p>
  736. </li>
  737. <li>
  738. <p>
  739. <strong>ADMIN</strong>: you can set permissions</p>
  740. </li>
  741. </ul>
  742. <p>The <em>CREATE</em> and <em>DELETE</em> permissions have been broken out of the <em>WRITE</em> permission for finer grained access controls. The cases for <em>CREATE</em> and <em>DELETE</em> are the following:</p>
  743. <p>You want A to be able to do a set on a zookeeper node, but not be able to <em>CREATE</em> or <em>DELETE</em> children.</p>
  744. <p>
  745. <em>CREATE</em> without <em>DELETE</em>: clients create requests by creating zookeeper nodes in a parent directory. You want all clients to be able to add, but only request processor can delete. (This is kind of like the APPEND permission for files.)</p>
  746. <p>Also, the <em>ADMIN</em> permission is there since Zookeeper doesn&rsquo;t have a notion of file owner. In some sense the <em>ADMIN</em> permission designates the entity as the owner. Zookeeper doesn&rsquo;t support the LOOKUP permission (execute permission bit on directories to allow you to LOOKUP even though you can't list the directory). Everyone implicitly has LOOKUP permission. This allows you to stat a node, but nothing more. (The problem is, if you want to call zoo_exists() on a node that doesn't exist, there is no permission to check.)</p>
  747. <a name="N102AE"></a><a name="sc_BuiltinACLSchemes"></a>
  748. <h4>Builtin ACL Schemes</h4>
  749. <p>ZooKeeeper has the following built in schemes:</p>
  750. <ul>
  751. <li>
  752. <p>
  753. <strong>world</strong> has a single id, <em>anyone</em>, that represents anyone.</p>
  754. </li>
  755. <li>
  756. <p>
  757. <strong>auth</strong> doesn't use any id, represents any authenticated user.</p>
  758. </li>
  759. <li>
  760. <p>
  761. <strong>digest</strong> uses a <em>username:password</em> string to generate MD5 hash which is then used as an ACL ID identity. Authentication is done by sending the <em>username:password</em> in clear text. When used in the ACL the expression will be the <em>username:base64</em>encoded<em>SHA1</em>password<em>digest</em>.</p>
  762. </li>
  763. <li>
  764. <p>
  765. <strong>host</strong> uses the client host name as an ACL ID identity. The ACL expression is a hostname suffix. For example, the ACL expression <em>host:corp.com</em> matches the ids <em>host:host1.corp.com</em> and <em>host:host2.corp.com</em>, but not <em>host:host1.store.com</em>.</p>
  766. </li>
  767. <li>
  768. <p>
  769. <strong>ip</strong> uses the client host IP as an ACL ID identity. The ACL expression is of the form <em>addr/bits</em> where the most significant <em>bits</em> of <em>addr</em> are matched against the most significant <em>bits</em> of the client host IP.</p>
  770. </li>
  771. </ul>
  772. <a name="N10303"></a><a name="Zookeeper+C+client+API"></a>
  773. <h4>Zookeeper C client API</h4>
  774. <p>The following constants are provided by the zookeeper C library:</p>
  775. <ul>
  776. <li>
  777. <p>
  778. <em>const</em> <em>int</em> PERM_READ; //can read node&rsquo;s value and list its children</p>
  779. </li>
  780. <li>
  781. <p>
  782. <em>const</em> <em>int</em> PERM_WRITE;// can set the node&rsquo;s value</p>
  783. </li>
  784. <li>
  785. <p>
  786. <em>const</em> <em>int</em> PERM_CREATE; //can create children</p>
  787. </li>
  788. <li>
  789. <p>
  790. <em>const</em> <em>int</em> PERM_DELETE;// can delete children</p>
  791. </li>
  792. <li>
  793. <p>
  794. <em>const</em> <em>int</em> PERM_ADMIN; //can execute set_acl()</p>
  795. </li>
  796. <li>
  797. <p>
  798. <em>const</em> <em>int</em> PERM_ALL;// all of the above flags OR&rsquo;d together</p>
  799. </li>
  800. </ul>
  801. <p>The following are the standard ACL IDs:</p>
  802. <ul>
  803. <li>
  804. <p>
  805. <em>struct</em> Id ANYONE_ID_UNSAFE; //(&lsquo;world&rsquo;,&rsquo;anyone&rsquo;)</p>
  806. </li>
  807. <li>
  808. <p>
  809. <em>struct</em> Id AUTH_IDS;// (&lsquo;auth&rsquo;,&rsquo;&rsquo;)</p>
  810. </li>
  811. </ul>
  812. <p>AUTH_IDS empty identity string should be interpreted as &ldquo;the identity of the creator&rdquo;.</p>
  813. <p>Zookeeper client comes with three standard ACLs:</p>
  814. <ul>
  815. <li>
  816. <p>
  817. <em>struct</em> ACL_vector OPEN_ACL_UNSAFE; //(PERM_ALL,ANYONE_ID_UNSAFE)</p>
  818. </li>
  819. <li>
  820. <p>
  821. <em>struct</em> ACL_vector READ_ACL_UNSAFE;// (PERM_READ, ANYONE_ID_UNSAFE)</p>
  822. </li>
  823. <li>
  824. <p>
  825. <em>struct</em> ACL_vector CREATOR_ALL_ACL; //(PERM_ALL,AUTH_IDS)</p>
  826. </li>
  827. </ul>
  828. <p>The 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 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>
  829. <p>The following zookeeper operations deal with ACLs:</p>
  830. <ul>
  831. <li>
  832. <p>
  833. <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>
  834. </li>
  835. <li>
  836. <p>
  837. <em>int</em> certLen, void_completion_t completion, <em>const</em> <em>void</em> *data);</p>
  838. </li>
  839. </ul>
  840. <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>
  841. <ul>
  842. <li>
  843. <p>
  844. <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>
  845. </li>
  846. <li>
  847. <p>
  848. <em>int</em> valuelen, <em>const</em> <em>struct</em> ACL_vector *acl, <em>int</em> flags,</p>
  849. </li>
  850. <li>
  851. <p>
  852. <em>char</em> *realpath, <em>int</em> max_realpath_len);</p>
  853. </li>
  854. </ul>
  855. <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>
  856. <ul>
  857. <li>
  858. <p>
  859. <em>int</em> <em>zoo_get_acl</em>(zhandle_t *zh, <em>const</em> <em>char</em> *path,</p>
  860. </li>
  861. <li>
  862. <p>
  863. <em>struct</em> ACL_vector *acl, <em>struct</em> Stat *stat);</p>
  864. </li>
  865. </ul>
  866. <p>This operation returns a node&rsquo;s ACL info.</p>
  867. <ul>
  868. <li>
  869. <p>
  870. <em>int</em> <em>zoo_set_acl</em>(zhandle_t *zh, <em>const</em> <em>char</em> *path, <em>int</em> version,</p>
  871. </li>
  872. <li>
  873. <p>
  874. <em>const</em> <em>struct</em> ACL_vector *acl);</p>
  875. </li>
  876. </ul>
  877. <p>This function replaces node&rsquo;s ACL list with a new one. The node must have the ADMIN permission set.</p>
  878. <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>
  879. <pre class="code">
  880. static zhandle_t *zh;
  881. void watcher(zhandle_t *zzh, int type, int state, const char *path) {
  882. }
  883. int main(int argc, char argv) {
  884. char buffer[512];
  885. char p[2048];
  886. char *cert=0;
  887. char appId[64];
  888. strcpy(appId, "example.foo_test");
  889. cert = foo_get_cert_once(appId);
  890. if(cert!=0) {
  891. fprintf(stderr,
  892. "Certificate for appid [%s] is [%s]\n",appId,cert);
  893. strncpy(p,cert, sizeof(p)-1);
  894. free(cert);
  895. } else {
  896. fprintf(stderr, "Certificate for appid [%s] not found\n",appId);
  897. strcpy(p, "dummy");
  898. }
  899. zoo_set_debug_level(LOG_LEVEL_DEBUG);
  900. zh = zookeeper_init(&ldquo;localhost:3181&rdquo;, watcher,10000,0, 0, 0);
  901. if (!zh) {
  902. return errno;
  903. }
  904. if(zoo_add_auth(zh,"foo",p,strlen(p),0,0)!=ZOK)
  905. return 2;
  906. struct ACL_CREATE_ONLY_ACL[] = {{PERM_CREATE, AUTH_IDS}};
  907. struct ACL_vector CREATE_ONLY = {1,_CREATE_ONLY_ACL};
  908. int rc = zoo_create(zh,"/xyz","value", 5, &amp;CREATE_ONLY, EPHEMERAL,
  909. buffer, sizeof(buffer)-1);
  910. this operation will fail with a ZNOAUTH error
  911. int buflen= sizeof(buffer);
  912. struct Stat stat;
  913. rc = zoo_get(zh, "/xyz",0,buffer,&amp;buflen,&amp;stat);
  914. if (rc) {
  915. fprintf(stderr, "Error %d for %s\n", rc, line);
  916. }
  917. zookeeper_close(zh);
  918. return 0;
  919. }
  920. </pre>
  921. </div>
  922. <a name="N10420"></a><a name="ch_zkGuarantees"></a>
  923. <h2 class="h3">Consistency Guarantees</h2>
  924. <div class="section">
  925. <p>ZooKeeper is a high performance, scalable service. Both reads and
  926. write operations are designed to be fast, though reads are faster than
  927. writes. The reason for this is that in the case of reads, ZooKeeper can
  928. serve older data, which in turn is due to ZooKeeper's consistency
  929. guarantees:</p>
  930. <dl>
  931. <dt>
  932. <term>Sequential Consistency</term>
  933. </dt>
  934. <dd>
  935. <p>Updates from a client will be applied in the order that they
  936. were sent.</p>
  937. </dd>
  938. <dt>
  939. <term>Atomicity</term>
  940. </dt>
  941. <dd>
  942. <p>Updates either succeed or fail -- there are no partial
  943. results.</p>
  944. </dd>
  945. <dt>
  946. <term>Single System Image</term>
  947. </dt>
  948. <dd>
  949. <p>A client will see the same view of the service regardless of
  950. the server that it connects to.</p>
  951. </dd>
  952. <dt>
  953. <term>Reliability</term>
  954. </dt>
  955. <dd>
  956. <p>Once an update has been applied, it will persist from that
  957. time forward until a client overwrites the update. This guarantee
  958. has two corollaries:</p>
  959. <ol>
  960. <li>
  961. <p>If a client gets a successful return code, the update will
  962. have been applied. On some failures (communication errors,
  963. timeouts, etc) the client will not know if the update has
  964. applied or not. We take steps to minimize the failures, but the
  965. only guarantee is only present with successful return codes.
  966. (This is called the <em>monotonicity condition</em> in Paxos.)</p>
  967. </li>
  968. <li>
  969. <p>Any updates that are seen by the client, through a read
  970. request or successful update, will never be rolled back when
  971. recovering from server failures.</p>
  972. </li>
  973. </ol>
  974. </dd>
  975. <dt>
  976. <term>Timeliness</term>
  977. </dt>
  978. <dd>
  979. <p>The clients view of the system is guaranteed to be up-to-date
  980. within a certain time bound. (On the order of tens of seconds.)
  981. Either system changes will be seen by a client within this bound, or
  982. the client will detect a service outage.</p>
  983. </dd>
  984. </dl>
  985. <p>Using these consistency guarantees it is easy to build higher level
  986. functions such as leader election, barriers, queues, and read/write
  987. revocable locks solely at the ZooKeeper client (no additions needed to
  988. ZooKeeper). See <a href="recipes.html">Recipes and Solutions</a>
  989. for more details.</p>
  990. <div class="note">
  991. <div class="label">Note</div>
  992. <div class="content">
  993. <p>Sometimes developers mistakenly assume one other guarantee that
  994. Zookeeper does <em>not</em> in fact make. This is:</p>
  995. <dl>
  996. <dt>
  997. <term>Simultaneously Conistent Cross-Client Views</term>
  998. </dt>
  999. <dd>
  1000. <p>ZooKeeper does not guarantee that at every instance in
  1001. time, two different clients will have identical views of
  1002. ZooKeeper data. Due to factors like network delays, one client
  1003. may perform an update before another client gets notified of the
  1004. change. Consider the scenario of two clients, A and B. If client
  1005. A sets the value of a znode /a from 0 to 1, then tells client B
  1006. to read /a, client B may read the old value of 0, depending on
  1007. which server in the ZooKeeper quorum it is connected to. If it
  1008. is important that Client A and Client B read the same value,
  1009. Client B should should call the <strong>sync()</strong> method from the ZooKeeper API
  1010. method before it performs its read.</p>
  1011. <p>So, ZooKeeper by itself doesn't guarantee instantaneous,
  1012. atomic, synchronization across its quorum, but ZooKeeper
  1013. primitives can be used to construct higher level functions that
  1014. provide complete client synchronization. (For more information,
  1015. see the <a href="recipes.html">Zookeeper Recipes</a>.
  1016. <em>[tbd:..]</em>).</p>
  1017. </dd>
  1018. </dl>
  1019. </div>
  1020. </div>
  1021. </div>
  1022. <a name="N10487"></a><a name="ch_bindings"></a>
  1023. <h2 class="h3">Bindings</h2>
  1024. <div class="section">
  1025. <p>The ZooKeeper client libraries come in two languages: Java and C.
  1026. The following sections describe these.</p>
  1027. <a name="N10490"></a><a name="Java+Binding"></a>
  1028. <h3 class="h4">Java Binding</h3>
  1029. <p>There are two packages that make up the ZooKeeper Java binding:
  1030. <strong>org.apache.zookeeper</strong> and <strong>org.apache.zookeeper.data</strong>. The rest of the
  1031. packages that make up ZooKeeper are used internally or are part of the
  1032. server implementation. The <strong>org.apache.zookeeper.data</strong> package is made up of
  1033. generated classes that are used simply as containers.</p>
  1034. <p>The main class used by a ZooKeeper Java client is the <strong>ZooKeeper</strong> class. Its two constructors differ only
  1035. by an optional session id and password. ZooKeeper supports session
  1036. recovery accross instances of a process. A Java program may save its
  1037. session id and password to stable storage, restart, and recover the
  1038. session that was used by the earlier instance of the program.</p>
  1039. <p>When a ZooKeeper object is created, two threads are created as
  1040. well: an IO thread and an event thread. All IO happens on the IO thread
  1041. (using Java NIO). All event callbacks happen on the event thread.
  1042. Session maintenance such as reconnecting to ZooKeeper servers and
  1043. maintaining heartbeat is done on the IO thread. Responses for
  1044. synchronous methods are also processed in the IO thread. All responses
  1045. to asynchronous methods and watch events are processed on the event
  1046. thread. There are a few things to notice that result from this
  1047. design:</p>
  1048. <ul>
  1049. <li>
  1050. <p>All completions for asynchronous calls and watcher callbacks
  1051. will be made in order, one at a time. The caller can do any
  1052. processing they wish, but no other callbacks will be processed
  1053. during that time.</p>
  1054. </li>
  1055. <li>
  1056. <p>Callbacks do not block the processing of the IO thread or the
  1057. processing of the synchronous calls.</p>
  1058. </li>
  1059. <li>
  1060. <p>Synchronous calls may not return in the correct order. For
  1061. example, assume a client does the following processing: issues an
  1062. asynchronous read of node <strong>/a</strong> with
  1063. <em>watch</em> set to true, and then in the completion
  1064. callback of the read it does a synchronous read of <strong>/a</strong>. (Maybe not good practice, but not illegal
  1065. either, and it makes for a simple example.)</p>
  1066. <p>Note that if there is a change to <strong>/a</strong> between the asynchronous read and the
  1067. synchronous read, the client library will receive the watch event
  1068. saying <strong>/a</strong> changed before the
  1069. response for the synchronous read, but because the completion
  1070. callback is blocking the event queue, the synchronous read will
  1071. return with the new value of <strong>/a</strong>
  1072. before the watch event is processed.</p>
  1073. </li>
  1074. </ul>
  1075. <p>Finally, the rules associated with shutdown are straightforward:
  1076. once a ZooKeeper object is closed or receives a fatal event
  1077. (SESSION_EXPIRED and AUTH_FAILED), the ZooKeeper object becomes invalid,
  1078. the two threads shut down, and any further ZooKeeper calls throw
  1079. errors.</p>
  1080. <a name="N104D9"></a><a name="C+Binding"></a>
  1081. <h3 class="h4">C Binding</h3>
  1082. <p>The C binding has a single-threaded and multi-threaded library.
  1083. The multi-threaded library is easiest to use and is most similar to the
  1084. Java API. This library will create an IO thread and an event dispatch
  1085. thread for handling connection maintenance and callbacks. The
  1086. single-threaded library allows ZooKeeper to be used in event driven
  1087. applications by exposing the event loop used in the multi-threaded
  1088. library.</p>
  1089. <p>The package includes two shared libraries: zookeeper_st and
  1090. zookeeper_mt. The former only provides the asynchronous APIs and
  1091. callbacks for integrating into the application's event loop. The only
  1092. reason this library exists is to support the platforms were a
  1093. <em>pthread</em> library is not available or is unstable
  1094. (i.e. FreeBSD 4.x). In all other cases, application developers should
  1095. link with zookeeper_mt, as it includes support for both Sync and Async
  1096. API.</p>
  1097. <a name="N104E8"></a><a name="Installation"></a>
  1098. <h4>Installation</h4>
  1099. <p>If you're building the client from a check-out from the Apache
  1100. repository, follow the steps outlined below. If you're building from a
  1101. project source package downloaded from apache, skip to step <strong>3</strong>.</p>
  1102. <ol>
  1103. <li>
  1104. <p>Run <span class="codefrag command">ant compile_just</span> from the zookeeper
  1105. top level directory (<span class="codefrag filename">.../trunk/zookeeper</span>).
  1106. This will create a directory named "generated" under
  1107. <span class="codefrag filename">zookeeper/c</span>.</p>
  1108. </li>
  1109. <li>
  1110. <p>Change directory to the<span class="codefrag filename">zookeeper/c</span> and
  1111. run <span class="codefrag command">autoreconf -i</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.
  1112. Skip to step<strong> 4</strong>.</p>
  1113. </li>
  1114. <li>
  1115. <p>If you are building from a project source package,
  1116. unzip/untar the source tarball and cd to the<span class="codefrag filename">
  1117. zookeeper-x.x.x/</span> directory.</p>
  1118. </li>
  1119. <li>
  1120. <p>Run <span class="codefrag command">./configure &lt;your-options&gt;</span> to
  1121. generate the makefile. Here are some of options the <strong>configure</strong> utility supports that can be
  1122. useful in this step:</p>
  1123. <ul>
  1124. <li>
  1125. <p>
  1126. <span class="codefrag command">--enable-debug</span>
  1127. </p>
  1128. <p>Enables optimization and enables debug info compiler
  1129. options. (Disabled by default.)</p>
  1130. </li>
  1131. <li>
  1132. <p>
  1133. <span class="codefrag command">--without-syncapi </span>
  1134. </p>
  1135. <p>Disables Sync API support; zookeeper_mt library won't be
  1136. built. (Enabled by default.)</p>
  1137. </li>
  1138. <li>
  1139. <p>
  1140. <span class="codefrag command">--disable-static </span>
  1141. </p>
  1142. <p>Do not build static libraries. (Enabled by
  1143. default.)</p>
  1144. </li>
  1145. <li>
  1146. <p>
  1147. <span class="codefrag command">--disable-shared</span>
  1148. </p>
  1149. <p>Do not build shared libraries. (Enabled by
  1150. default.)</p>
  1151. </li>
  1152. </ul>
  1153. <div class="note">
  1154. <div class="label">Note</div>
  1155. <div class="content">
  1156. <p>See INSTALL for general information about running
  1157. <strong>configure</strong>.</p>
  1158. </div>
  1159. </div>
  1160. </li>
  1161. <li>
  1162. <p>Run <span class="codefrag command">make</span> or <span class="codefrag command">make
  1163. install</span> to build the libraries and install them.</p>
  1164. </li>
  1165. <li>
  1166. <p>To generate doxygen documentation for the ZooKeeper API, run
  1167. <span class="codefrag command">make doxygen-doc</span>. All documentation will be
  1168. placed in a new subfolder named docs. By default, this command
  1169. only generates HTML. For information on other document formats,
  1170. run <span class="codefrag command">./configure --help</span>
  1171. </p>
  1172. </li>
  1173. </ol>
  1174. <a name="N10591"></a><a name="Using+the+Client"></a>
  1175. <h4>Using the Client</h4>
  1176. <p>You can test your client by running a zookeeper server (see
  1177. instructions on the project wiki page on how to run it) and connecting
  1178. to it using one of the cli applications that were built as part of the
  1179. installation procedure. cli_mt (multithreaded, built against
  1180. zookeeper_mt library) is shown in this example, but you could also use
  1181. cli_st (singlethreaded, built against zookeeper_st library):</p>
  1182. <p>
  1183. <span class="codefrag command">$ cli_mt zookeeper_host:9876</span>
  1184. </p>
  1185. <p>This
  1186. is a client application that gives you a shell for executing simple
  1187. zookeeper commands. Once succesully started and connected to the
  1188. server it displays a shell prompt. You can now enter zookeeper
  1189. commands. For example, to create a node:</p>
  1190. <pre class="code">&gt; create /my_new_node</pre>
  1191. <p>To verify that the node's been created:</p>
  1192. <p>You should see a list of node who are children of the root node
  1193. "/".</p>
  1194. <p>In order to be able to use the ZooKeeper API in your application
  1195. you have to remember to</p>
  1196. <ol>
  1197. <li>
  1198. <p>Include zookeeper header: #include
  1199. &lt;zookeeper/zookeeper.h</p>
  1200. </li>
  1201. <li>
  1202. <p>If you are building a multithreaded client, compile with
  1203. -DTHREADED compiler flag to enable the multi-threaded version of
  1204. the library, and then link against against the
  1205. <em>zookeeper_mt</em> library. If you are building a
  1206. single-threaded client, do not compile with -DTHREADED, and be
  1207. sure to link against the<em> zookeeper_st
  1208. </em>library.</p>
  1209. </li>
  1210. </ol>
  1211. <p>Refer to <a href="#ch_programStructureWithExample">Program Structure, with Simple Example</a>
  1212. for examples of usage in Java and C.
  1213. <em>[tbd]</em>
  1214. </p>
  1215. </div>
  1216. <a name="N105D0"></a><a name="ch_guideToZkOperations"></a>
  1217. <h2 class="h3">Building Blocks: A Guide to ZooKeeper Operations</h2>
  1218. <div class="section">
  1219. <p>This section surveys all the operations a developer can perform
  1220. against a ZooKeeper server. It is lower level information than the earlier
  1221. concepts chapters in this manual, but higher level than the ZooKeeper API
  1222. Reference. It covers these topics:</p>
  1223. <ul>
  1224. <li>
  1225. <p>
  1226. <a href="#sc_connectingToZk">Connecting to ZooKeeper</a>
  1227. </p>
  1228. </li>
  1229. </ul>
  1230. <a name="N105E4"></a><a name="sc_connectingToZk"></a>
  1231. <h3 class="h4">Connecting to ZooKeeper</h3>
  1232. <p></p>
  1233. <a name="N105ED"></a><a name="sc_readOps"></a>
  1234. <h3 class="h4">Read Operations</h3>
  1235. <p></p>
  1236. <a name="N105F6"></a><a name="sc_writeOps"></a>
  1237. <h3 class="h4">Write Operations</h3>
  1238. <p></p>
  1239. <a name="N105FF"></a><a name="sc_handlingWatches"></a>
  1240. <h3 class="h4">Handling Watches</h3>
  1241. <p></p>
  1242. <a name="N10608"></a><a name="sc_miscOps"></a>
  1243. <h3 class="h4">Miscelleaneous ZooKeeper Operations</h3>
  1244. <p></p>
  1245. </div>
  1246. <a name="N10612"></a><a name="ch_programStructureWithExample"></a>
  1247. <h2 class="h3">Program Structure, with Simple Example</h2>
  1248. <div class="section">
  1249. <p>
  1250. <em>[tbd]</em>
  1251. </p>
  1252. </div>
  1253. <a name="N1061D"></a><a name="ch_gotchas"></a>
  1254. <h2 class="h3">Gotchas: Common Problems and Troubleshooting</h2>
  1255. <div class="section">
  1256. <p>So now you know ZooKeeper. It's fast, simple, your application
  1257. works, but wait ... something's wrong. Here are some pitfalls that
  1258. ZooKeeper users fall into:</p>
  1259. <ol>
  1260. <li>
  1261. <p>If you are using watches, you must look for the connected watch
  1262. event. When a ZooKeeper client disconnects from a server, all the
  1263. watches are removed, so a client must treat the disconnect event as an
  1264. implicit trigger of watches. The easiest way to deal with this is to
  1265. act like the connected watch event is a watch trigger for all your
  1266. watches. The connected event makes a better trigger than the
  1267. disconnected event because you can access ZooKeeper and reestablish
  1268. watches when you are connected.</p>
  1269. </li>
  1270. <li>
  1271. <p>You must test ZooKeeper server failures. The ZooKeeper service
  1272. can survive failures as long as a majority of servers are active. The
  1273. question to ask is: can your application handle it? In the real world
  1274. a client's connection to ZooKeeper can break. (ZooKeeper server
  1275. failures and network partitions are common reasons for connection
  1276. loss.) The ZooKeeper client library takes care of recovering your
  1277. connection and letting you know what happened, but you must make sure
  1278. that you recover your state and any outstanding requests that failed.
  1279. Find out if you got it right in the test lab, not in production - test
  1280. with a ZooKeeper service made up of a several of servers and subject
  1281. them to reboots.</p>
  1282. </li>
  1283. <li>
  1284. <p>The list of ZooKeeper servers used by the client must match the
  1285. list of ZooKeeper servers that each ZooKeeper server has. Things can
  1286. work, although not optimally, if the client list is a subset of the
  1287. real list of ZooKeeper servers, but not if the client lists ZooKeeper
  1288. servers not in the ZooKeeper cluster.</p>
  1289. </li>
  1290. <li>
  1291. <p>Be careful where you put that transaction log. The most
  1292. performance-critical part of ZooKeeper is the transaction log.
  1293. ZooKeeper must sync transactions to media before it returns a
  1294. response. A dedicated transaction log device is key to consistent good
  1295. performance. Putting the log on a busy device will adversely effect
  1296. performance. If you only have one storage device, put trace files on
  1297. NFS and increase the snapshotCount; it doesn't eliminate the problem,
  1298. but it can mitigate it.</p>
  1299. </li>
  1300. <li>
  1301. <p>Set your Java max heap size correctly. It is very important to
  1302. <em>avoid swapping.</em> Going to disk unnecessarily will
  1303. almost certainly degrade your performance unacceptably. Remember, in
  1304. ZooKeeper, everything is ordered, so if one request hits the disk, all
  1305. other queued requests hit the disk.</p>
  1306. <p>To avoid swapping, try to set the heapsize to the amount of
  1307. physical memory you have, minus the amount needed by the OS and cache.
  1308. The best way to determine an optimal heap size for your configurations
  1309. is to <em>run load tests</em>. If for some reason you
  1310. can't, be conservative in your estimates and choose a number well
  1311. below the limit that would cause your machine to swap. For example, on
  1312. a 4G machine, a 3G heap is a conservative estimate to start
  1313. with.</p>
  1314. </li>
  1315. </ol>
  1316. </div>
  1317. <a name="apx_linksToOtherInfo"></a>
  1318. <appendix id="apx_linksToOtherInfo">
  1319. <title>Links to Other Information</title>
  1320. <p>Outside the formal documentation, there're several other sources of
  1321. information for ZooKeeper developers.</p>
  1322. <dl>
  1323. <dt>
  1324. <term>ZooKeeper Whitepaper <em>[tbd: find url]</em>
  1325. </term>
  1326. </dt>
  1327. <dd>
  1328. <p>The definitive discussion of ZooKeeper design and performance,
  1329. by Yahoo! Research</p>
  1330. </dd>
  1331. <dt>
  1332. <term>API Reference <em>[tbd: find url]</em>
  1333. </term>
  1334. </dt>
  1335. <dd>
  1336. <p>The complete reference to the ZooKeeper API</p>
  1337. </dd>
  1338. <dt>
  1339. <term>
  1340. <a href="http://us.dl1.yimg.com/download.yahoo.com/dl/ydn/zookeeper.m4v">Zookeeper
  1341. Talk at the Hadoup Summit 2008</a>
  1342. </term>
  1343. </dt>
  1344. <dd>
  1345. <p>A video introduction to ZooKeeper, by Benjamin Reed of Yahoo!
  1346. Research</p>
  1347. </dd>
  1348. <dt>
  1349. <term>
  1350. <a href="http://wiki.apache.org/hadoop/ZooKeeper/Tutorial">Barrier and
  1351. Queue Tutorial</a>
  1352. </term>
  1353. </dt>
  1354. <dd>
  1355. <p>The excellent Java tutorial by Flavio Junqueira, implementing
  1356. simple barriers and producer-consumer queues using ZooKeeper.</p>
  1357. </dd>
  1358. <dt>
  1359. <term>
  1360. <a href="http://wiki.apache.org/hadoop/ZooKeeper/ZooKeeperArticles">ZooKeeper
  1361. - A Reliable, Scalable Distributed Coordination System</a>
  1362. </term>
  1363. </dt>
  1364. <dd>
  1365. <p>An article by Todd Hoff (07/15/2008)</p>
  1366. </dd>
  1367. <dt>
  1368. <term>
  1369. <a href="recipes.html">Zookeeper Recipes</a>
  1370. </term>
  1371. </dt>
  1372. <dd>
  1373. <p>Pseudo-level discussion of the implementation of various
  1374. synchronization solutions with ZooKeeper: Event Handles, Queues,
  1375. Locks, and Two-phase Commits.</p>
  1376. </dd>
  1377. <dt>
  1378. <term>
  1379. <em>[tbd]</em>
  1380. </term>
  1381. </dt>
  1382. <dd>
  1383. <p>Any other good sources anyone can think of...</p>
  1384. </dd>
  1385. </dl>
  1386. </appendix>
  1387. <p align="right">
  1388. <font size="-2"></font>
  1389. </p>
  1390. </div>
  1391. <!--+
  1392. |end content
  1393. +-->
  1394. <div class="clearboth">&nbsp;</div>
  1395. </div>
  1396. <div id="footer">
  1397. <!--+
  1398. |start bottomstrip
  1399. +-->
  1400. <div class="lastmodified">
  1401. <script type="text/javascript"><!--
  1402. document.write("Last Published: " + document.lastModified);
  1403. // --></script>
  1404. </div>
  1405. <div class="copyright">
  1406. Copyright &copy;
  1407. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  1408. </div>
  1409. <!--+
  1410. |end bottomstrip
  1411. +-->
  1412. </div>
  1413. </body>
  1414. </html>