-
Notifications
You must be signed in to change notification settings - Fork 921
/
nodeconn.go
305 lines (248 loc) · 8.67 KB
/
nodeconn.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
package orchestrator
import (
"fmt"
"net"
"sync"
"time"
"github.com/botlabs-gg/yagpdb/v2/lib/dshardorchestrator"
)
// NodeConn represents a connection from a master server to a slave
type NodeConn struct {
Orchestrator *Orchestrator
Conn *dshardorchestrator.Conn
connected bool
disconnectedAt time.Time
// below fields are protected by this mutex
mu sync.Mutex
sessionEstablished bool
version string
runningShards []int
shardMigrationOtherNodeID string
shardMigrationShard int
shardMigrationMode dshardorchestrator.ShardMigrationMode
processedUserEvents int
shardmigrationTotalUserEvts int
shuttingDown bool
}
// NewNodeConn creates a new NodeConn (connection from master to slave) from a net.Conn
func (o *Orchestrator) NewNodeConn(netConn net.Conn) *NodeConn {
sc := &NodeConn{
Conn: dshardorchestrator.ConnFromNetCon(netConn, o.Logger),
Orchestrator: o,
connected: true,
}
sc.Conn.MessageHandler = sc.handleMessage
sc.Conn.ConnClosedHanlder = func() {
// TODO
sc.mu.Lock()
sc.connected = false
sc.disconnectedAt = time.Now()
sc.mu.Unlock()
}
return sc
}
func (nc *NodeConn) listen() {
nc.Conn.Listen()
}
func (nc *NodeConn) removeShard(shardID int) {
for i, v := range nc.runningShards {
if v == shardID {
nc.runningShards = append(nc.runningShards[:i], nc.runningShards[i+1:]...)
}
}
}
// Handle incoming messages
func (nc *NodeConn) handleMessage(msg *dshardorchestrator.Message) {
switch msg.EvtID {
case dshardorchestrator.EvtIdentify:
nc.handleIdentify(msg.DecodedBody.(*dshardorchestrator.IdentifyData))
case dshardorchestrator.EvtStartShards:
data := msg.DecodedBody.(*dshardorchestrator.StartShardsData)
nc.mu.Lock()
// add the shards to the tracked shards for this node
for _, v := range data.ShardIDs {
if !dshardorchestrator.ContainsInt(nc.runningShards, v) {
nc.runningShards = append(nc.runningShards, v)
}
if nc.shardMigrationShard == v && nc.shardMigrationMode != dshardorchestrator.ShardMigrationModeNone {
// we were migrating this shard, mark it as done
nc.shardMigrationMode = dshardorchestrator.ShardMigrationModeNone
}
}
nc.mu.Unlock()
case dshardorchestrator.EvtStopShard:
data := msg.DecodedBody.(*dshardorchestrator.StopShardData)
nc.mu.Lock()
nc.removeShard(data.ShardID)
nc.mu.Unlock()
case dshardorchestrator.EvtPrepareShardmigration:
data := msg.DecodedBody.(*dshardorchestrator.PrepareShardmigrationData)
nc.mu.Lock()
otherNodeID := nc.shardMigrationOtherNodeID
nc.mu.Unlock()
otherNode := nc.Orchestrator.FindNodeByID(otherNodeID)
if otherNode == nil {
nc.Conn.Log(dshardorchestrator.LogError, nil, "node dissapeared in the middle of shard migration")
return
}
if data.Origin {
nc.mu.Lock()
nc.removeShard(data.ShardID)
nc.mu.Unlock()
data.Origin = false
go otherNode.Conn.SendLogErr(dshardorchestrator.EvtPrepareShardmigration, data)
return
}
// start sending state data
go otherNode.Conn.SendLogErr(dshardorchestrator.EvtStartShardMigration, &dshardorchestrator.StartshardMigrationData{
ShardID: data.ShardID,
})
case dshardorchestrator.EvtAllUserdataSent:
nc.mu.Lock()
otherNodeID := nc.shardMigrationOtherNodeID
nc.shardMigrationMode = dshardorchestrator.ShardMigrationModeNone
nc.mu.Unlock()
otherNode := nc.Orchestrator.FindNodeByID(otherNodeID)
if otherNode == nil {
nc.Conn.Log(dshardorchestrator.LogError, nil, "node dissapeared in the middle of shard migration")
return
}
go otherNode.Conn.SendLogErr(dshardorchestrator.EvtAllUserdataSent, msg.DecodedBody)
default:
if msg.EvtID < 100 {
return
}
nc.mu.Lock()
otherNodeID := nc.shardMigrationOtherNodeID
nc.mu.Unlock()
otherNode := nc.Orchestrator.FindNodeByID(otherNodeID)
if otherNode == nil {
nc.Conn.Log(dshardorchestrator.LogError, nil, "node dissapeared in the middle of shard migration")
return
}
go otherNode.Conn.SendLogErr(msg.EvtID, msg.RawBody)
}
}
func (nc *NodeConn) validateTotalShards(data *dshardorchestrator.IdentifyData) (ok bool, totalShards int) {
nc.Orchestrator.mu.Lock()
if data.TotalShards == 0 && nc.Orchestrator.totalShards == 0 {
// we may need to fetch a fresh shard count, but wait 10 seconds to see if another node with already set shard count connects
nc.Orchestrator.mu.Unlock()
if !nc.Orchestrator.SkipSafeStartupDelayMaxShards {
for i := 0; i < 100; i++ {
time.Sleep(time.Millisecond * 100)
nc.Orchestrator.mu.Lock()
if nc.Orchestrator.totalShards != 0 {
nc.Orchestrator.mu.Unlock()
break
}
nc.Orchestrator.mu.Unlock()
}
}
// we need to fetch a fresh total shard count
for {
nc.Orchestrator.mu.Lock()
if nc.Orchestrator.totalShards != 0 {
break
}
sc, err := nc.Orchestrator.getTotalShardCount()
if err != nil {
nc.Orchestrator.mu.Unlock()
nc.Conn.Log(dshardorchestrator.LogError, err, "failed fetching total shard count, retrying in a second")
time.Sleep(time.Second)
continue
}
nc.Orchestrator.totalShards = sc
break // keep it locked out of the loop
}
}
totalShards = nc.Orchestrator.totalShards
if data.TotalShards > 0 && nc.Orchestrator.totalShards == 0 {
nc.Orchestrator.totalShards = data.TotalShards
totalShards = data.TotalShards
} else if data.TotalShards > 0 && data.TotalShards != nc.Orchestrator.totalShards {
// in this case there isn't much we can do, in the current state the orchestrator does not support varying shard counts so if this were to happen then yeah...
// in the future this will be handled, things like rescaling shard by doubling the count is a relatively easy process
// (shut down 1 shard completely, start 2 shards that combined were holding the same servers as the one shut down, works since it's doubled)
nc.Conn.Log(dshardorchestrator.LogError, nil, "NOT-MATCHING TOTAL SHARD COUNTS!")
nc.Orchestrator.mu.Unlock()
return false, totalShards
}
nc.Orchestrator.mu.Unlock()
return true, totalShards
}
func (nc *NodeConn) handleIdentify(data *dshardorchestrator.IdentifyData) {
if data.OrchestratorLogicVersion != 2 {
// TODO: maybe disconnect instead? altough this is the best way of handling it imo
// instead of accidentally launching multiple of already connected shards, just stop everything if this happens
panic("Incompatible node behaviour/protocol version")
}
valid, totalShards := nc.validateTotalShards(data)
if !valid {
return
}
nc.Conn.ID.Store(data.NodeID)
// check if were holding a duplicates
nc.Orchestrator.mu.Lock()
for i, n := range nc.Orchestrator.connectedNodes {
if n.Conn.GetID() == data.NodeID && n != nc {
go n.Conn.Close()
nc.Orchestrator.connectedNodes = append(nc.Orchestrator.connectedNodes[:i], nc.Orchestrator.connectedNodes[i+1:]...)
break
}
}
nc.Orchestrator.mu.Unlock()
// after this we have sucessfully established a session
resp := &dshardorchestrator.IdentifiedData{
NodeID: nc.Conn.GetID(),
TotalShards: totalShards,
}
nc.Conn.SendLogErr(dshardorchestrator.EvtIdentified, resp)
nc.mu.Lock()
nc.version = data.Version
nc.sessionEstablished = true
nc.runningShards = data.RunningShards
nc.mu.Unlock()
nc.Conn.Log(dshardorchestrator.LogInfo, nil, fmt.Sprintf("v%s - tot.shards: %d - running.shards: %v", data.Version, data.TotalShards, data.RunningShards))
}
// GetFullStatus returns the current status of the node
func (nc *NodeConn) GetFullStatus() *NodeStatus {
nc.mu.Lock()
defer nc.mu.Unlock()
status := &NodeStatus{
ID: nc.Conn.GetID(),
SessionEstablished: nc.sessionEstablished,
MigratingShard: nc.shardMigrationShard,
Connected: nc.connected,
DisconnectedAt: nc.disconnectedAt,
Version: nc.version,
}
status.Shards = make([]int, len(nc.runningShards))
copy(status.Shards, nc.runningShards)
if nc.shardMigrationMode == dshardorchestrator.ShardMigrationModeFrom {
status.MigratingTo = nc.shardMigrationOtherNodeID
} else if nc.shardMigrationMode == dshardorchestrator.ShardMigrationModeTo {
status.MigratingFrom = nc.shardMigrationOtherNodeID
}
return status
}
// StartShards tells the node to start the following shards
func (nc *NodeConn) StartShards(shards ...int) {
go nc.Conn.SendLogErr(dshardorchestrator.EvtStartShards, &dshardorchestrator.StartShardsData{
ShardIDs: shards,
})
}
// StopShard tells the node to stop the provided shard
func (nc *NodeConn) StopShard(shard int) {
go nc.Conn.SendLogErr(dshardorchestrator.EvtStopShard, &dshardorchestrator.StopShardData{
ShardID: shard,
})
}
// Shutdown tells the node to shut down compleely
func (nc *NodeConn) Shutdown() {
nc.mu.Lock()
nc.shuttingDown = true
go nc.Conn.SendLogErr(dshardorchestrator.EvtShutdown, nil)
nc.mu.Unlock()
return
}