Merge branch '17726-singularity-doc' into main
[arvados.git] / services / keep-balance / balance_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "crypto/md5"
9         "fmt"
10         "sort"
11         "strconv"
12         "testing"
13         "time"
14
15         "git.arvados.org/arvados.git/sdk/go/arvados"
16         "git.arvados.org/arvados.git/sdk/go/ctxlog"
17         check "gopkg.in/check.v1"
18 )
19
20 // Test with Gocheck
21 func Test(t *testing.T) {
22         check.TestingT(t)
23 }
24
25 var _ = check.Suite(&balancerSuite{})
26
27 type balancerSuite struct {
28         Balancer
29         srvs            []*KeepService
30         blks            map[string]tester
31         knownRendezvous [][]int
32         signatureTTL    int64
33 }
34
35 const (
36         // index into knownRendezvous
37         known0 = 0
38 )
39
40 type slots []int
41
42 type tester struct {
43         known       int
44         desired     map[string]int
45         current     slots
46         timestamps  []int64
47         shouldPull  slots
48         shouldTrash slots
49
50         shouldPullMounts  []string
51         shouldTrashMounts []string
52
53         expectBlockState *balancedBlockState
54         expectClassState map[string]balancedBlockState
55 }
56
57 func (bal *balancerSuite) SetUpSuite(c *check.C) {
58         bal.knownRendezvous = nil
59         for _, str := range []string{
60                 "3eab2d5fc9681074",
61                 "097dba52e648f1c3",
62                 "c5b4e023f8a7d691",
63                 "9d81c02e76a3bf54",
64         } {
65                 var slots []int
66                 for _, c := range []byte(str) {
67                         pos, _ := strconv.ParseUint(string(c), 16, 4)
68                         slots = append(slots, int(pos))
69                 }
70                 bal.knownRendezvous = append(bal.knownRendezvous, slots)
71         }
72
73         bal.signatureTTL = 3600
74         bal.Logger = ctxlog.TestLogger(c)
75 }
76
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 {
81                 srv := &KeepService{
82                         KeepService: arvados.KeepService{
83                                 UUID: fmt.Sprintf("zzzzz-bi6l4-%015x", i),
84                         },
85                 }
86                 srv.mounts = []*KeepMount{{
87                         KeepMount: arvados.KeepMount{
88                                 UUID:           fmt.Sprintf("zzzzz-mount-%015x", i),
89                                 StorageClasses: map[string]bool{"default": true},
90                         },
91                         KeepService: srv,
92                 }}
93                 bal.srvs[i] = srv
94                 bal.KeepServices[srv.UUID] = srv
95         }
96
97         bal.MinMtime = time.Now().UnixNano() - bal.signatureTTL*1e9
98         bal.cleanupMounts()
99 }
100
101 func (bal *balancerSuite) TestPerfect(c *check.C) {
102         bal.try(c, tester{
103                 desired:     map[string]int{"default": 2},
104                 current:     slots{0, 1},
105                 shouldPull:  nil,
106                 shouldTrash: nil,
107                 expectBlockState: &balancedBlockState{
108                         needed: 2,
109                 }})
110 }
111
112 func (bal *balancerSuite) TestDecreaseRepl(c *check.C) {
113         bal.try(c, tester{
114                 desired:     map[string]int{"default": 2},
115                 current:     slots{0, 2, 1},
116                 shouldTrash: slots{2},
117                 expectBlockState: &balancedBlockState{
118                         needed:   2,
119                         unneeded: 1,
120                 }})
121 }
122
123 func (bal *balancerSuite) TestDecreaseReplToZero(c *check.C) {
124         bal.try(c, tester{
125                 desired:     map[string]int{"default": 0},
126                 current:     slots{0, 1, 3},
127                 shouldTrash: slots{0, 1, 3},
128                 expectBlockState: &balancedBlockState{
129                         unneeded: 3,
130                 }})
131 }
132
133 func (bal *balancerSuite) TestIncreaseRepl(c *check.C) {
134         bal.try(c, tester{
135                 desired:    map[string]int{"default": 4},
136                 current:    slots{0, 1},
137                 shouldPull: slots{2, 3},
138                 expectBlockState: &balancedBlockState{
139                         needed:  2,
140                         pulling: 2,
141                 }})
142 }
143
144 func (bal *balancerSuite) TestSkipReadonly(c *check.C) {
145         bal.srvList(0, slots{3})[0].ReadOnly = true
146         bal.try(c, tester{
147                 desired:    map[string]int{"default": 4},
148                 current:    slots{0, 1},
149                 shouldPull: slots{2, 4},
150                 expectBlockState: &balancedBlockState{
151                         needed:  2,
152                         pulling: 2,
153                 }})
154 }
155
156 func (bal *balancerSuite) TestMultipleViewsReadOnly(c *check.C) {
157         bal.testMultipleViews(c, true)
158 }
159
160 func (bal *balancerSuite) TestMultipleViews(c *check.C) {
161         bal.testMultipleViews(c, false)
162 }
163
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),
172                                 ReadOnly:       readonly,
173                                 Replication:    1,
174                                 StorageClasses: map[string]bool{"default": true},
175                         },
176                         KeepService: srv,
177                 })
178         }
179         for i := 1; i < len(bal.srvs); i++ {
180                 c.Logf("i=%d", i)
181                 if i == 4 {
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
186                         // trash.
187                         bal.try(c, tester{
188                                 desired:     map[string]int{"default": 1},
189                                 current:     slots{0, i, i},
190                                 shouldTrash: slots{i}})
191                 } else if readonly {
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.
196                         bal.try(c, tester{
197                                 desired:     map[string]int{"default": 1},
198                                 current:     slots{0, i, i},
199                                 shouldTrash: slots{0, i}})
200                 } else {
201                         // Timestamps are all different, so both
202                         // replicas on the non-optimal server should
203                         // be trashed.
204                         bal.try(c, tester{
205                                 desired:     map[string]int{"default": 1},
206                                 current:     slots{0, i, i},
207                                 shouldTrash: slots{i, i}})
208                 }
209                 // If the three replicas have identical timestamps,
210                 // none of them can be trashed safely.
211                 bal.try(c, tester{
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
217                 // trashed.
218                 bal.try(c, tester{
219                         desired:     map[string]int{"default": 1},
220                         current:     slots{0, i, i},
221                         timestamps:  []int64{12345678, 12345679, 12345678},
222                         shouldTrash: slots{i}})
223         }
224 }
225
226 func (bal *balancerSuite) TestFixUnbalanced(c *check.C) {
227         bal.try(c, tester{
228                 desired:    map[string]int{"default": 2},
229                 current:    slots{2, 0},
230                 shouldPull: slots{1}})
231         bal.try(c, tester{
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:
236         bal.try(c, tester{
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:
242         bal.try(c, tester{
243                 desired:     map[string]int{"default": 2},
244                 current:     slots{2, 0, 1, 7},
245                 shouldTrash: slots{2, 7}})
246
247         // unbalanced + excessive replication => pull + trash
248         bal.try(c, tester{
249                 desired:     map[string]int{"default": 2},
250                 current:     slots{2, 5, 7},
251                 shouldPull:  slots{0, 1},
252                 shouldTrash: slots{7}})
253 }
254
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)
261                 }
262         }
263         bal.try(c, tester{
264                 desired:    map[string]int{"default": 2},
265                 current:    slots{0, 0},
266                 shouldPull: slots{1}})
267         bal.try(c, tester{
268                 desired:    map[string]int{"default": 2},
269                 current:    slots{2, 2},
270                 shouldPull: slots{0, 1}})
271         bal.try(c, tester{
272                 desired:     map[string]int{"default": 2},
273                 current:     slots{0, 0, 1},
274                 shouldTrash: slots{0}})
275         bal.try(c, tester{
276                 desired:     map[string]int{"default": 2},
277                 current:     slots{1, 1, 0},
278                 shouldTrash: slots{1}})
279         bal.try(c, tester{
280                 desired:     map[string]int{"default": 2},
281                 current:     slots{1, 0, 1, 0, 2},
282                 shouldTrash: slots{0, 1, 2}})
283         bal.try(c, tester{
284                 desired:     map[string]int{"default": 2},
285                 current:     slots{1, 1, 1, 0, 2},
286                 shouldTrash: slots{1, 1, 2}})
287         bal.try(c, tester{
288                 desired:     map[string]int{"default": 2},
289                 current:     slots{1, 1, 2},
290                 shouldPull:  slots{0},
291                 shouldTrash: slots{1}})
292         bal.try(c, tester{
293                 desired:     map[string]int{"default": 2},
294                 current:     slots{1, 1, 0},
295                 timestamps:  []int64{12345678, 12345678, 12345679},
296                 shouldTrash: nil})
297         bal.try(c, tester{
298                 desired:    map[string]int{"default": 2},
299                 current:    slots{1, 1},
300                 shouldPull: slots{0}})
301 }
302
303 func (bal *balancerSuite) TestIncreaseReplTimestampCollision(c *check.C) {
304         // For purposes of increasing replication, we assume identical
305         // replicas are distinct.
306         bal.try(c, tester{
307                 desired:    map[string]int{"default": 4},
308                 current:    slots{0, 1},
309                 timestamps: []int64{12345678, 12345678},
310                 shouldPull: slots{2, 3}})
311 }
312
313 func (bal *balancerSuite) TestDecreaseReplTimestampCollision(c *check.C) {
314         // For purposes of decreasing replication, we assume identical
315         // replicas are NOT distinct.
316         bal.try(c, tester{
317                 desired:    map[string]int{"default": 2},
318                 current:    slots{0, 1, 2},
319                 timestamps: []int64{12345678, 12345678, 12345678}})
320         bal.try(c, tester{
321                 desired:    map[string]int{"default": 2},
322                 current:    slots{0, 1, 2},
323                 timestamps: []int64{12345678, 10000000, 10000000}})
324 }
325
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.
330         bal.try(c, tester{
331                 desired:    map[string]int{"default": 2},
332                 current:    slots{0, 1, 2},
333                 timestamps: []int64{oldTime, newTime, newTime + 1},
334                 expectBlockState: &balancedBlockState{
335                         needed:   2,
336                         unneeded: 1,
337                 }})
338         // The best replicas are too new to delete, but the excess
339         // replica is old enough.
340         bal.try(c, tester{
341                 desired:     map[string]int{"default": 2},
342                 current:     slots{0, 1, 2},
343                 timestamps:  []int64{newTime, newTime + 1, oldTime},
344                 shouldTrash: slots{2}})
345 }
346
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)
352         bal.cleanupMounts()
353         c.Check(len(bal.srvs[3].mounts), check.Equals, 0)
354         bal.try(c, tester{
355                 known:      0,
356                 desired:    map[string]int{"default": 2},
357                 current:    slots{1},
358                 shouldPull: slots{2}})
359 }
360
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
364         bal.cleanupMounts()
365         // block 0 rendezvous is 3,e,a -- so slot 1 has repl=2
366         bal.try(c, tester{
367                 known:      0,
368                 desired:    map[string]int{"default": 2},
369                 current:    slots{1},
370                 shouldPull: slots{0},
371                 expectBlockState: &balancedBlockState{
372                         needed:  1,
373                         pulling: 1,
374                 }})
375         bal.try(c, tester{
376                 known:      0,
377                 desired:    map[string]int{"default": 2},
378                 current:    slots{0, 1},
379                 shouldPull: nil,
380                 expectBlockState: &balancedBlockState{
381                         needed: 2,
382                 }})
383         bal.try(c, tester{
384                 known:       0,
385                 desired:     map[string]int{"default": 2},
386                 current:     slots{0, 1, 2},
387                 shouldTrash: slots{2},
388                 expectBlockState: &balancedBlockState{
389                         needed:   2,
390                         unneeded: 1,
391                 }})
392         bal.try(c, tester{
393                 known:       0,
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{
399                         needed:   3,
400                         unneeded: 1,
401                         pulling:  1,
402                 }})
403         bal.try(c, tester{
404                 known:       0,
405                 desired:     map[string]int{"default": 3},
406                 current:     slots{0, 1, 2, 3, 4},
407                 shouldTrash: slots{2, 3, 4},
408                 expectBlockState: &balancedBlockState{
409                         needed:   2,
410                         unneeded: 3,
411                 }})
412         bal.try(c, tester{
413                 known:       0,
414                 desired:     map[string]int{"default": 4},
415                 current:     slots{0, 1, 2, 3, 4},
416                 shouldTrash: slots{3, 4},
417                 expectBlockState: &balancedBlockState{
418                         needed:   3,
419                         unneeded: 2,
420                 }})
421         // block 1 rendezvous is 0,9,7 -- so slot 0 has repl=2
422         bal.try(c, tester{
423                 known:   1,
424                 desired: map[string]int{"default": 2},
425                 current: slots{0},
426                 expectBlockState: &balancedBlockState{
427                         needed: 1,
428                 }})
429         bal.try(c, tester{
430                 known:      1,
431                 desired:    map[string]int{"default": 3},
432                 current:    slots{0},
433                 shouldPull: slots{1},
434                 expectBlockState: &balancedBlockState{
435                         needed:  1,
436                         pulling: 1,
437                 }})
438         bal.try(c, tester{
439                 known:      1,
440                 desired:    map[string]int{"default": 4},
441                 current:    slots{0},
442                 shouldPull: slots{1, 2},
443                 expectBlockState: &balancedBlockState{
444                         needed:  1,
445                         pulling: 2,
446                 }})
447         bal.try(c, tester{
448                 known:      1,
449                 desired:    map[string]int{"default": 4},
450                 current:    slots{2},
451                 shouldPull: slots{0, 1},
452                 expectBlockState: &balancedBlockState{
453                         needed:  1,
454                         pulling: 2,
455                 }})
456         bal.try(c, tester{
457                 known:      1,
458                 desired:    map[string]int{"default": 4},
459                 current:    slots{7},
460                 shouldPull: slots{0, 1, 2},
461                 expectBlockState: &balancedBlockState{
462                         needed:  1,
463                         pulling: 3,
464                 }})
465         bal.try(c, tester{
466                 known:       1,
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{
472                         needed:   2,
473                         unneeded: 2,
474                         pulling:  1,
475                 }})
476         bal.try(c, tester{
477                 known:       1,
478                 desired:     map[string]int{"default": 2},
479                 current:     slots{0, 1, 2},
480                 shouldTrash: slots{1, 2},
481                 expectBlockState: &balancedBlockState{
482                         needed:   1,
483                         unneeded: 2,
484                 }})
485 }
486
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
492         // device IDs.
493         bal.try(c, tester{
494                 known:      0,
495                 desired:    map[string]int{"default": 2},
496                 current:    slots{1},
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).
501         bal.try(c, tester{
502                 known:      1,
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.
508         bal.try(c, tester{
509                 known:      1,
510                 desired:    map[string]int{"default": 2},
511                 current:    slots{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.
516         bal.try(c, tester{
517                 known:      0,
518                 desired:    map[string]int{"default": 2},
519                 current:    slots{1, 9},
520                 shouldPull: slots{0},
521                 expectBlockState: &balancedBlockState{
522                         needed:  1,
523                         pulling: 1,
524                 }})
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.
528         bal.try(c, tester{
529                 known:   0,
530                 desired: map[string]int{"default": 2},
531                 current: slots{0, 1, 9},
532                 expectBlockState: &balancedBlockState{
533                         needed: 2,
534                 }})
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.
538         bal.try(c, tester{
539                 known:       0,
540                 desired:     map[string]int{"default": 2},
541                 current:     slots{0, 1, 5, 9, 12},
542                 shouldTrash: slots{5, 12},
543                 expectBlockState: &balancedBlockState{
544                         needed:   2,
545                         unneeded: 2,
546                 }})
547 }
548
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{
556                         Replication:    1,
557                         StorageClasses: map[string]bool{"special": true},
558                         UUID:           "zzzzz-mount-special00000009",
559                         DeviceID:       "9-special",
560                 },
561                 KeepService: bal.srvs[9],
562         }, {
563                 KeepMount: arvados.KeepMount{
564                         Replication:    1,
565                         StorageClasses: map[string]bool{"special": true, "special2": true},
566                         UUID:           "zzzzz-mount-special20000009",
567                         DeviceID:       "9-special-and-special2",
568                 },
569                 KeepService: bal.srvs[9],
570         }}
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{
576                         Replication:    1,
577                         StorageClasses: map[string]bool{"special2": true},
578                         UUID:           "zzzzz-mount-special2000000d",
579                         DeviceID:       "13-special2",
580                 },
581                 KeepService: bal.srvs[13],
582         }, {
583                 KeepMount: arvados.KeepMount{
584                         Replication:    1,
585                         StorageClasses: map[string]bool{"default": true},
586                         UUID:           "zzzzz-mount-00000000000000d",
587                         DeviceID:       "13-default",
588                 },
589                 KeepService: bal.srvs[13],
590         }}
591         // Pull to slot 9 because that's the only server with the
592         // desired class "special".
593         bal.try(c, tester{
594                 known:            0,
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).
603         bal.try(c, tester{
604                 known:            0,
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.
612         bal.try(c, tester{
613                 known:       0,
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".
618         bal.try(c, tester{
619                 known:            0,
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
625         // "special2".
626         bal.try(c, tester{
627                 known:            0,
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.,
634         // on slot 2.
635         bal.try(c, tester{
636                 known:      1,
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).
643         bal.try(c, tester{
644                 known:      1,
645                 desired:    map[string]int{"special2": 3},
646                 current:    slots{3},
647                 shouldPull: slots{0, 1}})
648         // Trash excess replica.
649         bal.try(c, tester{
650                 known:       3,
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
655         // gives us repl=1.
656         bal.try(c, tester{
657                 known:   3,
658                 desired: map[string]int{"special": 2},
659                 current: slots{0, 1}})
660 }
661
662 // Clear all servers' changesets, balance a single block, and verify
663 // the appropriate changes for that block have been added to the
664 // changesets.
665 func (bal *balancerSuite) try(c *check.C, t tester) {
666         bal.setupLookupTables()
667         blk := &BlockState{
668                 Replicas: bal.replList(t.known, t.current),
669                 Desired:  t.desired,
670         }
671         for i, t := range t.timestamps {
672                 blk.Replicas[i].Mtime = t
673         }
674         for _, srv := range bal.srvs {
675                 srv.ChangeSet = &ChangeSet{}
676         }
677         result := bal.balanceBlock(knownBlkid(t.known), blk)
678
679         var didPull, didTrash slots
680         var didPullMounts, didTrashMounts []string
681         for i, srv := range bal.srvs {
682                 var slot int
683                 for probeOrder, srvNum := range bal.knownRendezvous[t.known] {
684                         if srvNum == i {
685                                 slot = probeOrder
686                         }
687                 }
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))
692                 }
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))
697                 }
698         }
699
700         for _, list := range []slots{didPull, didTrash, t.shouldPull, t.shouldTrash} {
701                 sort.Sort(sort.IntSlice(list))
702         }
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)
708         }
709         if t.shouldTrashMounts != nil {
710                 sort.Strings(didTrashMounts)
711                 c.Check(didTrashMounts, check.DeepEquals, t.shouldTrashMounts)
712         }
713         if t.expectBlockState != nil {
714                 c.Check(result.blockState, check.Equals, *t.expectBlockState)
715         }
716         if t.expectClassState != nil {
717                 c.Check(result.classState, check.DeepEquals, t.expectClassState)
718         }
719 }
720
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]])
728         }
729         return
730 }
731
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
739                 n := nextMnt[srv]
740                 nextMnt[srv] = (n + 1) % len(srv.mounts)
741
742                 repls = append(repls, Replica{srv.mounts[n], mtime})
743                 mtime++
744         }
745         return
746 }
747
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)))))
752 }