-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslave.go
323 lines (302 loc) · 7.94 KB
/
slave.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package sentinel
import (
"fmt"
"sort"
"strconv"
"strings"
"sync"
"time"
)
func (s *slaveInstance) shutdown() {
s.mu.Lock()
s.killed = true
s.mu.Unlock()
}
func (s *slaveInstance) iskilled() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.killed
}
// This function is called in context current master is subj down only
// Redis version of this function also supports when master is alive, so some logic is missing
func (s *Sentinel) selectSlave(m *masterInstance) *slaveInstance {
m.mu.Lock()
defer m.mu.Unlock()
// TODO
maxDownTime := time.Since(m.downSince) + m.sentinelConf.DownAfter*10
qualifiedSlaves := slaveCandidates{}
for idx := range m.slaves {
slave := m.slaves[idx]
locked(&slave.mu, func() {
if slave.sDown {
return
}
if time.Since(slave.lastSucessfulPingAt) > 5*time.Second {
return
}
if slave.slavePriority == 0 {
return
}
infoValidityTime := 5 * time.Second
if time.Since(slave.lastSucessfulInfoAt) > infoValidityTime {
return
}
// accept the fact that this slave still does not see master is down somehow
if slave.masterDownSince > maxDownTime {
return
}
// copy to compare, avoid locking
qualifiedSlaves = append(qualifiedSlaves, slaveCandidate{
slavePriority: slave.slavePriority,
replOffset: slave.replOffset,
runID: slave.runID,
findBack: slave,
})
})
}
if len(qualifiedSlaves) > 0 {
sort.Sort(qualifiedSlaves)
chosen := qualifiedSlaves[len(qualifiedSlaves)-1]
return chosen.findBack
}
return nil
}
func (s *Sentinel) slaveInfoRoutine(sl *slaveInstance) {
infoDelay := time.Duration(10 * time.Second)
timer := time.NewTimer(infoDelay)
for !sl.iskilled() {
info, err := sl.client.Info()
if err != nil {
s.logger.Errorf("sl.client.Info: %s", err)
//TODO continue for now
} else {
sl.mu.Lock()
sl.lastSucessfulInfoAt = time.Now()
sl.mu.Unlock()
roleSwitched, err := s.parseInfoSlave(sl.reportedMaster, sl.addr, info)
if err != nil {
s.logger.Errorf("parseInfoslave error: %v", err)
continue
// continue for now
}
// slave change master post failover logic
// TODO
if roleSwitched {
timeout := time.NewTimer(1 * time.Second)
defer timeout.Stop()
select {
case <-timeout.C:
// treat it as if it is still slave
case sl.masterRoleSwitchChan <- struct{}{}:
// master routine is waiting for this signal, send and return, this will become new master
//TODO: what non-leader sentinel behaves when seeing this
return
}
}
}
timer.Reset(infoDelay)
select {
case <-sl.masterDownNotify:
infoDelay = 1 * time.Second
case <-timer.C:
}
}
}
func (s *Sentinel) newSlaveInstance(masterHost, masterPort, host, port string, replOffset int, master *masterInstance) (*slaveInstance, error) {
newslave := &slaveInstance{
masterHost: masterHost,
masterPort: masterPort,
host: host,
port: port,
addr: fmt.Sprintf("%s:%s", host, port),
replOffset: replOffset,
reportedMaster: master,
masterDownNotify: make(chan struct{}, 1),
masterRoleSwitchChan: make(chan struct{}),
}
// err := s.slaveFactory(newslave)
// if err != nil {
// s.logger.Errorf("s.slaveFactory: %s", err)
// return nil, err
// }
cl, err := s.clientFactory(fmt.Sprintf("%s:%s", host, port))
if err != nil {
return nil, err
}
newslave.client = cl
return newslave, nil
}
func (s *Sentinel) sayHelloRoutineToSlave(sl *slaveInstance, helloChan HelloChan) {
for !sl.iskilled() {
time.Sleep(2 * time.Second)
m := sl.reportedMaster
var (
masterName, masterIP, masterPort string
masterConfigEpoch int
)
locked(&m.mu, func() {
masterName = m.name
if m.promotedSlave != nil {
locked(&m.promotedSlave.mu, func() {
masterIP = m.promotedSlave.host
masterPort = m.promotedSlave.port
})
} else {
masterIP = m.host
masterPort = m.port
}
masterConfigEpoch = m.configEpoch
})
info := fmt.Sprintf("%s,%s,%s,%d,%s,%s,%s,%d",
s.conf.Binds[0], s.conf.Port, s.selfID(),
s.getCurrentEpoch(), masterName, masterIP, masterPort, masterConfigEpoch,
)
err := helloChan.Publish(info)
if err != nil {
s.logger.Errorf("helloChan.Publish: %s", err)
}
}
}
/* Format is composed of 8 tokens:
* 0=ip,1=port,2=runid,3=current_epoch,4=master_name,
* 5=master_ip,6=master_port,7=master_config_epoch. */
func (s *Sentinel) slaveHelloRoutine(sl *slaveInstance) {
//TODO: handle connection lost/broken pipe
helloChan := sl.client.SubscribeHelloChan()
defer helloChan.Close()
selfID := s.selfID()
go s.sayHelloRoutineToSlave(sl, helloChan)
for !sl.iskilled() {
newmsg, err := helloChan.Receive()
if err != nil {
s.logger.Errorf("helloChan.Receive: %s", err)
continue
//skip for now
}
parts := strings.Split(newmsg, ",")
if len(parts) != 8 {
s.logger.Errorf("helloChan.Receive: invalid format for newmsg: %s", newmsg)
continue
}
runid := parts[2]
if runid == selfID {
continue
}
m := sl.reportedMaster
m.mu.Lock()
masterName := m.name
_, ok := m.sentinels[runid]
if !ok {
client, err := NewRedisProtoInternalClient(parts[0], parts[1])
if err != nil {
s.logger.Errorf("NewRedisProtoInternalClient: cannot create new client to other sentinel with info: %s: %s\ndebug: %s", newmsg, err, newmsg)
m.mu.Unlock()
continue
}
si := &sentinelInstance{
mu: sync.Mutex{},
sdown: false,
client: client,
runID: runid,
}
m.sentinels[runid] = si
s.logger.Infof("subscribeHello: connected to new sentinel: %s", newmsg)
}
m.mu.Unlock()
masterNameInMsg, newHost, newPort := parts[4], parts[5], parts[6]
if masterName != masterNameInMsg {
continue
}
neighborEpoch, err := strconv.Atoi(parts[3])
if err != nil {
continue
}
neighborConfigEpoch, err := strconv.Atoi(parts[7])
if err != nil {
continue
}
s.mu.Lock()
currentEpoch := s.currentEpoch
if neighborEpoch > currentEpoch {
s.currentEpoch = neighborEpoch
// Not sure if this needs reseting anything in fsm
//TODO panic("not implemented")
}
s.mu.Unlock()
m.mu.Lock()
currentConfigEpoch := m.configEpoch
var switched bool
if currentConfigEpoch < neighborConfigEpoch {
m.configEpoch = neighborConfigEpoch
host, port := m.host, m.port
if port != newPort || host != newHost {
promotedSlave, exist := m.slaves[fmt.Sprintf("%s:%s", newHost, newPort)]
if exist {
switched = true
m.promotedSlave = promotedSlave
}
}
}
m.mu.Unlock()
if switched {
select {
case m.followerNewMasterNotify <- struct{}{}:
default:
}
}
}
}
func (s *Sentinel) slaveRoutine(sl *slaveInstance) {
s.logger.Debugw(logEventSlaveInstanceCreated,
"master_id", sl.reportedMaster.runID,
"address", sl.addr,
"sentinel_run_id", s.runID,
)
go s.slaveInfoRoutine(sl)
go s.slaveHelloRoutine(sl)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for !sl.iskilled() {
_, err := sl.client.Ping()
sl.mu.Lock()
if err != nil {
if time.Since(sl.lastSucessfulInfoAt) > sl.reportedMaster.sentinelConf.DownAfter {
sl.sDown = true
}
} else {
sl.lastSucessfulPingAt = time.Now()
}
sl.mu.Unlock()
<-ticker.C
}
}
type slaveCandidates []slaveCandidate
type slaveCandidate struct {
slavePriority int
replOffset int
runID string
findBack *slaveInstance
}
func (sl slaveCandidates) Len() int { return len(sl) }
func (sl slaveCandidates) Swap(i, j int) { sl[i], sl[j] = sl[j], sl[i] }
func (sl slaveCandidates) Less(i, j int) bool {
sli, slj := sl[i], sl[j]
if sli.slavePriority != slj.slavePriority {
return sli.slavePriority-slj.slavePriority < 0
}
if sli.replOffset > slj.replOffset {
return false
} else if sli.replOffset < slj.replOffset {
return true
}
// equal replication offset, compare lexicongraphically
cmp := strings.Compare(sli.runID, slj.runID)
switch cmp {
case -1:
return true
case 0:
return true
default:
return false
}
}