4 "git.curoverse.com/arvados.git/sdk/go/blockdigest"
5 "git.curoverse.com/arvados.git/services/datamanager/collection"
6 "git.curoverse.com/arvados.git/services/datamanager/keep"
11 func BlockSetFromSlice(digests []int) (bs BlockSet) {
13 for _, digest := range digests {
14 bs.Insert(blockdigest.MakeTestBlockDigest(digest))
19 func CollectionIndexSetFromSlice(indices []int) (cis CollectionIndexSet) {
20 cis = make(CollectionIndexSet)
21 for _, index := range indices {
27 func (cis CollectionIndexSet) ToSlice() (ints []int) {
28 ints = make([]int, len(cis))
30 for collectionIndex := range cis {
31 ints[i] = collectionIndex
37 func VerifyToCollectionIndexSet(
40 blockToCollectionIndices map[int][]int,
41 expectedCollections []int) {
43 expected := CollectionIndexSetFromSlice(expectedCollections)
45 rc := collection.ReadCollections{
46 BlockToCollectionIndices: map[blockdigest.BlockDigest][]int{},
48 for digest, indices := range blockToCollectionIndices {
49 rc.BlockToCollectionIndices[blockdigest.MakeTestBlockDigest(digest)] = indices
52 returned := make(CollectionIndexSet)
53 BlockSetFromSlice(blocks).ToCollectionIndexSet(rc, &returned)
55 if !reflect.DeepEqual(returned, expected) {
56 t.Errorf("Expected %v.ToCollectionIndexSet(%v) to return \n %v \n but instead received \n %v",
58 blockToCollectionIndices,
64 func TestToCollectionIndexSet(t *testing.T) {
65 VerifyToCollectionIndexSet(t, []int{4}, map[int][]int{4:[]int{1}}, []int{1})
66 VerifyToCollectionIndexSet(t, []int{4}, map[int][]int{4:[]int{1,9}}, []int{1,9})
67 VerifyToCollectionIndexSet(t, []int{5,6},
68 map[int][]int{5:[]int{2,3}, 6:[]int{3,4}},
73 func TestSimpleSummary(t *testing.T) {
74 rc := collection.MakeTestReadCollections([]collection.TestCollectionSpec{
75 collection.TestCollectionSpec{
83 // The internals aren't actually examined, so we can reuse the same one.
84 dummyBlockServerInfo := keep.BlockServerInfo{}
86 blockDigest1 := blockdigest.MakeTestBlockDigest(1)
87 blockDigest2 := blockdigest.MakeTestBlockDigest(2)
89 keepInfo := keep.ReadServers{
90 BlockToServers: map[blockdigest.BlockDigest][]keep.BlockServerInfo{
91 blockDigest1: []keep.BlockServerInfo{dummyBlockServerInfo},
92 blockDigest2: []keep.BlockServerInfo{dummyBlockServerInfo},
96 returnedSummary := SummarizeReplication(rc, keepInfo)
98 c := rc.UuidToCollection["col0"]
100 expectedSummary := ReplicationSummary{
101 CollectionBlocksNotInKeep: BlockSet{},
102 UnderReplicatedBlocks: BlockSet{},
103 OverReplicatedBlocks: BlockSet{},
104 CorrectlyReplicatedBlocks: BlockSetFromSlice([]int{1,2}),
105 KeepBlocksNotInCollections: BlockSet{},
107 CollectionsNotFullyInKeep: CollectionIndexSet{},
108 UnderReplicatedCollections: CollectionIndexSet{},
109 OverReplicatedCollections: CollectionIndexSet{},
110 CorrectlyReplicatedCollections: CollectionIndexSetFromSlice(
111 []int{rc.CollectionUuidToIndex[c.Uuid]}),
114 if !reflect.DeepEqual(returnedSummary, expectedSummary) {
115 t.Fatalf("Expected returnedSummary to look like %+v but instead it is %+v", expectedSummary, returnedSummary)
119 func TestMissingBlock(t *testing.T) {
120 rc := collection.MakeTestReadCollections([]collection.TestCollectionSpec{
121 collection.TestCollectionSpec{
129 // The internals aren't actually examined, so we can reuse the same one.
130 dummyBlockServerInfo := keep.BlockServerInfo{}
132 blockDigest1 := blockdigest.MakeTestBlockDigest(1)
134 keepInfo := keep.ReadServers{
135 BlockToServers: map[blockdigest.BlockDigest][]keep.BlockServerInfo{
136 blockDigest1: []keep.BlockServerInfo{dummyBlockServerInfo},
140 returnedSummary := SummarizeReplication(rc, keepInfo)
142 c := rc.UuidToCollection["col0"]
144 expectedSummary := ReplicationSummary{
145 CollectionBlocksNotInKeep: BlockSetFromSlice([]int{2}),
146 UnderReplicatedBlocks: BlockSet{},
147 OverReplicatedBlocks: BlockSet{},
148 CorrectlyReplicatedBlocks: BlockSetFromSlice([]int{1}),
149 KeepBlocksNotInCollections: BlockSet{},
151 CollectionsNotFullyInKeep: CollectionIndexSetFromSlice(
152 []int{rc.CollectionUuidToIndex[c.Uuid]}),
153 UnderReplicatedCollections: CollectionIndexSet{},
154 OverReplicatedCollections: CollectionIndexSet{},
155 CorrectlyReplicatedCollections: CollectionIndexSet{},
158 if !reflect.DeepEqual(returnedSummary, expectedSummary) {
159 t.Fatalf("Expected returnedSummary to look like %+v but instead it is %+v", expectedSummary, returnedSummary)
163 func TestUnderAndOverReplicatedBlocks(t *testing.T) {
164 rc := collection.MakeTestReadCollections([]collection.TestCollectionSpec{
165 collection.TestCollectionSpec{
173 // The internals aren't actually examined, so we can reuse the same one.
174 dummyBlockServerInfo := keep.BlockServerInfo{}
176 blockDigest1 := blockdigest.MakeTestBlockDigest(1)
177 blockDigest2 := blockdigest.MakeTestBlockDigest(2)
179 keepInfo := keep.ReadServers{
180 BlockToServers: map[blockdigest.BlockDigest][]keep.BlockServerInfo{
181 blockDigest1: []keep.BlockServerInfo{dummyBlockServerInfo},
182 blockDigest2: []keep.BlockServerInfo{dummyBlockServerInfo, dummyBlockServerInfo, dummyBlockServerInfo},
186 returnedSummary := SummarizeReplication(rc, keepInfo)
188 c := rc.UuidToCollection["col0"]
190 expectedSummary := ReplicationSummary{
191 CollectionBlocksNotInKeep: BlockSet{},
192 UnderReplicatedBlocks: BlockSetFromSlice([]int{1}),
193 OverReplicatedBlocks: BlockSetFromSlice([]int{2}),
194 CorrectlyReplicatedBlocks: BlockSet{},
195 KeepBlocksNotInCollections: BlockSet{},
197 CollectionsNotFullyInKeep: CollectionIndexSet{},
198 UnderReplicatedCollections: CollectionIndexSetFromSlice(
199 []int{rc.CollectionUuidToIndex[c.Uuid]}),
200 OverReplicatedCollections: CollectionIndexSetFromSlice(
201 []int{rc.CollectionUuidToIndex[c.Uuid]}),
202 CorrectlyReplicatedCollections: CollectionIndexSet{},
205 if !reflect.DeepEqual(returnedSummary, expectedSummary) {
206 t.Fatalf("Expected returnedSummary to look like %+v but instead it is %+v", expectedSummary, returnedSummary)
210 func TestMixedReplication(t *testing.T) {
211 rc := collection.MakeTestReadCollections([]collection.TestCollectionSpec{
212 collection.TestCollectionSpec{
216 collection.TestCollectionSpec{
220 collection.TestCollectionSpec{
228 // The internals aren't actually examined, so we can reuse the same one.
229 dummyBlockServerInfo := keep.BlockServerInfo{}
231 keepInfo := keep.ReadServers{
232 BlockToServers: map[blockdigest.BlockDigest][]keep.BlockServerInfo{
233 blockdigest.MakeTestBlockDigest(1): []keep.BlockServerInfo{dummyBlockServerInfo},
234 blockdigest.MakeTestBlockDigest(2): []keep.BlockServerInfo{dummyBlockServerInfo},
235 blockdigest.MakeTestBlockDigest(3): []keep.BlockServerInfo{dummyBlockServerInfo},
236 blockdigest.MakeTestBlockDigest(5): []keep.BlockServerInfo{dummyBlockServerInfo},
237 blockdigest.MakeTestBlockDigest(6): []keep.BlockServerInfo{dummyBlockServerInfo, dummyBlockServerInfo, dummyBlockServerInfo},
238 blockdigest.MakeTestBlockDigest(7): []keep.BlockServerInfo{dummyBlockServerInfo, dummyBlockServerInfo},
242 returnedSummary := SummarizeReplication(rc, keepInfo)
244 c0 := rc.UuidToCollection["col0"]
245 c1 := rc.UuidToCollection["col1"]
246 c2 := rc.UuidToCollection["col2"]
248 expectedSummary := ReplicationSummary{
249 CollectionBlocksNotInKeep: BlockSetFromSlice([]int{4}),
250 UnderReplicatedBlocks: BlockSetFromSlice([]int{5}),
251 OverReplicatedBlocks: BlockSetFromSlice([]int{6}),
252 CorrectlyReplicatedBlocks: BlockSetFromSlice([]int{1,2,3}),
253 KeepBlocksNotInCollections: BlockSetFromSlice([]int{7}),
255 CollectionsNotFullyInKeep: CollectionIndexSetFromSlice(
256 []int{rc.CollectionUuidToIndex[c1.Uuid]}),
257 UnderReplicatedCollections: CollectionIndexSetFromSlice(
258 []int{rc.CollectionUuidToIndex[c2.Uuid]}),
259 OverReplicatedCollections: CollectionIndexSetFromSlice(
260 []int{rc.CollectionUuidToIndex[c2.Uuid]}),
261 CorrectlyReplicatedCollections: CollectionIndexSetFromSlice(
262 []int{rc.CollectionUuidToIndex[c0.Uuid]}),
265 tempCis := make(CollectionIndexSet)
266 returnedSummary.CollectionBlocksNotInKeep.ToCollectionIndexSet(rc, &tempCis)
267 t.Logf("blocks not in keep: %v, collections not fully in keep: %v",
268 returnedSummary.CollectionBlocksNotInKeep,
271 if !reflect.DeepEqual(returnedSummary, expectedSummary) {
272 t.Fatalf("Expected returnedSummary to look like: \n%+v but instead it is: \n%+v. Index to UUID is %v. BlockToCollectionIndices is %v.", expectedSummary, returnedSummary, rc.CollectionIndexToUuid, rc.BlockToCollectionIndices)