REPMGR_METHOD.C, __repmgr_start_int ()
Initial 2 elect threads.
Repmgr_elect.c, __repmgr_init_election ()
__repmgr_elect_thread ()
__repmgr_elect_main ()
lease, preferred Master mode,
Rep_elect.c, __repmgr_elect ()
__rep_elect_init ()
Lockout,
if (Rep->egen! = egen)//Then out
Tiebreaker
/* Use the last commit record as the LSN of the vote
__rep_write_egen
__rep_tally//Tally our own vote
__rep_cmp_vote//record ourselves in advance as winner
__rep_send_vote ()//-send vote1, our own vote, rep_vote1
Phase1, wait ...
if (rep->sites >= rep->nvotes) {//satisfy enter PHASE2, not satisfied on exit
Rep->sites-sites heard from.
Rep->nvotes-number of votes needed.
Send vote2/or we ourselves are winner the case and cast ourselves a vote
Did I win?
REP_RECORD.C, __rep_process_message_int ()
Case REP_VOTE1:
RET = __rep_vote1 (env, RP, REC, Eid);
Break
Case REP_VOTE2:
RET = __REP_VOTE2 (env, REC, Eid);
__rep_vote1 ()
We ourselves are master, send Rep_newmaster, exit
If you receive a previous Egen vote, send rep_alive
If you receive a later Egen vote, terminate the current vote, update Egen
* Ignore Vote1 ' s if we ' re in phase 2.
__rep_tally-recorded as the new vote site, rep->sites++
__rep_cmp_vote//Compare this vote1 with our existing winner
If you have got all the site vote1, enter Phase2
-We are winner, claim; otherwise vote2 others
If necessary (full election, first time to get site vote1), resend our vote1 to this site
__rep_vote2 ()
/*
* Record this vote. In a VOTE2, the only valid entry
* In the vote information is the election generation.
*
* There is several things which can go wrong that we
* Need to account for:
* 1. If we receive a latent VOTE2 from an earlier election,
* We want to ignore it.
* 2. If we receive a VOTE2 from a site from which we never
* Received a VOTE1, we want to record it, because we simply
* May is processing messages out of the order or its vote1 got lost,
* But this site got all the votes it needed to send it.
* 3. If we have received a duplicate VOTE2 from this election
* From the same site we want to ignore it.
* 4. If this is from the current election and someone is
* Really voting for us, then we-finally get to record it.
*/
Rep_tally-If the new site is issued by Vote2, rep->votes++
#define I_have_won (rep, winner) \
(Rep)->votes >= (Rep)->nvotes && winner = = (Rep)->eid)
Rep->sites-sites heard from.
Rep->nvotes-number of votes needed.
Rep->votes-number of votes for this site.
Rep->nsites-number of sites in group.
/*
* We need to check sites = = Nsites, not more than half
* Like we did in __rep_elect and the VOTE2 code. The
* Reason is, we want to process all the incoming votes
* And not short-circuit once we reach more than half. The
* Real winner ' s vote is in the last half.
*/
#defineIS_PHASE1_DONE (rep) \
(Rep)->sites >= (Rep)->nsites && (REP)->winner! = db_eid_invalid)
u_int32_t egen;/ * Replication election generation. * *
Rep_newmaster-I'm the new master.
Rep_master_req-who is MASTER?
REP_UTIL.C, __rep_new_master () sync with new master
/*
* Election Gen file name
* The file contains an Egen number for a election this client have not
* Participated in. i.e. it is the number of a future election. We
* Create it when we create the rep region, if it doesn ' t already exist
* and initialize Egen to 1. If It does exist, we read it when we create
* The rep region. We write it immediately before sending our VOTE1 in
* an election. That is, if a client has ever sent a vote for any
* Election, the file is already going to being updated to reflect a future
* Election, should it crash.
*/
#defineREP_EGENNAME "__db.rep.egen"
typedef struct {
u_int32_tegen;/* voter ' s election generation. */
inteid;/* voter ' s ID. */
} rep_vtally;
Rep_elect.c, __rep_tally ()
* Ignore votes from earlier elections (i.e. we ' ve heard
* From this site in this election, but its vote from an
* Earlier election got delayed and we received it now).
* However, if we happened to hear from an earlier vote
* and we recorded it and we ' re now Hearin
__rep_cmp_vote ()
/* Make ourselves the winner to start. */
Rep->winner records the known winner
__rep_elect_done ()
-Clear elect flag, clear Rep->votes,.. rep->egen++
Berkeley DB replica mechanism-election algorithm