1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/sdk/go/arvados"
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 expectResult balanceResult
56 func (bal *balancerSuite) SetUpSuite(c *check.C) {
57 bal.knownRendezvous = nil
58 for _, str := range []string{
65 for _, c := range []byte(str) {
66 pos, _ := strconv.ParseUint(string(c), 16, 4)
67 slots = append(slots, int(pos))
69 bal.knownRendezvous = append(bal.knownRendezvous, slots)
72 bal.signatureTTL = 3600
75 func (bal *balancerSuite) SetUpTest(c *check.C) {
76 bal.srvs = make([]*KeepService, 16)
77 bal.KeepServices = make(map[string]*KeepService)
78 for i := range bal.srvs {
80 KeepService: arvados.KeepService{
81 UUID: fmt.Sprintf("zzzzz-bi6l4-%015x", i),
84 srv.mounts = []*KeepMount{{
85 KeepMount: arvados.KeepMount{
86 UUID: fmt.Sprintf("zzzzz-mount-%015x", i),
91 bal.KeepServices[srv.UUID] = srv
94 bal.MinMtime = time.Now().UnixNano() - bal.signatureTTL*1e9
98 func (bal *balancerSuite) TestPerfect(c *check.C) {
100 desired: map[string]int{"default": 2},
101 current: slots{0, 1},
106 func (bal *balancerSuite) TestDecreaseRepl(c *check.C) {
108 desired: map[string]int{"default": 2},
109 current: slots{0, 2, 1},
110 shouldTrash: slots{2}})
113 func (bal *balancerSuite) TestDecreaseReplToZero(c *check.C) {
115 desired: map[string]int{"default": 0},
116 current: slots{0, 1, 3},
117 shouldTrash: slots{0, 1, 3}})
120 func (bal *balancerSuite) TestIncreaseRepl(c *check.C) {
122 desired: map[string]int{"default": 4},
123 current: slots{0, 1},
124 shouldPull: slots{2, 3}})
127 func (bal *balancerSuite) TestSkipReadonly(c *check.C) {
128 bal.srvList(0, slots{3})[0].ReadOnly = true
130 desired: map[string]int{"default": 4},
131 current: slots{0, 1},
132 shouldPull: slots{2, 4}})
135 func (bal *balancerSuite) TestFixUnbalanced(c *check.C) {
137 desired: map[string]int{"default": 2},
138 current: slots{2, 0},
139 shouldPull: slots{1}})
141 desired: map[string]int{"default": 2},
142 current: slots{2, 7},
143 shouldPull: slots{0, 1}})
144 // if only one of the pulls succeeds, we'll see this next:
146 desired: map[string]int{"default": 2},
147 current: slots{2, 1, 7},
148 shouldPull: slots{0},
149 shouldTrash: slots{7}})
150 // if both pulls succeed, we'll see this next:
152 desired: map[string]int{"default": 2},
153 current: slots{2, 0, 1, 7},
154 shouldTrash: slots{2, 7}})
156 // unbalanced + excessive replication => pull + trash
158 desired: map[string]int{"default": 2},
159 current: slots{2, 5, 7},
160 shouldPull: slots{0, 1},
161 shouldTrash: slots{7}})
164 func (bal *balancerSuite) TestMultipleReplicasPerService(c *check.C) {
165 for _, srv := range bal.srvs {
166 for i := 0; i < 3; i++ {
167 m := *(srv.mounts[0])
168 srv.mounts = append(srv.mounts, &m)
172 desired: map[string]int{"default": 2},
173 current: slots{0, 0},
174 shouldPull: slots{1}})
176 desired: map[string]int{"default": 2},
177 current: slots{2, 2},
178 shouldPull: slots{0, 1}})
180 desired: map[string]int{"default": 2},
181 current: slots{0, 0, 1},
182 shouldTrash: slots{0}})
184 desired: map[string]int{"default": 2},
185 current: slots{1, 1, 0},
186 shouldTrash: slots{1}})
188 desired: map[string]int{"default": 2},
189 current: slots{1, 0, 1, 0, 2},
190 shouldTrash: slots{0, 1, 2}})
192 desired: map[string]int{"default": 2},
193 current: slots{1, 1, 1, 0, 2},
194 shouldTrash: slots{1, 1, 2}})
196 desired: map[string]int{"default": 2},
197 current: slots{1, 1, 2},
198 shouldPull: slots{0},
199 shouldTrash: slots{1}})
201 desired: map[string]int{"default": 2},
202 current: slots{1, 1, 0},
203 timestamps: []int64{12345678, 12345678, 12345679},
206 desired: map[string]int{"default": 2},
207 current: slots{1, 1},
208 shouldPull: slots{0}})
211 func (bal *balancerSuite) TestIncreaseReplTimestampCollision(c *check.C) {
212 // For purposes of increasing replication, we assume identical
213 // replicas are distinct.
215 desired: map[string]int{"default": 4},
216 current: slots{0, 1},
217 timestamps: []int64{12345678, 12345678},
218 shouldPull: slots{2, 3}})
221 func (bal *balancerSuite) TestDecreaseReplTimestampCollision(c *check.C) {
222 // For purposes of decreasing replication, we assume identical
223 // replicas are NOT distinct.
225 desired: map[string]int{"default": 2},
226 current: slots{0, 1, 2},
227 timestamps: []int64{12345678, 12345678, 12345678}})
229 desired: map[string]int{"default": 2},
230 current: slots{0, 1, 2},
231 timestamps: []int64{12345678, 10000000, 10000000}})
234 func (bal *balancerSuite) TestDecreaseReplBlockTooNew(c *check.C) {
235 oldTime := bal.MinMtime - 3600
236 newTime := bal.MinMtime + 3600
237 // The excess replica is too new to delete.
239 desired: map[string]int{"default": 2},
240 current: slots{0, 1, 2},
241 timestamps: []int64{oldTime, newTime, newTime + 1}})
242 // The best replicas are too new to delete, but the excess
243 // replica is old enough.
245 desired: map[string]int{"default": 2},
246 current: slots{0, 1, 2},
247 timestamps: []int64{newTime, newTime + 1, oldTime},
248 shouldTrash: slots{2}})
251 func (bal *balancerSuite) TestCleanupMounts(c *check.C) {
252 bal.srvs[3].mounts[0].KeepMount.ReadOnly = true
253 bal.srvs[3].mounts[0].KeepMount.DeviceID = "abcdef"
254 bal.srvs[14].mounts[0].KeepMount.DeviceID = "abcdef"
255 c.Check(len(bal.srvs[3].mounts), check.Equals, 1)
257 c.Check(len(bal.srvs[3].mounts), check.Equals, 0)
260 desired: map[string]int{"default": 2},
262 shouldPull: slots{2}})
265 func (bal *balancerSuite) TestVolumeReplication(c *check.C) {
266 bal.srvs[0].mounts[0].KeepMount.Replication = 2 // srv 0
267 bal.srvs[14].mounts[0].KeepMount.Replication = 2 // srv e
269 // block 0 rendezvous is 3,e,a -- so slot 1 has repl=2
272 desired: map[string]int{"default": 2},
274 shouldPull: slots{0}})
277 desired: map[string]int{"default": 2},
278 current: slots{0, 1},
282 desired: map[string]int{"default": 2},
283 current: slots{0, 1, 2},
284 shouldTrash: slots{2}})
287 desired: map[string]int{"default": 3},
288 current: slots{0, 2, 3, 4},
289 shouldPull: slots{1},
290 shouldTrash: slots{4}})
293 desired: map[string]int{"default": 3},
294 current: slots{0, 1, 2, 3, 4},
295 shouldTrash: slots{2, 3, 4}})
298 desired: map[string]int{"default": 4},
299 current: slots{0, 1, 2, 3, 4},
300 shouldTrash: slots{3, 4}})
301 // block 1 rendezvous is 0,9,7 -- so slot 0 has repl=2
304 desired: map[string]int{"default": 2},
308 desired: map[string]int{"default": 3},
310 shouldPull: slots{1}})
313 desired: map[string]int{"default": 4},
315 shouldPull: slots{1, 2}})
318 desired: map[string]int{"default": 4},
320 shouldPull: slots{0, 1}})
323 desired: map[string]int{"default": 4},
325 shouldPull: slots{0, 1, 2}})
328 desired: map[string]int{"default": 2},
329 current: slots{1, 2, 3, 4},
330 shouldPull: slots{0},
331 shouldTrash: slots{3, 4}})
334 desired: map[string]int{"default": 2},
335 current: slots{0, 1, 2},
336 shouldTrash: slots{1, 2}})
339 func (bal *balancerSuite) TestDeviceRWMountedByMultipleServers(c *check.C) {
340 bal.srvs[0].mounts[0].KeepMount.DeviceID = "abcdef"
341 bal.srvs[9].mounts[0].KeepMount.DeviceID = "abcdef"
342 bal.srvs[14].mounts[0].KeepMount.DeviceID = "abcdef"
343 // block 0 belongs on servers 3 and e, which have different
347 desired: map[string]int{"default": 2},
349 shouldPull: slots{0}})
350 // block 1 belongs on servers 0 and 9, which both report
351 // having a replica, but the replicas are on the same device
352 // ID -- so we should pull to the third position (7).
355 desired: map[string]int{"default": 2},
356 current: slots{0, 1},
357 shouldPull: slots{2}})
358 // block 1 can be pulled to the doubly-mounted device, but the
359 // pull should only be done on the first of the two servers.
362 desired: map[string]int{"default": 2},
364 shouldPull: slots{0}})
365 // block 0 has one replica on a single device mounted on two
366 // servers (e,9 at positions 1,9). Trashing the replica on 9
367 // would lose the block.
370 desired: map[string]int{"default": 2},
371 current: slots{1, 9},
372 shouldPull: slots{0},
373 expectResult: balanceResult{
375 classState: map[string]balancedBlockState{"default": {
378 unachievable: false}}}})
379 // block 0 is overreplicated, but the second and third
380 // replicas are the same replica according to DeviceID
381 // (despite different Mtimes). Don't trash the third replica.
384 desired: map[string]int{"default": 2},
385 current: slots{0, 1, 9},
386 expectResult: balanceResult{
388 classState: map[string]balancedBlockState{"default": {
391 unachievable: false}}}})
392 // block 0 is overreplicated; the third and fifth replicas are
393 // extra, but the fourth is another view of the second and
394 // shouldn't be trashed.
397 desired: map[string]int{"default": 2},
398 current: slots{0, 1, 5, 9, 12},
399 shouldTrash: slots{5, 12},
400 expectResult: balanceResult{
402 classState: map[string]balancedBlockState{"default": {
405 unachievable: false}}}})
408 func (bal *balancerSuite) TestChangeStorageClasses(c *check.C) {
409 // For known blocks 0/1/2/3, server 9 is slot 9/1/14/0 in
410 // probe order. For these tests we give it two mounts, one
411 // with classes=[special], one with
412 // classes=[special,special2].
413 bal.srvs[9].mounts = []*KeepMount{{
414 KeepMount: arvados.KeepMount{
416 StorageClasses: []string{"special"},
417 UUID: "zzzzz-mount-special00000009",
418 DeviceID: "9-special",
420 KeepService: bal.srvs[9],
422 KeepMount: arvados.KeepMount{
424 StorageClasses: []string{"special", "special2"},
425 UUID: "zzzzz-mount-special20000009",
426 DeviceID: "9-special-and-special2",
428 KeepService: bal.srvs[9],
430 // For known blocks 0/1/2/3, server 13 (d) is slot 5/3/11/1 in
431 // probe order. We give it two mounts, one with
432 // classes=[special3], one with classes=[default].
433 bal.srvs[13].mounts = []*KeepMount{{
434 KeepMount: arvados.KeepMount{
436 StorageClasses: []string{"special2"},
437 UUID: "zzzzz-mount-special2000000d",
438 DeviceID: "13-special2",
440 KeepService: bal.srvs[13],
442 KeepMount: arvados.KeepMount{
444 StorageClasses: []string{"default"},
445 UUID: "zzzzz-mount-00000000000000d",
446 DeviceID: "13-default",
448 KeepService: bal.srvs[13],
450 // Pull to slot 9 because that's the only server with the
451 // desired class "special".
454 desired: map[string]int{"default": 2, "special": 1},
455 current: slots{0, 1},
456 shouldPull: slots{9},
457 shouldPullMounts: []string{"zzzzz-mount-special00000009"}})
458 // If some storage classes are not satisfied, don't trash any
459 // excess replicas. (E.g., if someone desires repl=1 on
460 // class=durable, and we have two copies on class=volatile, we
461 // should wait for pull to succeed before trashing anything).
464 desired: map[string]int{"special": 1},
465 current: slots{0, 1},
466 shouldPull: slots{9},
467 shouldPullMounts: []string{"zzzzz-mount-special00000009"}})
468 // Once storage classes are satisfied, trash excess replicas
469 // that appear earlier in probe order but aren't needed to
470 // satisfy the desired classes.
473 desired: map[string]int{"special": 1},
474 current: slots{0, 1, 9},
475 shouldTrash: slots{0, 1}})
476 // Pull to slot 5, the best server with class "special2".
479 desired: map[string]int{"special2": 1},
480 current: slots{0, 1},
481 shouldPull: slots{5},
482 shouldPullMounts: []string{"zzzzz-mount-special2000000d"}})
483 // Pull to slot 5 and 9 to get replication 2 in desired class
487 desired: map[string]int{"special2": 2},
488 current: slots{0, 1},
489 shouldPull: slots{5, 9},
490 shouldPullMounts: []string{"zzzzz-mount-special20000009", "zzzzz-mount-special2000000d"}})
491 // Slot 0 has a replica in "default", slot 1 has a replica
492 // in "special"; we need another replica in "default", i.e.,
496 desired: map[string]int{"default": 2, "special": 1},
497 current: slots{0, 1},
498 shouldPull: slots{2}})
499 // Pull to best probe position 0 (despite wrong storage class)
500 // if it's impossible to achieve desired replication in the
501 // desired class (only slots 1 and 3 have special2).
504 desired: map[string]int{"special2": 3},
506 shouldPull: slots{0, 1}})
507 // Trash excess replica.
510 desired: map[string]int{"special": 1},
511 current: slots{0, 1},
512 shouldTrash: slots{1}})
513 // Leave one copy on slot 1 because slot 0 (server 9) only
517 desired: map[string]int{"special": 2},
518 current: slots{0, 1}})
521 // Clear all servers' changesets, balance a single block, and verify
522 // the appropriate changes for that block have been added to the
524 func (bal *balancerSuite) try(c *check.C, t tester) {
525 bal.setupLookupTables()
527 Replicas: bal.replList(t.known, t.current),
530 for i, t := range t.timestamps {
531 blk.Replicas[i].Mtime = t
533 for _, srv := range bal.srvs {
534 srv.ChangeSet = &ChangeSet{}
536 result := bal.balanceBlock(knownBlkid(t.known), blk)
538 var didPull, didTrash slots
539 var didPullMounts, didTrashMounts []string
540 for i, srv := range bal.srvs {
542 for probeOrder, srvNum := range bal.knownRendezvous[t.known] {
547 for _, pull := range srv.Pulls {
548 didPull = append(didPull, slot)
549 didPullMounts = append(didPullMounts, pull.To.UUID)
550 c.Check(pull.SizedDigest, check.Equals, knownBlkid(t.known))
552 for _, trash := range srv.Trashes {
553 didTrash = append(didTrash, slot)
554 didTrashMounts = append(didTrashMounts, trash.From.UUID)
555 c.Check(trash.SizedDigest, check.Equals, knownBlkid(t.known))
559 for _, list := range []slots{didPull, didTrash, t.shouldPull, t.shouldTrash} {
560 sort.Sort(sort.IntSlice(list))
562 c.Check(didPull, check.DeepEquals, t.shouldPull)
563 c.Check(didTrash, check.DeepEquals, t.shouldTrash)
564 if t.shouldPullMounts != nil {
565 sort.Strings(didPullMounts)
566 c.Check(didPullMounts, check.DeepEquals, t.shouldPullMounts)
568 if t.shouldTrashMounts != nil {
569 sort.Strings(didTrashMounts)
570 c.Check(didTrashMounts, check.DeepEquals, t.shouldTrashMounts)
572 if t.expectResult.have > 0 {
573 c.Check(result.have, check.Equals, t.expectResult.have)
575 if t.expectResult.classState != nil {
576 c.Check(result.classState, check.DeepEquals, t.expectResult.classState)
580 // srvList returns the KeepServices, sorted in rendezvous order and
581 // then selected by idx. For example, srvList(3, slots{0, 1, 4})
582 // returns the the first-, second-, and fifth-best servers for storing
583 // bal.knownBlkid(3).
584 func (bal *balancerSuite) srvList(knownBlockID int, order slots) (srvs []*KeepService) {
585 for _, i := range order {
586 srvs = append(srvs, bal.srvs[bal.knownRendezvous[knownBlockID][i]])
591 // replList is like srvList but returns an "existing replicas" slice,
592 // suitable for a BlockState test fixture.
593 func (bal *balancerSuite) replList(knownBlockID int, order slots) (repls []Replica) {
594 nextMnt := map[*KeepService]int{}
595 mtime := time.Now().UnixNano() - (bal.signatureTTL+86400)*1e9
596 for _, srv := range bal.srvList(knownBlockID, order) {
597 // round-robin repls onto each srv's mounts
599 nextMnt[srv] = (n + 1) % len(srv.mounts)
601 repls = append(repls, Replica{srv.mounts[n], mtime})
607 // generate the same data hashes that are tested in
608 // sdk/go/keepclient/root_sorter_test.go
609 func knownBlkid(i int) arvados.SizedDigest {
610 return arvados.SizedDigest(fmt.Sprintf("%x+64", md5.Sum([]byte(fmt.Sprintf("%064x", i)))))