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