open-courses
  • 公开课笔记
  • CMU 15-445/645 Database Systems
    • Relational Data Model
    • Advanced SQL
    • Database Storage
    • Buffer Pools
    • Hash Tables
    • Tree Indexes
    • Index Concurrency Control
    • Query Processing
    • Sorting&Aggregations
    • Join Algorithms
    • Query Optimization
    • Parallel Execution
    • Embedded Database Logic
    • Concurrency Control Theory
    • Two Phase Locking
    • Timestamp Ordering Concurrency Control
    • Multi-Version Concurrency Control
    • Logging Schemes
    • Database Recovery
    • Introduction to Distributed Databases
    • Distributed OLTP Databases
    • Distributed OLAP Databases
  • UCB - CS162
    • OS intro
    • Introduction to the Process
    • Processes, Fork, I/O, Files
    • I/O Continued, Sockets, Networking
    • Concurrency: Processes & Threads
    • Cooperating Threads, Synchronization
    • Semaphores, Condition Variables, Readers/Writers
    • Scheduling
    • Resource Contention & Deadlock
    • Address Translation, Caching
    • File System (18,19,20)
    • Distributed Systems, Networking, TCP/IP, RPC (21,22)
    • Distributed Storage, Key-Value Stores, Security (23)
    • Security & Cloud Computing (24)
    • Topic: Ensuring Data Reaches Disk
  • MIT - 6.006
    • Sequence and Set Interface
    • Data Structure for Dynamic Sequence Interface
    • Computation Complexity
    • Algorithms and Computation
    • Structure Of Computation
    • Graph & Search
    • Tree & Search
    • Weighted Shortest Paths
    • String Matching, Karp-Rabin
    • Priority Queue Interface & Implementation
    • Dictionary Problem & Implementation
    • Sorting
    • Dynamic Programming
    • Backtracking
    • Self-Balancing Tree
  • MIT - 6.824
    • 2PC & 3PC
    • Introduction and MapReduce
    • RPC and Threads
    • Primary/Backup Replication
    • Lab: Primary/Backup Key/Value Service
    • Google File System (GFS)
    • Raft
    • Lab: Raft - Leader Election
    • Lab: Raft - Log Replication
  • Stanford-CS107
    • 原始数据类型及相互转化
    • 指鹿为马
    • 泛型函数
    • 泛型栈
    • 运行时内存结构
    • 从 C 到汇编
    • 函数的活动记录
    • C 与 C++ 代码生成
    • 编译的预处理过程
    • 编译的链接过程
    • 函数的活动记录续、并发
    • 从顺序到并发和并行
    • 信号量与多线程 1
    • 信号量与多线程 2
    • 复杂多线程问题
    • 函数式编程 - Scheme 1
    • 函数式编程 - Scheme 2
    • 函数式编程 - Scheme 3
    • 函数式编程 - Scheme 4
    • 函数式编程 - Scheme 5
    • Python 基础
  • MIT - 6.001 - SICP
    • 什么是程序
    • 程序抽象
    • 替代模型
    • 时间/空间复杂度
    • 数据抽象
    • 高阶函数
    • Symbol
    • 数据驱动编程与防御式编程
    • 数据抽象中的效率与可读性
    • 数据修改
    • 环境模型
    • 面向对象-消息传递
    • 面向对象 - Scheme 实现
    • 构建 Scheme 解释器
    • Eval-Apply Loop
    • Normal Order (Lazy) Evaluation
    • 通用机
    • 寄存器机器
    • 子程序、栈与递归
    • 在寄存器机器中执行
    • 内存管理
  • MIT - 6.046
    • Randomized Algorithms
    • Skip Lists
  • System Design
    • Twitter
    • Cache Consistency & Coherence
  • DDIA 笔记
    • Replication
    • Transactions
    • The Trouble with Distributed Systems
    • Consistency & Consensus
  • Papers We Love
    • Consistent Hashing and Random Trees (1997)
    • Dynamic Hash Tables (1988)
    • LFU Implementation With O(1) Complexity (2010)
    • Time, Clocks, and the Ordering of Events in a Distributed System (1978)
    • Dapper, a Large-Scale Distributed Systems Tracing Infrastructure (2010)
    • Gorilla: A Fast, Scalable, In-Memory Time Series Database (2015)
  • Release It 笔记
    • Anti-patterns & Patterns in Microservice Architecture
  • Database Design
    • Log Structured Merge (LSM) Tree & Usages in KV Stores
    • Prometheus
Powered by GitBook
On this page
  • 说在前面
  • Election Timeout Daemon
  • Send RequestVote RPCs
  • RequestVoteArgs
  • Retry
  • Receive RequestVote RPCs
  • Majority Votes Checking Daemon
  1. MIT - 6.824

Lab: Raft - Leader Election

以下为本人在完成 Lab 2 过程中的总结,不涉及代码细节

PreviousRaftNextLab: Raft - Log Replication

Last updated 6 years ago

说在前面

在实验的过程中,一定要反复阅读:

  • Raft Paper Figure 2

  • 6.824 Course Notes

Leader Election 主要由以下几个部分组成:

  • Election Timeout Daemon

  • Send RequestVote RPC

  • Receive RequestVote RPC

  • Marjority Votes Checking Daemon

Election Timeout Daemon

每个 raft server 都需要一个 daemon 来检查选举超时的情况,当没有收到任何有效消息时,则应该发起一次新的选举;当收到有效消息时,则重置选举超时的计算。这里,有效的消息包括:

  • Candidate 发来有效的 RequestVote RPC

  • Leader 发来有效的 AppendEntries RPC

  • Candidate 收到有效的 RequestVote RPC Reply

  • Leader 收到有效的 AppendEntries RPC Reply

因此我们可以在 Make 中创建这样一个 daemon:

go func() {
    for {
        et := generateET()
        select {
        case <-rf.rCh:
        case <-time.After(et): {
            go rf.startsNewElection()
        }
    }
}

通过 rCh channel 来接受有效信息的信号。rf.startsNewElection 严格按照 Figure 2 描述的实现即可:

  • Increment currentTerm:每个 term 只能有一次选举

  • Vote for self:每个 server 在单次选举中只能投一票,参选者默认投给自己

  • Reset election timer

  • Send RequestVote RPCs to all other servers

Send RequestVote RPCs

RequestVoteArgs

RequestVoteArgs 需包含以下字段:

type RequestVoteArgs struct {
    Term         int
    CandidateID  int
    LastLogIndex int
    LastLogTerm  int
}

注意这里的 LastLog 指的是 raft server 的 log entries 的最后一个,目的在于让 Follower 判断 Candidate 是否至少和它一样与时俱进。注意不要和 AppendEntriesArgs 中的 PrevLog 混淆,后者指的是 Leader 上次发给 Follower 的最后一个 logEntry,因此 Leader 发给每个 Follower 的 AppendEntriesArgs 都可能不同。

Retry

由于可能出现网络分区,RequestVote 请求需要超时重发。

Receive RequestVote RPCs

严格按照 Figure2 描述的步骤实现即可

  1. 检查过时请求:term < currentTerm

  2. 当 server 还未投出自己宝贵一票,或者已经投了 Candidate 一票时,检查 Candidate 的 log entries 是否与自己与时俱进,如果是,则投它一票。当 server 已经投给 Candidate 一票时,server 的请求响应可能在网络中丢失,因此当再次收到相同请求时,有必要再次同意投票。

Majority Votes Checking Daemon

这里把检查是否得到多数票的逻辑放到一个 Daemon 里,主要目的在于方便组织代码,将其放在 Send RequestVote RPCs 之后也没有问题:

go func() {
    rf.mu.Lock()
    if rf.isCandidate() && rf.hasMajorityVotes() {
        rf.becomesLeader()
    }
    rf.mu.Unlock()
    time.Sleep(CheckMajorityVoteTimeout)
}

Students' Guide to Raft
Raft Q&A