/
placement_checker.go
158 lines (134 loc) · 4.67 KB
/
placement_checker.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
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package algo
import (
"github.com/m3db/m3/src/cluster/placement"
"github.com/m3db/m3/src/cluster/shard"
)
type (
placementChecker struct {
instanceEvaluator instanceEvaluatorFunc
}
shardCheckFunc func(shard.Shard) bool
instanceEvaluatorFunc func(map[string]struct{}, placement.Placement, shardCheckFunc) bool
)
var (
// globalChecker verifies that the specified list of instances are the only instances
// in the given state in the placement.
globalChecker = &placementChecker{
instanceEvaluator: onlyAmongAllInstancesInState,
}
// localChecker verifies that the specified list of instances are in the given state,
// and ignores the state of the rest of instances in the placement.
localChecker = &placementChecker{
instanceEvaluator: instancesInState,
}
)
func (pc *placementChecker) allInitializing(p placement.Placement, instances []string, nowNanos int64) bool {
ids := make(map[string]struct{}, len(instances))
for _, i := range instances {
ids[i] = struct{}{}
}
shardCheckFn := func(s shard.Shard) bool {
return s.State() == shard.Initializing && s.CutoverNanos() > nowNanos
}
return pc.instanceEvaluator(ids, p, shardCheckFn)
}
func (pc *placementChecker) allLeaving(p placement.Placement, instances []placement.Instance, nowNanos int64) bool {
ids := make(map[string]struct{}, len(instances))
for _, i := range instances {
ids[i.ID()] = struct{}{}
}
shardCheckFn := func(s shard.Shard) bool {
return s.State() == shard.Leaving && s.CutoffNanos() > nowNanos
}
return pc.instanceEvaluator(ids, p, shardCheckFn)
}
// allLeavingByIDs is a wrapper of allLeaving that uses string IDs
func (pc *placementChecker) allLeavingByIDs(p placement.Placement, instanceIDs []string, nowNanos int64) bool {
instances := make([]placement.Instance, len(instanceIDs))
for i, id := range instanceIDs {
curr, exists := p.Instance(id)
if !exists {
return false
}
instances[i] = curr
}
return pc.allLeaving(p, instances, nowNanos)
}
func (pc *placementChecker) allAvailable(p placement.Placement, instances []string, nowNanos int64) bool {
ids := make(map[string]struct{}, len(instances))
for _, i := range instances {
ids[i] = struct{}{}
}
shardCheckFn := func(s shard.Shard) bool {
return s.State() == shard.Available && s.CutoffNanos() > nowNanos
}
return pc.instanceEvaluator(ids, p, shardCheckFn)
}
// onlyAmongAllInstancesInState returns true when only the specified instances
// have all their shards in the expected state and the rest of instances
// in the placement do not.
func onlyAmongAllInstancesInState(
instanceIDs map[string]struct{},
p placement.Placement,
forEachShardFn shardCheckFunc,
) bool {
for _, instance := range p.Instances() {
if !instanceCheck(instance, forEachShardFn) {
continue
}
if _, ok := instanceIDs[instance.ID()]; !ok {
return false
}
delete(instanceIDs, instance.ID())
}
return len(instanceIDs) == 0
}
// instancesInState returns true when the specified instances
// have all their shards in the expected state, and ignores the
// rest of instances in the placement.
func instancesInState(
instanceIDs map[string]struct{},
p placement.Placement,
forEachShardFn shardCheckFunc,
) bool {
if len(instanceIDs) == 0 {
return false
}
for id := range instanceIDs {
instance, exist := p.Instance(id)
if !exist {
return false
}
if !instanceCheck(instance, forEachShardFn) {
return false
}
}
return true
}
func instanceCheck(instance placement.Instance, shardCheckFn func(s shard.Shard) bool) bool {
for _, s := range instance.Shards().All() {
if !shardCheckFn(s) {
return false
}
}
return true
}