Raft 实验记录

LAB2A

Bug

耗时最长的bug:

  • 产生原因
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    L:Leader F:Follower C:Candidate C:Crash
    s1(L) s2(F) s3(F) // initial
    s1(C) s2(L) s3(F) // s2 send HeartBeat to s1(GoRoutine x1) and s3(GoRoutine x2)
    // but s1 is crash, if we use Lock() and defer Unlock(), x1 will block x2 from Heartbeating until RPC faild(Takes Many times)
    s1(C) <-AppendEntries- s2(L)
    ...long time untile RPC return false
    s3(F) <-AppendEntries- s2(L) // at this moment, s3 has many election timeout
    [Server3:Term1] election timeout, Start an election
    [Server3:Term2] send requestVote to [Sever1]
    [Server3:Term2] send requestVote to [Sever2]
    ...
    [Server3:Term20] election timeout, Start an election
    [Server3:Term21] send requestVote to [Sever1]
    [Server3:Term21] send requestVote to [Sever2]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    func (rf *Raft) BroadcastHeartBeat() {
    BroadCastPrintf("[Server%v:Term%d] BroadCastHeartBeat", rf.me, rf.currentTerm)
    for i := range rf.peers {
    if i == rf.me {
    continue
    }
    go func(server int) {
    // Bug here
    // rf.mu.Lock()
    // defer rf.mu.Unlock()
    args := AppendEntriesArgs{
    Term: rf.currentTerm,
    LeaderId: rf.me,
    }
    var reply AppendEntriesReply
    EntryPrintf("[Server%v:Term%d] send AppendEntries RPC to [Server%d]", rf.me, rf.currentTerm, server)
    if rf.sendAppendEntries(server, &args, &reply) {
    // dosomething
    }
    }(i)
    }
    }