repmgr_method.c, __repmgr_start_int()

初始2个elect线程.

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 in the vote

__rep_write_egen

__rep_tally // tally our own vote

__rep_cmp_vote // 把我们自己预先记录为winner

__rep_send_vote() // -send vote1, our own vote, REP_VOTE1

phase1, wait...

if (rep->sites >= rep->nvotes) { // 满足进入phase2, 不满足就退出了

rep->sites - sites heard from.

rep->nvotes - Number of votes needed.

send vote2/ 或我们自己 是winner的情况, 投自己一票

我赢了么?

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()

我们自己是master, send REP_NEWMASTER, 退出

若收到以前egen的vote, send REP_ALIVE

若收到以后egen的vote, 终止当前vote, 更新egen

* Ignore vote1's if we're in phase 2.

__rep_tally - 记录下来, 如是新的vote site, rep->sites++

__rep_cmp_vote // 比较此vote1和我们已有的winner

如果已经得到所有site的vote1, 进入phase2

- 我们是winner, claim; 否则vote2 别人

如需要(full election?, 第一次拿到site的vote1), resend our vote1 到这个site

__rep_vote2()

/*
* Record this vote. In a VOTE2, the only valid entry
* in the vote information is the election generation.
*
* There are 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 be processing messages out of order or its vote1 got lost,
* but that 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 - 若 新的site发出的 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 do in __rep_elect and the VOTE2 code. The
* reason is that we want to process all the incoming votes
* and not short-circuit once we reach more than half. The
* real winner's vote may be in the last half.
*/
#define IS_PHASE1_DONE(rep) \
((rep)->sites >= (rep)->nsites && (rep)->winner != DB_EID_INVALID)

u_int32_t egen; /* Replication election generation. */

REP_NEWMASTER -  我是新的master

REP_MASTER_REQ - 谁是master?

rep_util.c, __rep_new_master() 与新master同步

/*
* Election gen file name
* The file contains an egen number for an election this client has 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 way, if a client has ever sent a vote for any
* election, the file is already going to be updated to reflect a future
* election, should it crash.
*/
#define REP_EGENNAME "__db.rep.egen"

typedef struct {
u_int32_t egen; /* Voter's election generation. */
int eid; /* 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 记录下已知的winner

__rep_elect_done()

- 清elect flag, 清rep->votes,.. rep->egen++

berkeley db replica机制 - election algorithm的更多相关文章

  1. berkeley db replica机制 - 消息处理

    repmgr_method.c, __repmgr_start_int()repmgr_method.c, __repmgr_start_msg_threads()repmgr_msg.c, __re ...

  2. berkeley db replica机制 - 主从同步

    repmgr/repmgr_net.c, __repmgr_send(): 做send_broadcast, 然后根据policy 对DB_REP_PERMANENT的处理 __repmgr_send ...

  3. The Architecture of Open Source Applications: Berkeley DB

    最近研究内存关系数据库的设计与实现,下面一篇为berkeley db原始两位作为的Berkeley DB设计回忆录: Conway's Law states that a design reflect ...

  4. Berkeley DB

    最近用BDB写点东西,写了挺多个测试工程.列下表,也理清楚最近的思路 1.测试BDB程序,包括打开增加记录,查询记录,获取所有记录.将数据转存mysql 程序的不足,增加记录仅仅只有key和value ...

  5. Oracle Berkeley DB Java 版

    Oracle Berkeley DB Java 版是一个开源的.可嵌入的事务存储引擎,是完全用 Java 编写的.它充分利用 Java 环境来简化开发和部署.Oracle Berkeley DB Ja ...

  6. 新浪研发中心: Berkeley DB 使用经验总结

    http://blog.sina.com.cn/s/blog_502c8cc40100yqkj.html NoSQL是现在互联网Web2.0时代备受关注的技术之一,被用来存储大量的非关系型的数据.Be ...

  7. Berkeley DB基础教程

    一.Berkeley DB的介绍 (1)Berkeley DB是一个嵌入式数据库,它适合于管理海量的.简单的数据.如Google使用其来保存账户信息,Heritrix用其来保存froniter. (2 ...

  8. Berkeley DB 使用经验总结

    作者:陈磊 NoSQL是现在互联网Web2.0时代备受关注的技术之一,被用来存储大量的非关系型的数据.Berkeley DB作为一款优秀的Key/Value存储引擎自然也在讨论之列.最近使用BDB来发 ...

  9. Berkeley DB Java Edition 简介

    一.             简介        Berkeley DB Java Edition (JE)是一个完全用JAVA写的,它适合于管理海量的,简单的数据. l         能够高效率的 ...

随机推荐

  1. DATAGUARD 在线重建备库

    环境: OS: CentOS 6.5 X64 DB: oracle 10.2.0.5 故障:之前由于错误激活备库主写导致主备日志同步,重建备库 1.关闭备库,删除数据文件及控制文件,redo文件 rm ...

  2. RedHat5--yun源无法使用问题解决

    YUM是Redhat Linux在线安装更新及软件的工具,但是这是RHEL5的收费功能,如果没有购买Redhat的服务时不能使用RHEL5的更新源的,会提示注册. 由于CentOS是从Redhat演化 ...

  3. MySQL定时执行脚本(计划任务)命令实例

    在mysql中我们可以直接进行一些参数设置让它成定时为我们执行一些任务了,这个虽然可以使用windows或者linux中的计划任务实现,但是mysql本身也能完成 查看event是否开启 复制代码 代 ...

  4. WebStorage 和 Cookie的区别

    sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...

  5. 关于Log和adb知识

    1,打印日志:adb logcat -v time >log 2,清除以上日志:adb logcat -c 2,查看设备是否连接电脑:adb devices 3,登陆手机设备:adb shell ...

  6. pyhton函数——黑板客老师课程学习

    1.基本语法 语法: def func_name (arguments)  定义 statements return x,y x,y=func_name(para)   调用 作用域: 可以给内置的函 ...

  7. 用window.showModelDialog() 打开的页面的返回值

    有两个页面也个 Default1.aspx   另外一个是 Default2.aspx Default1.aspx 有个按钮是用来打开Default2.aspx页面的 按钮的js代码是 var win ...

  8. UI自动化测试的那些事

    互联网产品的迭代速度远高于传统软件,尤其是移动APP不但更新频繁,还需要在不同硬件.系统版本的环境下进行大量兼容测试,这就给传统测试方法和测试工具带来了巨大挑战.为满足产品敏捷开发.快速迭代的需求,自 ...

  9. 绕过/*,web.xml直接访问jsp

    web.xml中如果配置了/* 全匹配,那么不能用servet去响应页面返回了,因为全都被会/*拦截. <servlet> <servlet-name>validateAuth ...

  10. codeforces 732/D 二分

    给出考试时间和考试需要准备的时间,问最早考完所有科目的时间 二分答案 NlogN 二分抄神犇的写法 感觉挺舒服的嘻嘻嘻 #include<bits/stdc++.h> using name ...