forked from lni/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterimage.go
517 lines (470 loc) · 12.6 KB
/
clusterimage.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package drummer
import (
"sort"
pb "github.com/lni/dragonboat/drummer/drummerpb"
"github.com/lni/dragonboat/internal/settings"
"github.com/lni/dragonboat/internal/utils/logutil"
)
var (
// nodeHostTTL defines the number of seconds without heartbeat required to
// consider a node host as dead.
nodeHostTTL = settings.Soft.NodeHostTTL
)
type node struct {
ClusterID uint64
NodeID uint64
Address string
IsLeader bool
Tick uint64
FirstObserved uint64
}
type cluster struct {
ClusterID uint64
ConfigChangeIndex uint64
Nodes map[uint64]*node
}
type multiCluster struct {
Clusters map[uint64]*cluster
NodesToKill []nodeToKill
}
type clusterRepair struct {
clusterID uint64
cluster *cluster
failedNodes []node
okNodes []node
nodesToStart []node
}
type nodeToKill struct {
ClusterID uint64
NodeID uint64
Address string
}
func (cr *clusterRepair) describe() string {
return logutil.ClusterID(cr.clusterID)
}
func (cr *clusterRepair) quorum() int {
count := len(cr.failedNodes) + len(cr.okNodes) + len(cr.nodesToStart)
return count/2 + 1
}
func (cr *clusterRepair) available() bool {
return len(cr.okNodes) >= cr.quorum()
}
func (cr *clusterRepair) addRequired() bool {
return len(cr.failedNodes) > 0 &&
len(cr.nodesToStart) == 0 && cr.available()
}
func (cr *clusterRepair) createRequired() bool {
return len(cr.nodesToStart) > 0
}
func (cr *clusterRepair) deleteRequired(expectedClusterSize int) bool {
return cr.available() && len(cr.failedNodes) > 0 &&
len(cr.failedNodes)+len(cr.okNodes) > expectedClusterSize
}
func (cr *clusterRepair) hasNodesToStart() bool {
return len(cr.nodesToStart) > 0
}
func (cr *clusterRepair) needToBeRestored() bool {
if cr.available() || len(cr.nodesToStart) > 0 {
return false
}
return true
}
func (cr *clusterRepair) readyToBeRestored(mnh *multiNodeHost,
currentTick uint64) ([]node, bool) {
if !cr.needToBeRestored() {
return nil, false
}
nl := make([]node, 0)
for _, n := range cr.failedNodes {
spec, ok := mnh.Nodehosts[n.Address]
if ok && spec.available(currentTick) && spec.hasLog(n.ClusterID, n.NodeID) {
nl = append(nl, n)
}
}
return nl, len(cr.okNodes)+len(nl) >= cr.quorum()
}
func (cr *clusterRepair) canBeRestored(mnh *multiNodeHost,
currentTick uint64) []node {
nl := make([]node, 0)
for _, n := range cr.failedNodes {
spec, ok := mnh.Nodehosts[n.Address]
if ok && spec.available(currentTick) && spec.hasLog(n.ClusterID, n.NodeID) {
nl = append(nl, n)
}
}
return nl
}
func (cr *clusterRepair) logUnableToRestoreCluster(mnh *multiNodeHost,
currentTick uint64) {
for _, n := range cr.failedNodes {
spec, ok := mnh.Nodehosts[n.Address]
if ok {
available := spec.available(currentTick)
hasLog := spec.hasLog(n.ClusterID, n.NodeID)
plog.Debugf("node %s available: %t", n.Address, available)
plog.Debugf("node %s hasLog for %s: %t", n.Address,
logutil.DescribeNode(n.ClusterID, n.NodeID), hasLog)
} else {
plog.Debugf("failed node %s not in mnh.nodehosts", n.Address)
}
}
}
// EntityFailed returns whether the timeline indicates that the entity has
// failed.
func EntityFailed(lastTick uint64, currentTick uint64) bool {
if currentTick-lastTick > nodeHostTTL {
return true
}
return false
}
func (n *node) failed(tick uint64) bool {
if n.Tick == 0 {
return n.FirstObserved == 0
}
return EntityFailed(n.Tick, tick)
}
func (n *node) describe() string {
return logutil.DescribeNode(n.ClusterID, n.NodeID)
}
func (n *node) waitingToBeStarted(tick uint64) bool {
return n.Tick == 0 && !n.failed(tick)
}
func (c *cluster) describe() string {
return logutil.ClusterID(c.ClusterID)
}
func (c *cluster) quorum() int {
return len(c.Nodes)/2 + 1
}
func (c *cluster) available(currentTick uint64) bool {
return len(c.getOkNodes(currentTick)) >= c.quorum()
}
func (c *cluster) getOkNodes(tick uint64) []node {
result := make([]node, 0)
for _, n := range c.Nodes {
if !n.failed(tick) && !n.waitingToBeStarted(tick) {
result = append(result, *n)
}
}
return result
}
func (c *cluster) getNodesToStart(tick uint64) []node {
result := make([]node, 0)
for _, n := range c.Nodes {
if n.waitingToBeStarted(tick) {
result = append(result, *n)
}
}
return result
}
func (c *cluster) getFailedNodes(tick uint64) []node {
result := make([]node, 0)
for _, n := range c.Nodes {
if n.failed(tick) {
result = append(result, *n)
}
}
return result
}
func (c *cluster) deepCopy() *cluster {
nc := &cluster{
ClusterID: c.ClusterID,
ConfigChangeIndex: c.ConfigChangeIndex,
}
nc.Nodes = make(map[uint64]*node)
for k, v := range c.Nodes {
nc.Nodes[k] = &node{
ClusterID: v.ClusterID,
NodeID: v.NodeID,
Address: v.Address,
IsLeader: v.IsLeader,
Tick: v.Tick,
FirstObserved: v.FirstObserved,
}
}
return nc
}
func newMultiCluster() *multiCluster {
return &multiCluster{
Clusters: make(map[uint64]*cluster),
}
}
func (mc *multiCluster) deepCopy() *multiCluster {
c := &multiCluster{
Clusters: make(map[uint64]*cluster),
}
for k, v := range mc.Clusters {
c.Clusters[k] = v.deepCopy()
}
return c
}
func (mc *multiCluster) getToKillNodes() []nodeToKill {
result := make([]nodeToKill, 0)
for _, ntk := range mc.NodesToKill {
n := nodeToKill{
ClusterID: ntk.ClusterID,
NodeID: ntk.NodeID,
Address: ntk.Address,
}
result = append(result, n)
}
mc.NodesToKill = mc.NodesToKill[:0]
return result
}
func (mc *multiCluster) size() int {
return len(mc.Clusters)
}
func (mc *multiCluster) getUnavailableClusters(currentTick uint64) []cluster {
cl := make([]cluster, 0)
for _, c := range mc.Clusters {
if !c.available(currentTick) {
cl = append(cl, *c)
}
}
return cl
}
func (mc *multiCluster) getClusterForRepair(currentTick uint64) []clusterRepair {
crl := make([]clusterRepair, 0)
for _, c := range mc.Clusters {
failedNodes := c.getFailedNodes(currentTick)
okNodes := c.getOkNodes(currentTick)
toStartNodes := c.getNodesToStart(currentTick)
if len(failedNodes) == 0 && len(toStartNodes) == 0 {
continue
}
sort.Slice(failedNodes, func(i, j int) bool {
return failedNodes[i].NodeID < failedNodes[j].NodeID
})
sort.Slice(okNodes, func(i, j int) bool {
return okNodes[i].NodeID < okNodes[j].NodeID
})
sort.Slice(toStartNodes, func(i, j int) bool {
return toStartNodes[i].NodeID < toStartNodes[j].NodeID
})
cr := clusterRepair{
cluster: c,
clusterID: c.ClusterID,
failedNodes: failedNodes,
okNodes: okNodes,
nodesToStart: toStartNodes,
}
crl = append(crl, cr)
}
return crl
}
func (mc *multiCluster) update(nhi pb.NodeHostInfo) {
toKill := mc.doUpdate(nhi)
for _, ntk := range toKill {
n := nodeToKill{
ClusterID: ntk.ClusterId,
NodeID: ntk.NodeId,
Address: nhi.RaftAddress,
}
mc.NodesToKill = append(mc.NodesToKill, n)
}
mc.syncLeaderInfo(nhi)
}
func (mc *multiCluster) syncLeaderInfo(nhi pb.NodeHostInfo) {
for _, ci := range nhi.ClusterInfo {
cid := ci.ClusterId
c, ok := mc.Clusters[cid]
if !ok {
continue
}
if c.ConfigChangeIndex > ci.ConfigChangeIndex {
continue
}
n, ok := c.Nodes[ci.NodeId]
if !ok {
continue
}
if !ci.IsLeader && n.IsLeader {
n.IsLeader = false
} else if ci.IsLeader && !n.IsLeader {
for _, cn := range c.Nodes {
cn.IsLeader = false
}
n.IsLeader = true
}
}
}
func (mc *multiCluster) reset() {
mc.NodesToKill = mc.NodesToKill[:0]
mc.Clusters = make(map[uint64]*cluster)
}
func (mc *multiCluster) updateNodeTick(nhi pb.NodeHostInfo) {
for _, cluster := range nhi.ClusterInfo {
cid := cluster.ClusterId
nid := cluster.NodeId
if ec, ok := mc.Clusters[cid]; ok {
if n, ok := ec.Nodes[nid]; ok {
n.Tick = nhi.LastTick
}
}
}
}
// doUpdate updates the local records of known clusters using recently received
// NodeHostInfo received from nodehost, it returns a list of ClusterInfo
// considered as zombie clusters that are expected to be killed.
func (mc *multiCluster) doUpdate(nhi pb.NodeHostInfo) []pb.ClusterInfo {
toKill := make([]pb.ClusterInfo, 0)
for _, currentCluster := range nhi.ClusterInfo {
cid := currentCluster.ClusterId
plog.Debugf("updating NodeHostInfo for %s, pending %t, incomplete %t",
logutil.DescribeNode(currentCluster.ClusterId, currentCluster.NodeId),
currentCluster.Pending, currentCluster.Incomplete)
if currentCluster.Pending {
if ec, ok := mc.Clusters[cid]; ok {
if len(ec.Nodes) > 0 && ec.ConfigChangeIndex > 0 &&
ec.killRequestRequired(currentCluster) {
toKill = append(toKill, currentCluster)
}
}
continue
}
if !currentCluster.Incomplete {
if ec, ok := mc.Clusters[cid]; !ok {
// create not in the map, create it
mc.Clusters[cid] = mc.getCluster(currentCluster, nhi.LastTick)
} else {
// cluster record is there, sync the info
rejected := ec.syncCluster(currentCluster, nhi.LastTick)
killRequired := ec.killRequestRequired(currentCluster)
if rejected && killRequired {
toKill = append(toKill, currentCluster)
}
}
} else {
// for this particular cluster, we don't have the full info
// check whether the cluster/node are available, if true, update
// the tick value
ec, ca := mc.Clusters[cid]
if ca {
if len(ec.Nodes) > 0 && ec.ConfigChangeIndex > 0 &&
ec.killRequestRequired(currentCluster) {
toKill = append(toKill, currentCluster)
}
}
}
}
// update nodes tick
mc.updateNodeTick(nhi)
return toKill
}
func (mc *multiCluster) GetCluster(clusterID uint64) *cluster {
return mc.getClusterInfo(clusterID)
}
func (c *cluster) killRequestRequired(ci pb.ClusterInfo) bool {
if c.ConfigChangeIndex <= ci.ConfigChangeIndex {
return false
}
for nid := range c.Nodes {
if nid == ci.NodeId {
return false
}
}
return true
}
func (mc *multiCluster) getClusterInfo(clusterID uint64) *cluster {
c, ok := mc.Clusters[clusterID]
if !ok {
return nil
}
return c.deepCopy()
}
func (mc *multiCluster) getCluster(ci pb.ClusterInfo, tick uint64) *cluster {
c := &cluster{
ClusterID: ci.ClusterId,
ConfigChangeIndex: ci.ConfigChangeIndex,
Nodes: make(map[uint64]*node),
}
for nodeID, address := range ci.Nodes {
n := &node{
NodeID: nodeID,
Address: address,
ClusterID: ci.ClusterId,
FirstObserved: tick,
}
c.Nodes[nodeID] = n
}
n, ok := c.Nodes[ci.NodeId]
if ok {
n.IsLeader = ci.IsLeader
}
return c
}
func (c *cluster) syncCluster(ci pb.ClusterInfo, lastTick uint64) bool {
if c.ConfigChangeIndex > ci.ConfigChangeIndex {
return true
}
if c.ConfigChangeIndex == ci.ConfigChangeIndex {
if len(c.Nodes) != len(ci.Nodes) {
plog.Panicf("same config change index with different node count")
}
for nid := range ci.Nodes {
_, ok := c.Nodes[nid]
if !ok {
plog.Panicf("different node id list")
}
}
}
if c.ConfigChangeIndex < ci.ConfigChangeIndex {
plog.Debugf("cluster %s is being updated, %v, %v",
c.describe(), c.Nodes, ci)
}
// we don't just re-create and populate the Nodes map because the
// FirstObserved value need to be kept after the sync.
c.ConfigChangeIndex = ci.ConfigChangeIndex
toRemove := make([]uint64, 0)
for nid := range c.Nodes {
_, ok := ci.Nodes[nid]
if !ok {
toRemove = append(toRemove, nid)
}
}
for _, nid := range toRemove {
plog.Debugf("cluster %s is removing node %d due to config change %d",
c.describe(), nid, ci.ConfigChangeIndex)
delete(c.Nodes, nid)
}
for nodeID, address := range ci.Nodes {
if _, ok := c.Nodes[nodeID]; !ok {
n := &node{
NodeID: nodeID,
Address: address,
ClusterID: ci.ClusterId,
FirstObserved: lastTick,
}
c.Nodes[nodeID] = n
plog.Debugf("cluster %s is adding node %d:%s due to config change %d",
c.describe(), nodeID, address, ci.ConfigChangeIndex)
} else {
n := c.Nodes[nodeID]
if n.Address != address {
plog.Panicf("changing node address in drummer")
}
}
}
addrMap := make(map[string]struct{})
for _, n := range c.Nodes {
_, ok := addrMap[n.Address]
if ok {
plog.Panicf("duplicated addr on drummer")
} else {
addrMap[n.Address] = struct{}{}
}
}
return false
}