21461: Avoids recreating the WB2 docker image if it already exists.
[arvados.git] / services / keep-balance / block_state_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package keepbalance
6
7 import (
8         "sync"
9         "time"
10
11         "git.arvados.org/arvados.git/sdk/go/arvados"
12         check "gopkg.in/check.v1"
13 )
14
15 var _ = check.Suite(&confirmedReplicationSuite{})
16
17 type confirmedReplicationSuite struct {
18         blockStateMap *BlockStateMap
19         mtime         int64
20 }
21
22 func (s *confirmedReplicationSuite) SetUpTest(c *check.C) {
23         t, _ := time.Parse(time.RFC3339Nano, time.RFC3339Nano)
24         s.mtime = t.UnixNano()
25         s.blockStateMap = NewBlockStateMap()
26         s.blockStateMap.AddReplicas(&KeepMount{KeepMount: arvados.KeepMount{
27                 Replication:    1,
28                 StorageClasses: map[string]bool{"default": true},
29         }}, []arvados.KeepServiceIndexEntry{
30                 {SizedDigest: knownBlkid(10), Mtime: s.mtime},
31         })
32         s.blockStateMap.AddReplicas(&KeepMount{KeepMount: arvados.KeepMount{
33                 Replication:    2,
34                 StorageClasses: map[string]bool{"default": true},
35         }}, []arvados.KeepServiceIndexEntry{
36                 {SizedDigest: knownBlkid(20), Mtime: s.mtime},
37         })
38 }
39
40 func (s *confirmedReplicationSuite) TestZeroReplication(c *check.C) {
41         n := s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(404), knownBlkid(409)}, []string{"default"})
42         c.Check(n, check.Equals, 0)
43         n = s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(10), knownBlkid(404)}, []string{"default"})
44         c.Check(n, check.Equals, 0)
45         n = s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(10), knownBlkid(404)}, nil)
46         c.Check(n, check.Equals, 0)
47 }
48
49 func (s *confirmedReplicationSuite) TestBlocksWithDifferentReplication(c *check.C) {
50         n := s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(10), knownBlkid(20)}, []string{"default"})
51         c.Check(n, check.Equals, 1)
52 }
53
54 func (s *confirmedReplicationSuite) TestBlocksInDifferentClasses(c *check.C) {
55         s.blockStateMap.AddReplicas(&KeepMount{KeepMount: arvados.KeepMount{
56                 Replication:    3,
57                 StorageClasses: map[string]bool{"three": true},
58         }}, []arvados.KeepServiceIndexEntry{
59                 {SizedDigest: knownBlkid(30), Mtime: s.mtime},
60         })
61
62         n := s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(30)}, []string{"three"})
63         c.Check(n, check.Equals, 3)
64         n = s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(20), knownBlkid(30)}, []string{"default"})
65         c.Check(n, check.Equals, 0) // block 30 has repl 0 @ "default"
66         n = s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(20), knownBlkid(30)}, []string{"three"})
67         c.Check(n, check.Equals, 0) // block 20 has repl 0 @ "three"
68         n = s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(20), knownBlkid(30)}, nil)
69         c.Check(n, check.Equals, 2)
70 }
71
72 func (s *confirmedReplicationSuite) TestBlocksOnMultipleMounts(c *check.C) {
73         s.blockStateMap.AddReplicas(&KeepMount{KeepMount: arvados.KeepMount{
74                 Replication:    2,
75                 StorageClasses: map[string]bool{"default": true, "four": true},
76         }}, []arvados.KeepServiceIndexEntry{
77                 {SizedDigest: knownBlkid(40), Mtime: s.mtime},
78                 {SizedDigest: knownBlkid(41), Mtime: s.mtime},
79         })
80         s.blockStateMap.AddReplicas(&KeepMount{KeepMount: arvados.KeepMount{
81                 Replication:    2,
82                 StorageClasses: map[string]bool{"four": true},
83         }}, []arvados.KeepServiceIndexEntry{
84                 {SizedDigest: knownBlkid(40), Mtime: s.mtime},
85                 {SizedDigest: knownBlkid(41), Mtime: s.mtime},
86         })
87         n := s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(40), knownBlkid(41)}, []string{"default"})
88         c.Check(n, check.Equals, 2)
89         n = s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(40), knownBlkid(41)}, []string{"four"})
90         c.Check(n, check.Equals, 4)
91         n = s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(40), knownBlkid(41)}, []string{"default", "four"})
92         c.Check(n, check.Equals, 2)
93         n = s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(40), knownBlkid(41)}, nil)
94         c.Check(n, check.Equals, 4)
95 }
96
97 func (s *confirmedReplicationSuite) TestConcurrency(c *check.C) {
98         var wg sync.WaitGroup
99         for i := 1000; i < 1256; i++ {
100                 i := i
101                 wg.Add(1)
102                 go func() {
103                         defer wg.Done()
104                         n := s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(i), knownBlkid(i)}, []string{"default"})
105                         c.Check(n, check.Equals, 0)
106                 }()
107                 wg.Add(1)
108                 go func() {
109                         defer wg.Done()
110                         n := s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(10)}, []string{"default"})
111                         c.Check(n, check.Equals, 1)
112                         n = s.blockStateMap.GetConfirmedReplication([]arvados.SizedDigest{knownBlkid(20)}, []string{"default"})
113                         c.Check(n, check.Equals, 2)
114                 }()
115         }
116         wg.Wait()
117 }