-
Notifications
You must be signed in to change notification settings - Fork 0
/
learner_sender.go
267 lines (214 loc) · 5.83 KB
/
learner_sender.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package gopaxos
import (
"math"
"time"
)
type learnerSender struct {
conf *config
learner *learner
paxosLog *paxosLog
slock *serialLock
isSending bool
absLastSendTime uint64
beginInstanceID uint64
sendToNodeID uint64
isConfirmed bool
ackInstanceID uint64
absLastAckTime uint64
ackLead int
isEnd bool
isStart bool
quit chan struct{}
}
func newLearnerSender(conf *config, learner *learner, paxosLog *paxosLog) *learnerSender {
ret := &learnerSender{
conf: conf,
learner: learner,
paxosLog: paxosLog,
ackLead: getInsideOptionsInstance().getLearnerSenderAckLead(),
slock: newSerialLock(),
}
ret.sendDone()
return ret
}
func (l *learnerSender) start() {
l.quit = make(chan struct{}, 1)
go l.run()
}
func (l *learnerSender) run() {
l.isStart = true
for {
l.waitToSend()
if l.isEnd {
lPLGHead(l.conf.groupIdx, "Learner.Sender [END]")
close(l.quit)
return
}
l.sendLearnedValue(l.beginInstanceID, l.sendToNodeID)
l.sendDone()
}
}
func (l *learnerSender) stop() {
if l.isStart {
l.isEnd = true
if l.quit != nil {
<-l.quit
l.quit = nil
}
}
}
func (l *learnerSender) refreshSending() {
l.absLastSendTime = getSteadyClockMS()
}
func (l *learnerSender) isIMSending() bool {
if !l.isSending {
return false
}
now := getSteadyClockMS()
var passTime uint64
if now > l.absLastSendTime {
passTime = now - l.absLastSendTime
}
if passTime >= uint64(getInsideOptionsInstance().getLearnerSenderPrepareTimeoutMs()) {
return false
}
return true
}
func (l *learnerSender) cutAckLead() {
receiveAckLead := getInsideOptionsInstance().getLearnerReceiverAckLead()
if l.ackLead-receiveAckLead > receiveAckLead {
l.ackLead = l.ackLead - receiveAckLead
}
}
func (l *learnerSender) checkAck(instanceID uint64) bool {
l.slock.lock()
defer l.slock.unlock()
if instanceID < l.ackInstanceID {
l.ackLead = getInsideOptionsInstance().getLearnerSenderAckLead()
lPLGImp(l.conf.groupIdx, "Already catch up, ack instanceid %d now send instanceid %d",
l.ackInstanceID, instanceID)
return false
}
for instanceID > l.ackInstanceID+uint64(l.ackLead) {
now := getSteadyClockMS()
var passTime uint64
if now > l.absLastAckTime {
passTime = now - l.absLastAckTime
}
if passTime >= uint64(getInsideOptionsInstance().getLearnerSenderAckTimeoutMs()) {
getBPInstance().SenderAckTimeout()
lPLGErr(l.conf.groupIdx, "Ack timeout, last acktime %d now send instanceid %d",
l.absLastAckTime, instanceID)
l.cutAckLead()
return false
}
getBPInstance().SenderAckDelay()
l.slock.waitTime(time.Millisecond * 20)
}
return true
}
func (l *learnerSender) prepare(beginInstanceID uint64, sendToNodeID uint64) bool {
l.slock.lock()
defer l.slock.unlock()
if !l.isIMSending() && !l.isConfirmed {
l.isSending = true
l.absLastAckTime = getSteadyClockMS()
l.absLastSendTime = l.absLastAckTime
l.ackInstanceID = beginInstanceID
l.beginInstanceID = beginInstanceID
l.sendToNodeID = sendToNodeID
return true
}
return false
}
func (l *learnerSender) confirm(beginInstanceID uint64, sendToNodeID uint64) bool {
l.slock.lock()
defer l.slock.unlock()
if l.isIMSending() && !l.isConfirmed {
if l.beginInstanceID == beginInstanceID && l.sendToNodeID == sendToNodeID {
l.isConfirmed = true
l.slock.interrupt()
return true
}
}
return false
}
func (l *learnerSender) ack(ackInstanceID uint64, fromNodeID uint64) {
l.slock.lock()
defer l.slock.unlock()
if l.isIMSending() && l.isConfirmed {
if l.sendToNodeID == fromNodeID && ackInstanceID > l.ackInstanceID {
l.ackInstanceID = ackInstanceID
l.absLastAckTime = getSteadyClockMS()
l.slock.interrupt()
}
}
}
func (l *learnerSender) waitToSend() {
l.slock.lock()
defer l.slock.unlock()
for !l.isConfirmed {
l.slock.waitTime(time.Millisecond * 1000)
if l.isEnd {
break
}
}
}
func (l *learnerSender) sendLearnedValue(beginInstanceID uint64, sendToNodeID uint64) {
lPLGHead(l.conf.groupIdx, "BeginInstanceID %d SendToNodeID %d", beginInstanceID, sendToNodeID)
sendInstanceID := beginInstanceID
sendQps := getInsideOptionsInstance().getLearnerSenderSendQps()
sleepMs := 1000 / sendQps
sendInterval := 1
if sendQps > 1000 {
sleepMs = 1
sendInterval = sendQps / 1000
}
lPLGDebug(l.conf.groupIdx, "SendQps %d SleepMs %d SendInterval %d AckLead %d",
sendQps, sleepMs, sendInterval, l.ackLead)
var lastChecksum uint32
sendCount := 0
for sendInstanceID < l.learner.getInstanceID() {
err := l.sendOne(sendInstanceID, sendToNodeID, &lastChecksum)
if err != nil {
lPLGErr(l.conf.groupIdx, "SendOne fail, SendInstanceID %d SendToNodeID %d, error: %v",
sendInstanceID, sendToNodeID, err)
return
}
if !l.checkAck(sendInstanceID) {
return
}
sendCount++
sendInstanceID++
l.refreshSending()
if sendCount >= sendInterval {
sendCount = 0
time.Sleep(time.Millisecond * time.Duration(sleepMs))
}
}
//succ send, reset ack lead.
l.ackLead = getInsideOptionsInstance().getLearnerSenderAckLead()
lPLGImp(l.conf.groupIdx, "SendDone, SendEndInstanceID %d", sendInstanceID)
}
func (l *learnerSender) sendOne(sendInstanceID uint64, sendToNodeID uint64, lastChecksum *uint32) error {
getBPInstance().SenderSendOnePaxosLog()
state, err := l.paxosLog.readState(l.conf.groupIdx, sendInstanceID)
if err != nil {
return err
}
ballot := newBallotNumber(state.GetAcceptedID(), state.GetAcceptedNodeID())
err = l.learner.sendLearnValue(sendToNodeID, sendInstanceID, ballot, state.GetAcceptedValue(), *lastChecksum, true)
*lastChecksum = state.GetChecksum()
return err
}
func (l *learnerSender) sendDone() {
l.slock.lock()
defer l.slock.unlock()
l.isSending = false
l.isConfirmed = false
l.beginInstanceID = math.MaxUint64
l.sendToNodeID = nullNode
l.absLastSendTime = 0
l.ackInstanceID = 0
l.absLastAckTime = 0
}