zookeeperReconfig.html 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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.9">
  7. <meta name="Forrest-skin-name" content="pelt">
  8. <title>ZooKeeper Dynamic Reconfiguration</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://zookeeper.apache.org/">ZooKeeper</a> &gt; <a href="http://zookeeper.apache.org/">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://zookeeper.apache.org/"><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="zookeeper.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://zookeeper.apache.org/">Project</a>
  65. </li>
  66. <li>
  67. <a class="unselected" href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/">Wiki</a>
  68. </li>
  69. <li class="current">
  70. <a class="selected" href="index.html">ZooKeeper 3.4 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_selected_1.3', 'skin/')" id="menu_selected_1.3Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">Admin &amp; Ops</div>
  138. <div id="menu_selected_1.3" class="selectedmenuitemgroup" style="display: block;">
  139. <div class="menuitem">
  140. <a href="zookeeperAdmin.html">Administrator's Guide</a>
  141. </div>
  142. <div class="menuitem">
  143. <a href="zookeeperQuotas.html">Quota Guide</a>
  144. </div>
  145. <div class="menuitem">
  146. <a href="zookeeperJMX.html">JMX</a>
  147. </div>
  148. <div class="menuitem">
  149. <a href="zookeeperObservers.html">Observers Guide</a>
  150. </div>
  151. <div class="menupage">
  152. <div class="menupagetitle">Dynamic Reconfiguration</div>
  153. </div>
  154. </div>
  155. <div onclick="SwitchMenu('menu_1.4', 'skin/')" id="menu_1.4Title" class="menutitle">Contributor</div>
  156. <div id="menu_1.4" class="menuitemgroup">
  157. <div class="menuitem">
  158. <a href="zookeeperInternals.html">ZooKeeper Internals</a>
  159. </div>
  160. </div>
  161. <div onclick="SwitchMenu('menu_1.5', 'skin/')" id="menu_1.5Title" class="menutitle">Miscellaneous</div>
  162. <div id="menu_1.5" class="menuitemgroup">
  163. <div class="menuitem">
  164. <a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER">Wiki</a>
  165. </div>
  166. <div class="menuitem">
  167. <a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/FAQ">FAQ</a>
  168. </div>
  169. <div class="menuitem">
  170. <a href="http://zookeeper.apache.org/mailing_lists.html">Mailing Lists</a>
  171. </div>
  172. </div>
  173. <div id="credit"></div>
  174. <div id="roundbottom">
  175. <img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
  176. <!--+
  177. |alternative credits
  178. +-->
  179. <div id="credit2"></div>
  180. </div>
  181. <!--+
  182. |end Menu
  183. +-->
  184. <!--+
  185. |start content
  186. +-->
  187. <div id="content">
  188. <div title="Portable Document Format" class="pdflink">
  189. <a class="dida" href="zookeeperReconfig.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
  190. PDF</a>
  191. </div>
  192. <h1>ZooKeeper Dynamic Reconfiguration</h1>
  193. <div id="front-matter">
  194. <div id="minitoc-area">
  195. <ul class="minitoc">
  196. <li>
  197. <a href="#ch_reconfig_intro">Overview</a>
  198. </li>
  199. <li>
  200. <a href="#ch_reconfig_format">Changes to Configuration Format</a>
  201. <ul class="minitoc">
  202. <li>
  203. <a href="#sc_reconfig_clientport">Specifying the client port</a>
  204. </li>
  205. <li>
  206. <a href="#sc_reconfig_standaloneEnabled">The standaloneEnabled flag</a>
  207. </li>
  208. <li>
  209. <a href="#sc_reconfig_file">Dynamic configuration file</a>
  210. </li>
  211. <li>
  212. <a href="#sc_reconfig_backward">Backward compatibility</a>
  213. </li>
  214. </ul>
  215. </li>
  216. <li>
  217. <a href="#ch_reconfig_upgrade">Upgrading to 3.5.0</a>
  218. </li>
  219. <li>
  220. <a href="#ch_reconfig_dyn">Dynamic Reconfiguration of the ZooKeeper Ensemble</a>
  221. <ul class="minitoc">
  222. <li>
  223. <a href="#sc_reconfig_retrieving">Retrieving the current dynamic configuration</a>
  224. </li>
  225. <li>
  226. <a href="#sc_reconfig_modifying">Modifying the current dynamic configuration</a>
  227. <ul class="minitoc">
  228. <li>
  229. <a href="#sc_reconfig_general">General</a>
  230. </li>
  231. <li>
  232. <a href="#sc_reconfig_incremental">Incremental mode</a>
  233. </li>
  234. <li>
  235. <a href="#sc_reconfig_nonincremental">Non-incremental mode</a>
  236. </li>
  237. <li>
  238. <a href="#sc_reconfig_conditional">Conditional reconfig</a>
  239. </li>
  240. <li>
  241. <a href="#sc_reconfig_errors">Error conditions</a>
  242. </li>
  243. <li>
  244. <a href="#sc_reconfig_additional">Additional comments</a>
  245. </li>
  246. </ul>
  247. </li>
  248. </ul>
  249. </li>
  250. <li>
  251. <a href="#ch_reconfig_rebalancing">Rebalancing Client Connections</a>
  252. </li>
  253. </ul>
  254. </div>
  255. </div>
  256. <a name="ch_reconfig_intro"></a>
  257. <h2 class="h3">Overview</h2>
  258. <div class="section">
  259. <p>Prior to the 3.5.0 release, the membership and all other configuration
  260. parameters of Zookeeper were static - loaded during boot and immutable at
  261. runtime. Operators resorted to ''rolling restarts'' - a manually intensive
  262. and error-prone method of changing the configuration that has caused data
  263. loss and inconsistency in production.</p>
  264. <p>Starting with 3.5.0, &ldquo;rolling restarts&rdquo; are no longer needed!
  265. ZooKeeper comes with full support for automated configuration changes: the
  266. set of Zookeeper servers, their roles (participant / observer), all ports,
  267. and even the quorum system can be changed dynamically, without service
  268. interruption and while maintaining data consistency. Reconfigurations are
  269. performed immediately, just like other operations in ZooKeeper. Multiple
  270. changes can be done using a single reconfiguration command. The dynamic
  271. reconfiguration functionality does not limit operation concurrency, does
  272. not require client operations to be stopped during reconfigurations, has a
  273. very simple interface for administrators and no added complexity to other
  274. client operations.</p>
  275. <p>New client-side features allow clients to find out about configuration
  276. changes and to update the connection string (list of servers and their
  277. client ports) stored in their ZooKeeper handle. A probabilistic algorithm
  278. is used to rebalance clients across the new configuration servers while
  279. keeping the extent of client migrations proportional to the change in
  280. ensemble membership.</p>
  281. <p>This document provides the administrator manual for reconfiguration.
  282. For a detailed description of the reconfiguration algorithms, performance
  283. measurements, and more, please see our paper:</p>
  284. <dl>
  285. <dt>
  286. <term>Shraer, A., Reed, B., Malkhi, D., Junqueira, F. Dynamic
  287. Reconfiguration of Primary/Backup Clusters. In <em>USENIX Annual
  288. Technical Conference (ATC) </em>(2012), 425-437</term>
  289. </dt>
  290. <dd>
  291. <p>Links: <a href="https://www.usenix.org/system/files/conference/atc12/atc12-final74.pdf">paper (pdf)</a>, <a href="https://www.usenix.org/sites/default/files/conference/protected-files/shraer_atc12_slides.pdf">slides (pdf)</a>, <a href="https://www.usenix.org/conference/atc12/technical-sessions/presentation/shraer">video</a>, <a href="http://www.slideshare.net/Hadoop_Summit/dynamic-reconfiguration-of-zookeeper">hadoop summit slides</a>
  292. </p>
  293. </dd>
  294. </dl>
  295. </div>
  296. <a name="ch_reconfig_format"></a>
  297. <h2 class="h3">Changes to Configuration Format</h2>
  298. <div class="section">
  299. <a name="sc_reconfig_clientport"></a>
  300. <h3 class="h4">Specifying the client port</h3>
  301. <p>A client port of a server is the port on which the server accepts
  302. client connection requests. Starting with 3.5.0 the
  303. <em>clientPort</em> and <em>clientPortAddress
  304. </em> configuration parameters should no longer be used. Instead,
  305. this information is now part of the server keyword specification, which
  306. becomes as follows:</p>
  307. <p>
  308. <span class="codefrag computeroutput">server.&lt;positive id&gt; = &lt;address1&gt;:&lt;port1&gt;:&lt;port2&gt;[:role];[&lt;client port address&gt;:]&lt;client port&gt;</span>
  309. </p>
  310. <p>The client port specification is to the right of the semicolon. The
  311. client port address is optional, and if not specified it defaults to
  312. "0.0.0.0". As usual, role is also optional, it can be
  313. <em>participant</em> or <em>observer</em>
  314. (<em>participant</em> by default).</p>
  315. <p> Examples of legal server statements: </p>
  316. <ul>
  317. <li>
  318. <p>
  319. <span class="codefrag computeroutput">server.5 = 125.23.63.23:1234:1235;1236</span>
  320. </p>
  321. </li>
  322. <li>
  323. <p>
  324. <span class="codefrag computeroutput">server.5 = 125.23.63.23:1234:1235:participant;1236</span>
  325. </p>
  326. </li>
  327. <li>
  328. <p>
  329. <span class="codefrag computeroutput">server.5 = 125.23.63.23:1234:1235:observer;1236</span>
  330. </p>
  331. </li>
  332. <li>
  333. <p>
  334. <span class="codefrag computeroutput">server.5 = 125.23.63.23:1234:1235;125.23.63.24:1236</span>
  335. </p>
  336. </li>
  337. <li>
  338. <p>
  339. <span class="codefrag computeroutput">server.5 = 125.23.63.23:1234:1235:participant;125.23.63.23:1236</span>
  340. </p>
  341. </li>
  342. </ul>
  343. <a name="sc_reconfig_standaloneEnabled"></a>
  344. <h3 class="h4">The standaloneEnabled flag</h3>
  345. <p>Prior to 3.5.0, one could run ZooKeeper in Standalone mode or in a
  346. Distributed mode. These are separate implementation stacks, and
  347. switching between them during run time is not possible. By default (for
  348. backward compatibility) <em>standaloneEnabled</em> is set to
  349. <em>true</em>. The consequence of using this default is that
  350. if started with a single server the ensemble will not be allowed to
  351. grow, and if started with more than one server it will not be allowed to
  352. shrink to contain fewer than two participants.</p>
  353. <p>Setting the flag to <em>false</em> instructs the system
  354. to run the Distributed software stack even if there is only a single
  355. participant in the ensemble. To achieve this the (static) configuration
  356. file should contain:</p>
  357. <p>
  358. <span class="codefrag computeroutput">standaloneEnabled=false</span>
  359. </p>
  360. <p>With this setting it is possible to start a ZooKeeper ensemble
  361. containing a single participant and to dynamically grow it by adding
  362. more servers. Similarly, it is possible to shrink an ensemble so that
  363. just a single participant remains, by removing servers.</p>
  364. <p>Since running the Distributed mode allows more flexibility, we
  365. recommend setting the flag to <em>false</em>. We expect that
  366. the legacy Standalone mode will be deprecated in the future.</p>
  367. <a name="sc_reconfig_file"></a>
  368. <h3 class="h4">Dynamic configuration file</h3>
  369. <p>Starting with 3.5.0 we're distinguishing between dynamic
  370. configuration parameters, which can be changed during runtime, and
  371. static configuration parameters, which are read from a configuration
  372. file when a server boots and don't change during its execution. For now,
  373. the following configuration keywords are considered part of the dynamic
  374. configuration: <em>server</em>, <em>group</em>
  375. and <em>weight</em>.</p>
  376. <p>Dynamic configuration parameters are stored in a separate file on
  377. the server (which we call the dynamic configuration file). This file is
  378. linked from the static config file using the new
  379. <em>dynamicConfigFile</em> keyword.</p>
  380. <p>
  381. <strong>Example</strong>
  382. </p>
  383. <div class="note example">
  384. <div class="label">zoo_replicated1.cfg</div>
  385. <div class="content">
  386. <title>zoo_replicated1.cfg</title>
  387. <pre class="code">tickTime=2000
  388. dataDir=/zookeeper/data/zookeeper1
  389. initLimit=5
  390. syncLimit=2
  391. dynamicConfigFile=/zookeeper/conf/zoo_replicated1.cfg.dynamic</pre>
  392. </div>
  393. </div>
  394. <div class="note example">
  395. <div class="label">zoo_replicated1.cfg.dynamic</div>
  396. <div class="content">
  397. <title>zoo_replicated1.cfg.dynamic</title>
  398. <pre class="code">server.1=125.23.63.23:2780:2783:participant;2791
  399. server.2=125.23.63.24:2781:2784:participant;2792
  400. server.3=125.23.63.25:2782:2785:participant;2793</pre>
  401. </div>
  402. </div>
  403. <p>When the ensemble configuration changes, the static configuration
  404. parameters remain the same. The dynamic parameters are pushed by
  405. ZooKeeper and overwrite the dynamic configuration files on all servers.
  406. Thus, the dynamic configuration files on the different servers are
  407. usually identical (they can only differ momentarily when a
  408. reconfiguration is in progress, or if a new configuration hasn't
  409. propagated yet to some of the servers). Once created, the dynamic
  410. configuration file should not be manually altered. Changed are only made
  411. through the new reconfiguration commands outlined below. Note that
  412. changing the config of an offline cluster could result in an
  413. inconsistency with respect to configuration information stored in the
  414. ZooKeeper log (and the special configuration znode, populated from the
  415. log) and is therefore highly discouraged.</p>
  416. <p>
  417. <strong>Example 2</strong>
  418. </p>
  419. <p>Users may prefer to initially specify a single configuration file.
  420. The following is thus also legal:</p>
  421. <div class="note example">
  422. <div class="label">zoo_replicated1.cfg</div>
  423. <div class="content">
  424. <title>zoo_replicated1.cfg</title>
  425. <pre class="code">tickTime=2000
  426. dataDir=/zookeeper/data/zookeeper1
  427. initLimit=5
  428. syncLimit=2
  429. clientPort=<strong>2791</strong> // note that this line is now redundant and therefore not recommended
  430. server.1=125.23.63.23:2780:2783:participant;<strong>2791</strong>
  431. server.2=125.23.63.24:2781:2784:participant;2792
  432. server.3=125.23.63.25:2782:2785:participant;2793</pre>
  433. </div>
  434. </div>
  435. <p>The configuration files on each server will be automatically split
  436. into dynamic and static files, if they are not already in this format.
  437. So the configuration file above will be automatically transformed into
  438. the two files in Example 1. Note that the clientPort and
  439. clientPortAddress lines (if specified) will be automatically removed
  440. during this process, if they are redundant (as in the example above).
  441. The original static configuration file is backed up (in a .bak
  442. file).</p>
  443. <a name="sc_reconfig_backward"></a>
  444. <h3 class="h4">Backward compatibility</h3>
  445. <p>We still support the old configuration format. For example, the
  446. following configuration file is acceptable (but not recommended):</p>
  447. <div class="note example">
  448. <div class="label">zoo_replicated1.cfg</div>
  449. <div class="content">
  450. <title>zoo_replicated1.cfg</title>
  451. <pre class="code">tickTime=2000
  452. dataDir=/zookeeper/data/zookeeper1
  453. initLimit=5
  454. syncLimit=2
  455. clientPort=2791
  456. server.1=125.23.63.23:2780:2783:participant
  457. server.2=125.23.63.24:2781:2784:participant
  458. server.3=125.23.63.25:2782:2785:participant</pre>
  459. </div>
  460. </div>
  461. <p>During boot, a dynamic configuration file is created and contains
  462. the dynamic part of the configuration as explained earlier. In this
  463. case, however, the line "clientPort=2791" will remain in the static
  464. configuration file of server 1 since it is not redundant -- it was not
  465. specified as part of the "server.1=..." using the format explained in
  466. the section <a href="#ch_reconfig_format">Changes to Configuration Format</a>. If a reconfiguration
  467. is invoked that sets the client port of server 1, we remove
  468. "clientPort=2791" from the static configuration file (the dynamic file
  469. now contain this information as part of the specification of server
  470. 1).</p>
  471. </div>
  472. <a name="ch_reconfig_upgrade"></a>
  473. <h2 class="h3">Upgrading to 3.5.0</h2>
  474. <div class="section">
  475. <p>Upgrading a running ZooKeeper ensemble to 3.5.0 should be done only
  476. after upgrading your ensemble to the 3.4.6 release. Note that this is only
  477. necessary for rolling upgrades (if you're fine with shutting down the
  478. system completely, you don't have to go through 3.4.6). If you attempt a
  479. rolling upgrade without going through 3.4.6 (for example from 3.4.5), you
  480. may get the following error:</p>
  481. <pre class="code">2013-01-30 11:32:10,663 [myid:2] - INFO [localhost/127.0.0.1:2784:QuorumCnxManager$Listener@498] - Received connection request /127.0.0.1:60876
  482. 2013-01-30 11:32:10,663 [myid:2] - WARN [localhost/127.0.0.1:2784:QuorumCnxManager@349] - Invalid server id: -65536</pre>
  483. <p>During a rolling upgrade, each server is taken down in turn and
  484. rebooted with the new 3.5.0 binaries. Before starting the server with
  485. 3.5.0 binaries, we highly recommend updating the configuration file so
  486. that all server statements "server.x=..." contain client ports (see the
  487. section <a href="#sc_reconfig_clientport">Specifying the client port</a>). As explained earlier
  488. you may leave the configuration in a single file, as well as leave the
  489. clientPort/clientPortAddress statements (although if you specify client
  490. ports in the new format, these statements are now redundant).</p>
  491. </div>
  492. <a name="ch_reconfig_dyn"></a>
  493. <h2 class="h3">Dynamic Reconfiguration of the ZooKeeper Ensemble</h2>
  494. <div class="section">
  495. <p>The ZooKeeper Java and C API were extended with getConfig and reconfig
  496. commands that facilitate reconfiguration. Both commands have a synchronous
  497. (blocking) variant and an asynchronous one. We demonstrate these commands
  498. here using the Java CLI, but note that you can similarly use the C CLI or
  499. invoke the commands directly from a program just like any other ZooKeeper
  500. command.</p>
  501. <a name="sc_reconfig_retrieving"></a>
  502. <h3 class="h4">Retrieving the current dynamic configuration</h3>
  503. <p>The dynamic configuration is stored in a special znode
  504. ZooDefs.CONFIG_NODE = /zookeeper/config. The new
  505. <span class="codefrag command">config</span> CLI command reads this znode (currently it is
  506. simply a wrapper to <span class="codefrag command">get /zookeeper/config</span>). As with
  507. normal reads, to retrieve the latest committed value you should do a
  508. <span class="codefrag command">sync</span> first.</p>
  509. <pre class="code">[zk: 127.0.0.1:2791(CONNECTED) 3] config
  510. server.1=localhost:2780:2783:participant;localhost:2791
  511. server.2=localhost:2781:2784:participant;localhost:2792
  512. server.3=localhost:2782:2785:participant;localhost:2793
  513. <strong>version=400000003</strong>
  514. </pre>
  515. <p>Notice the last line of the output. This is the configuration
  516. version. The version equals to the zxid of the reconfiguration command
  517. which created this configuration. The version of the first established
  518. configuration equals to the zxid of the NEWLEADER message sent by the
  519. first successfully established leader. When a configuration is written
  520. to a dynamic configuration file, the version automatically becomes part
  521. of the filename and the static configuration file is updated with the
  522. path to the new dynamic configuration file. Configuration files
  523. corresponding to earlier versions are retained for backup
  524. purposes.</p>
  525. <p>During boot time the version (if it exists) is extracted from the
  526. filename. The version should never be altered manually by users or the
  527. system administrator. It is used by the system to know which
  528. configuration is most up-to-date. Manipulating it manually can result in
  529. data loss and inconsistency.</p>
  530. <p>Just like a <span class="codefrag command">get</span> command, the
  531. <span class="codefrag command">config</span> CLI command accepts the <span class="codefrag option">-w</span>
  532. flag for setting a watch on the znode, and <span class="codefrag option">-s</span> flag for
  533. displaying the Stats of the znode. It additionally accepts a new flag
  534. <span class="codefrag option">-c</span> which outputs only the version and the client
  535. connection string corresponding to the current configuration. For
  536. example, for the configuration above we would get:</p>
  537. <pre class="code">[zk: 127.0.0.1:2791(CONNECTED) 17] config -c
  538. 400000003 localhost:2791,localhost:2793,localhost:2792</pre>
  539. <p>Note that when using the API directly, this command is called
  540. <span class="codefrag command">getConfig</span>.</p>
  541. <p>As any read command it returns the configuration known to the
  542. follower to which your client is connected, which may be slightly
  543. out-of-date. One can use the <span class="codefrag command">sync</span> command for
  544. stronger guarantees. For example using the Java API:</p>
  545. <pre class="code">zk.sync(ZooDefs.CONFIG_NODE, void_callback, context);
  546. zk.getConfig(watcher, callback, context);</pre>
  547. <p>Note: in 3.5.0 it doesn't really matter which path is passed to the
  548. <span class="codefrag command">sync() </span> command as all the server's state is brought
  549. up to date with the leader (so one could use a different path instead of
  550. ZooDefs.CONFIG_NODE). However, this may change in the future.</p>
  551. <a name="sc_reconfig_modifying"></a>
  552. <h3 class="h4">Modifying the current dynamic configuration</h3>
  553. <p>Modifying the configuration is done through the
  554. <span class="codefrag command">reconfig</span> command. There are two modes of
  555. reconfiguration: incremental and non-incremental (bulk). The
  556. non-incremental simply specifies the new dynamic configuration of the
  557. system. The incremental specifies changes to the current configuration.
  558. The <span class="codefrag command">reconfig</span> command returns the new
  559. configuration.</p>
  560. <p>A few examples are in: <span class="codefrag filename">ReconfigTest.java</span>,
  561. <span class="codefrag filename">ReconfigRecoveryTest.java</span> and
  562. <span class="codefrag filename">TestReconfigServer.cc</span>.</p>
  563. <a name="sc_reconfig_general"></a>
  564. <h4>General</h4>
  565. <p>
  566. <strong>Removing servers:</strong> Any server can
  567. be removed, including the leader (although removing the leader will
  568. result in a short unavailability, see Figures 6 and 8 in the <a href="https://www.usenix.org/conference/usenixfederatedconferencesweek/dynamic-recon%EF%AC%81guration-primarybackup-clusters">paper</a>). The server will not be shut-down automatically.
  569. Instead, it becomes a "non-voting follower". This is somewhat similar
  570. to an observer in that its votes don't count towards the Quorum of
  571. votes necessary to commit operations. However, unlike a non-voting
  572. follower, an observer doesn't actually see any operation proposals and
  573. does not ACK them. Thus a non-voting follower has a more significant
  574. negative effect on system throughput compared to an observer.
  575. Non-voting follower mode should only be used as a temporary mode,
  576. before shutting the server down, or adding it as a follower or as an
  577. observer to the ensemble. We do not shut the server down automatically
  578. for two main reasons. The first reason is that we do not want all the
  579. clients connected to this server to be immediately disconnected,
  580. causing a flood of connection requests to other servers. Instead, it
  581. is better if each client decides when to migrate independently. The
  582. second reason is that removing a server may sometimes (rarely) be
  583. necessary in order to change it from "observer" to "participant" (this
  584. is explained in the section <a href="#sc_reconfig_additional">Additional comments</a>).</p>
  585. <p>Note that the new configuration should have some minimal number of
  586. participants in order to be considered legal. If the proposed change
  587. would leave the cluster with less than 2 participants and standalone
  588. mode is enabled (standaloneEnabled=true, see the section <a href="#sc_reconfig_standaloneEnabled">The standaloneEnabled flag</a>), the reconfig will not be
  589. processed (BadArgumentsException). If standalone mode is disabled
  590. (standaloneEnabled=false) then its legal to remain with 1 or more
  591. participants.</p>
  592. <p>
  593. <strong>Adding servers:</strong> Before a
  594. reconfiguration is invoked, the administrator must make sure that a
  595. quorum (majority) of participants from the new configuration are
  596. already connected and synced with the current leader. To achieve this
  597. we need to connect a new joining server to the leader before it is
  598. officially part of the ensemble. This is done by starting the joining
  599. server using an initial list of servers which is technically not a
  600. legal configuration of the system but (a) contains the joiner, and (b)
  601. gives sufficient information to the joiner in order for it to find and
  602. connect to the current leader. We list a few different options of
  603. doing this safely.</p>
  604. <ol>
  605. <li>
  606. <p>Initial configuration of joiners is comprised of servers in
  607. the last committed configuration and one or more joiners, where
  608. <strong>joiners are listed as observers.</strong>
  609. For example, if servers D and E are added at the same time to (A,
  610. B, C) and server C is being removed, the initial configuration of
  611. D could be (A, B, C, D) or (A, B, C, D, E), where D and E are
  612. listed as observers. Similarly, the configuration of E could be
  613. (A, B, C, E) or (A, B, C, D, E), where D and E are listed as
  614. observers. <strong>Note that listing the joiners as
  615. observers will not actually make them observers - it will only
  616. prevent them from accidentally forming a quorum with other
  617. joiners.</strong> Instead, they will contact the servers in the
  618. current configuration and adopt the last committed configuration
  619. (A, B, C), where the joiners are absent. Configuration files of
  620. joiners are backed up and replaced automatically as this happens.
  621. After connecting to the current leader, joiners become non-voting
  622. followers until the system is reconfigured and they are added to
  623. the ensemble (as participant or observer, as appropriate).</p>
  624. </li>
  625. <li>
  626. <p>Initial configuration of each joiner is comprised of servers
  627. in the last committed configuration + <strong>the
  628. joiner itself, listed as a participant.</strong> For example, to
  629. add a new server D to a configuration consisting of servers (A, B,
  630. C), the administrator can start D using an initial configuration
  631. file consisting of servers (A, B, C, D). If both D and E are added
  632. at the same time to (A, B, C), the initial configuration of D
  633. could be (A, B, C, D) and the configuration of E could be (A, B,
  634. C, E). Similarly, if D is added and C is removed at the same time,
  635. the initial configuration of D could be (A, B, C, D). Never list
  636. more than one joiner as participant in the initial configuration
  637. (see warning below).</p>
  638. </li>
  639. <li>
  640. <p>Whether listing the joiner as an observer or as participant,
  641. it is also fine not to list all the current configuration servers,
  642. as long as the current leader is in the list. For example, when
  643. adding D we could start D with a configuration file consisting of
  644. just (A, D) if A is the current leader. however this is more
  645. fragile since if A fails before D officially joins the ensemble, D
  646. doesn&rsquo;t know anyone else and therefore the administrator will have
  647. to intervene and restart D with another server list.</p>
  648. </li>
  649. </ol>
  650. <div class="note">
  651. <div class="label">Warning</div>
  652. <div class="content">
  653. <title>Warning</title>
  654. <p>Never specify more than one joining server in the same initial
  655. configuration as participants. Currently, the joining servers don&rsquo;t
  656. know that they are joining an existing ensemble; if multiple joiners
  657. are listed as participants they may form an independent quorum
  658. creating a split-brain situation such as processing operations
  659. independently from your main ensemble. It is OK to list multiple
  660. joiners as observers in an initial config.</p>
  661. </div>
  662. </div>
  663. <p>Finally, note that once connected to the leader, a joiner adopts
  664. the last committed configuration, in which it is absent (the initial
  665. config of the joiner is backed up before being rewritten). If the
  666. joiner restarts in this state, it will not be able to boot since it is
  667. absent from its configuration file. In order to start it you&rsquo;ll once
  668. again have to specify an initial configuration.</p>
  669. <p>
  670. <strong>Modifying server parameters:</strong> One
  671. can modify any of the ports of a server, or its role
  672. (participant/observer) by adding it to the ensemble with different
  673. parameters. This works in both the incremental and the bulk
  674. reconfiguration modes. It is not necessary to remove the server and
  675. then add it back; just specify the new parameters as if the server is
  676. not yet in the system. The server will detect the configuration change
  677. and perform the necessary adjustments. See an example in the section
  678. <a href="#sc_reconfig_incremental">Incremental mode</a> and an exception to this
  679. rule in the section <a href="#sc_reconfig_additional">Additional comments</a>.</p>
  680. <p>It is also possible to change the Quorum System used by the
  681. ensemble (for example, change the Majority Quorum System to a
  682. Hierarchical Quorum System on the fly). This, however, is only allowed
  683. using the bulk (non-incremental) reconfiguration mode. In general,
  684. incremental reconfiguration only works with the Majority Quorum
  685. System. Bulk reconfiguration works with both Hierarchical and Majority
  686. Quorum Systems.</p>
  687. <p>
  688. <strong>Performance Impact:</strong> There is
  689. practically no performance impact when removing a follower, since it
  690. is not being automatically shut down (the effect of removal is that
  691. the server's votes are no longer being counted). When adding a server,
  692. there is no leader change and no noticeable performance disruption.
  693. For details and graphs please see Figures 6, 7 and 8 in the <a href="https://www.usenix.org/conference/usenixfederatedconferencesweek/dynamic-recon%EF%AC%81guration-primarybackup-clusters">paper</a>.</p>
  694. <p>The most significant disruption will happen when a leader change
  695. is caused, in one of the following cases:</p>
  696. <ol>
  697. <li>
  698. <p>Leader is removed from the ensemble.</p>
  699. </li>
  700. <li>
  701. <p>Leader's role is changed from participant to observer.</p>
  702. </li>
  703. <li>
  704. <p>The port used by the leader to send transactions to others
  705. (quorum port) is modified.</p>
  706. </li>
  707. </ol>
  708. <p>In these cases we perform a leader hand-off where the old leader
  709. nominates a new leader. The resulting unavailability is usually
  710. shorter than when a leader crashes since detecting leader failure is
  711. unnecessary and electing a new leader can usually be avoided during a
  712. hand-off (see Figures 6 and 8 in the <a href="https://www.usenix.org/conference/usenixfederatedconferencesweek/dynamic-recon%EF%AC%81guration-primarybackup-clusters">paper</a>).</p>
  713. <p>When the client port of a server is modified, it does not drop
  714. existing client connections. New connections to the server will have
  715. to use the new client port.</p>
  716. <p>
  717. <strong>Progress guarantees:</strong> Up to the
  718. invocation of the reconfig operation, a quorum of the old
  719. configuration is required to be available and connected for ZooKeeper
  720. to be able to make progress. Once reconfig is invoked, a quorum of
  721. both the old and of the new configurations must be available. The
  722. final transition happens once (a) the new configuration is activated,
  723. and (b) all operations scheduled before the new configuration is
  724. activated by the leader are committed. Once (a) and (b) happen, only a
  725. quorum of the new configuration is required. Note, however, that
  726. neither (a) nor (b) are visible to a client. Specifically, when a
  727. reconfiguration operation commits, it only means that an activation
  728. message was sent out by the leader. It does not necessarily mean that
  729. a quorum of the new configuration got this message (which is required
  730. in order to activate it) or that (b) has happened. If one wants to
  731. make sure that both (a) and (b) has already occurred (for example, in
  732. order to know that it is safe to shut down old servers that were
  733. removed), one can simply invoke an update
  734. (<span class="codefrag command">set-data</span>, or some other quorum operation, but not
  735. a <span class="codefrag command">sync</span>) and wait for it to commit. An alternative
  736. way to achieve this was to introduce another round to the
  737. reconfiguration protocol (which, for simplicity and compatibility with
  738. Zab, we decided to avoid).</p>
  739. <a name="sc_reconfig_incremental"></a>
  740. <h4>Incremental mode</h4>
  741. <p>The incremental mode allows adding and removing servers to the
  742. current configuration. Multiple changes are allowed. For
  743. example:</p>
  744. <p>
  745. <span class="codefrag userinput">&gt; reconfig -remove 3 -add
  746. server.5=125.23.63.23:1234:1235;1236</span>
  747. </p>
  748. <p>Both the add and the remove options get a list of comma separated
  749. arguments (no spaces):</p>
  750. <p>
  751. <span class="codefrag userinput">&gt; reconfig -remove 3,4 -add
  752. server.5=localhost:2111:2112;2113,6=localhost:2114:2115:observer;2116</span>
  753. </p>
  754. <p>The format of the server statement is exactly the same as
  755. described in the section <a href="#sc_reconfig_clientport">Specifying the client port</a> and
  756. includes the client port. Notice that here instead of "server.5=" you
  757. can just say "5=". In the example above, if server 5 is already in the
  758. system, but has different ports or is not an observer, it is updated
  759. and once the configuration commits becomes an observer and starts
  760. using these new ports. This is an easy way to turn participants into
  761. observers and vise versa or change any of their ports, without
  762. rebooting the server.</p>
  763. <p>ZooKeeper supports two types of Quorum Systems &ndash; the simple
  764. Majority system (where the leader commits operations after receiving
  765. ACKs from a majority of voters) and a more complex Hierarchical
  766. system, where votes of different servers have different weights and
  767. servers are divided into voting groups. Currently, incremental
  768. reconfiguration is allowed only if the last proposed configuration
  769. known to the leader uses a Majority Quorum System
  770. (BadArgumentsException is thrown otherwise).</p>
  771. <p>Incremental mode - examples using the Java API:</p>
  772. <pre class="code">List&lt;String&gt; leavingServers = new ArrayList&lt;String&gt;();
  773. leavingServers.add("1");
  774. leavingServers.add("2");
  775. byte[] config = zk.reconfig(null, leavingServers, null, -1, new Stat());</pre>
  776. <pre class="code">List&lt;String&gt; leavingServers = new ArrayList&lt;String&gt;();
  777. List&lt;String&gt; joiningServers = new ArrayList&lt;String&gt;();
  778. leavingServers.add("1");
  779. joiningServers.add("server.4=localhost:1234:1235;1236");
  780. byte[] config = zk.reconfig(joiningServers, leavingServers, null, -1, new Stat());
  781. String configStr = new String(config);
  782. System.out.println(configStr);</pre>
  783. <p>There is also an asynchronous API, and an API accepting comma
  784. separated Strings instead of List&lt;String&gt;. See
  785. src/java/main/org/apache/zookeeper/ZooKeeper.java.</p>
  786. <a name="sc_reconfig_nonincremental"></a>
  787. <h4>Non-incremental mode</h4>
  788. <p>The second mode of reconfiguration is non-incremental, whereby a
  789. client gives a complete specification of the new dynamic system
  790. configuration. The new configuration can either be given in place or
  791. read from a file:</p>
  792. <p>
  793. <span class="codefrag userinput">&gt; reconfig -file newconfig.cfg
  794. </span>//newconfig.cfg is a dynamic config file, see <a href="#sc_reconfig_file">Dynamic configuration file</a>
  795. </p>
  796. <p>
  797. <span class="codefrag userinput">&gt; reconfig -members
  798. server.1=125.23.63.23:2780:2783:participant;2791,server.2=125.23.63.24:2781:2784:participant;2792,server.3=125.23.63.25:2782:2785:participant;2793</span>
  799. </p>
  800. <p>The new configuration may use a different Quorum System. For
  801. example, you may specify a Hierarchical Quorum System even if the
  802. current ensemble uses a Majority Quorum System.</p>
  803. <p>Bulk mode - example using the Java API:</p>
  804. <pre class="code">ArrayList&lt;String&gt; newMembers = new ArrayList&lt;String&gt;();
  805. newMembers.add("server.1=1111:1234:1235;1236");
  806. newMembers.add("server.2=1112:1237:1238;1239");
  807. newMembers.add("server.3=1114:1240:1241:observer;1242");
  808. byte[] config = zk.reconfig(null, null, newMembers, -1, new Stat());
  809. String configStr = new String(config);
  810. System.out.println(configStr);</pre>
  811. <p>There is also an asynchronous API, and an API accepting comma
  812. separated String containing the new members instead of
  813. List&lt;String&gt;. See
  814. src/java/main/org/apache/zookeeper/ZooKeeper.java.</p>
  815. <a name="sc_reconfig_conditional"></a>
  816. <h4>Conditional reconfig</h4>
  817. <p>Sometimes (especially in non-incremental mode) a new proposed
  818. configuration depends on what the client "believes" to be the current
  819. configuration, and should be applied only to that configuration.
  820. Specifically, the <span class="codefrag command">reconfig</span> succeeds only if the
  821. last configuration at the leader has the specified version.</p>
  822. <p>
  823. <span class="codefrag userinput">&gt; reconfig -file &lt;filename&gt; -v &lt;version&gt;</span>
  824. </p>
  825. <p>In the previously listed Java examples, instead of -1 one could
  826. specify a configuration version to condition the
  827. reconfiguration.</p>
  828. <a name="sc_reconfig_errors"></a>
  829. <h4>Error conditions</h4>
  830. <p>In addition to normal ZooKeeper error conditions, a
  831. reconfiguration may fail for the following reasons:</p>
  832. <ol>
  833. <li>
  834. <p>another reconfig is currently in progress
  835. (ReconfigInProgress)</p>
  836. </li>
  837. <li>
  838. <p>the proposed change would leave the cluster with less than 2
  839. participants, in case standalone mode is enabled, or, if
  840. standalone mode is disabled then its legal to remain with 1 or
  841. more participants (BadArgumentsException)</p>
  842. </li>
  843. <li>
  844. <p>no quorum of the new configuration was connected and
  845. up-to-date with the leader when the reconfiguration processing
  846. began (NewConfigNoQuorum)</p>
  847. </li>
  848. <li>
  849. <p>
  850. <span class="codefrag userinput">-v x</span> was specified, but the version
  851. <span class="codefrag userinput">y</span> of the latest configuration is not
  852. <span class="codefrag userinput">x</span> (BadVersionException)</p>
  853. </li>
  854. <li>
  855. <p>an incremental reconfiguration was requested but the last
  856. configuration at the leader uses a Quorum System which is
  857. different from the Majority system (BadArgumentsException)</p>
  858. </li>
  859. <li>
  860. <p>syntax error (BadArgumentsException)</p>
  861. </li>
  862. <li>
  863. <p>I/O exception when reading the configuration from a file
  864. (BadArgumentsException)</p>
  865. </li>
  866. </ol>
  867. <p>Most of these are illustrated by test-cases in
  868. <span class="codefrag filename">ReconfigFailureCases.java</span>.</p>
  869. <a name="sc_reconfig_additional"></a>
  870. <h4>Additional comments</h4>
  871. <p>
  872. <strong>Liveness:</strong> To better understand
  873. the difference between incremental and non-incremental
  874. reconfiguration, suppose that client C1 adds server D to the system
  875. while a different client C2 adds server E. With the non-incremental
  876. mode, each client would first invoke <span class="codefrag command">config</span> to find
  877. out the current configuration, and then locally create a new list of
  878. servers by adding its own suggested server. The new configuration can
  879. then be submitted using the non-incremental
  880. <span class="codefrag command">reconfig</span> command. After both reconfigurations
  881. complete, only one of E or D will be added (not both), depending on
  882. which client's request arrives second to the leader, overwriting the
  883. previous configuration. The other client can repeat the process until
  884. its change takes effect. This method guarantees system-wide progress
  885. (i.e., for one of the clients), but does not ensure that every client
  886. succeeds. To have more control C2 may request to only execute the
  887. reconfiguration in case the version of the current configuration
  888. hasn't changed, as explained in the section <a href="#sc_reconfig_conditional">Conditional reconfig</a>. In this way it may avoid blindly
  889. overwriting the configuration of C1 if C1's configuration reached the
  890. leader first.</p>
  891. <p>With incremental reconfiguration, both changes will take effect as
  892. they are simply applied by the leader one after the other to the
  893. current configuration, whatever that is (assuming that the second
  894. reconfig request reaches the leader after it sends a commit message
  895. for the first reconfig request -- currently the leader will refuse to
  896. propose a reconfiguration if another one is already pending). Since
  897. both clients are guaranteed to make progress, this method guarantees
  898. stronger liveness. In practice, multiple concurrent reconfigurations
  899. are probably rare. Non-incremental reconfiguration is currently the
  900. only way to dynamically change the Quorum System. Incremental
  901. configuration is currently only allowed with the Majority Quorum
  902. System.</p>
  903. <p>
  904. <strong>Changing an observer into a
  905. follower:</strong> Clearly, changing a server that participates in
  906. voting into an observer may fail if error (2) occurs, i.e., if fewer
  907. than the minimal allowed number of participants would remain. However,
  908. converting an observer into a participant may sometimes fail for a
  909. more subtle reason: Suppose, for example, that the current
  910. configuration is (A, B, C, D), where A is the leader, B and C are
  911. followers and D is an observer. In addition, suppose that B has
  912. crashed. If a reconfiguration is submitted where D is said to become a
  913. follower, it will fail with error (3) since in this configuration, a
  914. majority of voters in the new configuration (any 3 voters), must be
  915. connected and up-to-date with the leader. An observer cannot
  916. acknowledge the history prefix sent during reconfiguration, and
  917. therefore it does not count towards these 3 required servers and the
  918. reconfiguration will be aborted. In case this happens, a client can
  919. achieve the same task by two reconfig commands: first invoke a
  920. reconfig to remove D from the configuration and then invoke a second
  921. command to add it back as a participant (follower). During the
  922. intermediate state D is a non-voting follower and can ACK the state
  923. transfer performed during the second reconfig comand.</p>
  924. </div>
  925. <a name="ch_reconfig_rebalancing"></a>
  926. <h2 class="h3">Rebalancing Client Connections</h2>
  927. <div class="section">
  928. <p>When a ZooKeeper cluster is started, if each client is given the same
  929. connection string (list of servers), the client will randomly choose a
  930. server in the list to connect to, which makes the expected number of
  931. client connections per server the same for each of the servers. We
  932. implemented a method that preserves this property when the set of servers
  933. changes through reconfiguration. See Sections 4 and 5.1 in the <a href="https://www.usenix.org/conference/usenixfederatedconferencesweek/dynamic-recon%EF%AC%81guration-primarybackup-clusters">paper</a>.</p>
  934. <p>In order for the method to work, all clients must subscribe to
  935. configuration changes (by setting a watch on /zookeeper/config either
  936. directly or through the <span class="codefrag command">getConfig</span> API command). When
  937. the watch is triggered, the client should read the new configuration by
  938. invoking <span class="codefrag command">sync</span> and <span class="codefrag command">getConfig</span> and if
  939. the configuration is indeed new invoke the
  940. <span class="codefrag command">updateServerList</span> API command. To avoid mass client
  941. migration at the same time, it is better to have each client sleep a
  942. random short period of time before invoking
  943. <span class="codefrag command">updateServerList</span>.</p>
  944. <p>A few examples can be found in:
  945. <span class="codefrag filename">StaticHostProviderTest.java</span> and
  946. <span class="codefrag filename">TestReconfig.cc</span>
  947. </p>
  948. <p>Example (this is not a recipe, but a simplified example just to
  949. explain the general idea):</p>
  950. <pre class="code">
  951. public void process(WatchedEvent event) {
  952. synchronized (this) {
  953. if (event.getType() == EventType.None) {
  954. connected = (event.getState() == KeeperState.SyncConnected);
  955. notifyAll();
  956. } else if (event.getPath()!=null &amp;&amp; event.getPath().equals(ZooDefs.CONFIG_NODE)) {
  957. // in prod code never block the event thread!
  958. zk.sync(ZooDefs.CONFIG_NODE, this, null);
  959. zk.getConfig(this, this, null);
  960. }
  961. }
  962. }
  963. public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
  964. if (path!=null &amp;&amp; path.equals(ZooDefs.CONFIG_NODE)) {
  965. String config[] = ConfigUtils.getClientConfigStr(new String(data)).split(" "); // similar to config -c
  966. long version = Long.parseLong(config[0], 16);
  967. if (this.configVersion == null){
  968. this.configVersion = version;
  969. } else if (version &gt; this.configVersion) {
  970. hostList = config[1];
  971. try {
  972. // the following command is not blocking but may cause the client to close the socket and
  973. // migrate to a different server. In practice its better to wait a short period of time, chosen
  974. // randomly, so that different clients migrate at different times
  975. zk.updateServerList(hostList);
  976. } catch (IOException e) {
  977. System.err.println("Error updating server list");
  978. e.printStackTrace();
  979. }
  980. this.configVersion = version;
  981. } } }</pre>
  982. </div>
  983. <p align="right">
  984. <font size="-2"></font>
  985. </p>
  986. </div>
  987. <!--+
  988. |end content
  989. +-->
  990. <div class="clearboth">&nbsp;</div>
  991. </div>
  992. <div id="footer">
  993. <!--+
  994. |start bottomstrip
  995. +-->
  996. <div class="lastmodified">
  997. <script type="text/javascript"><!--
  998. document.write("Last Published: " + document.lastModified);
  999. // --></script>
  1000. </div>
  1001. <div class="copyright">
  1002. Copyright &copy;
  1003. 2008-2013 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  1004. </div>
  1005. <!--+
  1006. |end bottomstrip
  1007. +-->
  1008. </div>
  1009. </body>
  1010. </html>