-
Notifications
You must be signed in to change notification settings - Fork 178
/
wal.go
222 lines (187 loc) · 5.82 KB
/
wal.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
package wal
import (
"fmt"
"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
prometheusWAL "github.com/prometheus/tsdb/wal"
"github.com/onflow/flow-go/storage/ledger/mtrie"
"github.com/onflow/flow-go/storage/ledger/mtrie/flattener"
"github.com/onflow/flow-go/model/flow"
)
const SegmentSize = 32 * 1024 * 1024
type LedgerWAL struct {
wal *prometheusWAL.WAL
forestCapacity int
keyByteSize int
}
// TODO use real logger and metrics, but that would require passing them to Trie storage
func NewWAL(logger log.Logger, reg prometheus.Registerer, dir string, forestCapacity int, keyByteSize int, segmentSize int) (*LedgerWAL, error) {
w, err := prometheusWAL.NewSize(logger, reg, dir, segmentSize)
if err != nil {
return nil, err
}
return &LedgerWAL{
wal: w,
forestCapacity: forestCapacity,
keyByteSize: keyByteSize,
}, nil
}
func (w *LedgerWAL) RecordUpdate(stateCommitment flow.StateCommitment, keys [][]byte, values [][]byte) error {
bytes := EncodeUpdate(stateCommitment, keys, values)
err := w.wal.Log(bytes)
if err != nil {
return fmt.Errorf("error while recording update in LedgerWAL: %w", err)
}
return nil
}
func (w *LedgerWAL) RecordDelete(stateCommitment flow.StateCommitment) error {
bytes := EncodeDelete(stateCommitment)
err := w.wal.Log(bytes)
if err != nil {
return fmt.Errorf("error while recording delete in LedgerWAL: %w", err)
}
return nil
}
func (w *LedgerWAL) ReplayOnMForest(mForest *mtrie.MForest) error {
return w.Replay(
func(forestSequencing *flattener.FlattenedForest) error {
rebuiltTries, err := flattener.RebuildTries(forestSequencing)
if err != nil {
return fmt.Errorf("rebuilding forest from sequenced nodes failed: %w", err)
}
err = mForest.AddTries(rebuiltTries)
if err != nil {
return fmt.Errorf("adding rebuilt tries to forest failed: %w", err)
}
return nil
},
func(stateCommitment flow.StateCommitment, keys [][]byte, values [][]byte) error {
_, err := mForest.Update(stateCommitment, keys, values)
// _, err := trie.UpdateRegisters(keys, values, stateCommitment)
return err
},
func(stateCommitment flow.StateCommitment) error {
mForest.RemoveTrie(stateCommitment)
return nil
},
)
}
func (w *LedgerWAL) Replay(
checkpointFn func(forestSequencing *flattener.FlattenedForest) error,
updateFn func(flow.StateCommitment, [][]byte, [][]byte) error,
deleteFn func(flow.StateCommitment) error,
) error {
from, to, err := w.wal.Segments()
if err != nil {
return err
}
return w.replay(from, to, checkpointFn, updateFn, deleteFn, true)
}
func (w *LedgerWAL) ReplayLogsOnly(
checkpointFn func(forestSequencing *flattener.FlattenedForest) error,
updateFn func(flow.StateCommitment, [][]byte, [][]byte) error,
deleteFn func(flow.StateCommitment) error,
) error {
from, to, err := w.wal.Segments()
if err != nil {
return err
}
return w.replay(from, to, checkpointFn, updateFn, deleteFn, false)
}
func (w *LedgerWAL) replay(
from, to int,
checkpointFn func(forestSequencing *flattener.FlattenedForest) error,
updateFn func(flow.StateCommitment, [][]byte, [][]byte) error,
deleteFn func(flow.StateCommitment) error,
useCheckpoints bool,
) error {
if to < from {
return fmt.Errorf("end of range cannot be smaller than beginning")
}
loadedCheckpoint := false
startSegment := from
checkpointer, err := w.NewCheckpointer()
if err != nil {
return fmt.Errorf("cannot create checkpointer: %w", err)
}
if useCheckpoints {
latestCheckpoint, err := checkpointer.LatestCheckpoint()
if err != nil {
return fmt.Errorf("cannot get latest checkpoint: %w", err)
}
if latestCheckpoint != -1 && latestCheckpoint+1 >= from { //+1 to account for connected checkpoint and segments
forestSequencing, err := checkpointer.LoadCheckpoint(latestCheckpoint)
if err != nil {
return fmt.Errorf("cannot load checkpoint %d: %w", latestCheckpoint, err)
}
err = checkpointFn(forestSequencing)
if err != nil {
return fmt.Errorf("error while handling checkpoint: %w", err)
}
loadedCheckpoint = true
}
if loadedCheckpoint && to == latestCheckpoint {
return nil
}
if loadedCheckpoint {
startSegment = latestCheckpoint + 1
}
}
if !loadedCheckpoint && startSegment == 0 {
hasRootCheckpoint, err := checkpointer.HasRootCheckpoint()
if err != nil {
return fmt.Errorf("cannot check root checkpoint existence: %w", err)
}
if hasRootCheckpoint {
flattenedForest, err := checkpointer.LoadRootCheckpoint()
if err != nil {
return fmt.Errorf("cannot load root checkpoint: %w", err)
}
err = checkpointFn(flattenedForest)
if err != nil {
return fmt.Errorf("error while handling root checkpoint: %w", err)
}
}
}
sr, err := prometheusWAL.NewSegmentsRangeReader(prometheusWAL.SegmentRange{
Dir: w.wal.Dir(),
First: startSegment,
Last: to,
})
if err != nil {
return fmt.Errorf("cannot create segment reader: %w", err)
}
reader := prometheusWAL.NewReader(sr)
defer sr.Close()
for reader.Next() {
record := reader.Record()
operation, commitment, keys, values, err := Decode(record)
if err != nil {
return fmt.Errorf("cannot decode LedgerWAL record: %w", err)
}
switch operation {
case WALUpdate:
err = updateFn(commitment, keys, values)
if err != nil {
return fmt.Errorf("error while processing LedgerWAL update: %w", err)
}
case WALDelete:
err = deleteFn(commitment)
if err != nil {
return fmt.Errorf("error while processing LedgerWAL deletion: %w", err)
}
}
err = reader.Err()
if err != nil {
return fmt.Errorf("cannot read LedgerWAL: %w", err)
}
}
return nil
}
// NewCheckpointer returns a Checkpointer for this WAL
func (w *LedgerWAL) NewCheckpointer() (*Checkpointer, error) {
return NewCheckpointer(w, w.keyByteSize, w.forestCapacity), nil
}
func (w *LedgerWAL) Close() error {
return w.wal.Close()
}