| 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>DbEnv::rep_elect()</title> |
| 7 | <link rel="stylesheet" href="apiReference.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 C++ API Reference" /> |
| 10 | <link rel="up" href="rep.html" title="Chapter 11. Replication Methods" /> |
| 11 | <link rel="prev" href="dbsite_set_config.html" title="DbSite::set_config()" /> |
| 12 | <link rel="next" href="repget_clockskew.html" title="DbEnv::rep_get_clockskew()" /> |
| 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">DbEnv::rep_elect()</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="dbsite_set_config.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 11. |
| 26 | Replication Methods |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="repget_clockskew.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="repelect"></a>DbEnv::rep_elect()</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <pre class="programlisting">#include <db_cxx.h> |
| 42 | |
| 43 | int |
| 44 | DbEnv::rep_elect(u_int32_t nsites, u_int32_t nvotes, u_int32_t flags);</pre> |
| 45 | <p> |
| 46 | The <code class="methodname">DbEnv::rep_elect()</code> method holds an election for the master of a |
| 47 | replication group. |
| 48 | </p> |
| 49 | <p> |
| 50 | The <code class="methodname">DbEnv::rep_elect()</code> method is not called by most replication |
| 51 | applications. It should only be called by Base API applications implementing |
| 52 | their own network transport layer, explicitly holding replication |
| 53 | group elections and handling replication messages outside of the |
| 54 | Replication Manager framework. |
| 55 | </p> |
| 56 | <p> |
| 57 | If the election is successful, Berkeley DB will notify the application |
| 58 | of the results of the election by means of either the |
| 59 | <a class="link" href="envevent_notify.html#event_notify_DB_EVENT_REP_ELECTED">DB_EVENT_REP_ELECTED</a> |
| 60 | or <a class="link" href="envevent_notify.html#event_notify_DB_EVENT_REP_NEWMASTER">DB_EVENT_REP_NEWMASTER</a> |
| 61 | events (see <a class="xref" href="envevent_notify.html" title="DbEnv::set_event_notify()">DbEnv::set_event_notify()</a> |
| 62 | method for more information). The application is responsible for |
| 63 | adjusting its relationship to the other database environments in the |
| 64 | replication group, including directing all database updates to the |
| 65 | newly selected master, in accordance with the results of the election. |
| 66 | </p> |
| 67 | <p> |
| 68 | The thread of control that calls the <code class="methodname">DbEnv::rep_elect()</code> method must |
| 69 | not be the thread of control that processes incoming messages; |
| 70 | processing the incoming messages is necessary to successfully complete |
| 71 | an election. |
| 72 | </p> |
| 73 | <p> |
| 74 | Before calling this method do the following: |
| 75 | </p> |
| 76 | <div class="itemizedlist"> |
| 77 | <ul type="disc"> |
| 78 | <li> |
| 79 | <p> |
| 80 | open the database environment by calling the |
| 81 | <a class="xref" href="envopen.html" title="DbEnv::open()">DbEnv::open()</a> method. |
| 82 | </p> |
| 83 | </li> |
| 84 | <li> |
| 85 | <p> |
| 86 | configure the database environment to send replication messages by calling the |
| 87 | <a class="xref" href="reptransport.html" title="DbEnv::rep_set_transport()">DbEnv::rep_set_transport()</a> method. |
| 88 | </p> |
| 89 | </li> |
| 90 | <li> |
| 91 | <p> |
| 92 | configure the database environment as a client or a master |
| 93 | by calling the <a class="xref" href="repstart.html" title="DbEnv::rep_start()">DbEnv::rep_start()</a> method. |
| 94 | </p> |
| 95 | </li> |
| 96 | </ul> |
| 97 | </div> |
| 98 | <div class="sect2" lang="en" xml:lang="en"> |
| 99 | <div class="titlepage"> |
| 100 | <div> |
| 101 | <div> |
| 102 | <h3 class="title"><a id="idp9406320"></a>How Elections are Held</h3> |
| 103 | </div> |
| 104 | </div> |
| 105 | </div> |
| 106 | <p> |
| 107 | Elections are done in two parts: first, replication sites collect information from the other replication |
| 108 | sites they know about, and second, replication sites cast their votes for a new master. The second phase |
| 109 | is triggered by one of two things: either the replication site gets election information from <span class="bold"><strong>nsites</strong></span> sites, or the election timeout |
| 110 | expires. Once the second phase is triggered, the replication site will cast a vote for the new master of |
| 111 | its choice if, and only if, the site has election information from at least <span class="bold"><strong>nvotes</strong></span> sites. If a site receives <span class="bold"><strong>nvotes</strong></span> |
| 112 | votes for it to become the new master, then it will become the new master. |
| 113 | </p> |
| 114 | <p> |
| 115 | We recommend <span class="bold"><strong>nvotes</strong></span> be set to at least: |
| 116 | </p> |
| 117 | <pre class="programlisting"> (sites participating in the election / 2) + 1</pre> |
| 118 | <p> |
| 119 | to ensure there are never more than two masters active at the same time even in the case of a |
| 120 | network partition. When a network partitions, the side of the partition with more than half the |
| 121 | environments will elect a new master and continue, while the environments communicating with fewer |
| 122 | than half of the environments will fail to find a new master, as no site can get |
| 123 | <span class="bold"><strong>nvotes</strong></span> votes. |
| 124 | </p> |
| 125 | <p> |
| 126 | We recommend <span class="bold"><strong>nsites</strong></span> be set to: |
| 127 | </p> |
| 128 | <pre class="programlisting"> number of sites in the replication group - 1</pre> |
| 129 | <p> |
| 130 | when choosing a new master after a current master fails. This allows the group to reach a consensus |
| 131 | without having to wait for the timeout to expire. |
| 132 | </p> |
| 133 | <p> |
| 134 | When choosing a master from among a group of client sites all restarting at the same time, it makes |
| 135 | more sense to set <span class="bold"><strong>nsites</strong></span> to the total number of sites in the group, |
| 136 | since there is no known missing site. Furthermore, in order to ensure the best choice from among |
| 137 | sites that may take longer to boot than the local site, setting <span class="bold"><strong>nvotes</strong></span> also to this same total number of sites will guarantee that |
| 138 | every site in the group is considered. Alternatively, using the special timeout for full elections |
| 139 | allows full participation on restart but allows election of a master if one site does not reboot and |
| 140 | rejoin the group in a reasonable amount of time. (See the |
| 141 | <a href="../../programmer_reference/rep_elect.html" class="olink">Elections</a> section in the |
| 142 | <em class="citetitle">Berkeley DB Programmer's Reference Guide</em> for more information.) |
| 143 | </p> |
| 144 | <p> |
| 145 | Setting <span class="bold"><strong>nsites</strong></span> to lower values can increase the speed of an |
| 146 | election, but can also result in election failure, and is usually not recommended. |
| 147 | </p> |
| 148 | </div> |
| 149 | <div class="sect2" lang="en" xml:lang="en"> |
| 150 | <div class="titlepage"> |
| 151 | <div> |
| 152 | <div> |
| 153 | <h3 class="title"><a id="idp9425040"></a>Parameters</h3> |
| 154 | </div> |
| 155 | </div> |
| 156 | </div> |
| 157 | <div class="sect3" lang="en" xml:lang="en"> |
| 158 | <div class="titlepage"> |
| 159 | <div> |
| 160 | <div> |
| 161 | <h4 class="title"><a id="idp9428808"></a>nsites</h4> |
| 162 | </div> |
| 163 | </div> |
| 164 | </div> |
| 165 | <p> |
| 166 | The <span class="bold"><strong>nsites</strong></span> parameter specifies the |
| 167 | number of replication sites expected to participate in the election. |
| 168 | Once the current site has election information from that many sites, |
| 169 | it will short-circuit the election and immediately cast its vote for a |
| 170 | new master. The <span class="bold"><strong>nsites</strong></span> parameter must |
| 171 | be no less than <span class="bold"><strong>nvotes</strong></span>, or 0 if the |
| 172 | election should use the value previously set using the |
| 173 | <a class="xref" href="repnsites.html" title="DbEnv::rep_set_nsites()">DbEnv::rep_set_nsites()</a> |
| 174 | method. If an application is using master leases, then the value |
| 175 | <span class="bold"><strong>must</strong></span> be 0 and the value from |
| 176 | <a class="xref" href="repnsites.html" title="DbEnv::rep_set_nsites()">DbEnv::rep_set_nsites()</a> |
| 177 | method must be used. |
| 178 | </p> |
| 179 | </div> |
| 180 | <div class="sect3" lang="en" xml:lang="en"> |
| 181 | <div class="titlepage"> |
| 182 | <div> |
| 183 | <div> |
| 184 | <h4 class="title"><a id="idp9450312"></a>nvotes</h4> |
| 185 | </div> |
| 186 | </div> |
| 187 | </div> |
| 188 | <p> |
| 189 | The <span class="bold"><strong>nvotes</strong></span> parameter specifies the |
| 190 | minimum number of replication sites from which the current site must |
| 191 | have election information, before the current site will cast a vote |
| 192 | for a new master. The <span class="bold"><strong>nvotes</strong></span> |
| 193 | parameter must be no greater than <span class="bold"><strong>nsites</strong></span>, or 0 if the election should use the |
| 194 | value ((<span class="bold"><strong>nsites</strong></span> / 2) + 1) as the |
| 195 | <span class="bold"><strong>nvotes</strong></span> argument. |
| 196 | </p> |
| 197 | </div> |
| 198 | <div class="sect3" lang="en" xml:lang="en"> |
| 199 | <div class="titlepage"> |
| 200 | <div> |
| 201 | <div> |
| 202 | <h4 class="title"><a id="idp9441920"></a>flags</h4> |
| 203 | </div> |
| 204 | </div> |
| 205 | </div> |
| 206 | <p> |
| 207 | The <span class="bold"><strong>flags</strong></span> parameter is currently |
| 208 | unused, and must be set to 0. |
| 209 | </p> |
| 210 | </div> |
| 211 | </div> |
| 212 | <div class="sect2" lang="en" xml:lang="en"> |
| 213 | <div class="titlepage"> |
| 214 | <div> |
| 215 | <div> |
| 216 | <h3 class="title"><a id="idp9429976"></a>Errors</h3> |
| 217 | </div> |
| 218 | </div> |
| 219 | </div> |
| 220 | <p> |
| 221 | The <code class="methodname">DbEnv::rep_elect()</code> <span> |
| 222 | |
| 223 | <span> |
| 224 | method may fail and throw a <a class="link" href="dbexception.html" title="Chapter 6. The DbException Class">DbException</a> |
| 225 | exception, encapsulating one of the following non-zero errors, or return one |
| 226 | of the following non-zero errors: |
| 227 | </span> |
| 228 | </span> |
| 229 | </p> |
| 230 | <div class="sect3" lang="en" xml:lang="en"> |
| 231 | <div class="titlepage"> |
| 232 | <div> |
| 233 | <div> |
| 234 | <h4 class="title"><a id="idp9400200"></a>DB_REP_UNAVAIL</h4> |
| 235 | </div> |
| 236 | </div> |
| 237 | </div> |
| 238 | <p> |
| 239 | The replication group was unable to elect a master, or was unable to |
| 240 | complete the election in the election timeout period (see |
| 241 | <a class="xref" href="repset_timeout.html" title="DbEnv::rep_set_timeout()">DbEnv::rep_set_timeout()</a> |
| 242 | method for more information). |
| 243 | </p> |
| 244 | </div> |
| 245 | <div class="sect3" lang="en" xml:lang="en"> |
| 246 | <div class="titlepage"> |
| 247 | <div> |
| 248 | <div> |
| 249 | <h4 class="title"><a id="idp9459424"></a>EINVAL</h4> |
| 250 | </div> |
| 251 | </div> |
| 252 | </div> |
| 253 | <p> |
| 254 | If the database environment was not already configured to |
| 255 | communicate with a replication group by a call to |
| 256 | <a class="xref" href="reptransport.html" title="DbEnv::rep_set_transport()">DbEnv::rep_set_transport()</a>; |
| 257 | if the database environment was not already opened; if this |
| 258 | method is called from a Replication Manager application; or if |
| 259 | an invalid flag value or parameter was specified. |
| 260 | </p> |
| 261 | </div> |
| 262 | </div> |
| 263 | <div class="sect2" lang="en" xml:lang="en"> |
| 264 | <div class="titlepage"> |
| 265 | <div> |
| 266 | <div> |
| 267 | <h3 class="title"><a id="idp9438720"></a>Class</h3> |
| 268 | </div> |
| 269 | </div> |
| 270 | </div> |
| 271 | <p> |
| 272 | <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> |
| 273 | </p> |
| 274 | </div> |
| 275 | <div class="sect2" lang="en" xml:lang="en"> |
| 276 | <div class="titlepage"> |
| 277 | <div> |
| 278 | <div> |
| 279 | <h3 class="title"><a id="idp9406968"></a>See Also</h3> |
| 280 | </div> |
| 281 | </div> |
| 282 | </div> |
| 283 | <p> |
| 284 | <a class="xref" href="rep.html#replist" title="Replication and Related Methods">Replication and Related Methods</a> |
| 285 | </p> |
| 286 | </div> |
| 287 | </div> |
| 288 | <div class="navfooter"> |
| 289 | <hr /> |
| 290 | <table width="100%" summary="Navigation footer"> |
| 291 | <tr> |
| 292 | <td width="40%" align="left"><a accesskey="p" href="dbsite_set_config.html">Prev</a> </td> |
| 293 | <td width="20%" align="center"> |
| 294 | <a accesskey="u" href="rep.html">Up</a> |
| 295 | </td> |
| 296 | <td width="40%" align="right"> <a accesskey="n" href="repget_clockskew.html">Next</a></td> |
| 297 | </tr> |
| 298 | <tr> |
| 299 | <td width="40%" align="left" valign="top">DbSite::set_config() </td> |
| 300 | <td width="20%" align="center"> |
| 301 | <a accesskey="h" href="index.html">Home</a> |
| 302 | </td> |
| 303 | <td width="40%" align="right" valign="top"> DbEnv::rep_get_clockskew()</td> |
| 304 | </tr> |
| 305 | </table> |
| 306 | </div> |
| 307 | </body> |
| 308 | </html> |