-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstorage_health_check.go
165 lines (139 loc) · 4.63 KB
/
storage_health_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Atree - Scalable Arrays and Ordered Maps
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package atree
import "fmt"
// CheckStorageHealth checks for the health of slab storage.
// It traverses the slabs and checks these factors:
// - All non-root slabs only has a single parent reference (no double referencing)
// - Every child of a parent shares the same ownership (childSlabID.Address == parentSlabID.Address)
// - The number of root slabs are equal to the expected number (skipped if expectedNumberOfRootSlabs is -1)
// This should be used for testing purposes only, as it might be slow to process
func CheckStorageHealth(storage SlabStorage, expectedNumberOfRootSlabs int) (map[SlabID]struct{}, error) {
parentOf := make(map[SlabID]SlabID)
leaves := make([]SlabID, 0)
slabIterator, err := storage.SlabIterator()
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to create slab iterator")
}
slabs := map[SlabID]Slab{}
for {
id, slab := slabIterator()
if id == SlabIDUndefined {
break
}
if _, ok := slabs[id]; ok {
return nil, NewFatalError(fmt.Errorf("duplicate slab %s", id))
}
slabs[id] = slab
atLeastOneExternalSlab := false
childStorables := slab.ChildStorables()
for len(childStorables) > 0 {
var next []Storable
for _, s := range childStorables {
if sids, ok := s.(SlabIDStorable); ok {
sid := SlabID(sids)
if _, found := parentOf[sid]; found {
return nil, NewFatalError(fmt.Errorf("two parents are captured for the slab %s", sid))
}
parentOf[sid] = id
atLeastOneExternalSlab = true
}
// This handles inlined slab because inlined slab is a child storable (s) and
// we traverse s.ChildStorables() for its inlined elements.
next = append(next, s.ChildStorables()...)
}
childStorables = next
}
if !atLeastOneExternalSlab {
leaves = append(leaves, id)
}
}
rootsMap := make(map[SlabID]struct{})
visited := make(map[SlabID]struct{})
var id SlabID
for _, leaf := range leaves {
id = leaf
if _, ok := visited[id]; ok {
return nil, NewFatalError(fmt.Errorf("at least two references found to the leaf slab %s", id))
}
visited[id] = struct{}{}
for {
parentID, found := parentOf[id]
if !found {
// we reach the root
rootsMap[id] = struct{}{}
break
}
visited[parentID] = struct{}{}
childSlab, ok, err := storage.Retrieve(id)
if !ok {
return nil, NewSlabNotFoundErrorf(id, "failed to get child slab")
}
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to retrieve child slab %s", id))
}
parentSlab, ok, err := storage.Retrieve(parentID)
if !ok {
return nil, NewSlabNotFoundErrorf(id, "failed to get parent slab")
}
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to retrieve parent slab %s", parentID))
}
childOwner := childSlab.SlabID().address
parentOwner := parentSlab.SlabID().address
if childOwner != parentOwner {
return nil, NewFatalError(
fmt.Errorf(
"parent and child are not owned by the same account: child.owner %s, parent.owner %s",
childOwner,
parentOwner,
))
}
id = parentID
}
}
if len(visited) != len(slabs) {
var unreachableID SlabID
var unreachableSlab Slab
for id, slab := range slabs {
if _, ok := visited[id]; !ok {
unreachableID = id
unreachableSlab = slab
break
}
}
return nil, NewFatalError(
fmt.Errorf(
"slab was not reachable from leaves: %s: %s",
unreachableID,
unreachableSlab,
))
}
if (expectedNumberOfRootSlabs >= 0) && (len(rootsMap) != expectedNumberOfRootSlabs) {
return nil, NewFatalError(
fmt.Errorf(
"number of root slabs doesn't match: expected %d, got %d",
expectedNumberOfRootSlabs,
len(rootsMap),
))
}
return rootsMap, nil
}