-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwrite.go
More file actions
372 lines (302 loc) · 10.8 KB
/
write.go
File metadata and controls
372 lines (302 loc) · 10.8 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2020 dfuse Platform Inc.
//
// 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 fluxdb
import (
"bytes"
"context"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/streamingfast/bstream"
pbbstream "github.com/streamingfast/bstream/pb/sf/bstream/v1"
"github.com/streamingfast/fluxdb/store"
"github.com/streamingfast/logging"
pbfluxdb "github.com/streamingfast/pbgo/sf/fluxdb/v1"
"github.com/streamingfast/sf-tracing/tracex"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/protobuf/proto"
)
var logWriteBlockStats = os.Getenv("STATEDB_SIZE_STATS") != ""
func (fdb *FluxDB) WriteBatch(ctx context.Context, w []*WriteRequest) error {
ctx, span := ttracer.Start(ctx, "write batch", tracex.Attributes("write_request_count", len(w)))
defer span.End()
if err := fdb.isNextBlock(ctx, w[0].Height); err != nil {
return fmt.Errorf("next block check: %w", err)
}
batch := fdb.store.NewBatch(zlog)
for _, req := range w {
if err := fdb.writeBlock(ctx, batch, req); err != nil {
return fmt.Errorf("write block: %w", err)
}
if _, err := batch.FlushIfFull(ctx); err != nil {
return fmt.Errorf("flushing if full: %w", err)
}
}
if err := batch.Flush(ctx); err != nil {
return fmt.Errorf("flush: %w", err)
}
if sched := fdb.idxCache.IndexingSchedule(); len(sched) != 0 {
err := fdb.IndexTables(ctx)
if err != nil {
return fmt.Errorf("index tables: %w", err)
}
}
return nil
}
type shardProgressStats struct {
HighestHeight uint64
BlockRefByShard map[int]bstream.BlockRef
ReferenceBlockRef bstream.BlockRef
FaultyShards []int
MissingShards []int
}
func (fdb *FluxDB) VerifyAllShardsWritten(ctx context.Context) (*shardProgressStats, error) {
stats, err := fdb.fetchAllShardProgressStats(ctx)
if err != nil {
return nil, fmt.Errorf("fetch shard progress: %w", err)
}
if len(stats.MissingShards) > 0 {
err = multierr.Append(err, fmt.Errorf("missing shards: %v", stats.MissingShards))
}
if len(stats.FaultyShards) > 0 {
faultyShards := make([]string, len(stats.FaultyShards))
for i, faultyShard := range stats.FaultyShards {
faultyShards[i] = fmt.Sprintf("%d @ #%d", faultyShard, stats.BlockRefByShard[faultyShard].Num())
}
err = multierr.Append(err, fmt.Errorf("shards not matching reference block %s, they might be in progress (shards with block mismatch [%s])", stats.ReferenceBlockRef, strings.Join(faultyShards, ", ")))
}
return stats, err
}
func (fdb *FluxDB) fetchAllShardProgressStats(ctx context.Context) (*shardProgressStats, error) {
stats := &shardProgressStats{
BlockRefByShard: map[int]bstream.BlockRef{},
ReferenceBlockRef: bstream.BlockRefEmpty,
}
seen := make(map[int]bstream.BlockRef)
err := fdb.store.ScanLastShardsWrittenCheckpoint(ctx, []byte("shard-"), func(key []byte, value []byte) error {
height, block, err := unmarshalCheckpoint(value)
if err != nil {
return fmt.Errorf("unable to unmarshal checkpoint: %w", err)
}
shardIndexRaw := string(bytes.TrimPrefix(key, []byte("shard-")))
shardIndex, err := strconv.Atoi(shardIndexRaw)
if err != nil {
return fmt.Errorf("invalid shard index %q: %w", shardIndexRaw, err)
}
seen[shardIndex] = block
if height > stats.HighestHeight {
stats.HighestHeight = height
}
if stats.ReferenceBlockRef == bstream.BlockRefEmpty {
stats.ReferenceBlockRef = block
if tracer.Enabled() {
zlog.Debug("shard progression updating reference block", zap.Stringer("reference_block", stats.ReferenceBlockRef))
}
}
return nil
})
if err != nil {
return nil, err
}
if tracer.Enabled() {
zlog.Debug("shard progression initial fetching done",
zap.Int("seen_count", len(seen)),
zap.Uint64("highest_height", stats.HighestHeight),
zap.Stringer("reference_block", stats.ReferenceBlockRef),
)
}
emptyBlockRefCount := 0
for i := 0; i < fdb.shardCount; i++ {
seenBlock, found := seen[i]
if !found {
emptyBlockRefCount++
seenBlock = bstream.BlockRefEmpty
}
stats.BlockRefByShard[i] = seenBlock
}
for shardIndex, shardBlock := range stats.BlockRefByShard {
if bstream.EqualsBlockRefs(shardBlock, bstream.BlockRefEmpty) {
stats.MissingShards = append(stats.MissingShards, shardIndex)
}
if !bstream.EqualsBlockRefs(shardBlock, stats.ReferenceBlockRef) {
stats.FaultyShards = append(stats.FaultyShards, shardIndex)
}
}
if tracer.Enabled() {
zlog.Debug("shard progression fetching done",
zap.Int("empty_shard_count", emptyBlockRefCount),
zap.Int("missing_shard_count", len(stats.MissingShards)),
zap.Int("faulyt_shard_count", len(stats.FaultyShards)),
)
}
return stats, nil
}
func (fdb *FluxDB) WriteShardingFinalCheckpoint(ctx context.Context, height uint64, block bstream.BlockRef) error {
batch := fdb.store.NewBatch(zlog)
if err := fdb.setFinalCheckpoint(batch, height, block); err != nil {
return fmt.Errorf("set last checkpoint: %w", err)
}
if err := batch.Flush(ctx); err != nil {
return fmt.Errorf("flushing last block marker: %w", err)
}
return nil
}
func (fdb *FluxDB) DeleteAllShardCheckpoints(ctx context.Context) error {
return fdb.store.DeleteShardsCheckpoint(ctx, []byte("shard-"))
}
func (fdb *FluxDB) writeBlock(ctx context.Context, batch store.Batch, w *WriteRequest) (err error) {
var stats *writeBlockStats
if logWriteBlockStats {
stats = &writeBlockStats{
Block: w.BlockRef,
TotalSizePerSinglet: map[string]uint64{},
TotalSizePerTablet: map[string]uint64{},
}
}
for _, entry := range w.SingletEntries {
var value []byte
if !entry.IsDeletion() {
value, err = entry.MarshalValue()
if err != nil {
return fmt.Errorf("singlet to proto: %w", err)
}
}
key := KeyForSingletEntry(entry)
if logWriteBlockStats {
singletKey := entry.Singlet().String()
size := uint64(len(key) + len(value))
stats.TotalSize += size
stats.TotalSizePerSinglet[singletKey] = stats.TotalSizePerSinglet[singletKey] + size
stats.SingleEntryCount++
}
batch.SetRow(key, value)
}
for _, row := range w.TabletRows {
var value []byte
if !row.IsDeletion() {
value, err = row.MarshalValue()
if err != nil {
return fmt.Errorf("tablet to proto: %w", err)
}
}
tablet := row.Tablet()
key := KeyForTabletRowFromParts(tablet, row.Height(), row.PrimaryKey())
if logWriteBlockStats {
tabletKey := tablet.String()
size := uint64(len(key) + len(value))
stats.TotalSize += size
stats.TotalSizePerTablet[tabletKey] = stats.TotalSizePerTablet[tabletKey] + size
stats.TabletRowCount++
}
batch.SetRow(key, value)
if !fdb.disableIndexing {
// We could group `w.TabletRows` by tablet here greatly reducing the number of time
// we need to compute the tablet key, reducing memory allocation an GC at the same time.
tabletKey := KeyForTablet(tablet)
fdb.idxCache.IncCount(tabletKey)
if fdb.idxCache.shouldTriggerIndexing(tabletKey) {
fdb.idxCache.ScheduleIndex(tabletKey, w.Height)
}
}
}
if logWriteBlockStats {
zlog.Info("write block stats", zap.Object("stats", stats))
}
return fdb.setLastCheckpoint(batch, w.Height, w.BlockRef)
}
type writeBlockStats struct {
Block bstream.BlockRef
TotalSize uint64
TotalSizePerSinglet map[string]uint64
TotalSizePerTablet map[string]uint64
SingleEntryCount uint64
TabletRowCount uint64
}
func (s *writeBlockStats) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("block", s.Block.String())
encoder.AddUint64("total_size_in_bytes", s.TotalSize)
if len(s.TotalSizePerSinglet) > 0 {
singlets := make([]string, 0, len(s.TotalSizePerSinglet))
for singlet := range s.TotalSizePerSinglet {
singlets = append(singlets, singlet)
}
sort.Slice(singlets, func(i, j int) bool { return s.TotalSizePerSinglet[singlets[i]] > s.TotalSizePerSinglet[singlets[j]] })
elements := make([]string, 0, 5)
for i, singlet := range singlets {
if i >= 5 {
break
}
elements = append(elements, fmt.Sprintf("%s (%d bytes)", singlet, s.TotalSizePerSinglet[singlet]))
}
encoder.AddString("singlet_biggest", strings.Join(elements, ", "))
}
if len(s.TotalSizePerTablet) > 0 {
tablets := make([]string, 0, len(s.TotalSizePerTablet))
for tablet := range s.TotalSizePerTablet {
tablets = append(tablets, tablet)
}
sort.Slice(tablets, func(i, j int) bool { return s.TotalSizePerTablet[tablets[i]] > s.TotalSizePerTablet[tablets[j]] })
elements := make([]string, 0, 5)
for i, tablet := range tablets {
if i >= 5 {
break
}
elements = append(elements, fmt.Sprintf("%s (%d bytes)", tablet, s.TotalSizePerTablet[tablet]))
}
encoder.AddString("tablet_biggest", strings.Join(elements, ", "))
}
encoder.AddInt("singlet_count", len(s.TotalSizePerSinglet))
encoder.AddUint64("singlet_entry_count", s.SingleEntryCount)
encoder.AddInt("tablet_count", len(s.TotalSizePerTablet))
encoder.AddUint64("tablet_row_count", s.TabletRowCount)
return nil
}
func (fdb *FluxDB) isNextBlock(ctx context.Context, writeHeight uint64) error {
zlogger := logging.Logger(ctx, zlog)
zlogger.Debug("checking if is next block", zap.Uint64("height", writeHeight))
_, lastBlock, err := fdb.FetchLastWrittenCheckpoint(ctx)
if err != nil {
return err
}
// FIXME (height): This works only for block num, if we move to a "height" structure, we should just check if linear probably
lastHeight := lastBlock.Num()
if !(lastHeight >= writeHeight-1) && lastHeight != 0 && lastHeight != 1 {
return fmt.Errorf("block %d does not follow last block %d in db", writeHeight, lastHeight)
}
return nil
}
func (fdb *FluxDB) setLastCheckpoint(batch store.Batch, height uint64, lastBlock bstream.BlockRef) error {
return fdb.setCheckpoint(batch, fdb.lastCheckpointKey(), height, lastBlock)
}
func (fdb *FluxDB) setFinalCheckpoint(batch store.Batch, height uint64, lastBlock bstream.BlockRef) error {
return fdb.setCheckpoint(batch, fdb.finalCheckpointKey(), height, lastBlock)
}
func (fdb *FluxDB) setCheckpoint(batch store.Batch, key []byte, height uint64, lastBlock bstream.BlockRef) error {
if fdb.disableLastCheckpointWrite {
return nil
}
cellData, err := proto.Marshal(&pbfluxdb.Checkpoint{
Height: height,
Block: &pbbstream.BlockRef{Id: lastBlock.ID(), Num: lastBlock.Num()},
})
if err != nil {
return fmt.Errorf("unable to marshal checkpoint: %w", err)
}
batch.SetLastCheckpoint(key, cellData)
return nil
}