-
Notifications
You must be signed in to change notification settings - Fork 453
/
non_mirrored.go
312 lines (274 loc) · 9.68 KB
/
non_mirrored.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
// Copyright (c) 2017 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 selector
import (
"errors"
"fmt"
"sort"
"github.com/m3db/m3/src/cluster/placement"
"github.com/m3db/m3/src/cluster/placement/algo"
)
var (
errInstanceAbsent = errors.New("could not remove or replace a instance that does not exist")
errNoValidInstance = errors.New("no valid instance in the candidate list")
)
type nonMirroredSelector struct {
opts placement.Options
}
func newNonMirroredSelector(opts placement.Options) placement.InstanceSelector {
return &nonMirroredSelector{opts: opts}
}
func (f *nonMirroredSelector) SelectInitialInstances(
candidates []placement.Instance,
rf int,
) ([]placement.Instance, error) {
return getValidCandidates(placement.NewPlacement(), candidates, f.opts)
}
func (f *nonMirroredSelector) SelectAddingInstances(
candidates []placement.Instance,
p placement.Placement,
) ([]placement.Instance, error) {
candidates, err := getValidCandidates(p, candidates, f.opts)
if err != nil {
return nil, err
}
if f.opts.AddAllCandidates() {
return candidates, nil
}
instance, err := selectSingleCandidate(candidates, p)
if err != nil {
return nil, err
}
return []placement.Instance{instance}, nil
}
func (f *nonMirroredSelector) SelectReplaceInstances(
candidates []placement.Instance,
leavingInstanceIDs []string,
p placement.Placement,
) ([]placement.Instance, error) {
candidates, err := getValidCandidates(p, candidates, f.opts)
if err != nil {
return nil, err
}
leavingInstances := make([]placement.Instance, 0, len(leavingInstanceIDs))
for _, id := range leavingInstanceIDs {
leavingInstance, exist := p.Instance(id)
if !exist {
return nil, errInstanceAbsent
}
leavingInstances = append(leavingInstances, leavingInstance)
}
if f.opts.AddAllCandidates() {
if err := f.validateReplaceInstances(candidates, leavingInstances); err != nil {
return nil, err
}
return candidates, nil
}
return f.selectReplaceInstances(candidates, leavingInstances, p)
}
// selectReplaceInstances returns a sufficient number of instances to replace the weight
// of the leaving instances.
func (f *nonMirroredSelector) selectReplaceInstances(
candidates, leavingInstances []placement.Instance,
p placement.Placement,
) ([]placement.Instance, error) {
// Map isolation group to instances.
candidateGroups := buildIsolationGroupMap(candidates)
// Sort the candidate instances by the number of conflicts.
ph := algo.NewPlacementHelper(p, f.opts)
instances := make([]sortableValue, 0, len(candidateGroups))
for group, instancesInGroup := range candidateGroups {
conflicts := 0
for _, leaving := range leavingInstances {
for _, s := range leaving.Shards().All() {
if !ph.CanMoveShard(s.ID(), leaving, group) {
conflicts++
}
}
}
for _, instance := range instancesInGroup {
instances = append(instances, sortableValue{value: instance, weight: conflicts})
}
}
groups := groupInstancesByConflict(instances, f.opts)
if len(groups) == 0 {
return nil, errNoValidInstance
}
var totalWeight uint32
for _, instance := range leavingInstances {
totalWeight += instance.Weight()
}
result, leftWeight := fillWeight(groups, int(totalWeight))
if leftWeight > 0 && !f.opts.AllowPartialReplace() {
return nil, fmt.Errorf("could not find enough instances to replace %v, %d weight could not be replaced",
leavingInstances, leftWeight)
}
return result, nil
}
func (f *nonMirroredSelector) validateReplaceInstances(
candidates, leavingInstances []placement.Instance,
) error {
var leavingWeight int
for _, instance := range leavingInstances {
leavingWeight += int(instance.Weight())
}
var candidateWeight int
for _, instance := range candidates {
candidateWeight += int(instance.Weight())
}
if leavingWeight > candidateWeight && !f.opts.AllowPartialReplace() {
return fmt.Errorf("could not find enough instances to replace %v, %d weight could not be replaced",
leavingInstances, leavingWeight)
}
return nil
}
func groupInstancesByConflict(instancesSortedByConflicts []sortableValue, opts placement.Options) [][]placement.Instance {
allowConflict := opts.AllowPartialReplace()
sort.Sort(sortableValues(instancesSortedByConflicts))
var groups [][]placement.Instance
lastSeenConflict := -1
for _, instance := range instancesSortedByConflicts {
if !allowConflict && instance.weight > 0 {
break
}
if instance.weight > lastSeenConflict {
lastSeenConflict = instance.weight
groups = append(groups, []placement.Instance{})
}
if lastSeenConflict == instance.weight {
groups[len(groups)-1] = append(groups[len(groups)-1], instance.value.(placement.Instance))
}
}
return groups
}
func fillWeight(groups [][]placement.Instance, targetWeight int) ([]placement.Instance, int) {
var (
result []placement.Instance
instancesInGroup []placement.Instance
)
for _, group := range groups {
sort.Sort(placement.ByIDAscending(group))
instancesInGroup, targetWeight = knapsack(group, targetWeight)
result = append(result, instancesInGroup...)
if targetWeight <= 0 {
break
}
}
return result, targetWeight
}
func knapsack(instances []placement.Instance, targetWeight int) ([]placement.Instance, int) {
totalWeight := 0
for _, instance := range instances {
totalWeight += int(instance.Weight())
}
if totalWeight <= targetWeight {
return instances[:], targetWeight - totalWeight
}
// totalWeight > targetWeight, there is a combination of instances to meet targetWeight for sure
// we do dp until totalWeight rather than targetWeight here because we need to
// at least cover the targetWeight, which is a little bit different than the knapsack problem
weights := make([]int, totalWeight+1)
combination := make([][]placement.Instance, totalWeight+1)
// dp: weights[i][j] = max(weights[i-1][j], weights[i-1][j-instance.Weight] + instance.Weight)
// when there are multiple combination to reach a target weight, we prefer the one with less instances
for i := range instances {
weight := int(instances[i].Weight())
// this loop needs to go from len to 1 because weights is being updated in place
for j := totalWeight; j >= 1; j-- {
if j-weight < 0 {
continue
}
newWeight := weights[j-weight] + weight
if newWeight > weights[j] {
weights[j] = weights[j-weight] + weight
combination[j] = append(combination[j-weight], instances[i])
} else if newWeight == weights[j] {
// if can reach same weight, find a combination with less instances
if len(combination[j-weight])+1 < len(combination[j]) {
combination[j] = append(combination[j-weight], instances[i])
}
}
}
}
for i := targetWeight; i <= totalWeight; i++ {
if weights[i] >= targetWeight {
return combination[i], targetWeight - weights[i]
}
}
panic("should never reach here")
}
func buildIsolationGroupMap(candidates []placement.Instance) map[string][]placement.Instance {
result := make(map[string][]placement.Instance, len(candidates))
for _, instance := range candidates {
if _, exist := result[instance.IsolationGroup()]; !exist {
result[instance.IsolationGroup()] = make([]placement.Instance, 0)
}
result[instance.IsolationGroup()] = append(result[instance.IsolationGroup()], instance)
}
return result
}
func selectSingleCandidate(
candidates []placement.Instance,
p placement.Placement,
) (placement.Instance, error) {
candidateGroups := buildIsolationGroupMap(candidates)
existingGroups := buildIsolationGroupMap(p.Instances())
// If there is an isolation group not in the current placement, prefer the isolation group.
for r, instances := range candidateGroups {
if _, exist := existingGroups[r]; !exist {
// All the isolation groups have at least 1 instance.
return instances[0], nil
}
}
// Otherwise sort the isolation groups in the current placement
// by capacity and find a instance from least sized isolation group.
groups := make(sortableValues, 0, len(existingGroups))
for group, instances := range existingGroups {
weight := 0
for _, i := range instances {
weight += int(i.Weight())
}
groups = append(groups, sortableValue{value: group, weight: weight})
}
sort.Sort(groups)
for _, group := range groups {
if i, exist := candidateGroups[group.value.(string)]; exist {
for _, instance := range i {
return instance, nil
}
}
}
// no instance in the candidate instances can be added to the placement
return nil, errNoValidInstance
}
type sortableValue struct {
value interface{}
weight int
}
type sortableValues []sortableValue
func (values sortableValues) Len() int {
return len(values)
}
func (values sortableValues) Less(i, j int) bool {
return values[i].weight < values[j].weight
}
func (values sortableValues) Swap(i, j int) {
values[i], values[j] = values[j], values[i]
}