-
Notifications
You must be signed in to change notification settings - Fork 166
/
checkpointer.go
435 lines (350 loc) · 11.6 KB
/
checkpointer.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package wal
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"sort"
"strconv"
"strings"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/ledger/complete/mtrie"
"github.com/onflow/flow-go/ledger/complete/mtrie/flattener"
"github.com/onflow/flow-go/ledger/complete/mtrie/trie"
"github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/module/metrics"
utilsio "github.com/onflow/flow-go/utils/io"
)
const checkpointFilenamePrefix = "checkpoint."
const MagicBytes uint16 = 0x2137
const VersionV1 uint16 = 0x01
// Versions was reset while changing trie format, so now bump it to 3 to avoid conflicts
// Version 3 contains a file checksum for detecting corrupted checkpoint files.
const VersionV3 uint16 = 0x03
type Checkpointer struct {
dir string
wal *DiskWAL
keyByteSize int
forestCapacity int
}
func NewCheckpointer(wal *DiskWAL, keyByteSize int, forestCapacity int) *Checkpointer {
return &Checkpointer{
dir: wal.wal.Dir(),
wal: wal,
keyByteSize: keyByteSize,
forestCapacity: forestCapacity,
}
}
// listCheckpoints returns all the numbers (unsorted) of the checkpoint files, and the number of the last checkpoint.
func (c *Checkpointer) listCheckpoints() ([]int, int, error) {
list := make([]int, 0)
files, err := ioutil.ReadDir(c.dir)
if err != nil {
return nil, -1, fmt.Errorf("cannot list directory [%s] content: %w", c.dir, err)
}
last := -1
for _, fn := range files {
if !strings.HasPrefix(fn.Name(), checkpointFilenamePrefix) {
continue
}
justNumber := fn.Name()[len(checkpointFilenamePrefix):]
k, err := strconv.Atoi(justNumber)
if err != nil {
continue
}
list = append(list, k)
// the last check point is the one with the highest number
if k > last {
last = k
}
}
return list, last, nil
}
// Checkpoints returns all the checkpoint numbers in asc order
func (c *Checkpointer) Checkpoints() ([]int, error) {
list, _, err := c.listCheckpoints()
if err != nil {
return nil, fmt.Errorf("could not fetch all checkpoints: %w", err)
}
sort.Ints(list)
return list, nil
}
// LatestCheckpoint returns number of latest checkpoint or -1 if there are no checkpoints
func (c *Checkpointer) LatestCheckpoint() (int, error) {
_, last, err := c.listCheckpoints()
return last, err
}
// NotCheckpointedSegments - returns numbers of segments which are not checkpointed yet,
// or -1, -1 if there are no segments
func (c *Checkpointer) NotCheckpointedSegments() (from, to int, err error) {
latestCheckpoint, err := c.LatestCheckpoint()
if err != nil {
return -1, -1, fmt.Errorf("cannot get last checkpoint: %w", err)
}
first, last, err := c.wal.Segments()
if err != nil {
return -1, -1, fmt.Errorf("cannot get range of segments: %w", err)
}
// there are no segments at all, there is nothing to checkpoint
if first == -1 && last == -1 {
return -1, -1, nil
}
// no checkpoints
if latestCheckpoint == -1 {
return first, last, nil
}
// segments before checkpoint
if last <= latestCheckpoint {
return -1, -1, nil
}
// there is gap between last checkpoint and segments
if last > latestCheckpoint && latestCheckpoint < first-1 {
return -1, -1, fmt.Errorf("gap between last checkpoint and segments")
}
return latestCheckpoint + 1, last, nil
}
// Checkpoint creates new checkpoint stopping at given segment
func (c *Checkpointer) Checkpoint(to int, targetWriter func() (io.WriteCloser, error)) (err error) {
_, notCheckpointedTo, err := c.NotCheckpointedSegments()
if err != nil {
return fmt.Errorf("cannot get not checkpointed segments: %w", err)
}
latestCheckpoint, err := c.LatestCheckpoint()
if err != nil {
return fmt.Errorf("cannot get latest checkpoint: %w", err)
}
if latestCheckpoint == to {
return nil //nothing to do
}
if notCheckpointedTo < to {
return fmt.Errorf("no segments to checkpoint to %d, latests not checkpointed segment: %d", to, notCheckpointedTo)
}
forest, err := mtrie.NewForest(c.forestCapacity, &metrics.NoopCollector{}, func(evictedTrie *trie.MTrie) error {
return nil
})
if err != nil {
return fmt.Errorf("cannot create Forest: %w", err)
}
err = c.wal.replay(0, to,
func(forestSequencing *flattener.FlattenedForest) error {
tries, err := flattener.RebuildTries(forestSequencing)
if err != nil {
return err
}
for _, t := range tries {
err := forest.AddTrie(t)
if err != nil {
return err
}
}
return nil
},
func(update *ledger.TrieUpdate) error {
_, err := forest.Update(update)
return err
}, func(rootHash ledger.RootHash) error {
return nil
}, true)
if err != nil {
return fmt.Errorf("cannot replay WAL: %w", err)
}
c.wal.log.Info().Msgf("flattening forest for checkpoint %d", to)
forestSequencing, err := flattener.FlattenForest(forest)
if err != nil {
return fmt.Errorf("cannot get storables: %w", err)
}
c.wal.log.Info().Msgf("serializing checkpoint %d", to)
writer, err := targetWriter()
if err != nil {
return fmt.Errorf("cannot generate writer: %w", err)
}
defer func() {
closeErr := writer.Close()
// Return close error if there isn't any prior error to return.
if err == nil {
err = closeErr
}
}()
err = StoreCheckpoint(forestSequencing, writer)
return err
}
func NumberToFilenamePart(n int) string {
return fmt.Sprintf("%08d", n)
}
func NumberToFilename(n int) string {
return fmt.Sprintf("%s%s", checkpointFilenamePrefix, NumberToFilenamePart(n))
}
func (c *Checkpointer) CheckpointWriter(to int) (io.WriteCloser, error) {
return CreateCheckpointWriter(c.dir, to)
}
func CreateCheckpointWriter(dir string, fileNo int) (io.WriteCloser, error) {
return CreateCheckpointWriterForFile(dir, NumberToFilename(fileNo))
}
// CreateCheckpointWriterForFile returns a file writer that will write to a temporary file and then move it to the checkpoint folder by renaming it.
func CreateCheckpointWriterForFile(dir, filename string) (io.WriteCloser, error) {
fullname := path.Join(dir, filename)
if utilsio.FileExists(fullname) {
return nil, fmt.Errorf("checkpoint file %s already exists", fullname)
}
tmpFile, err := ioutil.TempFile(dir, "writing-chkpnt-*")
if err != nil {
return nil, fmt.Errorf("cannot create temporary file for checkpoint %v: %w", tmpFile, err)
}
writer := bufio.NewWriter(tmpFile)
return &SyncOnCloseRenameFile{
file: tmpFile,
targetName: fullname,
Writer: writer,
}, nil
}
// StoreCheckpoint writes the given checkpoint to disk, and also append with a CRC32 file checksum for integrity check.
func StoreCheckpoint(forestSequencing *flattener.FlattenedForest, writer io.Writer) error {
storableNodes := forestSequencing.Nodes
storableTries := forestSequencing.Tries
header := make([]byte, 4+8+2)
crc32Writer := NewCRC32Writer(writer)
pos := writeUint16(header, 0, MagicBytes)
pos = writeUint16(header, pos, VersionV3)
pos = writeUint64(header, pos, uint64(len(storableNodes)-1)) // -1 to account for 0 node meaning nil
writeUint16(header, pos, uint16(len(storableTries)))
_, err := crc32Writer.Write(header)
if err != nil {
return fmt.Errorf("cannot write checkpoint header: %w", err)
}
// 0 element = nil, we don't need to store it
for i := 1; i < len(storableNodes); i++ {
bytes := flattener.EncodeStorableNode(storableNodes[i])
_, err = crc32Writer.Write(bytes)
if err != nil {
return fmt.Errorf("error while writing node date: %w", err)
}
}
for _, storableTrie := range storableTries {
bytes := flattener.EncodeStorableTrie(storableTrie)
_, err = crc32Writer.Write(bytes)
if err != nil {
return fmt.Errorf("error while writing trie date: %w", err)
}
}
// add CRC32 sum
crc32buf := make([]byte, 4)
writeUint32(crc32buf, 0, crc32Writer.Crc32())
_, err = writer.Write(crc32buf)
if err != nil {
return fmt.Errorf("cannot write crc32: %w", err)
}
return nil
}
func (c *Checkpointer) LoadCheckpoint(checkpoint int) (*flattener.FlattenedForest, error) {
filepath := path.Join(c.dir, NumberToFilename(checkpoint))
return LoadCheckpoint(filepath)
}
func (c *Checkpointer) LoadRootCheckpoint() (*flattener.FlattenedForest, error) {
filepath := path.Join(c.dir, bootstrap.FilenameWALRootCheckpoint)
return LoadCheckpoint(filepath)
}
func (c *Checkpointer) HasRootCheckpoint() (bool, error) {
if _, err := os.Stat(path.Join(c.dir, bootstrap.FilenameWALRootCheckpoint)); err == nil {
return true, nil
} else if os.IsNotExist(err) {
return false, nil
} else {
return false, err
}
}
func (c *Checkpointer) RemoveCheckpoint(checkpoint int) error {
return os.Remove(path.Join(c.dir, NumberToFilename(checkpoint)))
}
func LoadCheckpoint(filepath string) (*flattener.FlattenedForest, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, fmt.Errorf("cannot open checkpoint file %s: %w", filepath, err)
}
defer func() {
_ = file.Close()
}()
return ReadCheckpoint(file)
}
func ReadCheckpoint(r io.Reader) (*flattener.FlattenedForest, error) {
var bufReader io.Reader = bufio.NewReader(r)
crcReader := NewCRC32Reader(bufReader)
var reader io.Reader = crcReader
header := make([]byte, 4+8+2)
_, err := io.ReadFull(reader, header)
if err != nil {
return nil, fmt.Errorf("cannot read header bytes: %w", err)
}
magicBytes, pos := readUint16(header, 0)
version, pos := readUint16(header, pos)
nodesCount, pos := readUint64(header, pos)
triesCount, _ := readUint16(header, pos)
if magicBytes != MagicBytes {
return nil, fmt.Errorf("unknown file format. Magic constant %x does not match expected %x", magicBytes, MagicBytes)
}
if version != VersionV1 && version != VersionV3 {
return nil, fmt.Errorf("unsupported file version %x ", version)
}
if version != VersionV3 {
reader = bufReader //switch back to plain reader
}
nodes := make([]*flattener.StorableNode, nodesCount+1) //+1 for 0 index meaning nil
tries := make([]*flattener.StorableTrie, triesCount)
for i := uint64(1); i <= nodesCount; i++ {
storableNode, err := flattener.ReadStorableNode(reader)
if err != nil {
return nil, fmt.Errorf("cannot read storable node %d: %w", i, err)
}
nodes[i] = storableNode
}
// TODO version ?
for i := uint16(0); i < triesCount; i++ {
storableTrie, err := flattener.ReadStorableTrie(reader)
if err != nil {
return nil, fmt.Errorf("cannot read storable trie %d: %w", i, err)
}
tries[i] = storableTrie
}
if version == VersionV3 {
crc32buf := make([]byte, 4)
_, err := bufReader.Read(crc32buf)
if err != nil {
return nil, fmt.Errorf("error while reading CRC32 checksum: %w", err)
}
readCrc32, _ := readUint32(crc32buf, 0)
calculatedCrc32 := crcReader.Crc32()
if calculatedCrc32 != readCrc32 {
return nil, fmt.Errorf("checkpoint checksum failed! File contains %x but read data checksums to %x", readCrc32, calculatedCrc32)
}
}
return &flattener.FlattenedForest{
Nodes: nodes,
Tries: tries,
}, nil
}
func writeUint16(buffer []byte, location int, value uint16) int {
binary.BigEndian.PutUint16(buffer[location:], value)
return location + 2
}
func readUint16(buffer []byte, location int) (uint16, int) {
value := binary.BigEndian.Uint16(buffer[location:])
return value, location + 2
}
func writeUint32(buffer []byte, location int, value uint32) int {
binary.BigEndian.PutUint32(buffer[location:], value)
return location + 4
}
func readUint32(buffer []byte, location int) (uint32, int) {
value := binary.BigEndian.Uint32(buffer[location:])
return value, location + 4
}
func readUint64(buffer []byte, location int) (uint64, int) {
value := binary.BigEndian.Uint64(buffer[location:])
return value, location + 8
}
func writeUint64(buffer []byte, location int, value uint64) int {
binary.BigEndian.PutUint64(buffer[location:], value)
return location + 8
}