-
Notifications
You must be signed in to change notification settings - Fork 107
/
checkpoint_sync.go
506 lines (452 loc) · 14.5 KB
/
checkpoint_sync.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package committee
import (
"bytes"
"container/heap"
"context"
"errors"
"fmt"
"io"
"sort"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/oasisprotocol/oasis-core/go/runtime/nodes/grpc"
storageApi "github.com/oasisprotocol/oasis-core/go/storage/api"
storageClient "github.com/oasisprotocol/oasis-core/go/storage/client"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/checkpoint"
)
const (
// cpListTimeout is the timeout for fetching a list of checkpoints from a node.
cpListTimeout = 5 * time.Second
// cpListsTimeout is the timeout for fetching checkpoints from all nodes.
cpListsTimeout = 30 * time.Second
// cpRestoreTimeout is the timeout for restoring a checkpoint chunk from a node.
cpRestoreTimeout = 60 * time.Second
retryInterval = 1 * time.Second
maxRetries = 30
checkpointStatusDone = 0
checkpointStatusNext = 1
checkpointStatusBail = 2
// LogEventCheckpointSyncSuccess is a log event value that signals that checkpoint sync was successful.
LogEventCheckpointSyncSuccess = "worker/storage/checkpoint-sync-success"
)
// ErrNoUsableCheckpoints is the error returned when none of the checkpoints could be synced.
var ErrNoUsableCheckpoints = errors.New("storage: no checkpoint could be synced")
type restoreResult struct {
done bool
err error
}
type chunkHeap struct {
array []*checkpoint.ChunkMetadata
length int
}
func (h chunkHeap) Len() int { return h.length }
func (h chunkHeap) Less(i, j int) bool { return h.array[i].Index < h.array[j].Index }
func (h chunkHeap) Swap(i, j int) { h.array[i], h.array[j] = h.array[j], h.array[i] }
func (h *chunkHeap) Push(x interface{}) {
h.array[h.length] = x.(*checkpoint.ChunkMetadata)
h.length++
}
func (h *chunkHeap) Pop() interface{} {
h.length--
ret := h.array[h.length]
h.array[h.length] = nil
return ret
}
// goWithNodes runs the given operation with all the connections in the provided nodesClient.
func (n *Node) goWithNodes(
nodesClient grpc.NodesClient,
fn func(context.Context, *grpc.ConnWithNodeMeta) error,
) (
context.CancelFunc,
chan interface{},
error,
) {
connCh := make(chan []*grpc.ConnWithNodeMeta)
connGetter := func() error {
conns := nodesClient.GetConnectionsWithMeta()
if len(conns) == 0 {
return storageClient.ErrStorageNotAvailable
}
connCh <- conns
return nil
}
go func() {
sched := backoff.WithMaxRetries(backoff.NewConstantBackOff(retryInterval), maxRetries)
_ = backoff.Retry(connGetter, backoff.WithContext(sched, n.ctx))
close(connCh)
}()
conns, ok := <-connCh
if !ok || len(conns) == 0 {
return nil, nil, storageClient.ErrStorageNotAvailable
}
workerCtx, workerCancel := context.WithCancel(n.ctx)
var workerGroup sync.WaitGroup
doneCh := make(chan interface{})
for _, conn := range conns {
workerGroup.Add(1)
go func(conn *grpc.ConnWithNodeMeta) {
defer workerGroup.Done()
op := func() error {
return fn(workerCtx, conn)
}
sched := backoff.WithMaxRetries(backoff.NewConstantBackOff(retryInterval), maxRetries)
_ = backoff.Retry(op, backoff.WithContext(sched, workerCtx))
}(conn)
}
go func() {
defer close(doneCh)
workerGroup.Wait()
}()
return workerCancel, doneCh, nil
}
func (n *Node) nodeWorker(
ctx context.Context,
conn *grpc.ConnWithNodeMeta,
chunkDispatchCh chan *checkpoint.ChunkMetadata,
chunkReturnCh chan *checkpoint.ChunkMetadata,
errorCh chan int,
) error {
api := storageApi.NewStorageClient(conn.ClientConn)
for {
var chunk *checkpoint.ChunkMetadata
var ok bool
select {
case <-ctx.Done():
return backoff.Permanent(ctx.Err())
case chunk, ok = <-chunkDispatchCh:
if !ok {
return nil
}
}
chunkCtx, cancel := context.WithTimeout(ctx, cpRestoreTimeout)
defer cancel()
restoreCh := make(chan *restoreResult)
rd, wr := io.Pipe()
go func() {
done, err := n.localStorage.Checkpointer().RestoreChunk(chunkCtx, chunk.Index, rd)
restoreCh <- &restoreResult{
done: done,
err: err,
}
}()
err := api.GetCheckpointChunk(chunkCtx, chunk, wr)
wr.Close()
result := <-restoreCh
cancel()
// GetCheckpointChunk errors.
// The chunk probably always needs to be returned here
// (otherwise there's a deadlock risk with one worker's backoff just aborting
// and another worker then blocking on its chunk).
switch {
case err == nil:
// Fall out of the switch.
case err != nil:
n.logger.Error("can't fetch chunk from storage node", "node", conn.Node.ID, "chunk", chunk.Index, "err", err)
chunkReturnCh <- chunk
fallthrough
case errors.Is(err, checkpoint.ErrChunkNotFound):
return backoff.Permanent(err)
default:
return err
}
// RestoreChunk errors.
switch {
case result.done:
// Signal to the toplevel handler that we're done.
chunkReturnCh <- nil
return nil
case result.err != nil:
n.logger.Error("chunk restoration failed",
"node", conn.Node.ID,
"chunk", chunk.Index,
"root", chunk.Root,
"err", result.err,
)
switch {
case errors.Is(result.err, checkpoint.ErrChunkCorrupted):
chunkReturnCh <- chunk
return result.err
case errors.Is(result.err, checkpoint.ErrChunkProofVerificationFailed):
errorCh <- checkpointStatusNext
return backoff.Permanent(result.err)
default:
errorCh <- checkpointStatusBail
return backoff.Permanent(result.err)
}
}
}
}
func (n *Node) handleCheckpoint(check *checkpoint.Metadata, nodesClient grpc.NodesClient, groupSize uint16) (cpStatus int, rerr error) {
if err := n.localStorage.Checkpointer().StartRestore(n.ctx, check); err != nil {
// Any previous restores were already aborted by the driver up the call stack, so
// things should have been going smoothly here; bail.
return checkpointStatusBail, fmt.Errorf("can't start checkpoint restore: %w", err)
}
// This defer has to be here so that we're sure no workers are running anymore during
// any potential aborts.
defer func() {
if cpStatus == checkpointStatusDone {
return
}
// Abort has to succeed even if we were interrupted by context cancellation.
ctx := context.Background()
if err := n.localStorage.Checkpointer().AbortRestore(ctx); err != nil {
cpStatus = checkpointStatusBail
n.logger.Error("error while aborting checkpoint restore on handler exit, aborting sync",
"err", err,
)
}
}()
chunkDispatchCh := make(chan *checkpoint.ChunkMetadata)
defer close(chunkDispatchCh)
chunkReturnCh := make(chan *checkpoint.ChunkMetadata, groupSize)
errorCh := make(chan int, groupSize)
worker := func(ctx context.Context, conn *grpc.ConnWithNodeMeta) error {
return n.nodeWorker(ctx, conn, chunkDispatchCh, chunkReturnCh, errorCh)
}
cancel, doneCh, err := n.goWithNodes(nodesClient, worker)
if err != nil {
return checkpointStatusBail, fmt.Errorf("can't fetch chunks from committee nodes: %w", err)
}
// Cancel on exit and wait for the worker pool to drain so that the abort
// above can proceed safely.
defer func() {
cancel()
<-doneCh
}()
// Prepare the heap of chunks.
chunks := &chunkHeap{
array: make([]*checkpoint.ChunkMetadata, len(check.Chunks)),
length: 0,
}
heap.Init(chunks)
for i, c := range check.Chunks {
heap.Push(chunks, &checkpoint.ChunkMetadata{
Version: 1,
Index: uint64(i),
Digest: c,
Root: check.Root,
})
}
n.logger.Debug("checkpoint chunks prepared for dispatch",
"chunks", len(check.Chunks),
"checkpoint_root", check.Root,
)
// Feed the workers with chunks.
var next *checkpoint.ChunkMetadata
var outChan chan *checkpoint.ChunkMetadata
for {
if chunks.length == 0 {
next = nil
outChan = nil
} else {
next = heap.Pop(chunks).(*checkpoint.ChunkMetadata)
outChan = chunkDispatchCh
}
select {
case <-n.ctx.Done():
return checkpointStatusBail, n.ctx.Err()
case returned := <-chunkReturnCh:
if returned == nil {
// Restoration completed, no more chunks.
return checkpointStatusDone, nil
}
heap.Push(chunks, returned)
case status := <-errorCh:
return status, nil
// If there's no chunk to send, outChan will be nil here, blocking forever. We still need to wait
// for other events even if there's no chunk to dispatch, since they may simply all be in processing.
case outChan <- next:
next = nil
case <-doneCh:
// No usable committee connections left, move on to the next checkpoint.
return checkpointStatusNext, storageClient.ErrStorageNotAvailable
}
if next != nil {
heap.Push(chunks, next)
}
}
}
func (n *Node) getCheckpointList(nodesClient grpc.NodesClient) ([]*checkpoint.Metadata, error) {
// Get checkpoint list from all current committee members.
listCh := make(chan []*checkpoint.Metadata)
req := &checkpoint.GetCheckpointsRequest{
Version: 1,
Namespace: n.commonNode.Runtime.ID(),
}
getter := func(opCtx context.Context, conn *grpc.ConnWithNodeMeta) error {
ctx, cancel := context.WithTimeout(opCtx, cpListTimeout)
defer cancel()
api := storageApi.NewStorageClient(conn.ClientConn)
meta, err := api.GetCheckpoints(ctx, req)
if err != nil {
n.logger.Error("error calling GetCheckpoints",
"err", err,
"node", conn.Node.ID,
"this_node", n.commonNode.Identity.NodeSigner.Public,
)
return err
}
n.logger.Debug("got checkpoint list from a node",
"length", len(meta),
"node", conn.Node.ID,
)
listCh <- meta
return nil
}
cancel, doneCh, err := n.goWithNodes(nodesClient, getter)
if err != nil {
return nil, err
}
defer cancel()
// Wait at most cpListsTimeout for results.
go func() {
<-time.After(cpListsTimeout)
cancel()
}()
var list []*checkpoint.Metadata
resultLoop:
for {
select {
case <-doneCh:
break resultLoop
case <-n.ctx.Done():
return nil, n.ctx.Err()
case meta := <-listCh:
list = append(list, meta...)
}
}
// Prepare the list: sort and deduplicate.
sort.Slice(list, func(i, j int) bool {
// Descending!
if list[j].Root.Version == list[i].Root.Version {
return bytes.Compare(list[j].Root.Hash[:], list[i].Root.Hash[:]) < 0
}
return list[j].Root.Version < list[i].Root.Version
})
retList := make([]*checkpoint.Metadata, len(list))
var prevCheckpoint *checkpoint.Metadata
cursor := 0
for i := 0; i < len(list); i++ {
if prevCheckpoint == nil || !list[i].Root.Equal(&prevCheckpoint.Root) {
retList[cursor] = list[i]
cursor++
}
}
return retList[:cursor], nil
}
func (n *Node) checkCheckpointUsable(cp *checkpoint.Metadata, remainingMask outstandingMask) bool {
namespace := n.commonNode.Runtime.ID()
if !namespace.Equal(&cp.Root.Namespace) {
// Not for the right runtime.
return false
}
blk, err := n.commonNode.Runtime.History().GetBlock(n.ctx, cp.Root.Version)
if err != nil {
n.logger.Error("can't get block information for checkpoint, skipping", "err", err, "root", cp.Root)
return false
}
_, lastIORoot, lastStateRoot := n.GetLastSynced()
lastVersions := map[storageApi.RootType]uint64{
storageApi.RootTypeIO: lastIORoot.Version,
storageApi.RootTypeState: lastStateRoot.Version,
}
if namespace.Equal(&blk.Header.Namespace) {
for _, root := range blk.Header.StorageRoots() {
if cp.Root.Type == root.Type && root.Hash.Equal(&cp.Root.Hash) {
// Do we already have this root?
if lastVersions[cp.Root.Type] < cp.Root.Version && remainingMask.contains(cp.Root.Type) {
return true
}
return false
}
}
}
n.logger.Info("checkpoint for unknown root skipped", "root", cp.Root)
return false
}
func (n *Node) syncCheckpoints(genesisRound uint64) (*blockSummary, error) {
// Store roots and round info for checkpoints that finished syncing.
// Round and namespace info will get overwritten as rounds are skipped
// for errors, driven by remainingRoots.
var syncState blockSummary
// Try getting the active descriptor first.
descriptor, err := n.commonNode.Runtime.ActiveDescriptor(n.ctx)
if err != nil {
return nil, fmt.Errorf("can't get runtime descriptor: %w", err)
}
// Fetch metadata from the current committee.
metadata, err := n.getCheckpointList(n.storageNodesGrpc)
if err != nil {
return nil, fmt.Errorf("can't get checkpoint list from storage committee: %w", err)
}
// Try all the checkpoints now, from most recent backwards.
var (
prevVersion uint64 = ^uint64(0)
multipartRunning bool
mask outstandingMask
)
remainingRoots := outstandingMaskFull
defer func() {
if !multipartRunning {
return
}
if err := n.localStorage.NodeDB().AbortMultipartInsert(); err != nil {
n.logger.Error("error aborting multipart restore on exit from syncer",
"err", err,
)
}
}()
for _, check := range metadata {
if check.Root.Version < genesisRound || !n.checkCheckpointUsable(check, remainingRoots) {
continue
}
if check.Root.Version != prevVersion {
// Starting a new round, so we need to clean up all state from
// previous retores. Aborting multipart works with no multipart in
// progress too.
multipartRunning = false
if err := n.localStorage.NodeDB().AbortMultipartInsert(); err != nil {
return nil, fmt.Errorf("error aborting previous multipart restore: %w", err)
}
if err := n.localStorage.NodeDB().StartMultipartInsert(check.Root.Version); err != nil {
return nil, fmt.Errorf("error starting multipart insert for round %d: %w", check.Root.Version, err)
}
multipartRunning = true
remainingRoots = outstandingMaskFull
prevVersion = check.Root.Version
syncState.Roots = nil
}
status, err := n.handleCheckpoint(check, n.storageNodesGrpc, descriptor.Storage.GroupSize)
switch status {
case checkpointStatusDone:
n.logger.Info("successfully restored from checkpoint", "root", check.Root, "mask", mask)
syncState.Namespace = check.Root.Namespace
syncState.Round = check.Root.Version
syncState.Roots = append(syncState.Roots, check.Root)
remainingRoots.remove(check.Root.Type)
if remainingRoots.isEmpty() {
if err = n.localStorage.NodeDB().Finalize(n.ctx, syncState.Roots); err != nil {
n.logger.Error("can't finalize version after all checkpoints restored",
"err", err,
"version", prevVersion,
"roots", syncState.Roots,
)
// Likely a local problem, so just bail.
return nil, fmt.Errorf("can't finalize version after checkpoints restored: %w", err)
}
multipartRunning = false
return &syncState, nil
}
continue
case checkpointStatusNext:
n.logger.Info("error trying to restore from checkpoint, trying next most recent", "root", check.Root, "err", err)
continue
case checkpointStatusBail:
n.logger.Error("error trying to restore from checkpoint, unrecoverable", "root", check.Root, "err", err)
return nil, fmt.Errorf("error restoring from checkpoints: %w", err)
}
}
return nil, ErrNoUsableCheckpoints
}