zookeeperProgrammers.html 55 KB

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