14714: Uses lib/cmd prometheus registry
[arvados.git] / services / keep-balance / balance_test.go
index 37be185dcc1af9fbc4ebf9f83cd8302ca15b8e6d..e372d37841a7b095cc659216bb11b87b7bc793dd 100644 (file)
@@ -13,7 +13,6 @@ import (
        "time"
 
        "git.curoverse.com/arvados.git/sdk/go/arvados"
-
        check "gopkg.in/check.v1"
 )
 
@@ -132,6 +131,75 @@ func (bal *balancerSuite) TestSkipReadonly(c *check.C) {
                shouldPull: slots{2, 4}})
 }
 
+func (bal *balancerSuite) TestMultipleViewsReadOnly(c *check.C) {
+       bal.testMultipleViews(c, true)
+}
+
+func (bal *balancerSuite) TestMultipleViews(c *check.C) {
+       bal.testMultipleViews(c, false)
+}
+
+func (bal *balancerSuite) testMultipleViews(c *check.C, readonly bool) {
+       for i, srv := range bal.srvs {
+               // Add a mount to each service
+               srv.mounts[0].KeepMount.DeviceID = fmt.Sprintf("writable-by-srv-%x", i)
+               srv.mounts = append(srv.mounts, &KeepMount{
+                       KeepMount: arvados.KeepMount{
+                               DeviceID:    fmt.Sprintf("writable-by-srv-%x", (i+1)%len(bal.srvs)),
+                               UUID:        fmt.Sprintf("zzzzz-mount-%015x", i<<16),
+                               ReadOnly:    readonly,
+                               Replication: 1,
+                       },
+                       KeepService: srv,
+               })
+       }
+       for i := 1; i < len(bal.srvs); i++ {
+               c.Logf("i=%d", i)
+               if i == 4 {
+                       // Timestamps are all different, but one of
+                       // the mounts on srv[4] has the same device ID
+                       // where the non-deletable replica is stored
+                       // on srv[3], so only one replica is safe to
+                       // trash.
+                       bal.try(c, tester{
+                               desired:     map[string]int{"default": 1},
+                               current:     slots{0, i, i},
+                               shouldTrash: slots{i}})
+               } else if readonly {
+                       // Timestamps are all different, and the third
+                       // replica can't be trashed because it's on a
+                       // read-only mount, so the first two replicas
+                       // should be trashed.
+                       bal.try(c, tester{
+                               desired:     map[string]int{"default": 1},
+                               current:     slots{0, i, i},
+                               shouldTrash: slots{0, i}})
+               } else {
+                       // Timestamps are all different, so both
+                       // replicas on the non-optimal server should
+                       // be trashed.
+                       bal.try(c, tester{
+                               desired:     map[string]int{"default": 1},
+                               current:     slots{0, i, i},
+                               shouldTrash: slots{i, i}})
+               }
+               // If the three replicas have identical timestamps,
+               // none of them can be trashed safely.
+               bal.try(c, tester{
+                       desired:    map[string]int{"default": 1},
+                       current:    slots{0, i, i},
+                       timestamps: []int64{12345678, 12345678, 12345678}})
+               // If the first and third replicas have identical
+               // timestamps, only the second replica should be
+               // trashed.
+               bal.try(c, tester{
+                       desired:     map[string]int{"default": 1},
+                       current:     slots{0, i, i},
+                       timestamps:  []int64{12345678, 12345679, 12345678},
+                       shouldTrash: slots{i}})
+       }
+}
+
 func (bal *balancerSuite) TestFixUnbalanced(c *check.C) {
        bal.try(c, tester{
                desired:    map[string]int{"default": 2},
@@ -162,9 +230,10 @@ func (bal *balancerSuite) TestFixUnbalanced(c *check.C) {
 }
 
 func (bal *balancerSuite) TestMultipleReplicasPerService(c *check.C) {
-       for _, srv := range bal.srvs {
+       for s, srv := range bal.srvs {
                for i := 0; i < 3; i++ {
                        m := *(srv.mounts[0])
+                       m.UUID = fmt.Sprintf("zzzzz-mount-%015x", (s<<10)+i)
                        srv.mounts = append(srv.mounts, &m)
                }
        }
@@ -455,7 +524,7 @@ func (bal *balancerSuite) TestChangeStorageClasses(c *check.C) {
        bal.srvs[9].mounts = []*KeepMount{{
                KeepMount: arvados.KeepMount{
                        Replication:    1,
-                       StorageClasses: []string{"special"},
+                       StorageClasses: map[string]bool{"special": true},
                        UUID:           "zzzzz-mount-special00000009",
                        DeviceID:       "9-special",
                },
@@ -463,7 +532,7 @@ func (bal *balancerSuite) TestChangeStorageClasses(c *check.C) {
        }, {
                KeepMount: arvados.KeepMount{
                        Replication:    1,
-                       StorageClasses: []string{"special", "special2"},
+                       StorageClasses: map[string]bool{"special": true, "special2": true},
                        UUID:           "zzzzz-mount-special20000009",
                        DeviceID:       "9-special-and-special2",
                },
@@ -475,7 +544,7 @@ func (bal *balancerSuite) TestChangeStorageClasses(c *check.C) {
        bal.srvs[13].mounts = []*KeepMount{{
                KeepMount: arvados.KeepMount{
                        Replication:    1,
-                       StorageClasses: []string{"special2"},
+                       StorageClasses: map[string]bool{"special2": true},
                        UUID:           "zzzzz-mount-special2000000d",
                        DeviceID:       "13-special2",
                },
@@ -483,7 +552,7 @@ func (bal *balancerSuite) TestChangeStorageClasses(c *check.C) {
        }, {
                KeepMount: arvados.KeepMount{
                        Replication:    1,
-                       StorageClasses: []string{"default"},
+                       StorageClasses: map[string]bool{"default": true},
                        UUID:           "zzzzz-mount-00000000000000d",
                        DeviceID:       "13-default",
                },
@@ -624,7 +693,7 @@ func (bal *balancerSuite) try(c *check.C, t tester) {
 
 // srvList returns the KeepServices, sorted in rendezvous order and
 // then selected by idx. For example, srvList(3, slots{0, 1, 4})
-// returns the the first-, second-, and fifth-best servers for storing
+// returns the first-, second-, and fifth-best servers for storing
 // bal.knownBlkid(3).
 func (bal *balancerSuite) srvList(knownBlockID int, order slots) (srvs []*KeepService) {
        for _, i := range order {