1<!--$Id: rep_elect.so,v 1.28 2007/12/03 21:01:23 sue Exp $-->
2<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
3<!--See the file LICENSE for redistribution information.-->
4<html>
5<head>
6<title>Berkeley DB: DB_ENV-&gt;rep_elect</title>
7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
9</head>
10<body bgcolor=white>
11<table width="100%"><tr valign=top>
12<td>
13<b>DB_ENV-&gt;rep_elect</b>
14</td>
15<td align=right>
16<a href="../api_c/api_core.html"><img src="../images/api.gif" alt="API"></a>
17<a href="../ref/toc.html"><img src="../images/ref.gif" alt="Ref"></a></td>
18</tr></table>
19<hr size=1 noshade>
20<tt>
21<b><pre>
22#include &lt;db.h&gt;
23<p>
24int
25DB_ENV-&gt;rep_elect(DB_ENV *env,
26    u_int32_t nsites, u_int32_t nvotes, u_int32_t flags);
27</pre></b>
28<hr size=1 noshade>
29<b>Description: DB_ENV-&gt;rep_elect</b>
30<p>The DB_ENV-&gt;rep_elect method holds an election for the master of a replication
31group.</p>
32<p>The DB_ENV-&gt;rep_elect method is not called by most replication
33applications.  It should only be called by applications implementing
34their own network transport layer, explicitly holding replication group
35elections and handling replication messages outside of the replication
36manager framework.</p>
37<p>If the election is successful, Berkeley DB will notify the application of
38the results of the election by means of either the
39<a href="../api_c/env_event_notify.html#DB_EVENT_REP_ELECTED">DB_EVENT_REP_ELECTED</a> or <a href="../api_c/env_event_notify.html#DB_EVENT_REP_NEWMASTER">DB_EVENT_REP_NEWMASTER</a> events
40(see <a href="../api_c/env_event_notify.html">DB_ENV-&gt;set_event_notify</a> method for more information).  The
41application is responsible for adjusting its relationship to the other
42database environments in the replication group, including directing
43all database updates to the newly selected master, in accordance with
44the results of the election.</p>
45<p>The thread of control that calls the DB_ENV-&gt;rep_elect method must not be the
46thread of control that processes incoming messages; processing the
47incoming messages is necessary to successfully complete an election.</p>
48<b>Parameters</b> <br>
49 <b>nsites</b><ul compact><li>The <b>nsites</b> parameter specifies the number of replication sites
50expected to participate in the election.  Once the current site has
51election information from that many sites, it will short-circuit the
52election and immediately cast its vote for a new master. The
53<b>nsites</b> parameter must be no less than
54<b>nvotes</b>, or 0 if the election should use the value previously set
55using the <a href="../api_c/rep_nsites.html">DB_ENV-&gt;rep_set_nsites</a> method.  If an application is using master
56leases, then the value <b>must</b> be 0 and the value from
57<a href="../api_c/rep_nsites.html">DB_ENV-&gt;rep_set_nsites</a> method must be used.</ul>
58 <b>nvotes</b><ul compact><li>The <b>nvotes</b> parameter specifies the minimum number of replication
59sites from which the current site must have election information, before
60the current site will cast a vote for a new master.  The <b>nvotes</b>
61parameter must be no greater than <b>nsites</b>,
62or 0 if the election should use the value ((<b>nsites</b> / 2) + 1) as
63the <b>nvotes</b> argument.</ul>
64 <b>flags</b><ul compact><li>The <b>flags</b> parameter is currently unused, and must be set to 0.</ul>
65<br>
66<p>Elections are done in two parts: first, replication sites collect
67information from the other replication sites they know about, and
68second, replication sites cast their votes for a new master.  The second
69phase is triggered by one of two things: either the replication site
70gets election information from <b>nsites</b> sites, or the election
71<b>timeout</b> expires.  Once the second phase is triggered, the
72replication site will cast a vote for the new master of its choice if,
73and only if, the site has election information from at least
74<b>nvotes</b> sites.  If a site receives <b>nvotes</b> votes for it
75to become the new master, then it will become the new master.</p>
76<p>We recommend <b>nvotes</b> be set to at least:</p>
77<blockquote><pre>(sites participating in the election / 2) + 1</pre></blockquote>
78<p>to ensure there are never more than two masters active at the same time
79even in the case of a network partition.  When a network partitions, the
80side of the partition with more than half the environments will elect a
81new master and continue, while the environments communicating with fewer
82than half of the environments will fail to find a new master, as no site
83can get <b>nvotes</b> votes.</p>
84<p>We recommend <b>nsites</b> be set to:</p>
85<blockquote><pre>number of sites in the replication group - 1</pre></blockquote>
86<p>when choosing a new master after a current master fails.  This allows
87the group to reach a consensus without having to wait for the timeout
88to expire.</p>
89<p>When choosing a master from among a group of client
90sites all restarting at the same time, it makes more sense to set
91<b>nsites</b> to the total number of sites in the group, since there is no
92known missing site.  Furthermore, in order to ensure the best choice
93from among sites that may take longer to boot than the local site,
94setting <b>nvotes</b> also to this same total number of sites will
95guarantee that every site in the group is considered.  Alternatively,
96using the special timeout for full elections allows full participation
97on restart but allows election of a master if one site does not
98reboot and rejoin the group in a reasonable amount of time. (See the
99<a href="../ref/rep/elect.html">Elections</a>
100section in the Berkeley DB Reference Guide for more information.)</p>
101<p>Setting <b>nsites</b> to lower values can increase the speed of an
102election, but can also result in election failure, and is usually not
103recommended.</p>
104<a name="2"><!--meow--></a>
105<br><b>Errors</b>
106<p>The DB_ENV-&gt;rep_elect method
107may fail and return one of the following non-zero errors:</p>
108<br>
109<b>DB_REP_UNAVAIL</b><ul compact><li>The replication group was unable to elect a master, or was unable to
110complete the election in the election timeout period (see
111<a href="../api_c/rep_timeout.html">DB_ENV-&gt;rep_set_timeout</a> method for more information).</ul>
112<br>
113<hr size=1 noshade>
114<br><b>Class</b>
115<a href="../api_c/env_class.html">DB_ENV</a>
116<br><b>See Also</b>
117<a href="../api_c/rep_list.html">Replication and Related Methods</a>
118</tt>
119<table width="100%"><tr><td><br></td><td align=right>
120<a href="../api_c/api_core.html"><img src="../images/api.gif" alt="API"></a><a href="../ref/toc.html"><img src="../images/ref.gif" alt="Ref"></a>
121</td></tr></table>
122<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
123</body>
124</html>
125