This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
forked from streamingfast/sf-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_blocks.go
232 lines (187 loc) · 5.99 KB
/
check_blocks.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package sftools
import (
"context"
"fmt"
"io"
"regexp"
"strconv"
"github.com/golang/protobuf/proto"
"github.com/streamingfast/bstream"
"github.com/streamingfast/dstore"
"github.com/streamingfast/jsonpb"
"go.uber.org/zap"
)
var numberRegex = regexp.MustCompile(`(\d{10})`)
type PrintDetails uint8
const (
PrintNothing PrintDetails = iota
PrintStats
PrintFull
)
func CheckMergedBlocks(
ctx context.Context,
logger *zap.Logger,
storeURL string,
fileBlockSize uint32,
blockRange BlockRange,
blockPrinter func(block *bstream.Block),
printDetails PrintDetails,
) error {
fmt.Printf("Checking block holes on %s\n", storeURL)
var expected uint32
var count int
var baseNum32 uint32
holeFound := false
expected = RoundToBundleStartBlock(uint32(blockRange.Start), fileBlockSize)
currentStartBlk := uint32(blockRange.Start)
seenFilters := map[string]FilteringFilters{}
blocksStore, err := dstore.NewDBinStore(storeURL)
if err != nil {
return err
}
walkPrefix := WalkBlockPrefix(blockRange, fileBlockSize)
logger.Debug("walking merged blocks", zap.Stringer("block_range", blockRange), zap.String("walk_prefix", walkPrefix))
err = blocksStore.Walk(ctx, walkPrefix, ".tmp", func(filename string) error {
match := numberRegex.FindStringSubmatch(filename)
if match == nil {
return nil
}
logger.Debug("received merged blocks", zap.String("filename", filename))
count++
baseNum, _ := strconv.ParseUint(match[1], 10, 32)
if baseNum+uint64(fileBlockSize)-1 < blockRange.Start {
logger.Debug("base num lower then block range start, quitting", zap.Uint64("base_num", baseNum), zap.Uint64("starting_at", blockRange.Start))
return nil
}
baseNum32 = uint32(baseNum)
if printDetails != PrintNothing {
newSeenFilters := validateBlockSegment(ctx, blocksStore, filename, fileBlockSize, blockRange, blockPrinter, printDetails)
for key, filters := range newSeenFilters {
seenFilters[key] = filters
}
}
if baseNum32 != expected {
// There is no previous valid block range if we are at the ever first seen file
if count > 1 {
fmt.Printf("✅ Range %s\n", BlockRange{uint64(currentStartBlk), uint64(RoundToBundleEndBlock(expected-fileBlockSize, fileBlockSize))})
}
// Otherwise, we do not follow last seen element (previous is `100 - 199` but we are `299 - 300`)
missingRange := BlockRange{uint64(expected), uint64(RoundToBundleEndBlock(baseNum32-fileBlockSize, fileBlockSize))}
fmt.Printf("❌ Range %s! (Missing, [%s])\n", missingRange, missingRange.ReprocRange())
currentStartBlk = baseNum32
holeFound = true
}
expected = baseNum32 + fileBlockSize
if count%10000 == 0 {
fmt.Printf("✅ Range %s\n", BlockRange{uint64(currentStartBlk), uint64(RoundToBundleEndBlock(baseNum32, fileBlockSize))})
currentStartBlk = baseNum32 + fileBlockSize
}
if !blockRange.Unbounded() && RoundToBundleEndBlock(baseNum32, fileBlockSize) >= uint32(blockRange.Stop-1) {
return errStopWalk
}
return nil
})
if err != nil && err != errStopWalk {
return err
}
actualEndBlock := RoundToBundleEndBlock(baseNum32, fileBlockSize)
if !blockRange.Unbounded() {
actualEndBlock = uint32(blockRange.Stop)
}
fmt.Printf("✅ Range %s\n", BlockRange{uint64(currentStartBlk), uint64(actualEndBlock)})
if len(seenFilters) > 0 {
fmt.Println()
fmt.Println("Seen filters")
for _, filters := range seenFilters {
fmt.Printf("- [Include %q, Exclude %q, System %q]\n", filters.Include, filters.Exclude, filters.System)
}
fmt.Println()
}
if holeFound {
fmt.Printf("🆘 Holes found!\n")
} else {
fmt.Printf("🆗 No hole found\n")
}
return nil
}
func validateBlockSegment(
ctx context.Context,
store dstore.Store,
segment string,
fileBlockSize uint32,
blockRange BlockRange,
blockPrinter func(block *bstream.Block),
printDetails PrintDetails,
) (seenFilters map[string]FilteringFilters) {
reader, err := store.OpenObject(ctx, segment)
if err != nil {
fmt.Printf("❌ Unable to read blocks segment %s: %s\n", segment, err)
return
}
defer reader.Close()
readerFactory, err := bstream.GetBlockReaderFactory.New(reader)
if err != nil {
fmt.Printf("❌ Unable to read blocks segment %s: %s\n", segment, err)
return
}
// FIXME: Need to track block continuity (100, 101, 102a, 102b, 103, ...) and report which one are missing
seenBlockCount := 0
for {
block, err := readerFactory.Read()
if block != nil {
if !blockRange.Unbounded() {
if block.Number >= blockRange.Stop {
return
}
if block.Number < blockRange.Start {
continue
}
}
seenBlockCount++
if printDetails == PrintStats {
blockPrinter(block)
}
if printDetails == PrintFull {
out, err := jsonpb.MarshalIndentToString(block.ToNative().(proto.Message), " ")
if err != nil {
fmt.Printf("❌ Unable to print full block %s: %s\n", block.AsRef(), err)
continue
}
fmt.Println(out)
}
continue
}
if block == nil && err == io.EOF {
if seenBlockCount < expectedBlockCount(segment, fileBlockSize) {
fmt.Printf("❌ Segment %s contained only %d blocks, expected at least 100\n", segment, seenBlockCount)
}
return
}
if err != nil {
fmt.Printf("❌ Unable to read all blocks from segment %s after reading %d blocks: %s\n", segment, seenBlockCount, err)
return
}
}
}
func WalkBlockPrefix(blockRange BlockRange, fileBlockSize uint32) string {
if blockRange.Unbounded() {
return ""
}
startString := fmt.Sprintf("%010d", RoundToBundleStartBlock(uint32(blockRange.Start), fileBlockSize))
endString := fmt.Sprintf("%010d", RoundToBundleEndBlock(uint32(blockRange.Stop-1), fileBlockSize)+1)
offset := 0
for i := 0; i < len(startString); i++ {
if startString[i] != endString[i] {
return string(startString[0:i])
}
offset++
}
// At this point, the two strings are equal, to return the string
return startString
}
func expectedBlockCount(segment string, fileBlockSize uint32) int {
if segment == "0000000000" {
return int(fileBlockSize) - int(bstream.GetProtocolFirstStreamableBlock)
}
return int(fileBlockSize)
}