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: bal.srvs[(i+1)%len(bal.srvs)].mounts[0].KeepMount.DeviceID,
171 UUID: bal.srvs[(i+1)%len(bal.srvs)].mounts[0].KeepMount.UUID,
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.UUID = bal.srvs[3].mounts[0].KeepMount.UUID
351 bal.srvs[14].mounts[0].KeepMount.DeviceID = "abcdef"
352 c.Check(len(bal.srvs[3].mounts), check.Equals, 1)
354 c.Check(len(bal.srvs[3].mounts), check.Equals, 0)
357 desired: map[string]int{"default": 2},
359 shouldPull: slots{2}})
362 func (bal *balancerSuite) TestVolumeReplication(c *check.C) {
363 bal.srvs[0].mounts[0].KeepMount.Replication = 2 // srv 0
364 bal.srvs[14].mounts[0].KeepMount.Replication = 2 // srv e
366 // block 0 rendezvous is 3,e,a -- so slot 1 has repl=2
369 desired: map[string]int{"default": 2},
371 shouldPull: slots{0},
372 expectBlockState: &balancedBlockState{
378 desired: map[string]int{"default": 2},
379 current: slots{0, 1},
381 expectBlockState: &balancedBlockState{
386 desired: map[string]int{"default": 2},
387 current: slots{0, 1, 2},
388 shouldTrash: slots{2},
389 expectBlockState: &balancedBlockState{
395 desired: map[string]int{"default": 3},
396 current: slots{0, 2, 3, 4},
397 shouldPull: slots{1},
398 shouldTrash: slots{4},
399 expectBlockState: &balancedBlockState{
406 desired: map[string]int{"default": 3},
407 current: slots{0, 1, 2, 3, 4},
408 shouldTrash: slots{2, 3, 4},
409 expectBlockState: &balancedBlockState{
415 desired: map[string]int{"default": 4},
416 current: slots{0, 1, 2, 3, 4},
417 shouldTrash: slots{3, 4},
418 expectBlockState: &balancedBlockState{
422 // block 1 rendezvous is 0,9,7 -- so slot 0 has repl=2
425 desired: map[string]int{"default": 2},
427 expectBlockState: &balancedBlockState{
432 desired: map[string]int{"default": 3},
434 shouldPull: slots{1},
435 expectBlockState: &balancedBlockState{
441 desired: map[string]int{"default": 4},
443 shouldPull: slots{1, 2},
444 expectBlockState: &balancedBlockState{
450 desired: map[string]int{"default": 4},
452 shouldPull: slots{0, 1},
453 expectBlockState: &balancedBlockState{
459 desired: map[string]int{"default": 4},
461 shouldPull: slots{0, 1, 2},
462 expectBlockState: &balancedBlockState{
468 desired: map[string]int{"default": 2},
469 current: slots{1, 2, 3, 4},
470 shouldPull: slots{0},
471 shouldTrash: slots{3, 4},
472 expectBlockState: &balancedBlockState{
479 desired: map[string]int{"default": 2},
480 current: slots{0, 1, 2},
481 shouldTrash: slots{1, 2},
482 expectBlockState: &balancedBlockState{
488 func (bal *balancerSuite) TestDeviceRWMountedByMultipleServers(c *check.C) {
489 dupUUID := bal.srvs[0].mounts[0].KeepMount.UUID
490 bal.srvs[9].mounts[0].KeepMount.UUID = dupUUID
491 bal.srvs[14].mounts[0].KeepMount.UUID = dupUUID
492 // block 0 belongs on servers 3 and e, which have different
496 desired: map[string]int{"default": 2},
498 shouldPull: slots{0}})
499 // block 1 belongs on servers 0 and 9, which both report
500 // having a replica, but the replicas are on the same volume
501 // -- so we should pull to the third position (7).
504 desired: map[string]int{"default": 2},
505 current: slots{0, 1},
506 shouldPull: slots{2}})
507 // block 1 can be pulled to the doubly-mounted volume, but the
508 // pull should only be done on the first of the two servers.
511 desired: map[string]int{"default": 2},
513 shouldPull: slots{0}})
514 // block 0 has one replica on a single volume mounted on two
515 // servers (e,9 at positions 1,9). Trashing the replica on 9
516 // would lose the block.
519 desired: map[string]int{"default": 2},
520 current: slots{1, 9},
521 shouldPull: slots{0},
522 expectBlockState: &balancedBlockState{
526 // block 0 is overreplicated, but the second and third
527 // replicas are the same replica according to volume UUID
528 // (despite different Mtimes). Don't trash the third replica.
531 desired: map[string]int{"default": 2},
532 current: slots{0, 1, 9},
533 expectBlockState: &balancedBlockState{
536 // block 0 is overreplicated; the third and fifth replicas are
537 // extra, but the fourth is another view of the second and
538 // shouldn't be trashed.
541 desired: map[string]int{"default": 2},
542 current: slots{0, 1, 5, 9, 12},
543 shouldTrash: slots{5, 12},
544 expectBlockState: &balancedBlockState{
550 func (bal *balancerSuite) TestChangeStorageClasses(c *check.C) {
551 // For known blocks 0/1/2/3, server 9 is slot 9/1/14/0 in
552 // probe order. For these tests we give it two mounts, one
553 // with classes=[special], one with
554 // classes=[special,special2].
555 bal.srvs[9].mounts = []*KeepMount{{
556 KeepMount: arvados.KeepMount{
558 StorageClasses: map[string]bool{"special": true},
559 UUID: "zzzzz-mount-special00000009",
560 DeviceID: "9-special",
562 KeepService: bal.srvs[9],
564 KeepMount: arvados.KeepMount{
566 StorageClasses: map[string]bool{"special": true, "special2": true},
567 UUID: "zzzzz-mount-special20000009",
568 DeviceID: "9-special-and-special2",
570 KeepService: bal.srvs[9],
572 // For known blocks 0/1/2/3, server 13 (d) is slot 5/3/11/1 in
573 // probe order. We give it two mounts, one with
574 // classes=[special3], one with classes=[default].
575 bal.srvs[13].mounts = []*KeepMount{{
576 KeepMount: arvados.KeepMount{
578 StorageClasses: map[string]bool{"special2": true},
579 UUID: "zzzzz-mount-special2000000d",
580 DeviceID: "13-special2",
582 KeepService: bal.srvs[13],
584 KeepMount: arvados.KeepMount{
586 StorageClasses: map[string]bool{"default": true},
587 UUID: "zzzzz-mount-00000000000000d",
588 DeviceID: "13-default",
590 KeepService: bal.srvs[13],
592 // Pull to slot 9 because that's the only server with the
593 // desired class "special".
596 desired: map[string]int{"default": 2, "special": 1},
597 current: slots{0, 1},
598 shouldPull: slots{9},
599 shouldPullMounts: []string{"zzzzz-mount-special20000009"}})
600 // If some storage classes are not satisfied, don't trash any
601 // excess replicas. (E.g., if someone desires repl=1 on
602 // class=durable, and we have two copies on class=volatile, we
603 // should wait for pull to succeed before trashing anything).
606 desired: map[string]int{"special": 1},
607 current: slots{0, 1},
608 shouldPull: slots{9},
609 shouldPullMounts: []string{"zzzzz-mount-special20000009"}})
610 // Once storage classes are satisfied, trash excess replicas
611 // that appear earlier in probe order but aren't needed to
612 // satisfy the desired classes.
615 desired: map[string]int{"special": 1},
616 current: slots{0, 1, 9},
617 shouldTrash: slots{0, 1}})
618 // Pull to slot 5, the best server with class "special2".
621 desired: map[string]int{"special2": 1},
622 current: slots{0, 1},
623 shouldPull: slots{5},
624 shouldPullMounts: []string{"zzzzz-mount-special2000000d"}})
625 // Pull to slot 5 and 9 to get replication 2 in desired class
629 desired: map[string]int{"special2": 2},
630 current: slots{0, 1},
631 shouldPull: slots{5, 9},
632 shouldPullMounts: []string{"zzzzz-mount-special20000009", "zzzzz-mount-special2000000d"}})
633 // Slot 0 has a replica in "default", slot 1 has a replica
634 // in "special"; we need another replica in "default", i.e.,
638 desired: map[string]int{"default": 2, "special": 1},
639 current: slots{0, 1},
640 shouldPull: slots{2}})
641 // Pull to best probe position 0 (despite wrong storage class)
642 // if it's impossible to achieve desired replication in the
643 // desired class (only slots 1 and 3 have special2).
646 desired: map[string]int{"special2": 3},
648 shouldPull: slots{0, 1}})
649 // Trash excess replica.
652 desired: map[string]int{"special": 1},
653 current: slots{0, 1},
654 shouldTrash: slots{1}})
655 // Leave one copy on slot 1 because slot 0 (server 9) only
659 desired: map[string]int{"special": 2},
660 current: slots{0, 1}})
663 // Clear all servers' changesets, balance a single block, and verify
664 // the appropriate changes for that block have been added to the
666 func (bal *balancerSuite) try(c *check.C, t tester) {
667 bal.setupLookupTables()
669 Replicas: bal.replList(t.known, t.current),
672 for i, t := range t.timestamps {
673 blk.Replicas[i].Mtime = t
675 for _, srv := range bal.srvs {
676 srv.ChangeSet = &ChangeSet{}
678 result := bal.balanceBlock(knownBlkid(t.known), blk)
680 var didPull, didTrash slots
681 var didPullMounts, didTrashMounts []string
682 for i, srv := range bal.srvs {
684 for probeOrder, srvNum := range bal.knownRendezvous[t.known] {
689 for _, pull := range srv.Pulls {
690 didPull = append(didPull, slot)
691 didPullMounts = append(didPullMounts, pull.To.UUID)
692 c.Check(pull.SizedDigest, check.Equals, knownBlkid(t.known))
694 for _, trash := range srv.Trashes {
695 didTrash = append(didTrash, slot)
696 didTrashMounts = append(didTrashMounts, trash.From.UUID)
697 c.Check(trash.SizedDigest, check.Equals, knownBlkid(t.known))
701 for _, list := range []slots{didPull, didTrash, t.shouldPull, t.shouldTrash} {
702 sort.Sort(sort.IntSlice(list))
704 c.Check(didPull, check.DeepEquals, t.shouldPull)
705 c.Check(didTrash, check.DeepEquals, t.shouldTrash)
706 if t.shouldPullMounts != nil {
707 sort.Strings(didPullMounts)
708 c.Check(didPullMounts, check.DeepEquals, t.shouldPullMounts)
710 if t.shouldTrashMounts != nil {
711 sort.Strings(didTrashMounts)
712 c.Check(didTrashMounts, check.DeepEquals, t.shouldTrashMounts)
714 if t.expectBlockState != nil {
715 c.Check(result.blockState, check.Equals, *t.expectBlockState)
717 if t.expectClassState != nil {
718 c.Check(result.classState, check.DeepEquals, t.expectClassState)
722 // srvList returns the KeepServices, sorted in rendezvous order and
723 // then selected by idx. For example, srvList(3, slots{0, 1, 4})
724 // returns the first-, second-, and fifth-best servers for storing
725 // bal.knownBlkid(3).
726 func (bal *balancerSuite) srvList(knownBlockID int, order slots) (srvs []*KeepService) {
727 for _, i := range order {
728 srvs = append(srvs, bal.srvs[bal.knownRendezvous[knownBlockID][i]])
733 // replList is like srvList but returns an "existing replicas" slice,
734 // suitable for a BlockState test fixture.
735 func (bal *balancerSuite) replList(knownBlockID int, order slots) (repls []Replica) {
736 nextMnt := map[*KeepService]int{}
737 mtime := time.Now().UnixNano() - (bal.signatureTTL+86400)*1e9
738 for _, srv := range bal.srvList(knownBlockID, order) {
739 // round-robin repls onto each srv's mounts
741 nextMnt[srv] = (n + 1) % len(srv.mounts)
743 repls = append(repls, Replica{srv.mounts[n], mtime})
749 // generate the same data hashes that are tested in
750 // sdk/go/keepclient/root_sorter_test.go
751 func knownBlkid(i int) arvados.SizedDigest {
752 return arvados.SizedDigest(fmt.Sprintf("%x+64", md5.Sum([]byte(fmt.Sprintf("%064x", i)))))