-
Notifications
You must be signed in to change notification settings - Fork 1
/
toy.go
357 lines (313 loc) · 7.76 KB
/
toy.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package sentinel
import (
"bytes"
"fmt"
"strconv"
"sync"
"time"
"github.com/google/uuid"
)
type InstancesManager struct {
currentTerm int
currentMaster *ToyKeva
addrMap map[string]*ToyKeva
mu *sync.Mutex
simulateConnections map[string]*toyClient
}
func (m *InstancesManager) killCurrentMaster() {
m.mu.Lock()
master := m.currentMaster
m.mu.Unlock()
master.kill()
}
func (m *InstancesManager) getInstanceByAddr(addr string) *ToyKeva {
m.mu.Lock()
defer m.mu.Unlock()
instance, ok := m.addrMap[addr]
if !ok {
panic(fmt.Sprintf("instance %s does not exist", addr))
}
return instance
}
func (m *InstancesManager) getMaster() *ToyKeva {
m.mu.Lock()
defer m.mu.Unlock()
return m.currentMaster
}
// ToyKeva simulator used for testing purpose
type ToyKeva struct {
host string
port string
mu *sync.Mutex
alive bool
diedAt *time.Time
role string
id string
slaves []*ToyKeva
subs map[string]chan string //key by fake sessionid
*slaveInfo // only set if current keva is a slave
*InstancesManager
}
func (keva *ToyKeva) getAddr() string {
return fmt.Sprintf("%s:%s", keva.host, keva.port)
}
func (keva *ToyKeva) slaveOf(host, port string) {
addr := fmt.Sprintf("%s:%s", host, port)
instance := keva.InstancesManager.getInstanceByAddr(addr)
keva.mu.Lock()
keva.role = "slave"
keva.slaves = nil
keva.master = instance
keva.masterHost = host
keva.masterPort = port
keva.mu.Unlock()
}
func (keva *ToyKeva) info() string {
keva.mu.Lock()
var ret = bytes.Buffer{}
ret.WriteString(fmt.Sprintf("role:%s\n", keva.role))
ret.WriteString(fmt.Sprintf("run_id:%s\n", keva.id))
slaves := keva.slaves
isSlave := keva.role == "slave"
keva.mu.Unlock()
for idx := range slaves {
sl := keva.slaves[idx]
sl.mu.Lock()
state := "online"
if !sl.alive {
state = "offline"
}
ret.WriteString(fmt.Sprintf("slave%d:ip=%s,port=%s,state=%s,offset=%d,lag=%d\n",
idx, sl.host, sl.port, state, sl.offset, sl.lag,
))
sl.mu.Unlock()
}
var (
masterDead bool
diedSince int
)
if isSlave {
locked(keva.master.mu, func() {
masterDead = !keva.master.alive
if masterDead {
diedSince = int(time.Since(*keva.master.diedAt).Seconds())
}
})
}
locked(keva.mu, func() {
if keva.role == "slave" {
ret.WriteString(fmt.Sprintf("master_host:%s\n", keva.masterHost))
ret.WriteString(fmt.Sprintf("master_port:%s\n", keva.masterPort))
if masterDead {
ret.WriteString(fmt.Sprintf("master_link_down_since_seconds:%d\n", diedSince))
ret.WriteString("master_link_status:down\n")
} else {
ret.WriteString("master_link_down_since_seconds:-1\n")
ret.WriteString("master_link_status:up\n")
}
ret.WriteString(fmt.Sprintf("slave_repl_offset:%d\n", keva.offset))
ret.WriteString(fmt.Sprintf("slave_priority:%d\n", keva.priority))
}
})
return ret.String()
}
func (sl *slaveInfo) String() string {
return fmt.Sprintf("offset: %d, priority: %d", sl.offset, sl.priority)
}
type slaveInfo struct {
// runID string
masterHost string
masterPort string
offset int
lag int
priority int
master *ToyKeva
}
func (keva *ToyKeva) diedSince() time.Duration {
keva.mu.Lock()
defer keva.mu.Unlock()
return time.Since(*keva.diedAt)
}
func (keva *ToyKeva) kill() {
keva.mu.Lock()
defer keva.mu.Unlock()
keva.alive = false
now := time.Now()
keva.diedAt = &now
}
func (keva *ToyKeva) isAlive() bool {
keva.mu.Lock()
defer keva.mu.Unlock()
return keva.alive
}
type toyClient struct {
link *ToyKeva
connected bool
mu *sync.Mutex
}
func (m *InstancesManager) NewSlaveKeva(host, port string) *ToyKeva {
addr := host + ":" + port
instance := &ToyKeva{
role: "slave",
id: uuid.NewString(),
mu: &sync.Mutex{},
subs: map[string]chan string{},
alive: true,
host: host,
port: port,
InstancesManager: m,
}
m.addrMap[addr] = instance
return instance
}
func (m *InstancesManager) NewMasterToyKeva(host, port string) *ToyKeva {
addr := host + ":" + port
instance := &ToyKeva{
role: "master",
id: uuid.NewString(),
mu: &sync.Mutex{},
subs: map[string]chan string{},
alive: true,
host: host,
port: port,
InstancesManager: m,
}
m.addrMap[addr] = instance
m.currentMaster = instance
return instance
}
func (m *InstancesManager) SpawnSlaves(num int) map[string]*ToyKeva {
m.mu.Lock()
defer m.mu.Unlock()
slaves := []*ToyKeva{}
slaveMap := map[string]*ToyKeva{}
master := m.currentMaster
for i := 0; i < num; i++ {
newSlave := m.NewSlaveKeva("localhost", strconv.Itoa(i)) // fake port, toy master does not call toy slave through network call
newSlave.slaveInfo = &slaveInfo{
masterHost: master.host,
masterPort: master.port,
priority: 1,
offset: 0,
lag: 0, // TODO: don't understand what it means
master: master,
}
addr := fmt.Sprintf("%s:%s", newSlave.host, newSlave.port)
slaves = append(slaves, newSlave)
slaveMap[addr] = newSlave
}
master.slaves = slaves
return slaveMap
}
// func (keva *ToyKeva) turnToSlave() {
// keva.role = "slave"
// keva.alive = true
// }
func (keva *ToyKeva) turnToMaster() {
keva.mu.Lock()
keva.role = "master"
keva.alive = true
keva.slaveInfo = nil
keva.mu.Unlock()
locked(keva.InstancesManager.mu, func() {
keva.InstancesManager.currentMaster = keva
})
}
func NewToyKevaClient(keva *ToyKeva) *toyClient {
return &toyClient{
link: keva,
mu: &sync.Mutex{},
connected: true,
}
}
func (cl *toyClient) SlaveOf(host, port string) error {
if !cl.link.isAlive() {
return fmt.Errorf("dead")
}
cl.link.slaveOf(host, port)
return nil
}
func (cl *toyClient) SlaveOfNoOne() error {
cl.link.turnToMaster()
return nil
}
func (cl *toyClient) Info() (string, error) {
if !cl.link.isAlive() {
return "", fmt.Errorf("dead")
}
return cl.link.info(), nil
}
func (cl *toyClient) disconnect() {
cl.mu.Lock()
defer cl.mu.Unlock()
cl.connected = false
}
// simulate network partition
func (cl *toyClient) isConnected() bool {
cl.mu.Lock()
defer cl.mu.Unlock()
return cl.connected
}
func (cl *toyClient) Ping() (string, error) {
if !cl.link.isAlive() || !cl.isConnected() {
return "", fmt.Errorf("dead keva")
}
return "pong", nil
}
type toyHelloChan struct {
root *ToyKeva
subChan chan string
sessionID string //
}
func (c *toyHelloChan) Publish(toBroadcast string) error {
c.root.mu.Lock()
for sessionID, sub := range c.root.subs {
if sessionID == c.sessionID {
continue
}
sub <- toBroadcast
}
c.root.mu.Unlock()
return nil
}
func (c *toyHelloChan) Receive() (string, error) {
newMsg := <-c.subChan
return newMsg, nil
}
func (c *toyHelloChan) Close() error {
c.root.mu.Lock()
delete(c.root.subs, c.sessionID)
c.root.mu.Unlock()
close(c.subChan)
return nil
}
func (cl *toyClient) SubscribeHelloChan() HelloChan {
newChan := &toyHelloChan{
root: cl.link,
subChan: make(chan string, 1),
sessionID: uuid.NewString(),
}
cl.link.mu.Lock()
cl.link.subs[newChan.sessionID] = newChan.subChan
cl.link.mu.Unlock()
return newChan
}
// func (cl *toyClient) ExchangeSentinel(intro Intro) (ExchangeSentinelResponse, error) {
// cl.link.mu.Lock()
// var ret []SentinelIntroResponse
// for _, s := range cl.link.sentinels {
// ret = append(ret, SentinelIntroResponse{
// Addr: s.addr,
// Port: s.port,
// RunID: s.runID,
// MasterName: s.masterName,
// MasterPort: s.masterPort,
// MasterAddr: s.masterAddr,
// Epoch: s.epoch,
// MasterEpoch: s.masterEpoch,
// })
// }
// cl.link.mu.Unlock()
// cl.link.addSentinel(intro)
// return ExchangeSentinelResponse{Sentinels: ret}, nil
// }