| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>Elections</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="rep.html" title="Chapter 12. Berkeley DB Replication" /> |
| 11 | <link rel="prev" href="rep_mgr_ack.html" title="Choosing a Replication Manager Ack Policy" /> |
| 12 | <link rel="next" href="rep_mastersync.html" title="Synchronizing with a master" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Elections</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="rep_mgr_ack.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 12. |
| 26 | Berkeley DB Replication |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="rep_mastersync.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="rep_elect"></a>Elections</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>Replication Manager automatically conducts elections when necessary, |
| 42 | based on configuration information supplied to the |
| 43 | <a href="../api_reference/C/reppriority.html" class="olink">DB_ENV->rep_set_priority()</a> method, unless the application turns off automatic |
| 44 | elections using the <a href="../api_reference/C/repconfig.html" class="olink">DB_ENV->rep_set_config()</a> method.</p> |
| 45 | <p>It is the responsibility of a Base API application |
| 46 | to initiate elections if desired. It is never dangerous |
| 47 | to hold an election, as the Berkeley DB election process ensures there is |
| 48 | never more than a single master database environment. Clients should |
| 49 | initiate an election whenever they lose contact with the master |
| 50 | environment, whenever they see a return of <a href="../api_reference/C/repmessage.html#repmsg_DB_REP_HOLDELECTION" class="olink">DB_REP_HOLDELECTION</a> |
| 51 | from the <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a> method, or when, for whatever reason, they do |
| 52 | not know who the master is. It is not necessary for applications to |
| 53 | immediately hold elections when they start, as any existing master |
| 54 | will be discovered after calling <a href="../api_reference/C/repstart.html" class="olink">DB_ENV->rep_start()</a>. If no master has |
| 55 | been found after a short wait period, then the application should call |
| 56 | for an election.</p> |
| 57 | <p>For a client to win an election, the replication group must currently |
| 58 | have no master, and the client must have the most recent log records. |
| 59 | In the case of clients having equivalent log records, the priority of |
| 60 | the database environments participating in the election will determine |
| 61 | the winner. The application specifies the minimum number of replication |
| 62 | group members that must participate in an election for a winner to be |
| 63 | declared. We recommend at least ((N/2) + 1) members. If fewer than the |
| 64 | simple majority are specified, a warning will be given.</p> |
| 65 | <p>If an application's policy for what site should win an election can be |
| 66 | parameterized in terms of the database environment's information (that |
| 67 | is, the number of sites, available log records and a relative priority |
| 68 | are all that matter), then Berkeley DB can handle all elections transparently. |
| 69 | However, there are cases where the application has more complete |
| 70 | knowledge and needs to affect the outcome of elections. For example, |
| 71 | applications may choose to handle master selection, explicitly |
| 72 | designating master and client sites. Applications in these cases may |
| 73 | never need to call for an election. Alternatively, applications may |
| 74 | choose to use <a href="../api_reference/C/repelect.html" class="olink">DB_ENV->rep_elect()</a>'s arguments to force the correct outcome |
| 75 | to an election. That is, if an application has three sites, A, B, and |
| 76 | C, and after a failure of C determines that A must become the winner, |
| 77 | the application can guarantee an election's outcome by specifying |
| 78 | priorities appropriately after an election:</p> |
| 79 | <pre class="programlisting">on A: priority 100, nsites 2 |
| 80 | on B: priority 0, nsites 2</pre> |
| 81 | <p>It is dangerous to configure more than one master environment using the |
| 82 | <a href="../api_reference/C/repstart.html" class="olink">DB_ENV->rep_start()</a> method, and applications should be careful not to do so. |
| 83 | Applications should only configure themselves as the master environment |
| 84 | if they are the only possible master, or if they have won an election. |
| 85 | An application knows it has won an election when it receives the |
| 86 | <a href="../api_reference/C/envevent_notify.html#event_notify_DB_EVENT_REP_ELECTED" class="olink">DB_EVENT_REP_ELECTED</a> event.</p> |
| 87 | <p> |
| 88 | Normally, when a master failure is detected it is desired that an |
| 89 | election finish quickly so the application can continue to service |
| 90 | updates. Also, participating sites are already up and can participate. |
| 91 | However, in the case of restarting a whole group after an |
| 92 | administrative shutdown, it is possible that a slower booting site had |
| 93 | later logs than any other site. To cover that case, an application |
| 94 | would like to give the election more time to ensure all sites have a |
| 95 | chance to participate. Since it is intractable for a starting site to |
| 96 | determine which case the whole group is in, the use of a long timeout |
| 97 | gives all sites a reasonable chance to participate. If an application |
| 98 | wanting full participation sets the <a href="../api_reference/C/repelect.html" class="olink">DB_ENV->rep_elect()</a> method's |
| 99 | <span class="bold"><strong>nvotes</strong></span> argument to the number of sites |
| 100 | in the group and one site does not reboot, a master can never be elected |
| 101 | without manual intervention. |
| 102 | </p> |
| 103 | <p> |
| 104 | In those cases, the desired action at a group level is to hold |
| 105 | a full election if all sites crashed and a majority election if |
| 106 | a subset of sites crashed or rebooted. Since an individual site cannot know |
| 107 | which number of votes to require, a mechanism is available to |
| 108 | accomplish this using timeouts. By setting a long timeout (perhaps |
| 109 | on the order of minutes) using the <span class="bold"><strong>DB_REP_FULL_ELECTION_TIMEOUT</strong></span> |
| 110 | flag to the <a href="../api_reference/C/repset_timeout.html" class="olink">DB_ENV->rep_set_timeout()</a> method, an application can |
| 111 | allow Berkeley DB to elect a master even without full participation. |
| 112 | Sites may also want to set a normal election timeout for majority |
| 113 | based elections using the <span class="bold"><strong>DB_REP_ELECTION_TIMEOUT</strong></span> flag |
| 114 | to the <a href="../api_reference/C/repset_timeout.html" class="olink">DB_ENV->rep_set_timeout()</a> method.</p> |
| 115 | <p> |
| 116 | Consider 3 sites, A, B, and C where A is the master. In the |
| 117 | case where all three sites crash and all reboot, all sites |
| 118 | will set a timeout for a full election, say 10 minutes, but only |
| 119 | require a majority for <span class="bold"><strong>nvotes</strong></span> to the <a href="../api_reference/C/repelect.html" class="olink">DB_ENV->rep_elect()</a> method. |
| 120 | Once all three sites are booted the election will complete |
| 121 | immediately if they reboot within 10 minutes of each other. Consider |
| 122 | if all three sites crash and only two reboot. The two sites will |
| 123 | enter the election, but after the 10 minute timeout they will |
| 124 | elect with the majority of two sites. Using the full election |
| 125 | timeout sets a threshold for allowing a site to reboot and rejoin |
| 126 | the group.</p> |
| 127 | <p>To add a database environment to the replication group with the intent |
| 128 | of it becoming the master, first add it as a client. Since it may be |
| 129 | out-of-date with respect to the current master, allow it to update |
| 130 | itself from the current master. Then, shut the current master down. |
| 131 | Presumably, the added client will win the subsequent election. If the |
| 132 | client does not win the election, it is likely that it was not given |
| 133 | sufficient time to update itself with respect to the current master.</p> |
| 134 | <p>If a client is unable to find a master or win an election, it means that |
| 135 | the network has been partitioned and there are not enough environments |
| 136 | participating in the election for one of the participants to win. |
| 137 | In this case, the application should repeatedly call <a href="../api_reference/C/repstart.html" class="olink">DB_ENV->rep_start()</a> |
| 138 | and <a href="../api_reference/C/repelect.html" class="olink">DB_ENV->rep_elect()</a>, alternating between attempting to discover an |
| 139 | existing master, and holding an election to declare a new one. In |
| 140 | desperate circumstances, an application could simply declare itself the |
| 141 | master by calling <a href="../api_reference/C/repstart.html" class="olink">DB_ENV->rep_start()</a>, or by reducing the number of |
| 142 | participants required to win an election until the election is won. |
| 143 | Neither of these solutions is recommended: in the case of a network |
| 144 | partition, either of these choices can result in there being two masters |
| 145 | in one replication group, and the databases in the environment might |
| 146 | irretrievably diverge as they are modified in different ways by the |
| 147 | masters.</p> |
| 148 | <p>Note that this presents a special problem for a replication group |
| 149 | consisting of only two environments. If a master site fails, the |
| 150 | remaining client can never comprise a majority of sites in the group. |
| 151 | If the client application can reach a remote network site, or some other |
| 152 | external tie-breaker, it may be able to determine whether it is safe |
| 153 | to declare itself master. Otherwise it must choose between providing |
| 154 | availability of a writable master (at the risk of duplicate masters), |
| 155 | or strict protection against duplicate masters (but no master when a |
| 156 | failure occurs). Replication Manager offers this choice via the |
| 157 | <a href="../api_reference/C/repconfig.html" class="olink">DB_ENV->rep_set_config()</a> method <a href="../api_reference/C/repconfig.html#config_DB_REPMGR_CONF_2SITE_STRICT" class="olink">DB_REPMGR_CONF_2SITE_STRICT</a> flag. Base API |
| 158 | applications can accomplish this by judicious setting of the |
| 159 | <span class="bold"><strong>nvotes</strong></span> and |
| 160 | <span class="bold"><strong>nsites</strong></span> parameters to the |
| 161 | <a href="../api_reference/C/repelect.html" class="olink">DB_ENV->rep_elect()</a> method. </p> |
| 162 | <p>It is possible for a less-preferred database environment to win an |
| 163 | election if a number of systems crash at the same time. Because an |
| 164 | election winner is declared as soon as enough environments participate |
| 165 | in the election, the environment on a slow booting but well-connected |
| 166 | machine might lose to an environment on a badly connected but faster |
| 167 | booting machine. In the case of a number of environments crashing at |
| 168 | the same time (for example, a set of replicated servers in a single |
| 169 | machine room), applications should bring the database environments on |
| 170 | line as clients initially (which will allow them to process read queries |
| 171 | immediately), and then hold an election after sufficient time has passed |
| 172 | for the slower booting machines to catch up.</p> |
| 173 | <p>If, for any reason, a less-preferred database environment becomes the |
| 174 | master, it is possible to switch masters in a replicated environment. |
| 175 | For example, the preferred master crashes, and one of the replication |
| 176 | group clients becomes the group master. In order to restore the |
| 177 | preferred master to master status, take the following steps:</p> |
| 178 | <div class="orderedlist"> |
| 179 | <ol type="1"> |
| 180 | <li>The preferred master should reboot and re-join the replication group |
| 181 | as a client.</li> |
| 182 | <li>Once the preferred master has caught up with the replication group, the |
| 183 | application on the current master should complete all active transactions |
| 184 | and reconfigure itself as a client using the <a href="../api_reference/C/repstart.html" class="olink">DB_ENV->rep_start()</a> method.</li> |
| 185 | <li>Then, the current or preferred master should call for an election using |
| 186 | the <a href="../api_reference/C/repelect.html" class="olink">DB_ENV->rep_elect()</a> method.</li> |
| 187 | </ol> |
| 188 | </div> |
| 189 | </div> |
| 190 | <div class="navfooter"> |
| 191 | <hr /> |
| 192 | <table width="100%" summary="Navigation footer"> |
| 193 | <tr> |
| 194 | <td width="40%" align="left"><a accesskey="p" href="rep_mgr_ack.html">Prev</a> </td> |
| 195 | <td width="20%" align="center"> |
| 196 | <a accesskey="u" href="rep.html">Up</a> |
| 197 | </td> |
| 198 | <td width="40%" align="right"> <a accesskey="n" href="rep_mastersync.html">Next</a></td> |
| 199 | </tr> |
| 200 | <tr> |
| 201 | <td width="40%" align="left" valign="top">Choosing a Replication Manager Ack Policy </td> |
| 202 | <td width="20%" align="center"> |
| 203 | <a accesskey="h" href="index.html">Home</a> |
| 204 | </td> |
| 205 | <td width="40%" align="right" valign="top"> Synchronizing with a master</td> |
| 206 | </tr> |
| 207 | </table> |
| 208 | </div> |
| 209 | </body> |
| 210 | </html> |