-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft_membership.go
84 lines (77 loc) · 2.38 KB
/
raft_membership.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package raft
import (
"github.com/pingcap-incubator/tinykv/log"
pb "github.com/pingcap-incubator/tinykv/proto/pkg/eraftpb"
)
//The New configuration takes effect on each server as soon as it is added to the server's log
//只需要appendEntry,就已经生效.(通过pendingConfIndex)
//raft 协议作者 Diego 在其博士论文中已经详细介绍了Single Cluser MemberShip Change 机制,包括Security、Avaliable方面的详细说明, 并且作者也说明了在实际工程实现过程中更加推荐Single方式,
// 首先因为简单,
// 再则所有的集群变更方式都可以通过Single 一次一个节点的方式达到任何想要的Cluster 状态。
//func (r *Raft) updatePendingConfByAppendEntries(entries []*pb.Entry) {
// if r.PendingConfIndex > 0 {
// return
// }
// prevPos := 0
// for i := 0; i < len(entries); i++ {
// ent := entries[i]
// if ent.EntryType == pb.EntryType_EntryConfChange {
// if r.PendingConfIndex > 0 {
// prev := fromEntry(entries[prevPos])
// now := fromEntry(ent)
// log.Warnf(`multi confChange in raft system.prev(%v-->%d) execute and drop now(%v-->%d)`, prev.ChangeType, prev.NodeId, now.ChangeType, now.NodeId)
// } else {
// r.pendingConf = fromEntry(ent)
// if r.pendingConf == nil {
// panic(nil)
// }
// r.PendingConfIndex = ent.Index
// prevPos = i
// }
// }
// }
// //if r.PendingConfIndex == 0{
// // if r.pendingConf.ChangeType == pb.ConfChangeType_AddNode{
// // if
// // }
// //}
//}
//本节点是否还在raft系统中?
func (r *Raft) isInRaft(id uint64) bool {
_, ok := r.Prs[id]
return ok
}
func fromEntry(ent *pb.Entry) *pb.ConfChange {
if ent.GetEntryType() != pb.EntryType_EntryConfChange {
log.Warnf(`fromEntry was not ConfChange`)
return nil
}
var cc pb.ConfChange
err := cc.Unmarshal(ent.Data)
if err != nil {
log.Errorf(`fromEntry error:%s.`, err.Error())
return nil
}
return &cc
}
func (r *Raft) sendTimeoutNow(transfee uint64) {
pr := r.Prs[transfee]
var hb ReqHeartbeat
err := r.makeHeartbeat(pr, &hb)
if err != nil {
//
log.Warnf(`%s handleTransferLeader(%d) err:%s.sendAppend`, r.tag, transfee, err.Error())
r.sendAppend(transfee)
return
}
//send to `to`;
toMsg := hb.toPbMsg()
toMsg.MsgType = pb.MessageType_MsgTimeoutNow
r.sendPb(transfee, toMsg)
r.leadTransferee = None
}
func (r *Raft) ResetPrs() {
r.Prs = map[uint64]*Progress{
r.id: &Progress{0, 0},
}
}