-
Notifications
You must be signed in to change notification settings - Fork 455
/
mirrored.go
569 lines (501 loc) · 16.6 KB
/
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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// 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 algo
import (
"errors"
"fmt"
"strconv"
"github.com/m3db/m3/src/cluster/placement"
"github.com/m3db/m3/src/cluster/shard"
)
var (
errIncompatibleWithMirrorAlgo = errors.New("could not apply mirrored algo on the placement")
)
type mirroredAlgorithm struct {
opts placement.Options
shardedAlgo placement.Algorithm
}
func newMirroredAlgorithm(opts placement.Options) placement.Algorithm {
return mirroredAlgorithm{
opts: opts,
// Mirrored algorithm requires full replacement.
shardedAlgo: newShardedAlgorithm(opts.SetAllowPartialReplace(false)),
}
}
func (a mirroredAlgorithm) IsCompatibleWith(p placement.Placement) error {
if !p.IsMirrored() {
return errIncompatibleWithMirrorAlgo
}
if !p.IsSharded() {
return errIncompatibleWithMirrorAlgo
}
return nil
}
func (a mirroredAlgorithm) InitialPlacement(
instances []placement.Instance,
shards []uint32,
rf int,
) (placement.Placement, error) {
mirrorInstances, err := groupInstancesByShardSetID(instances, rf)
if err != nil {
return nil, err
}
// We use the sharded algorithm to generate a mirror placement with rf equals 1.
mirrorPlacement, err := a.shardedAlgo.InitialPlacement(mirrorInstances, shards, 1)
if err != nil {
return nil, err
}
return placementFromMirror(mirrorPlacement, instances, rf)
}
func (a mirroredAlgorithm) AddReplica(p placement.Placement) (placement.Placement, error) {
// TODO(cw): We could support AddReplica(p placement.Placement, instances []placement.Instance)
// and apply the shards from the new replica to the adding instances in the future.
return nil, errors.New("not supported")
}
func (a mirroredAlgorithm) RemoveInstances(
p placement.Placement,
instanceIDs []string,
) (placement.Placement, error) {
if err := a.IsCompatibleWith(p); err != nil {
return nil, err
}
nowNanos := a.opts.NowFn()().UnixNano()
// If the instances being removed are all the initializing instances in the placement.
// We just need to return these shards back to their sources.
if allInitializing(p, instanceIDs, nowNanos) {
return a.returnInitializingShards(p, instanceIDs)
}
p, _, err := a.MarkAllShardsAvailable(p)
if err != nil {
return nil, err
}
removingInstances := make([]placement.Instance, 0, len(instanceIDs))
for _, id := range instanceIDs {
instance, ok := p.Instance(id)
if !ok {
return nil, fmt.Errorf("instance %s does not exist in the placement", id)
}
removingInstances = append(removingInstances, instance)
}
mirrorPlacement, err := mirrorFromPlacement(p)
if err != nil {
return nil, err
}
mirrorInstances, err := groupInstancesByShardSetID(removingInstances, p.ReplicaFactor())
if err != nil {
return nil, err
}
for _, instance := range mirrorInstances {
if mirrorPlacement, err = a.shardedAlgo.RemoveInstances(
mirrorPlacement,
[]string{instance.ID()},
); err != nil {
return nil, err
}
}
return placementFromMirror(mirrorPlacement, p.Instances(), p.ReplicaFactor())
}
func (a mirroredAlgorithm) AddInstances(
p placement.Placement,
addingInstances []placement.Instance,
) (placement.Placement, error) {
if err := a.IsCompatibleWith(p); err != nil {
return nil, err
}
nowNanos := a.opts.NowFn()().UnixNano()
// If the instances being added are all the leaving instances in the placement.
// We just need to get their shards back.
if allLeaving(p, addingInstances, nowNanos) {
return a.reclaimLeavingShards(p, addingInstances)
}
p, _, err := a.MarkAllShardsAvailable(p)
if err != nil {
return nil, err
}
// At this point, all leaving instances in the placement are cleaned up.
if addingInstances, err = validAddingInstances(p, addingInstances); err != nil {
return nil, err
}
mirrorPlacement, err := mirrorFromPlacement(p)
if err != nil {
return nil, err
}
mirrorInstances, err := groupInstancesByShardSetID(addingInstances, p.ReplicaFactor())
if err != nil {
return nil, err
}
for _, instance := range mirrorInstances {
if mirrorPlacement, err = a.shardedAlgo.AddInstances(
mirrorPlacement,
[]placement.Instance{instance},
); err != nil {
return nil, err
}
}
return placementFromMirror(mirrorPlacement, append(p.Instances(), addingInstances...), p.ReplicaFactor())
}
func (a mirroredAlgorithm) ReplaceInstances(
p placement.Placement,
leavingInstanceIDs []string,
addingInstances []placement.Instance,
) (placement.Placement, error) {
err := a.IsCompatibleWith(p)
if err != nil {
return nil, err
}
if len(addingInstances) != len(leavingInstanceIDs) {
return nil, fmt.Errorf("could not replace %d instances with %d instances for mirrored replace", len(leavingInstanceIDs), len(addingInstances))
}
nowNanos := a.opts.NowFn()().UnixNano()
if allLeaving(p, addingInstances, nowNanos) && allInitializing(p, leavingInstanceIDs, nowNanos) {
if p, err = a.reclaimLeavingShards(p, addingInstances); err != nil {
return nil, err
}
// NB(cw) I don't think we will ever get here, but just being defensive.
return a.returnInitializingShards(p, leavingInstanceIDs)
}
if p, _, err = a.MarkAllShardsAvailable(p); err != nil {
return nil, err
}
// At this point, all leaving instances in the placement are cleaned up.
if addingInstances, err = validAddingInstances(p, addingInstances); err != nil {
return nil, err
}
for i := range leavingInstanceIDs {
// We want full replacement for each instance.
if p, err = a.shardedAlgo.ReplaceInstances(
p,
leavingInstanceIDs[i:i+1],
addingInstances[i:i+1],
); err != nil {
return nil, err
}
}
return p, nil
}
func (a mirroredAlgorithm) MarkShardsAvailable(
p placement.Placement,
instanceID string,
shardIDs ...uint32,
) (placement.Placement, error) {
if err := a.IsCompatibleWith(p); err != nil {
return nil, err
}
return a.shardedAlgo.MarkShardsAvailable(p, instanceID, shardIDs...)
}
func (a mirroredAlgorithm) MarkAllShardsAvailable(
p placement.Placement,
) (placement.Placement, bool, error) {
if err := a.IsCompatibleWith(p); err != nil {
return nil, false, err
}
return a.shardedAlgo.MarkAllShardsAvailable(p)
}
// allInitializing returns true when
// 1: the given list of instances matches all the initializing instances in the placement.
// 2: the shards are not cutover yet.
func allInitializing(p placement.Placement, instances []string, nowNanos int64) bool {
ids := make(map[string]struct{}, len(instances))
for _, i := range instances {
ids[i] = struct{}{}
}
return allInstancesInState(ids, p, func(s shard.Shard) bool {
return s.State() == shard.Initializing && s.CutoverNanos() > nowNanos
})
}
// allLeaving returns true when
// 1: the given list of instances matches all the leaving instances in the placement.
// 2: the shards are not cutoff yet.
func 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{}{}
}
return allInstancesInState(ids, p, func(s shard.Shard) bool {
return s.State() == shard.Leaving && s.CutoffNanos() > nowNanos
})
}
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
}
func allInstancesInState(
instanceIDs map[string]struct{},
p placement.Placement,
forEachShardFn func(s shard.Shard) bool,
) 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
}
// returnInitializingShards tries to return initializing shards on the given instances
// and retries until no more initializing shards could be returned.
func (a mirroredAlgorithm) returnInitializingShards(
p placement.Placement,
instanceIDs []string,
) (placement.Placement, error) {
for {
madeProgess := false
for _, id := range instanceIDs {
_, exist := p.Instance(id)
if !exist {
continue
}
ph, instance, err := newRemoveInstanceHelper(p, id, a.opts)
if err != nil {
return nil, err
}
numInitShards := instance.Shards().NumShardsForState(shard.Initializing)
ph.returnInitializingShards(instance)
if instance.Shards().NumShardsForState(shard.Initializing) < numInitShards {
// Made some progress on returning shards.
madeProgess = true
}
p = ph.generatePlacement()
if instance.Shards().NumShards() > 0 {
p = p.SetInstances(append(p.Instances(), instance))
}
}
if !madeProgess {
break
}
}
for _, id := range instanceIDs {
instance, ok := p.Instance(id)
if !ok {
continue
}
numInitializingShards := instance.Shards().NumShardsForState(shard.Initializing)
if numInitializingShards != 0 {
return nil, fmt.Errorf("there are %d initializing shards could not be returned for instance %s", numInitializingShards, id)
}
}
return p, nil
}
// reclaimLeavingShards tries to reclaim leaving shards on the given instances
// and retries until no more leaving shards could be reclaimed.
func (a mirroredAlgorithm) reclaimLeavingShards(
p placement.Placement,
addingInstances []placement.Instance,
) (placement.Placement, error) {
for {
madeProgess := false
for _, instance := range addingInstances {
ph, instance, err := newAddInstanceHelper(p, instance, a.opts, withAvailableOrLeavingShardsOnly)
if err != nil {
return nil, err
}
numLeavingShards := instance.Shards().NumShardsForState(shard.Leaving)
ph.reclaimLeavingShards(instance)
if instance.Shards().NumShardsForState(shard.Leaving) < numLeavingShards {
// Made some progress on reclaiming shards.
madeProgess = true
}
p = ph.generatePlacement()
}
if !madeProgess {
break
}
}
for _, instance := range addingInstances {
id := instance.ID()
instance, ok := p.Instance(id)
if !ok {
return nil, fmt.Errorf("could not find instance %s in placement after reclaiming leaving shards", id)
}
numLeavingShards := instance.Shards().NumShardsForState(shard.Leaving)
if numLeavingShards != 0 {
return nil, fmt.Errorf("there are %d leaving shards could not be reclaimed for instance %s", numLeavingShards, id)
}
}
return p, nil
}
func validAddingInstances(p placement.Placement, addingInstances []placement.Instance) ([]placement.Instance, error) {
for i, instance := range addingInstances {
if _, exist := p.Instance(instance.ID()); exist {
return nil, fmt.Errorf("instance %s already exist in the placement", instance.ID())
}
if instance.IsLeaving() {
// The instance was leaving in placement, after markAllShardsAsAvailable it is now removed
// from the placement, so we should treat them as fresh new instances.
addingInstances[i] = instance.SetShards(shard.NewShards(nil))
}
}
return addingInstances, nil
}
func groupInstancesByShardSetID(
instances []placement.Instance,
rf int,
) ([]placement.Instance, error) {
var (
shardSetMap = make(map[uint32]*shardSetMetadata, len(instances))
res = make([]placement.Instance, 0, len(instances))
)
for _, instance := range instances {
var (
ssID = instance.ShardSetID()
weight = instance.Weight()
group = instance.IsolationGroup()
shards = instance.Shards()
)
meta, ok := shardSetMap[ssID]
if !ok {
meta = &shardSetMetadata{
weight: weight,
groups: make(map[string]struct{}, rf),
shards: shards,
}
shardSetMap[ssID] = meta
}
if _, ok := meta.groups[group]; ok {
return nil, fmt.Errorf("found duplicated isolation group %s for shardset id %d", group, ssID)
}
if meta.weight != weight {
return nil, fmt.Errorf("found different weights: %d and %d, for shardset id %d", meta.weight, weight, ssID)
}
if !meta.shards.Equals(shards) {
return nil, fmt.Errorf("found different shards: %v and %v, for shardset id %d", meta.shards, shards, ssID)
}
meta.groups[group] = struct{}{}
meta.count++
}
for ssID, meta := range shardSetMap {
if meta.count != rf {
return nil, fmt.Errorf("found %d count of shard set id %d, expecting %d", meta.count, ssID, rf)
}
// NB(cw) The shard set ID should to be assigned in placement service,
// the algorithm does not change the shard set id assigned to each instance.
ssIDStr := strconv.Itoa(int(ssID))
res = append(
res,
placement.NewInstance().
SetID(ssIDStr).
SetIsolationGroup(ssIDStr).
SetWeight(meta.weight).
SetShardSetID(ssID).
SetShards(meta.shards.Clone()),
)
}
return res, nil
}
// mirrorFromPlacement zips all instances with the same shardSetID into a virtual instance
// and create a placement with those virtual instance and rf=1.
func mirrorFromPlacement(p placement.Placement) (placement.Placement, error) {
mirrorInstances, err := groupInstancesByShardSetID(p.Instances(), p.ReplicaFactor())
if err != nil {
return nil, err
}
return placement.NewPlacement().
SetInstances(mirrorInstances).
SetReplicaFactor(1).
SetShards(p.Shards()).
SetCutoverNanos(p.CutoverNanos()).
SetIsSharded(true).
SetIsMirrored(true).
SetMaxShardSetID(p.MaxShardSetID()), nil
}
// placementFromMirror duplicates the shards for each shard set id and assign
// them to the instance with the shard set id.
func placementFromMirror(
mirror placement.Placement,
instances []placement.Instance,
rf int,
) (placement.Placement, error) {
var (
mirrorInstances = mirror.Instances()
shardSetMap = make(map[uint32][]placement.Instance, len(mirrorInstances))
instancesWithShards = make([]placement.Instance, 0, len(instances))
)
for _, instance := range instances {
instances, ok := shardSetMap[instance.ShardSetID()]
if !ok {
instances = make([]placement.Instance, 0, rf)
}
instances = append(instances, instance)
shardSetMap[instance.ShardSetID()] = instances
}
for _, mirrorInstance := range mirrorInstances {
instances, err := instancesFromMirror(mirrorInstance, shardSetMap)
if err != nil {
return nil, err
}
instancesWithShards = append(instancesWithShards, instances...)
}
return placement.NewPlacement().
SetInstances(instancesWithShards).
SetReplicaFactor(rf).
SetShards(mirror.Shards()).
SetCutoverNanos(mirror.CutoverNanos()).
SetIsMirrored(true).
SetIsSharded(true).
SetMaxShardSetID(mirror.MaxShardSetID()), nil
}
func instancesFromMirror(
mirrorInstance placement.Instance,
instancesMap map[uint32][]placement.Instance,
) ([]placement.Instance, error) {
ssID := mirrorInstance.ShardSetID()
instances, ok := instancesMap[ssID]
if !ok {
return nil, fmt.Errorf("could not find shard set id %d in placement", ssID)
}
shards := mirrorInstance.Shards()
for i, instance := range instances {
newShards := make([]shard.Shard, shards.NumShards())
for j, s := range shards.All() {
// TODO move clone() to shard interface
newShard := shard.NewShard(s.ID()).SetState(s.State()).SetCutoffNanos(s.CutoffNanos()).SetCutoverNanos(s.CutoverNanos())
sourceID := s.SourceID()
if sourceID != "" {
// The sourceID in the mirror placement is shardSetID, need to be converted
// to instanceID.
shardSetID, err := strconv.Atoi(sourceID)
if err != nil {
return nil, fmt.Errorf("could not convert source id %s to shard set id", sourceID)
}
sourceInstances, ok := instancesMap[uint32(shardSetID)]
if !ok {
return nil, fmt.Errorf("could not find source id %s in placement", sourceID)
}
sourceID = sourceInstances[i].ID()
}
newShards[j] = newShard.SetSourceID(sourceID)
}
instances[i] = instance.SetShards(shard.NewShards(newShards))
}
return instances, nil
}
type shardSetMetadata struct {
weight uint32
count int
groups map[string]struct{}
shards shard.Shards
}