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
54 func (bal *balancerSuite) SetUpSuite(c *check.C) {
55 bal.knownRendezvous = nil
56 for _, str := range []string{
63 for _, c := range []byte(str) {
64 pos, _ := strconv.ParseUint(string(c), 16, 4)
65 slots = append(slots, int(pos))
67 bal.knownRendezvous = append(bal.knownRendezvous, slots)
70 bal.signatureTTL = 3600
73 func (bal *balancerSuite) SetUpTest(c *check.C) {
74 bal.srvs = make([]*KeepService, 16)
75 bal.KeepServices = make(map[string]*KeepService)
76 for i := range bal.srvs {
78 KeepService: arvados.KeepService{
79 UUID: fmt.Sprintf("zzzzz-bi6l4-%015x", i),
82 srv.mounts = []*KeepMount{{
83 KeepMount: arvados.KeepMount{
84 UUID: fmt.Sprintf("zzzzz-mount-%015x", i),
89 bal.KeepServices[srv.UUID] = srv
92 bal.MinMtime = time.Now().UnixNano() - bal.signatureTTL*1e9
95 func (bal *balancerSuite) TestPerfect(c *check.C) {
97 desired: map[string]int{"default": 2},
103 func (bal *balancerSuite) TestDecreaseRepl(c *check.C) {
105 desired: map[string]int{"default": 2},
106 current: slots{0, 2, 1},
107 shouldTrash: slots{2}})
110 func (bal *balancerSuite) TestDecreaseReplToZero(c *check.C) {
112 desired: map[string]int{"default": 0},
113 current: slots{0, 1, 3},
114 shouldTrash: slots{0, 1, 3}})
117 func (bal *balancerSuite) TestIncreaseRepl(c *check.C) {
119 desired: map[string]int{"default": 4},
120 current: slots{0, 1},
121 shouldPull: slots{2, 3}})
124 func (bal *balancerSuite) TestSkipReadonly(c *check.C) {
125 bal.srvList(0, slots{3})[0].ReadOnly = true
127 desired: map[string]int{"default": 4},
128 current: slots{0, 1},
129 shouldPull: slots{2, 4}})
132 func (bal *balancerSuite) TestFixUnbalanced(c *check.C) {
134 desired: map[string]int{"default": 2},
135 current: slots{2, 0},
136 shouldPull: slots{1}})
138 desired: map[string]int{"default": 2},
139 current: slots{2, 7},
140 shouldPull: slots{0, 1}})
141 // if only one of the pulls succeeds, we'll see this next:
143 desired: map[string]int{"default": 2},
144 current: slots{2, 1, 7},
145 shouldPull: slots{0},
146 shouldTrash: slots{7}})
147 // if both pulls succeed, we'll see this next:
149 desired: map[string]int{"default": 2},
150 current: slots{2, 0, 1, 7},
151 shouldTrash: slots{2, 7}})
153 // unbalanced + excessive replication => pull + trash
155 desired: map[string]int{"default": 2},
156 current: slots{2, 5, 7},
157 shouldPull: slots{0, 1},
158 shouldTrash: slots{7}})
161 func (bal *balancerSuite) TestMultipleReplicasPerService(c *check.C) {
162 for _, srv := range bal.srvs {
163 for i := 0; i < 3; i++ {
164 m := *(srv.mounts[0])
165 srv.mounts = append(srv.mounts, &m)
169 desired: map[string]int{"default": 2},
170 current: slots{0, 0},
171 shouldPull: slots{1}})
173 desired: map[string]int{"default": 2},
174 current: slots{2, 2},
175 shouldPull: slots{0, 1}})
177 desired: map[string]int{"default": 2},
178 current: slots{0, 0, 1},
179 shouldTrash: slots{0}})
181 desired: map[string]int{"default": 2},
182 current: slots{1, 1, 0},
183 shouldTrash: slots{1}})
185 desired: map[string]int{"default": 2},
186 current: slots{1, 0, 1, 0, 2},
187 shouldTrash: slots{0, 1, 2}})
189 desired: map[string]int{"default": 2},
190 current: slots{1, 1, 1, 0, 2},
191 shouldTrash: slots{1, 1, 2}})
193 desired: map[string]int{"default": 2},
194 current: slots{1, 1, 2},
195 shouldPull: slots{0},
196 shouldTrash: slots{1}})
198 desired: map[string]int{"default": 2},
199 current: slots{1, 1, 0},
200 timestamps: []int64{12345678, 12345678, 12345679},
203 desired: map[string]int{"default": 2},
204 current: slots{1, 1},
205 shouldPull: slots{0}})
208 func (bal *balancerSuite) TestIncreaseReplTimestampCollision(c *check.C) {
209 // For purposes of increasing replication, we assume identical
210 // replicas are distinct.
212 desired: map[string]int{"default": 4},
213 current: slots{0, 1},
214 timestamps: []int64{12345678, 12345678},
215 shouldPull: slots{2, 3}})
218 func (bal *balancerSuite) TestDecreaseReplTimestampCollision(c *check.C) {
219 // For purposes of decreasing replication, we assume identical
220 // replicas are NOT distinct.
222 desired: map[string]int{"default": 2},
223 current: slots{0, 1, 2},
224 timestamps: []int64{12345678, 12345678, 12345678}})
226 desired: map[string]int{"default": 2},
227 current: slots{0, 1, 2},
228 timestamps: []int64{12345678, 10000000, 10000000}})
231 func (bal *balancerSuite) TestDecreaseReplBlockTooNew(c *check.C) {
232 oldTime := bal.MinMtime - 3600
233 newTime := bal.MinMtime + 3600
234 // The excess replica is too new to delete.
236 desired: map[string]int{"default": 2},
237 current: slots{0, 1, 2},
238 timestamps: []int64{oldTime, newTime, newTime + 1}})
239 // The best replicas are too new to delete, but the excess
240 // replica is old enough.
242 desired: map[string]int{"default": 2},
243 current: slots{0, 1, 2},
244 timestamps: []int64{newTime, newTime + 1, oldTime},
245 shouldTrash: slots{2}})
248 func (bal *balancerSuite) TestDedupDevices(c *check.C) {
249 bal.srvs[3].mounts[0].KeepMount.ReadOnly = true
250 bal.srvs[3].mounts[0].KeepMount.DeviceID = "abcdef"
251 bal.srvs[14].mounts[0].KeepMount.DeviceID = "abcdef"
252 c.Check(len(bal.srvs[3].mounts), check.Equals, 1)
254 c.Check(len(bal.srvs[3].mounts), check.Equals, 0)
257 desired: map[string]int{"default": 2},
259 shouldPull: slots{2}})
262 func (bal *balancerSuite) TestChangeStorageClasses(c *check.C) {
263 // For known blocks 0/1/2/3, server 9 is slot 9/1/14/0 in
264 // probe order. For these tests we give it two mounts, one
265 // with classes=[special], one with
266 // classes=[special,special2].
267 bal.srvs[9].mounts = []*KeepMount{{
268 KeepMount: arvados.KeepMount{
270 StorageClasses: []string{"special"},
271 UUID: "zzzzz-mount-special00000009",
272 DeviceID: "9-special",
274 KeepService: bal.srvs[9],
276 KeepMount: arvados.KeepMount{
278 StorageClasses: []string{"special", "special2"},
279 UUID: "zzzzz-mount-special20000009",
280 DeviceID: "9-special-and-special2",
282 KeepService: bal.srvs[9],
284 // For known blocks 0/1/2/3, server 13 (d) is slot 5/3/11/1 in
285 // probe order. We give it two mounts, one with
286 // classes=[special3], one with classes=[default].
287 bal.srvs[13].mounts = []*KeepMount{{
288 KeepMount: arvados.KeepMount{
290 StorageClasses: []string{"special2"},
291 UUID: "zzzzz-mount-special2000000d",
292 DeviceID: "13-special2",
294 KeepService: bal.srvs[13],
296 KeepMount: arvados.KeepMount{
298 StorageClasses: []string{"default"},
299 UUID: "zzzzz-mount-00000000000000d",
300 DeviceID: "13-default",
302 KeepService: bal.srvs[13],
304 // Pull to slot 9 because that's the only server with the
305 // desired class "special".
308 desired: map[string]int{"default": 2, "special": 1},
309 current: slots{0, 1},
310 shouldPull: slots{9},
311 shouldPullMounts: []string{"zzzzz-mount-special00000009"}})
312 // If some storage classes are not satisfied, don't trash any
313 // excess replicas. (E.g., if someone desires repl=1 on
314 // class=durable, and we have two copies on class=volatile, we
315 // should wait for pull to succeed before trashing anything).
318 desired: map[string]int{"special": 1},
319 current: slots{0, 1},
320 shouldPull: slots{9},
321 shouldPullMounts: []string{"zzzzz-mount-special00000009"}})
322 // Once storage classes are satisfied, trash excess replicas
323 // that appear earlier in probe order but aren't needed to
324 // satisfy the desired classes.
327 desired: map[string]int{"special": 1},
328 current: slots{0, 1, 9},
329 shouldTrash: slots{0, 1}})
330 // Pull to slot 5, the best server with class "special2".
333 desired: map[string]int{"special2": 1},
334 current: slots{0, 1},
335 shouldPull: slots{5},
336 shouldPullMounts: []string{"zzzzz-mount-special2000000d"}})
337 // Pull to slot 5 and 9 to get replication 2 in desired class
341 desired: map[string]int{"special2": 2},
342 current: slots{0, 1},
343 shouldPull: slots{5, 9},
344 shouldPullMounts: []string{"zzzzz-mount-special20000009", "zzzzz-mount-special2000000d"}})
345 // Slot 0 has a replica in "default", slot 1 has a replica
346 // in "special"; we need another replica in "default", i.e.,
350 desired: map[string]int{"default": 2, "special": 1},
351 current: slots{0, 1},
352 shouldPull: slots{2}})
353 // Pull to best probe position 0 (despite wrong storage class)
354 // if it's impossible to achieve desired replication in the
355 // desired class (only slots 1 and 3 have special2).
358 desired: map[string]int{"special2": 3},
360 shouldPull: slots{0, 1}})
361 // Trash excess replica.
364 desired: map[string]int{"special": 1},
365 current: slots{0, 1},
366 shouldTrash: slots{1}})
367 // Leave one copy on slot 1 because slot 0 (server 9) only
371 desired: map[string]int{"special": 2},
372 current: slots{0, 1}})
375 // Clear all servers' changesets, balance a single block, and verify
376 // the appropriate changes for that block have been added to the
378 func (bal *balancerSuite) try(c *check.C, t tester) {
379 bal.setupLookupTables()
381 Replicas: bal.replList(t.known, t.current),
384 for i, t := range t.timestamps {
385 blk.Replicas[i].Mtime = t
387 for _, srv := range bal.srvs {
388 srv.ChangeSet = &ChangeSet{}
390 bal.balanceBlock(knownBlkid(t.known), blk)
392 var didPull, didTrash slots
393 var didPullMounts, didTrashMounts []string
394 for i, srv := range bal.srvs {
396 for probeOrder, srvNum := range bal.knownRendezvous[t.known] {
401 for _, pull := range srv.Pulls {
402 didPull = append(didPull, slot)
403 didPullMounts = append(didPullMounts, pull.To.UUID)
404 c.Check(pull.SizedDigest, check.Equals, knownBlkid(t.known))
406 for _, trash := range srv.Trashes {
407 didTrash = append(didTrash, slot)
408 didTrashMounts = append(didTrashMounts, trash.From.UUID)
409 c.Check(trash.SizedDigest, check.Equals, knownBlkid(t.known))
413 for _, list := range []slots{didPull, didTrash, t.shouldPull, t.shouldTrash} {
414 sort.Sort(sort.IntSlice(list))
416 c.Check(didPull, check.DeepEquals, t.shouldPull)
417 c.Check(didTrash, check.DeepEquals, t.shouldTrash)
418 if t.shouldPullMounts != nil {
419 sort.Strings(didPullMounts)
420 c.Check(didPullMounts, check.DeepEquals, t.shouldPullMounts)
422 if t.shouldTrashMounts != nil {
423 sort.Strings(didTrashMounts)
424 c.Check(didTrashMounts, check.DeepEquals, t.shouldTrashMounts)
428 // srvList returns the KeepServices, sorted in rendezvous order and
429 // then selected by idx. For example, srvList(3, slots{0, 1, 4})
430 // returns the the first-, second-, and fifth-best servers for storing
431 // bal.knownBlkid(3).
432 func (bal *balancerSuite) srvList(knownBlockID int, order slots) (srvs []*KeepService) {
433 for _, i := range order {
434 srvs = append(srvs, bal.srvs[bal.knownRendezvous[knownBlockID][i]])
439 // replList is like srvList but returns an "existing replicas" slice,
440 // suitable for a BlockState test fixture.
441 func (bal *balancerSuite) replList(knownBlockID int, order slots) (repls []Replica) {
442 nextMnt := map[*KeepService]int{}
443 mtime := time.Now().UnixNano() - (bal.signatureTTL+86400)*1e9
444 for _, srv := range bal.srvList(knownBlockID, order) {
445 // round-robin repls onto each srv's mounts
447 nextMnt[srv] = (n + 1) % len(srv.mounts)
449 repls = append(repls, Replica{srv.mounts[n], mtime})
455 // generate the same data hashes that are tested in
456 // sdk/go/keepclient/root_sorter_test.go
457 func knownBlkid(i int) arvados.SizedDigest {
458 return arvados.SizedDigest(fmt.Sprintf("%x+64", md5.Sum([]byte(fmt.Sprintf("%064x", i)))))