1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/sdk/go/arvados"
16 "git.arvados.org/arvados.git/sdk/go/ctxlog"
17 check "gopkg.in/check.v1"
21 func Test(t *testing.T) {
25 var _ = check.Suite(&balancerSuite{})
27 type balancerSuite struct {
30 blks map[string]tester
31 knownRendezvous [][]int
36 // index into knownRendezvous
44 desired map[string]int
50 shouldPullMounts []string
51 shouldTrashMounts []string
53 expectBlockState *balancedBlockState
54 expectClassState map[string]balancedBlockState
57 func (bal *balancerSuite) SetUpSuite(c *check.C) {
58 bal.knownRendezvous = nil
59 for _, str := range []string{
66 for _, c := range []byte(str) {
67 pos, _ := strconv.ParseUint(string(c), 16, 4)
68 slots = append(slots, int(pos))
70 bal.knownRendezvous = append(bal.knownRendezvous, slots)
73 bal.signatureTTL = 3600
74 bal.Logger = ctxlog.TestLogger(c)
77 func (bal *balancerSuite) SetUpTest(c *check.C) {
78 bal.srvs = make([]*KeepService, 16)
79 bal.KeepServices = make(map[string]*KeepService)
80 for i := range bal.srvs {
82 KeepService: arvados.KeepService{
83 UUID: fmt.Sprintf("zzzzz-bi6l4-%015x", i),
86 srv.mounts = []*KeepMount{{
87 KeepMount: arvados.KeepMount{
88 UUID: fmt.Sprintf("zzzzz-mount-%015x", i),
89 StorageClasses: map[string]bool{"default": true},
94 bal.KeepServices[srv.UUID] = srv
97 bal.MinMtime = time.Now().UnixNano() - bal.signatureTTL*1e9
101 func (bal *balancerSuite) TestPerfect(c *check.C) {
103 desired: map[string]int{"default": 2},
104 current: slots{0, 1},
107 expectBlockState: &balancedBlockState{
112 func (bal *balancerSuite) TestDecreaseRepl(c *check.C) {
114 desired: map[string]int{"default": 2},
115 current: slots{0, 2, 1},
116 shouldTrash: slots{2},
117 expectBlockState: &balancedBlockState{
123 func (bal *balancerSuite) TestDecreaseReplToZero(c *check.C) {
125 desired: map[string]int{"default": 0},
126 current: slots{0, 1, 3},
127 shouldTrash: slots{0, 1, 3},
128 expectBlockState: &balancedBlockState{
133 func (bal *balancerSuite) TestIncreaseRepl(c *check.C) {
135 desired: map[string]int{"default": 4},
136 current: slots{0, 1},
137 shouldPull: slots{2, 3},
138 expectBlockState: &balancedBlockState{
144 func (bal *balancerSuite) TestSkipReadonly(c *check.C) {
145 bal.srvList(0, slots{3})[0].ReadOnly = true
147 desired: map[string]int{"default": 4},
148 current: slots{0, 1},
149 shouldPull: slots{2, 4},
150 expectBlockState: &balancedBlockState{
156 func (bal *balancerSuite) TestMultipleViewsReadOnly(c *check.C) {
157 bal.testMultipleViews(c, true)
160 func (bal *balancerSuite) TestMultipleViews(c *check.C) {
161 bal.testMultipleViews(c, false)
164 func (bal *balancerSuite) testMultipleViews(c *check.C, readonly bool) {
165 for i, srv := range bal.srvs {
166 // Add a mount to each service
167 srv.mounts[0].KeepMount.DeviceID = fmt.Sprintf("writable-by-srv-%x", i)
168 srv.mounts = append(srv.mounts, &KeepMount{
169 KeepMount: arvados.KeepMount{
170 DeviceID: fmt.Sprintf("writable-by-srv-%x", (i+1)%len(bal.srvs)),
171 UUID: fmt.Sprintf("zzzzz-mount-%015x", i<<16),
174 StorageClasses: map[string]bool{"default": true},
179 for i := 1; i < len(bal.srvs); i++ {
182 // Timestamps are all different, but one of
183 // the mounts on srv[4] has the same device ID
184 // where the non-deletable replica is stored
185 // on srv[3], so only one replica is safe to
188 desired: map[string]int{"default": 1},
189 current: slots{0, i, i},
190 shouldTrash: slots{i}})
192 // Timestamps are all different, and the third
193 // replica can't be trashed because it's on a
194 // read-only mount, so the first two replicas
195 // should be trashed.
197 desired: map[string]int{"default": 1},
198 current: slots{0, i, i},
199 shouldTrash: slots{0, i}})
201 // Timestamps are all different, so both
202 // replicas on the non-optimal server should
205 desired: map[string]int{"default": 1},
206 current: slots{0, i, i},
207 shouldTrash: slots{i, i}})
209 // If the three replicas have identical timestamps,
210 // none of them can be trashed safely.
212 desired: map[string]int{"default": 1},
213 current: slots{0, i, i},
214 timestamps: []int64{12345678, 12345678, 12345678}})
215 // If the first and third replicas have identical
216 // timestamps, only the second replica should be
219 desired: map[string]int{"default": 1},
220 current: slots{0, i, i},
221 timestamps: []int64{12345678, 12345679, 12345678},
222 shouldTrash: slots{i}})
226 func (bal *balancerSuite) TestFixUnbalanced(c *check.C) {
228 desired: map[string]int{"default": 2},
229 current: slots{2, 0},
230 shouldPull: slots{1}})
232 desired: map[string]int{"default": 2},
233 current: slots{2, 7},
234 shouldPull: slots{0, 1}})
235 // if only one of the pulls succeeds, we'll see this next:
237 desired: map[string]int{"default": 2},
238 current: slots{2, 1, 7},
239 shouldPull: slots{0},
240 shouldTrash: slots{7}})
241 // if both pulls succeed, we'll see this next:
243 desired: map[string]int{"default": 2},
244 current: slots{2, 0, 1, 7},
245 shouldTrash: slots{2, 7}})
247 // unbalanced + excessive replication => pull + trash
249 desired: map[string]int{"default": 2},
250 current: slots{2, 5, 7},
251 shouldPull: slots{0, 1},
252 shouldTrash: slots{7}})
255 func (bal *balancerSuite) TestMultipleReplicasPerService(c *check.C) {
256 for s, srv := range bal.srvs {
257 for i := 0; i < 3; i++ {
258 m := *(srv.mounts[0])
259 m.UUID = fmt.Sprintf("zzzzz-mount-%015x", (s<<10)+i)
260 srv.mounts = append(srv.mounts, &m)
264 desired: map[string]int{"default": 2},
265 current: slots{0, 0},
266 shouldPull: slots{1}})
268 desired: map[string]int{"default": 2},
269 current: slots{2, 2},
270 shouldPull: slots{0, 1}})
272 desired: map[string]int{"default": 2},
273 current: slots{0, 0, 1},
274 shouldTrash: slots{0}})
276 desired: map[string]int{"default": 2},
277 current: slots{1, 1, 0},
278 shouldTrash: slots{1}})
280 desired: map[string]int{"default": 2},
281 current: slots{1, 0, 1, 0, 2},
282 shouldTrash: slots{0, 1, 2}})
284 desired: map[string]int{"default": 2},
285 current: slots{1, 1, 1, 0, 2},
286 shouldTrash: slots{1, 1, 2}})
288 desired: map[string]int{"default": 2},
289 current: slots{1, 1, 2},
290 shouldPull: slots{0},
291 shouldTrash: slots{1}})
293 desired: map[string]int{"default": 2},
294 current: slots{1, 1, 0},
295 timestamps: []int64{12345678, 12345678, 12345679},
298 desired: map[string]int{"default": 2},
299 current: slots{1, 1},
300 shouldPull: slots{0}})
303 func (bal *balancerSuite) TestIncreaseReplTimestampCollision(c *check.C) {
304 // For purposes of increasing replication, we assume identical
305 // replicas are distinct.
307 desired: map[string]int{"default": 4},
308 current: slots{0, 1},
309 timestamps: []int64{12345678, 12345678},
310 shouldPull: slots{2, 3}})
313 func (bal *balancerSuite) TestDecreaseReplTimestampCollision(c *check.C) {
314 // For purposes of decreasing replication, we assume identical
315 // replicas are NOT distinct.
317 desired: map[string]int{"default": 2},
318 current: slots{0, 1, 2},
319 timestamps: []int64{12345678, 12345678, 12345678}})
321 desired: map[string]int{"default": 2},
322 current: slots{0, 1, 2},
323 timestamps: []int64{12345678, 10000000, 10000000}})
326 func (bal *balancerSuite) TestDecreaseReplBlockTooNew(c *check.C) {
327 oldTime := bal.MinMtime - 3600
328 newTime := bal.MinMtime + 3600
329 // The excess replica is too new to delete.
331 desired: map[string]int{"default": 2},
332 current: slots{0, 1, 2},
333 timestamps: []int64{oldTime, newTime, newTime + 1},
334 expectBlockState: &balancedBlockState{
338 // The best replicas are too new to delete, but the excess
339 // replica is old enough.
341 desired: map[string]int{"default": 2},
342 current: slots{0, 1, 2},
343 timestamps: []int64{newTime, newTime + 1, oldTime},
344 shouldTrash: slots{2}})
347 func (bal *balancerSuite) TestCleanupMounts(c *check.C) {
348 bal.srvs[3].mounts[0].KeepMount.ReadOnly = true
349 bal.srvs[3].mounts[0].KeepMount.DeviceID = "abcdef"
350 bal.srvs[14].mounts[0].KeepMount.DeviceID = "abcdef"
351 c.Check(len(bal.srvs[3].mounts), check.Equals, 1)
353 c.Check(len(bal.srvs[3].mounts), check.Equals, 0)
356 desired: map[string]int{"default": 2},
358 shouldPull: slots{2}})
361 func (bal *balancerSuite) TestVolumeReplication(c *check.C) {
362 bal.srvs[0].mounts[0].KeepMount.Replication = 2 // srv 0
363 bal.srvs[14].mounts[0].KeepMount.Replication = 2 // srv e
365 // block 0 rendezvous is 3,e,a -- so slot 1 has repl=2
368 desired: map[string]int{"default": 2},
370 shouldPull: slots{0},
371 expectBlockState: &balancedBlockState{
377 desired: map[string]int{"default": 2},
378 current: slots{0, 1},
380 expectBlockState: &balancedBlockState{
385 desired: map[string]int{"default": 2},
386 current: slots{0, 1, 2},
387 shouldTrash: slots{2},
388 expectBlockState: &balancedBlockState{
394 desired: map[string]int{"default": 3},
395 current: slots{0, 2, 3, 4},
396 shouldPull: slots{1},
397 shouldTrash: slots{4},
398 expectBlockState: &balancedBlockState{
405 desired: map[string]int{"default": 3},
406 current: slots{0, 1, 2, 3, 4},
407 shouldTrash: slots{2, 3, 4},
408 expectBlockState: &balancedBlockState{
414 desired: map[string]int{"default": 4},
415 current: slots{0, 1, 2, 3, 4},
416 shouldTrash: slots{3, 4},
417 expectBlockState: &balancedBlockState{
421 // block 1 rendezvous is 0,9,7 -- so slot 0 has repl=2
424 desired: map[string]int{"default": 2},
426 expectBlockState: &balancedBlockState{
431 desired: map[string]int{"default": 3},
433 shouldPull: slots{1},
434 expectBlockState: &balancedBlockState{
440 desired: map[string]int{"default": 4},
442 shouldPull: slots{1, 2},
443 expectBlockState: &balancedBlockState{
449 desired: map[string]int{"default": 4},
451 shouldPull: slots{0, 1},
452 expectBlockState: &balancedBlockState{
458 desired: map[string]int{"default": 4},
460 shouldPull: slots{0, 1, 2},
461 expectBlockState: &balancedBlockState{
467 desired: map[string]int{"default": 2},
468 current: slots{1, 2, 3, 4},
469 shouldPull: slots{0},
470 shouldTrash: slots{3, 4},
471 expectBlockState: &balancedBlockState{
478 desired: map[string]int{"default": 2},
479 current: slots{0, 1, 2},
480 shouldTrash: slots{1, 2},
481 expectBlockState: &balancedBlockState{
487 func (bal *balancerSuite) TestDeviceRWMountedByMultipleServers(c *check.C) {
488 bal.srvs[0].mounts[0].KeepMount.DeviceID = "abcdef"
489 bal.srvs[9].mounts[0].KeepMount.DeviceID = "abcdef"
490 bal.srvs[14].mounts[0].KeepMount.DeviceID = "abcdef"
491 // block 0 belongs on servers 3 and e, which have different
495 desired: map[string]int{"default": 2},
497 shouldPull: slots{0}})
498 // block 1 belongs on servers 0 and 9, which both report
499 // having a replica, but the replicas are on the same device
500 // ID -- so we should pull to the third position (7).
503 desired: map[string]int{"default": 2},
504 current: slots{0, 1},
505 shouldPull: slots{2}})
506 // block 1 can be pulled to the doubly-mounted device, but the
507 // pull should only be done on the first of the two servers.
510 desired: map[string]int{"default": 2},
512 shouldPull: slots{0}})
513 // block 0 has one replica on a single device mounted on two
514 // servers (e,9 at positions 1,9). Trashing the replica on 9
515 // would lose the block.
518 desired: map[string]int{"default": 2},
519 current: slots{1, 9},
520 shouldPull: slots{0},
521 expectBlockState: &balancedBlockState{
525 // block 0 is overreplicated, but the second and third
526 // replicas are the same replica according to DeviceID
527 // (despite different Mtimes). Don't trash the third replica.
530 desired: map[string]int{"default": 2},
531 current: slots{0, 1, 9},
532 expectBlockState: &balancedBlockState{
535 // block 0 is overreplicated; the third and fifth replicas are
536 // extra, but the fourth is another view of the second and
537 // shouldn't be trashed.
540 desired: map[string]int{"default": 2},
541 current: slots{0, 1, 5, 9, 12},
542 shouldTrash: slots{5, 12},
543 expectBlockState: &balancedBlockState{
549 func (bal *balancerSuite) TestChangeStorageClasses(c *check.C) {
550 // For known blocks 0/1/2/3, server 9 is slot 9/1/14/0 in
551 // probe order. For these tests we give it two mounts, one
552 // with classes=[special], one with
553 // classes=[special,special2].
554 bal.srvs[9].mounts = []*KeepMount{{
555 KeepMount: arvados.KeepMount{
557 StorageClasses: map[string]bool{"special": true},
558 UUID: "zzzzz-mount-special00000009",
559 DeviceID: "9-special",
561 KeepService: bal.srvs[9],
563 KeepMount: arvados.KeepMount{
565 StorageClasses: map[string]bool{"special": true, "special2": true},
566 UUID: "zzzzz-mount-special20000009",
567 DeviceID: "9-special-and-special2",
569 KeepService: bal.srvs[9],
571 // For known blocks 0/1/2/3, server 13 (d) is slot 5/3/11/1 in
572 // probe order. We give it two mounts, one with
573 // classes=[special3], one with classes=[default].
574 bal.srvs[13].mounts = []*KeepMount{{
575 KeepMount: arvados.KeepMount{
577 StorageClasses: map[string]bool{"special2": true},
578 UUID: "zzzzz-mount-special2000000d",
579 DeviceID: "13-special2",
581 KeepService: bal.srvs[13],
583 KeepMount: arvados.KeepMount{
585 StorageClasses: map[string]bool{"default": true},
586 UUID: "zzzzz-mount-00000000000000d",
587 DeviceID: "13-default",
589 KeepService: bal.srvs[13],
591 // Pull to slot 9 because that's the only server with the
592 // desired class "special".
595 desired: map[string]int{"default": 2, "special": 1},
596 current: slots{0, 1},
597 shouldPull: slots{9},
598 shouldPullMounts: []string{"zzzzz-mount-special00000009"}})
599 // If some storage classes are not satisfied, don't trash any
600 // excess replicas. (E.g., if someone desires repl=1 on
601 // class=durable, and we have two copies on class=volatile, we
602 // should wait for pull to succeed before trashing anything).
605 desired: map[string]int{"special": 1},
606 current: slots{0, 1},
607 shouldPull: slots{9},
608 shouldPullMounts: []string{"zzzzz-mount-special00000009"}})
609 // Once storage classes are satisfied, trash excess replicas
610 // that appear earlier in probe order but aren't needed to
611 // satisfy the desired classes.
614 desired: map[string]int{"special": 1},
615 current: slots{0, 1, 9},
616 shouldTrash: slots{0, 1}})
617 // Pull to slot 5, the best server with class "special2".
620 desired: map[string]int{"special2": 1},
621 current: slots{0, 1},
622 shouldPull: slots{5},
623 shouldPullMounts: []string{"zzzzz-mount-special2000000d"}})
624 // Pull to slot 5 and 9 to get replication 2 in desired class
628 desired: map[string]int{"special2": 2},
629 current: slots{0, 1},
630 shouldPull: slots{5, 9},
631 shouldPullMounts: []string{"zzzzz-mount-special20000009", "zzzzz-mount-special2000000d"}})
632 // Slot 0 has a replica in "default", slot 1 has a replica
633 // in "special"; we need another replica in "default", i.e.,
637 desired: map[string]int{"default": 2, "special": 1},
638 current: slots{0, 1},
639 shouldPull: slots{2}})
640 // Pull to best probe position 0 (despite wrong storage class)
641 // if it's impossible to achieve desired replication in the
642 // desired class (only slots 1 and 3 have special2).
645 desired: map[string]int{"special2": 3},
647 shouldPull: slots{0, 1}})
648 // Trash excess replica.
651 desired: map[string]int{"special": 1},
652 current: slots{0, 1},
653 shouldTrash: slots{1}})
654 // Leave one copy on slot 1 because slot 0 (server 9) only
658 desired: map[string]int{"special": 2},
659 current: slots{0, 1}})
662 // Clear all servers' changesets, balance a single block, and verify
663 // the appropriate changes for that block have been added to the
665 func (bal *balancerSuite) try(c *check.C, t tester) {
666 bal.setupLookupTables()
668 Replicas: bal.replList(t.known, t.current),
671 for i, t := range t.timestamps {
672 blk.Replicas[i].Mtime = t
674 for _, srv := range bal.srvs {
675 srv.ChangeSet = &ChangeSet{}
677 result := bal.balanceBlock(knownBlkid(t.known), blk)
679 var didPull, didTrash slots
680 var didPullMounts, didTrashMounts []string
681 for i, srv := range bal.srvs {
683 for probeOrder, srvNum := range bal.knownRendezvous[t.known] {
688 for _, pull := range srv.Pulls {
689 didPull = append(didPull, slot)
690 didPullMounts = append(didPullMounts, pull.To.UUID)
691 c.Check(pull.SizedDigest, check.Equals, knownBlkid(t.known))
693 for _, trash := range srv.Trashes {
694 didTrash = append(didTrash, slot)
695 didTrashMounts = append(didTrashMounts, trash.From.UUID)
696 c.Check(trash.SizedDigest, check.Equals, knownBlkid(t.known))
700 for _, list := range []slots{didPull, didTrash, t.shouldPull, t.shouldTrash} {
701 sort.Sort(sort.IntSlice(list))
703 c.Check(didPull, check.DeepEquals, t.shouldPull)
704 c.Check(didTrash, check.DeepEquals, t.shouldTrash)
705 if t.shouldPullMounts != nil {
706 sort.Strings(didPullMounts)
707 c.Check(didPullMounts, check.DeepEquals, t.shouldPullMounts)
709 if t.shouldTrashMounts != nil {
710 sort.Strings(didTrashMounts)
711 c.Check(didTrashMounts, check.DeepEquals, t.shouldTrashMounts)
713 if t.expectBlockState != nil {
714 c.Check(result.blockState, check.Equals, *t.expectBlockState)
716 if t.expectClassState != nil {
717 c.Check(result.classState, check.DeepEquals, t.expectClassState)
721 // srvList returns the KeepServices, sorted in rendezvous order and
722 // then selected by idx. For example, srvList(3, slots{0, 1, 4})
723 // returns the first-, second-, and fifth-best servers for storing
724 // bal.knownBlkid(3).
725 func (bal *balancerSuite) srvList(knownBlockID int, order slots) (srvs []*KeepService) {
726 for _, i := range order {
727 srvs = append(srvs, bal.srvs[bal.knownRendezvous[knownBlockID][i]])
732 // replList is like srvList but returns an "existing replicas" slice,
733 // suitable for a BlockState test fixture.
734 func (bal *balancerSuite) replList(knownBlockID int, order slots) (repls []Replica) {
735 nextMnt := map[*KeepService]int{}
736 mtime := time.Now().UnixNano() - (bal.signatureTTL+86400)*1e9
737 for _, srv := range bal.srvList(knownBlockID, order) {
738 // round-robin repls onto each srv's mounts
740 nextMnt[srv] = (n + 1) % len(srv.mounts)
742 repls = append(repls, Replica{srv.mounts[n], mtime})
748 // generate the same data hashes that are tested in
749 // sdk/go/keepclient/root_sorter_test.go
750 func knownBlkid(i int) arvados.SizedDigest {
751 return arvados.SizedDigest(fmt.Sprintf("%x+64", md5.Sum([]byte(fmt.Sprintf("%064x", i)))))