-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc.go
230 lines (191 loc) · 5.28 KB
/
rpc.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package jsn_raft
import (
"errors"
"sync"
"time"
jsn_rpc "github.com/jsn4ke/jsn_net/rpc"
"github.com/jsn4ke/jsn_raft/v2/pb"
)
const (
AppendEntries = "RaftNew.AppendEntries"
Vote = "RaftNew.Vote"
)
func (r *Raft) registerRpc() {
r.rpcServer.RegisterExecutor(new(pb.VoteRequest), r.rpcVote)
r.rpcServer.RegisterExecutor(new(pb.AppendEntriesRequest), r.rpcAppendEntries)
}
func (r *Raft) rpcVote(in jsn_rpc.RpcUnit) (jsn_rpc.RpcUnit, error) {
args, _ := in.(*pb.VoteRequest)
if nil == args {
return nil, errors.New("invalid request")
}
now := time.Now()
defer func() {
diff := time.Since(now)
if diff*5 > r.rpcTimeout() {
r.logger.Info("[%v] rpcVote from %s cost %v",
r.who, args.CandidateId, diff)
}
}()
reply := new(pb.VoteResponse)
err := r.Vote(args, reply)
return reply, err
}
func (r *Raft) rpcAppendEntries(in jsn_rpc.RpcUnit) (jsn_rpc.RpcUnit, error) {
args, _ := in.(*pb.AppendEntriesRequest)
if nil == args {
return nil, errors.New("invalid request")
}
now := time.Now()
defer func() {
diff := time.Since(now)
if diff*5 > r.rpcTimeout() {
r.logger.Info("[%v] rpcAppendEntries from %s cost %v",
r.who, args.LeaderId, diff)
}
}()
reply := new(pb.AppendEntriesResponse)
err := r.AppendEntries(args, reply)
return reply, err
}
func (r *Raft) Vote(args *pb.VoteRequest, reply *pb.VoteResponse) error {
wrap := &rpcWrap{
In: args,
Resp: reply,
Done: make(chan error, 1),
}
r.rpcChannel <- wrap
err := <-wrap.Done
return err
}
func (r *Raft) AppendEntries(args *pb.AppendEntriesRequest, reply *pb.AppendEntriesResponse) error {
wrap := &rpcWrap{
In: args,
Resp: reply,
Done: make(chan error, 1),
}
r.rpcChannel <- wrap
err := <-wrap.Done
return err
}
type rpcWrap struct {
In any
Resp any
Done chan error
}
func (r *Raft) handlerRpc(wrap *rpcWrap) {
var err error
switch tp := wrap.In.(type) {
case *pb.VoteRequest:
err = r.vote(tp, wrap.Resp.(*pb.VoteResponse))
case *pb.AppendEntriesRequest:
err = r.appendEntries(tp, wrap.Resp.(*pb.AppendEntriesResponse))
}
wrap.Done <- err
}
var (
clis = sync.Map{}
)
func (r *Raft) rpcCall(who string, args jsn_rpc.RpcUnit, reply jsn_rpc.RpcUnit, done <-chan struct{}, timeout time.Duration) error {
err := r.rpcClients[who].Call(args, reply, done, timeout)
// err := rpcCall(who, args, reply, done, timeout)
return err
}
func (r *Raft) vote(args *pb.VoteRequest, reply *pb.VoteResponse) error {
reply.CurrentTerm = r.getCurrentTerm()
reply.VoteGranted = false
if args.Term < r.getCurrentTerm() {
return nil
} else if args.Term > r.getCurrentTerm() {
r.setCurrentTerm(args.Term)
r.setServerState(follower)
reply.CurrentTerm = r.getCurrentTerm()
}
// 一个服务器最多会对一个任期号投出一张选票,按照先来先服务的原则
if r.voteForTerm == args.Term {
if 0 != len(r.voteFor) && r.voteFor != string(args.CandidateId) {
return nil
}
} else if r.voteForTerm > args.Term {
return nil
}
lastLogIndex, lastLogTerm := r.lastLog()
if lastLogTerm > args.LastLogTerm {
r.logger.Info("[%v] vote candiate last log term %v mine %v",
r.who, args.LastLogTerm, lastLogTerm)
return nil
}
if lastLogTerm == args.LastLogTerm && lastLogIndex > args.LastLogIndex {
r.logger.Info("[%v] vote candiate last log index,term [%v,%v] mine [%v,%v]",
r.who, args.LastLogTerm, args.LastLogIndex, lastLogIndex, lastLogTerm)
return nil
}
reply.VoteGranted = true
r.setVoteFor(string(args.CandidateId))
r.logger.Debug("[%v] vote for %v term %v",
r.who, string(args.CandidateId), r.getCurrentTerm())
r.lastFollowerCheck = time.Now()
return nil
}
func (r *Raft) appendEntries(args *pb.AppendEntriesRequest, reply *pb.AppendEntriesResponse) error {
reply.CurrentTerm = r.getCurrentTerm()
reply.Success = false
defer func() {
if !args.Heartbeat {
r.logger.Debug("[%v] append entries from %s commit %v res %v",
r.who, args.LeaderId, r.getCommitIndex(), reply.Success)
}
}()
if args.Term < r.getCurrentTerm() {
return nil
} else if args.Term > r.getCurrentTerm() {
r.setServerState(follower)
r.setCurrentTerm(args.Term)
reply.CurrentTerm = r.getCurrentTerm()
}
// 如果这个领导人的任期号(包含在此次的 RPC中)不小于候选人当前的任期号,那么候选人会承认领导人合法并回到跟随者状态
if candidate == r.getServerState() {
r.setServerState(follower)
}
// if args.Heartbeat {
// r.lastFollowerCheck = time.Now()
// return nil
// }
if 0 != args.PrevLogIndex {
if !r.matchLog(args.PrevLogIndex, args.PrevLogTerm) {
return nil
}
}
lastLogIndex, _ := r.lastLog()
var entries []*pb.JsnLog
for i, entry := range args.Entries {
if entry.Index > lastLogIndex {
entries = args.Entries[i:]
break
}
jlog := r.getLog(entry.Index)
if nil == jlog {
return nil
}
if jlog.Term != entry.Term {
r.logTruncate(entry.Index)
entries = args.Entries[i:]
break
}
}
if 0 != len(entries) {
r.appendLogs(entries)
}
// 强保证一致性,至少learder落了,这里才能落下
if args.LeaderCommitIndex > r.getCommitIndex() {
lastLogIndex, _ = r.lastLog()
if lastLogIndex < args.LeaderCommitIndex {
r.setCommitIndex(lastLogIndex)
} else {
r.setCommitIndex(args.LeaderCommitIndex)
}
}
reply.Success = true
r.lastFollowerCheck = time.Now()
return nil
}