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