forked from couchbaselabs/blance
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmoves.go
136 lines (118 loc) · 4.34 KB
/
moves.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
// Copyright 2015-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package blance
// A NodeStateOp associates a node with a state change and operation.
// An array of NodeStateOp's could be interpreted as a series of
// node-by-node state transitions for a partition. For example, for
// partition X, the NodeState transitions might be: first add node A
// to "primary", then demote node B to "replica", then remove (or del)
// partition X from node C.
type NodeStateOp struct {
Node string
State string
Op string // Ex: "add", "del", "promote", "demote".
}
// CalcPartitionMoves computes the step-by-step moves to transition a
// partition from begNodesByState to endNodesByState.
//
// The states is an array of state names, like ["primary",
// "hotStandBy", "coldStandBy"], and should be ordered by more
// superior or important states coming earlier. For example, "primary"
// should come before "replica".
//
// The begNodesByState and endNodesByState are keyed by stateName,
// where the values are an array of node names. For example,
// {"primary": ["a"], "replica": ["b", "c"]}.
//
// The favorMinNodes should be true if the moves should be computed to
// have the partition assigned to the least number of nodes at any
// time (i.e., favoring max of single primary, if even there are
// temporarily no primaries for a time); if false, then the algorithm
// will instead try to assign the partition to 1 or more nodes,
// favoring partition availability across multiple nodes during moves.
func CalcPartitionMoves(
states []string,
begNodesByState map[string][]string,
endNodesByState map[string][]string,
favorMinNodes bool,
) []NodeStateOp {
var moves []NodeStateOp
seen := map[string]bool{}
addMoves := func(nodes []string, state, op string) {
for _, node := range nodes {
if !seen[node] {
seen[node] = true
moves = append(moves, NodeStateOp{node, state, op})
}
}
}
begNodes := flattenNodesByState(begNodesByState)
endNodes := flattenNodesByState(endNodesByState)
adds := StringsRemoveStrings(endNodes, begNodes)
dels := StringsRemoveStrings(begNodes, endNodes)
if !favorMinNodes {
for statei, state := range states {
// Handle promotions of inferiorTo(state) to state.
addMoves(findStateChanges(statei+1, len(states),
state, states, begNodesByState, endNodesByState),
state, "promote")
// Handle demotions of superiorTo(state) to state.
addMoves(findStateChanges(0, statei,
state, states, begNodesByState, endNodesByState),
state, "demote")
// Handle clean additions of state.
addMoves(StringsIntersectStrings(StringsRemoveStrings(
endNodesByState[state], begNodesByState[state]),
adds),
state, "add")
// Handle clean deletions of state.
addMoves(StringsIntersectStrings(StringsRemoveStrings(
begNodesByState[state], endNodesByState[state]),
dels),
"", "del")
}
} else {
for statei := len(states) - 1; statei >= 0; statei-- {
state := states[statei]
// Handle clean deletions of state.
addMoves(StringsIntersectStrings(StringsRemoveStrings(
begNodesByState[state], endNodesByState[state]),
dels),
"", "del")
// Handle demotions of superiorTo(state) to state.
addMoves(findStateChanges(0, statei,
state, states, begNodesByState, endNodesByState),
state, "demote")
// Handle promotions of inferiorTo(state) to state.
addMoves(findStateChanges(statei+1, len(states),
state, states, begNodesByState, endNodesByState),
state, "promote")
// Handle clean additions of state.
addMoves(StringsIntersectStrings(StringsRemoveStrings(
endNodesByState[state], begNodesByState[state]),
adds),
state, "add")
}
}
return moves
}
func findStateChanges(begStateIdx, endStateIdx int,
state string, states []string,
begNodesByState map[string][]string,
endNodesByState map[string][]string) (rv []string) {
for _, node := range endNodesByState[state] {
for i := begStateIdx; i < endStateIdx; i++ {
for _, n := range begNodesByState[states[i]] {
if n == node {
rv = append(rv, node)
}
}
}
}
return rv
}