27

Raft Read Index 的实现

 4 years ago
source link: http://www.ideawu.net/blog/archives/1121.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

首先, 获取"集群"的 ReadIndex. 注意, 不是某个节点上面的某个状态, 而集群的多个节点共同确定的一个变量. 这个变量如何获取, 下面说明.

func GetClusterReadIndex(){
    foreach(peers as peer){
        i = peer.rpc_GetLastIndex();
        ret = max(ret, i);
        if(recv_majority){
            break;
        }
    }
    return ret;
}

获取至少半数以上成员的 LastIndex, 也就是每个节点持久化的最新一条日志的 index. 一条日志只要在一个节点上被持久化, 那么这条日志要么被集群 commit, 要么被覆盖之后再 commit, 没有其它的选项.

由此可见, ReadIndex 不一定已经被 commit, 但一定会被 commit, 最终也将会 apply. 所以, 拿到集群的 ReadIndex 之后, 我们只需要等, 等它被任意一个节点 apply 即可.

ReadIndex = cluster.GetReadIndex();
while(1){
    foreach(peers as peer){
        if(ReadIndex >= peer.LastApplied()){
            return peer.GetValue();
        }
    }
}

不断地重复轮询全部节点, 如果谁 apply 了, 就以谁的值为准. 还有一种方案, 那就是等本地的状态机 apply, 这样避免网络传输.

ReadIndex = cluster.GetReadIndex();
while(1){
    if(ReadIndex >= self.LastApplied()){
        return self.GetValue();
    }
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK