zookeeperInternals.html 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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 Internals</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="ZooKeeper: distributed coordination"></a>
  43. </div>
  44. <!--+
  45. |end Project Logo
  46. +-->
  47. <!--+
  48. |start Search
  49. +-->
  50. <div class="searchbox">
  51. <form action="http://www.google.com/search" method="get" class="roundtopsmall">
  52. <input value="hadoop.apache.org" name="sitesearch" type="hidden"><input onFocus="getBlank (this, 'Search the site with google');" size="25" name="q" id="query" type="text" value="Search the site with google">&nbsp;
  53. <input name="Search" value="Search" type="submit">
  54. </form>
  55. </div>
  56. <!--+
  57. |end search
  58. +-->
  59. <!--+
  60. |start Tabs
  61. +-->
  62. <ul id="tabs">
  63. <li>
  64. <a class="unselected" href="http://hadoop.apache.org/zookeeper/">Project</a>
  65. </li>
  66. <li>
  67. <a class="unselected" href="http://wiki.apache.org/hadoop/ZooKeeper">Wiki</a>
  68. </li>
  69. <li class="current">
  70. <a class="selected" href="index.html">ZooKeeper 3.3 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_1.2', 'skin/')" id="menu_1.2Title" class="menutitle">Developer</div>
  120. <div id="menu_1.2" class="menuitemgroup">
  121. <div class="menuitem">
  122. <a href="api/index.html">API Docs</a>
  123. </div>
  124. <div class="menuitem">
  125. <a href="zookeeperProgrammers.html">Programmer's Guide</a>
  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">BookKeeper</div>
  138. <div id="menu_1.3" class="menuitemgroup">
  139. <div class="menuitem">
  140. <a href="bookkeeperStarted.html">Getting started</a>
  141. </div>
  142. <div class="menuitem">
  143. <a href="bookkeeperOverview.html">Overview</a>
  144. </div>
  145. <div class="menuitem">
  146. <a href="bookkeeperConfig.html">Setup guide</a>
  147. </div>
  148. <div class="menuitem">
  149. <a href="bookkeeperProgrammer.html">Programmer's guide</a>
  150. </div>
  151. </div>
  152. <div onclick="SwitchMenu('menu_1.4', 'skin/')" id="menu_1.4Title" class="menutitle">Admin &amp; Ops</div>
  153. <div id="menu_1.4" class="menuitemgroup">
  154. <div class="menuitem">
  155. <a href="zookeeperAdmin.html">Administrator's Guide</a>
  156. </div>
  157. <div class="menuitem">
  158. <a href="zookeeperQuotas.html">Quota Guide</a>
  159. </div>
  160. <div class="menuitem">
  161. <a href="zookeeperJMX.html">JMX</a>
  162. </div>
  163. <div class="menuitem">
  164. <a href="zookeeperObservers.html">Observers Guide</a>
  165. </div>
  166. </div>
  167. <div onclick="SwitchMenu('menu_selected_1.5', 'skin/')" id="menu_selected_1.5Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">Contributor</div>
  168. <div id="menu_selected_1.5" class="selectedmenuitemgroup" style="display: block;">
  169. <div class="menupage">
  170. <div class="menupagetitle">ZooKeeper Internals</div>
  171. </div>
  172. </div>
  173. <div onclick="SwitchMenu('menu_1.6', 'skin/')" id="menu_1.6Title" class="menutitle">Miscellaneous</div>
  174. <div id="menu_1.6" class="menuitemgroup">
  175. <div class="menuitem">
  176. <a href="http://wiki.apache.org/hadoop/ZooKeeper">Wiki</a>
  177. </div>
  178. <div class="menuitem">
  179. <a href="http://wiki.apache.org/hadoop/ZooKeeper/FAQ">FAQ</a>
  180. </div>
  181. <div class="menuitem">
  182. <a href="http://hadoop.apache.org/zookeeper/mailing_lists.html">Mailing Lists</a>
  183. </div>
  184. </div>
  185. <div id="credit"></div>
  186. <div id="roundbottom">
  187. <img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
  188. <!--+
  189. |alternative credits
  190. +-->
  191. <div id="credit2"></div>
  192. </div>
  193. <!--+
  194. |end Menu
  195. +-->
  196. <!--+
  197. |start content
  198. +-->
  199. <div id="content">
  200. <div title="Portable Document Format" class="pdflink">
  201. <a class="dida" href="zookeeperInternals.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
  202. PDF</a>
  203. </div>
  204. <h1>ZooKeeper Internals</h1>
  205. <div id="minitoc-area">
  206. <ul class="minitoc">
  207. <li>
  208. <a href="#ch_Introduction">Introduction</a>
  209. </li>
  210. <li>
  211. <a href="#sc_atomicBroadcast">Atomic Broadcast</a>
  212. <ul class="minitoc">
  213. <li>
  214. <a href="#sc_guaranteesPropertiesDefinitions">Guarantees, Properties, and Definitions</a>
  215. </li>
  216. <li>
  217. <a href="#sc_leaderElection">Leader Activation</a>
  218. </li>
  219. <li>
  220. <a href="#sc_activeMessaging">Active Messaging</a>
  221. </li>
  222. <li>
  223. <a href="#sc_summary">Summary</a>
  224. </li>
  225. <li>
  226. <a href="#sc_comparisons">Comparisons</a>
  227. </li>
  228. </ul>
  229. </li>
  230. <li>
  231. <a href="#sc_quorum">Quorums</a>
  232. </li>
  233. <li>
  234. <a href="#sc_logging">Logging</a>
  235. <ul class="minitoc">
  236. <li>
  237. <a href="#sc_developerGuidelines">Developer Guidelines</a>
  238. <ul class="minitoc">
  239. <li>
  240. <a href="#sc_rightLevel">Logging at the Right Level</a>
  241. </li>
  242. <li>
  243. <a href="#sc_log4jIdioms">Use of Standard log4j Idioms</a>
  244. </li>
  245. </ul>
  246. </li>
  247. </ul>
  248. </li>
  249. </ul>
  250. </div>
  251. <a name="N10009"></a><a name="ch_Introduction"></a>
  252. <h2 class="h3">Introduction</h2>
  253. <div class="section">
  254. <p>This document contains information on the inner workings of ZooKeeper.
  255. So far, it discusses these topics:
  256. </p>
  257. <ul>
  258. <li>
  259. <p>
  260. <a href="#sc_atomicBroadcast">Atomic Broadcast</a>
  261. </p>
  262. </li>
  263. <li>
  264. <p>
  265. <a href="#sc_logging">Logging</a>
  266. </p>
  267. </li>
  268. </ul>
  269. </div>
  270. <a name="N10022"></a><a name="sc_atomicBroadcast"></a>
  271. <h2 class="h3">Atomic Broadcast</h2>
  272. <div class="section">
  273. <p>
  274. At the heart of ZooKeeper is an atomic messaging system that keeps all of the servers in sync.</p>
  275. <a name="N1002B"></a><a name="sc_guaranteesPropertiesDefinitions"></a>
  276. <h3 class="h4">Guarantees, Properties, and Definitions</h3>
  277. <p>
  278. The specific guarantees provided by the messaging system used by ZooKeeper are the following:</p>
  279. <dl>
  280. <dt>
  281. <term>
  282. <em>Reliable delivery</em>
  283. </term>
  284. </dt>
  285. <dd>
  286. <p>If a message, m, is delivered
  287. by one server, it will be eventually delivered by all servers.</p>
  288. </dd>
  289. <dt>
  290. <term>
  291. <em>Total order</em>
  292. </term>
  293. </dt>
  294. <dd>
  295. <p> If a message is
  296. delivered before message b by one server, a will be delivered before b by all
  297. servers. If a and b are delivered messages, either a will be delivered before b
  298. or b will be delivered before a.</p>
  299. </dd>
  300. <dt>
  301. <term>
  302. <em>Causal order</em>
  303. </term>
  304. </dt>
  305. <dd>
  306. <p>
  307. If a message b is sent after a message a has been delivered by the sender of b,
  308. a must be ordered before b. If a sender sends c after sending b, c must be ordered after b.
  309. </p>
  310. </dd>
  311. </dl>
  312. <p>
  313. The ZooKeeper messaging system also needs to be efficient, reliable, and easy to
  314. implement and maintain. We make heavy use of messaging, so we need the system to
  315. be able to handle thousands of requests per second. Although we can require at
  316. least k+1 correct servers to send new messages, we must be able to recover from
  317. correlated failures such as power outages. When we implemented the system we had
  318. little time and few engineering resources, so we needed a protocol that is
  319. accessible to engineers and is easy to implement. We found that our protocol
  320. satisfied all of these goals.
  321. </p>
  322. <p>
  323. Our protocol assumes that we can construct point-to-point FIFO channels between
  324. the servers. While similar services usually assume message delivery that can
  325. lose or reorder messages, our assumption of FIFO channels is very practical
  326. given that we use TCP for communication. Specifically we rely on the following property of TCP:</p>
  327. <dl>
  328. <dt>
  329. <term>
  330. <em>Ordered delivery</em>
  331. </term>
  332. </dt>
  333. <dd>
  334. <p>Data is delivered in the same order it is sent and a message m is
  335. delivered only after all messages sent before m have been delivered.
  336. (The corollary to this is that if message m is lost all messages after m will be lost.)</p>
  337. </dd>
  338. <dt>
  339. <term>
  340. <em>No message after close</em>
  341. </term>
  342. </dt>
  343. <dd>
  344. <p>Once a FIFO channel is closed, no messages will be received from it.</p>
  345. </dd>
  346. </dl>
  347. <p>
  348. FLP proved that consensus cannot be achieved in asynchronous distributed systems
  349. if failures are possible. To ensure we achieve consensus in the presence of failures
  350. we use timeouts. However, we rely on times for liveness not for correctness. So,
  351. if timeouts stop working (clocks malfunction for example) the messaging system may
  352. hang, but it will not violate its guarantees.</p>
  353. <p>When describing the ZooKeeper messaging protocol we will talk of packets,
  354. proposals, and messages:</p>
  355. <dl>
  356. <dt>
  357. <term>
  358. <em>Packet</em>
  359. </term>
  360. </dt>
  361. <dd>
  362. <p>a sequence of bytes sent through a FIFO channel</p>
  363. </dd>
  364. <dt>
  365. <term>
  366. <em>Proposal</em>
  367. </term>
  368. </dt>
  369. <dd>
  370. <p>a unit of agreement. Proposals are agreed upon by exchanging packets
  371. with a quorum of ZooKeeper servers. Most proposals contain messages, however the
  372. NEW_LEADER proposal is an example of a proposal that does not correspond to a message.</p>
  373. </dd>
  374. <dt>
  375. <term>
  376. <em>Message</em>
  377. </term>
  378. </dt>
  379. <dd>
  380. <p>a sequence of bytes to be atomically broadcast to all ZooKeeper
  381. servers. A message put into a proposal and agreed upon before it is delivered.</p>
  382. </dd>
  383. </dl>
  384. <p>
  385. As stated above, ZooKeeper guarantees a total order of messages, and it also
  386. guarantees a total order of proposals. ZooKeeper exposes the total ordering using
  387. a ZooKeeper transaction id (<em>zxid</em>). All proposals will be stamped with a zxid when
  388. it is proposed and exactly reflects the total ordering. Proposals are sent to all
  389. ZooKeeper servers and committed when a quorum of them acknowledge the proposal.
  390. If a proposal contains a message, the message will be delivered when the proposal
  391. is committed. Acknowledgement means the server has recorded the proposal to persistent storage.
  392. Our quorums have the requirement that any pair of quorum must have at least one server
  393. in common. We ensure this by requiring that all quorums have size (<em>n/2+1</em>) where
  394. n is the number of servers that make up a ZooKeeper service.
  395. </p>
  396. <p>
  397. The zxid has two parts: the epoch and a counter. In our implementation the zxid
  398. is a 64-bit number. We use the high order 32-bits for the epoch and the low order
  399. 32-bits for the counter. Because it has two parts represent the zxid both as a
  400. number and as a pair of integers, (<em>epoch, count</em>). The epoch number represents a
  401. change in leadership. Each time a new leader comes into power it will have its
  402. own epoch number. We have a simple algorithm to assign a unique zxid to a proposal:
  403. the leader simply increments the zxid to obtain a unique zxid for each proposal.
  404. <em>Leadership activation will ensure that only one leader uses a given epoch, so our
  405. simple algorithm guarantees that every proposal will have a unique id.</em>
  406. </p>
  407. <p>
  408. ZooKeeper messaging consists of two phases:</p>
  409. <dl>
  410. <dt>
  411. <term>
  412. <em>Leader activation</em>
  413. </term>
  414. </dt>
  415. <dd>
  416. <p>In this phase a leader establishes the correct state of the system
  417. and gets ready to start making proposals.</p>
  418. </dd>
  419. <dt>
  420. <term>
  421. <em>Active messaging</em>
  422. </term>
  423. </dt>
  424. <dd>
  425. <p>In this phase a leader accepts messages to propose and coordinates message delivery.</p>
  426. </dd>
  427. </dl>
  428. <p>
  429. ZooKeeper is a holistic protocol. We do not focus on individual proposals, rather
  430. look at the stream of proposals as a whole. Our strict ordering allows us to do this
  431. efficiently and greatly simplifies our protocol. Leadership activation embodies
  432. this holistic concept. A leader becomes active only when a quorum of followers
  433. (The leader counts as a follower as well. You can always vote for yourself ) has synced
  434. up with the leader, they have the same state. This state consists of all of the
  435. proposals that the leader believes have been committed and the proposal to follow
  436. the leader, the NEW_LEADER proposal. (Hopefully you are thinking to
  437. yourself, <em>Does the set of proposals that the leader believes has been committed
  438. included all the proposals that really have been committed?</em> The answer is <em>yes</em>.
  439. Below, we make clear why.)
  440. </p>
  441. <a name="N100B9"></a><a name="sc_leaderElection"></a>
  442. <h3 class="h4">Leader Activation</h3>
  443. <p>
  444. Leader activation includes leader election. We currently have two leader election
  445. algorithms in ZooKeeper: LeaderElection and FastLeaderElection (AuthFastLeaderElection
  446. is a variant of FastLeaderElection that uses UDP and allows servers to perform a simple
  447. form of authentication to avoid IP spoofing). ZooKeeper messaging doesn't care about the
  448. exact method of electing a leader has long as the following holds:
  449. </p>
  450. <ul>
  451. <li>
  452. <p>The leader has seen the highest zxid of all the followers.</p>
  453. </li>
  454. <li>
  455. <p>A quorum of servers have committed to following the leader.</p>
  456. </li>
  457. </ul>
  458. <p>
  459. Of these two requirements only the first, the highest zxid amoung the followers
  460. needs to hold for correct operation. The second requirement, a quorum of followers,
  461. just needs to hold with high probability. We are going to recheck the second requirement,
  462. so if a failure happens during or after the leader election and quorum is lost,
  463. we will recover by abandoning leader activation and running another election.
  464. </p>
  465. <p>
  466. After leader election a single server will be designated as a leader and start
  467. waiting for followers to connect. The rest of the servers will try to connect to
  468. the leader. The leader will sync up with followers by sending any proposals they
  469. are missing, or if a follower is missing too many proposals, it will send a full
  470. snapshot of the state to the follower.
  471. </p>
  472. <p>
  473. There is a corner case in which a follower that has proposals, U, not seen
  474. by a leader arrives. Proposals are seen in order, so the proposals of U will have a zxids
  475. higher than zxids seen by the leader. The follower must have arrived after the
  476. leader election, otherwise the follower would have been elected leader given that
  477. it has seen a higher zxid. Since committed proposals must be seen by a quorum of
  478. servers, and a quorum of servers that elected the leader did not see U, the proposals
  479. of you have not been committed, so they can be discarded. When the follower connects
  480. to the leader, the leader will tell the follower to discard U.
  481. </p>
  482. <p>
  483. A new leader establishes a zxid to start using for new proposals by getting the
  484. epoch, e, of the highest zxid it has seen and setting the next zxid to use to be
  485. (e+1, 0), fter the leader syncs with a follower, it will propose a NEW_LEADER
  486. proposal. Once the NEW_LEADER proposal has been committed, the leader will activate
  487. and start receiving and issuing proposals.
  488. </p>
  489. <p>
  490. It all sounds complicated but here are the basic rules of operation during leader
  491. activation:
  492. </p>
  493. <ul>
  494. <li>
  495. <p>A follower will ACK the NEW_LEADER proposal after it has synced with the leader.</p>
  496. </li>
  497. <li>
  498. <p>A follower will only ACK a NEW_LEADER proposal with a given zxid from a single server.</p>
  499. </li>
  500. <li>
  501. <p>A new leader will COMMIT the NEW_LEADER proposal when a quorum of followers have ACKed it.</p>
  502. </li>
  503. <li>
  504. <p>A follower will commit any state it received from the leader when the NEW_LEADER proposal is COMMIT.</p>
  505. </li>
  506. <li>
  507. <p>A new leader will not accept new proposals until the NEW_LEADER proposal has been COMMITED.</p>
  508. </li>
  509. </ul>
  510. <p>
  511. If leader election terminates erroneously, we don't have a problem since the
  512. NEW_LEADER proposal will not be committed since the leader will not have quorum.
  513. When this happens, the leader and any remaining followers will timeout and go back
  514. to leader election.
  515. </p>
  516. <a name="N100F7"></a><a name="sc_activeMessaging"></a>
  517. <h3 class="h4">Active Messaging</h3>
  518. <p>
  519. Leader Activation does all the heavy lifting. Once the leader is coronated he can
  520. start blasting out proposals. As long as he remains the leader no other leader can
  521. emerge since no other leader will be able to get a quorum of followers. If a new
  522. leader does emerge,
  523. it means that the leader has lost quorum, and the new leader will clean up any
  524. mess left over during her leadership activation.
  525. </p>
  526. <p>ZooKeeper messaging operates similar to a classic two-phase commit.</p>
  527. <img alt="" src="images/2pc.jpg"><p>
  528. All communication channels are FIFO, so everything is done in order. Specifically
  529. the following operating constraints are observed:</p>
  530. <ul>
  531. <li>
  532. <p>The leader sends proposals to all followers using
  533. the same order. Moreover, this order follows the order in which requests have been
  534. received. Because we use FIFO channels this means that followers also receive proposals in order.
  535. </p>
  536. </li>
  537. <li>
  538. <p>Followers process messages in the order they are received. This
  539. means that messages will be ACKed in order and the leader will receive ACKs from
  540. followers in order, due to the FIFO channels. It also means that if message $m$
  541. has been written to non-volatile storage, all messages that were proposed before
  542. $m$ have been written to non-volatile storage.</p>
  543. </li>
  544. <li>
  545. <p>The leader will issue a COMMIT to all followers as soon as a
  546. quorum of followers have ACKed a message. Since messages are ACKed in order,
  547. COMMITs will be sent by the leader as received by the followers in order.</p>
  548. </li>
  549. <li>
  550. <p>COMMITs are processed in order. Followers deliver a proposals
  551. message when that proposal is committed.</p>
  552. </li>
  553. </ul>
  554. <a name="N1011E"></a><a name="sc_summary"></a>
  555. <h3 class="h4">Summary</h3>
  556. <p>So there you go. Why does it work? Specifically, why does is set of proposals
  557. believed by a new leader always contain any proposal that has actually been committed?
  558. First, all proposals have a unique zxid, so unlike other protocols, we never have
  559. to worry about two different values being proposed for the same zxid; followers
  560. (a leader is also a follower) see and record proposals in order; proposals are
  561. committed in order; there is only one active leader at a time since followers only
  562. follow a single leader at a time; a new leader has seen all committed proposals
  563. from the previous epoch since it has seen the highest zxid from a quorum of servers;
  564. any uncommited proposals from a previous epoch seen by a new leader will be committed
  565. by that leader before it becomes active.</p>
  566. <a name="N10127"></a><a name="sc_comparisons"></a>
  567. <h3 class="h4">Comparisons</h3>
  568. <p>
  569. Isn't this just Multi-Paxos? No, Multi-Paxos requires some way of assuring that
  570. there is only a single coordinator. We do not count on such assurances. Instead
  571. we use the leader activation to recover from leadership change or old leaders
  572. believing they are still active.
  573. </p>
  574. <p>
  575. Isn't this just Paxos? Your active messaging phase looks just like phase 2 of Paxos?
  576. Actually, to us active messaging looks just like 2 phase commit without the need to
  577. handle aborts. Active messaging is different from both in the sense that it has
  578. cross proposal ordering requirements. If we do not maintain strict FIFO ordering of
  579. all packets, it all falls apart. Also, our leader activation phase is different from
  580. both of them. In particular, our use of epochs allows us to skip blocks of uncommitted
  581. proposals and to not worry about duplicate proposals for a given zxid.
  582. </p>
  583. </div>
  584. <a name="N10134"></a><a name="sc_quorum"></a>
  585. <h2 class="h3">Quorums</h2>
  586. <div class="section">
  587. <p>
  588. Atomic broadcast and leader election use the notion of quorum to guarantee a consistent
  589. view of the system. By default, ZooKeeper uses majority quorums, which means that every
  590. voting that happens in one of these protocols requires a majority to vote on. One example is
  591. acknowledging a leader proposal: the leader can only commit once it receives an
  592. acknowledgement from a quorum of servers.
  593. </p>
  594. <p>
  595. If we extract the properties that we really need from our use of majorities, we have that we only
  596. need to guarantee that groups of processes used to validate an operation by voting (e.g., acknowledging
  597. a leader proposal) pairwise intersect in at least one server. Using majorities guarantees such a property.
  598. However, there are other ways of constructing quorums different from majorities. For example, we can assign
  599. weights to the votes of servers, and say that the votes of some servers are more important. To obtain a quorum,
  600. we get enough votes so that the sum of weights of all votes is larger than half of the total sum of all weights.
  601. </p>
  602. <p>
  603. A different construction that uses weights and is useful in wide-area deployments (co-locations) is a hierarchical
  604. one. With this construction, we split the servers into disjoint groups and assign weights to processes. To form
  605. a quorum, we have to get a hold of enough servers from a majority of groups G, such that for each group g in G,
  606. the sum of votes from g is larger than half of the sum of weights in g. Interestingly, this construction enables
  607. smaller quorums. If we have, for example, 9 servers, we split them into 3 groups, and assign a weight of 1 to each
  608. server, then we are able to form quorums of size 4. Note that two subsets of processes composed each of a majority
  609. of servers from each of a majority of groups necessarily have a non-empty intersection. It is reasonable to expect
  610. that a majority of co-locations will have a majority of servers available with high probability.
  611. </p>
  612. <p>
  613. With ZooKeeper, we provide a user with the ability of configuring servers to use majority quorums, weights, or a
  614. hierarchy of groups.
  615. </p>
  616. </div>
  617. <a name="N10147"></a><a name="sc_logging"></a>
  618. <h2 class="h3">Logging</h2>
  619. <div class="section">
  620. <p>
  621. ZooKeeper uses
  622. <a href="http://logging.apache.org/log4j">log4j</a>
  623. version 1.2 as its logging infrastructure. For information on configuring log4j for
  624. ZooKeeper, see the <a href="zookeeperAdmin.html#sc_logging">Logging</a> section
  625. of the <a href="zookeeperAdmin.html">ZooKeeper Administrator's Guide.</a>
  626. </p>
  627. <a name="N1015C"></a><a name="sc_developerGuidelines"></a>
  628. <h3 class="h4">Developer Guidelines</h3>
  629. <p>Please follow these guidelines when submitting code. Patch reviewers will look for the following:</p>
  630. <a name="N10164"></a><a name="sc_rightLevel"></a>
  631. <h4>Logging at the Right Level</h4>
  632. <p>
  633. There are <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html#FATAL">6 levels of logging in log4j</a>.
  634. It's important to pick the right one. In order of higher to lower severity:</p>
  635. <ol>
  636. <li>
  637. <p> FATAL level designates very severe error events that will presumably lead the application to abort</p>
  638. </li>
  639. <li>
  640. <p>ERROR level designates error events that might still allow the application to continue running.</p>
  641. </li>
  642. <li>
  643. <p>WARN level designates potentially harmful situations.</p>
  644. </li>
  645. <li>
  646. <p>INFO level designates informational messages that highlight the progress of the application at coarse-grained level.</p>
  647. </li>
  648. <li>
  649. <p>EBUG Level designates fine-grained informational events that are most useful to debug an application.</p>
  650. </li>
  651. <li>
  652. <p>TRACE Level designates finer-grained informational events than the DEBUG.</p>
  653. </li>
  654. </ol>
  655. <p>
  656. ZooKeeper is typically run in production such that log messages of INFO level
  657. severity and higher (more severe) are output to the log.</p>
  658. <a name="N1018F"></a><a name="sc_log4jIdioms"></a>
  659. <h4>Use of Standard log4j Idioms</h4>
  660. <p>
  661. <em>Static Message Logging</em>
  662. </p>
  663. <pre class="code">
  664. LOG.debug("process completed successfully!");
  665. </pre>
  666. <p>However when creating a message from a number of components (string
  667. concatenation), the log call should be wrapped with a "isXEnabled()" call. this
  668. eliminates the string concatenation overhead when debug level logging is not enabled.
  669. </p>
  670. <pre class="code">
  671. if (LOG.isDebugEnabled()) {
  672. LOG.debug("got " + count + " messages in " + time + " minutes");
  673. }
  674. </pre>
  675. <p>
  676. <em>Naming</em>
  677. </p>
  678. <p>
  679. Loggers should be named after the class in which they are used. (See the
  680. <a href="http://logging.apache.org/log4j/1.2/faq.html#2.4">log4j faq</a>
  681. for reasons why this is a good idea.)
  682. </p>
  683. <pre class="code">
  684. public class Foo {
  685. private static final Logger LOG = Logger.getLogger(Foo.class);
  686. ....
  687. public Foo() {
  688. LOG.info("constructing Foo");
  689. </pre>
  690. <p>
  691. <em>Exception handling</em>
  692. </p>
  693. <pre class="code">
  694. try {
  695. // code
  696. } catch (XYZException e) {
  697. // do this
  698. LOG.error("Something bad happened", e);
  699. // don't do this (generally)
  700. // LOG.error(e);
  701. // why? because "don't do" case hides the stack trace
  702. // continue process here as you need... recover or (re)throw
  703. }
  704. </pre>
  705. </div>
  706. <p align="right">
  707. <font size="-2"></font>
  708. </p>
  709. </div>
  710. <!--+
  711. |end content
  712. +-->
  713. <div class="clearboth">&nbsp;</div>
  714. </div>
  715. <div id="footer">
  716. <!--+
  717. |start bottomstrip
  718. +-->
  719. <div class="lastmodified">
  720. <script type="text/javascript"><!--
  721. document.write("Last Published: " + document.lastModified);
  722. // --></script>
  723. </div>
  724. <div class="copyright">
  725. Copyright &copy;
  726. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  727. </div>
  728. <!--+
  729. |end bottomstrip
  730. +-->
  731. </div>
  732. </body>
  733. </html>