forked from hyperledger-labs/mirbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersisted.go
317 lines (278 loc) · 7.06 KB
/
persisted.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mirbft
import (
"fmt"
pb "github.com/IBM/mirbft/mirbftpb"
)
type logIterator struct {
onQEntry func(*pb.QEntry)
onPEntry func(*pb.PEntry)
onCEntry func(*pb.CEntry)
onNEntry func(*pb.NEntry)
onFEntry func(*pb.FEntry)
onECEntry func(*pb.ECEntry)
onTEntry func(*pb.TEntry)
onSuspect func(*pb.Suspect)
shouldExit func() bool
// TODO, suspect_ready
}
type logEntry struct {
index uint64
entry *pb.Persistent
next *logEntry
}
type persisted struct {
nextIndex uint64
logHead *logEntry
logTail *logEntry
logger Logger
}
func newPersisted(logger Logger) *persisted {
return &persisted{
logger: logger,
}
}
func (p *persisted) appendInitialLoad(entry *WALEntry) {
if p.logHead == nil {
p.nextIndex = entry.Index
p.logHead = &logEntry{
index: entry.Index,
entry: entry.Data,
}
p.logTail = p.logHead
} else {
p.logTail.next = &logEntry{
index: entry.Index,
entry: entry.Data,
}
p.logTail = p.logTail.next
}
if p.nextIndex != entry.Index {
panic(fmt.Sprintf("WAL indexes out of order! Expected %d got %d, was your WAL corrupted?", p.nextIndex, entry.Index))
}
p.nextIndex = entry.Index + 1
}
func (p *persisted) appendLogEntry(entry *pb.Persistent) *Actions {
p.logTail.next = &logEntry{
index: p.nextIndex,
entry: entry,
}
p.logTail = p.logTail.next
result := (&Actions{}).persist(p.nextIndex, entry)
p.nextIndex++
return result
}
func (p *persisted) addPEntry(pEntry *pb.PEntry) *Actions {
d := &pb.Persistent{
Type: &pb.Persistent_PEntry{
PEntry: pEntry,
},
}
return p.appendLogEntry(d)
}
func (p *persisted) addQEntry(qEntry *pb.QEntry) *Actions {
d := &pb.Persistent{
Type: &pb.Persistent_QEntry{
QEntry: qEntry,
},
}
return p.appendLogEntry(d)
}
func (p *persisted) addNEntry(nEntry *pb.NEntry) *Actions {
d := &pb.Persistent{
Type: &pb.Persistent_NEntry{
NEntry: nEntry,
},
}
return p.appendLogEntry(d)
}
func (p *persisted) addCEntry(cEntry *pb.CEntry) *Actions {
assertNotEqual(cEntry.NetworkState, nil, "network config must be set")
d := &pb.Persistent{
Type: &pb.Persistent_CEntry{
CEntry: cEntry,
},
}
return p.appendLogEntry(d)
}
func (p *persisted) addSuspect(suspect *pb.Suspect) *Actions {
d := &pb.Persistent{
Type: &pb.Persistent_Suspect{
Suspect: suspect,
},
}
return p.appendLogEntry(d)
}
func (p *persisted) addECEntry(ecEntry *pb.ECEntry) *Actions {
d := &pb.Persistent{
Type: &pb.Persistent_ECEntry{
ECEntry: ecEntry,
},
}
return p.appendLogEntry(d)
}
func (p *persisted) addTEntry(tEntry *pb.TEntry) *Actions {
d := &pb.Persistent{
Type: &pb.Persistent_TEntry{
TEntry: tEntry,
},
}
return p.appendLogEntry(d)
}
func (p *persisted) truncate(lowWatermark uint64) *Actions {
for logEntry := p.logHead; logEntry != nil; logEntry = logEntry.next {
switch d := logEntry.entry.Type.(type) {
case *pb.Persistent_CEntry:
if d.CEntry.SeqNo < lowWatermark {
continue
}
case *pb.Persistent_NEntry:
if d.NEntry.SeqNo <= lowWatermark {
continue
}
default:
continue
}
p.logger.Log(LevelDebug, "truncating WAL", "seq_no", lowWatermark, "index", logEntry.index)
if p.logHead == logEntry {
break
}
p.logHead = logEntry
return &Actions{
WriteAhead: []*Write{
{
Truncate: &logEntry.index,
},
},
}
}
return &Actions{}
}
// staticcheck hack
var _ = (&persisted{}).logEntries
// logWAL is not called in the course of normal operation but it can be extremely useful
// to call from other parts of the code in debugging situations
func (p *persisted) logEntries() {
p.logger.Log(LevelDebug, "printing persisted log entries")
for logEntry := p.logHead; logEntry != nil; logEntry = logEntry.next {
p.logger.Log(LevelDebug, " log entry", "type", fmt.Sprintf("%T", logEntry.entry.Type), "index", logEntry.index, "value", fmt.Sprintf("%+v", logEntry.entry))
}
}
func (p *persisted) iterate(li logIterator) {
for logEntry := p.logHead; logEntry != nil; logEntry = logEntry.next {
switch d := logEntry.entry.Type.(type) {
case *pb.Persistent_PEntry:
if li.onPEntry != nil {
li.onPEntry(d.PEntry)
}
case *pb.Persistent_QEntry:
if li.onQEntry != nil {
li.onQEntry(d.QEntry)
}
case *pb.Persistent_CEntry:
if li.onCEntry != nil {
li.onCEntry(d.CEntry)
}
case *pb.Persistent_NEntry:
if li.onNEntry != nil {
li.onNEntry(d.NEntry)
}
case *pb.Persistent_FEntry:
if li.onFEntry != nil {
li.onFEntry(d.FEntry)
}
case *pb.Persistent_ECEntry:
if li.onECEntry != nil {
li.onECEntry(d.ECEntry)
}
case *pb.Persistent_TEntry:
if li.onTEntry != nil {
li.onTEntry(d.TEntry)
}
case *pb.Persistent_Suspect:
if li.onSuspect != nil {
li.onSuspect(d.Suspect)
}
// TODO, suspect_ready
default:
panic(fmt.Sprintf("unsupported log entry type '%T'", logEntry.entry.Type))
}
if li.shouldExit != nil && li.shouldExit() {
break
}
}
}
func (p *persisted) constructEpochChange(newEpoch uint64) *pb.EpochChange {
newEpochChange := &pb.EpochChange{
NewEpoch: newEpoch,
}
// To avoid putting redundant entries into the pSet, we count
// how many are in the log for each sequence so that we may
// skip all but the last entry for each sequence number
pSkips := map[uint64]int{}
var logEpoch *uint64
p.iterate(logIterator{
shouldExit: func() bool {
return logEpoch != nil && *logEpoch >= newEpoch
},
onPEntry: func(pEntry *pb.PEntry) {
count := pSkips[pEntry.SeqNo]
pSkips[pEntry.SeqNo] = count + 1
},
onNEntry: func(nEntry *pb.NEntry) {
logEpoch = &nEntry.EpochConfig.Number
},
onFEntry: func(fEntry *pb.FEntry) {
logEpoch = &fEntry.EndsEpochConfig.Number
},
})
logEpoch = nil
p.iterate(logIterator{
shouldExit: func() bool {
return logEpoch != nil && *logEpoch >= newEpoch
},
onPEntry: func(pEntry *pb.PEntry) {
count := pSkips[pEntry.SeqNo]
if count != 1 {
pSkips[pEntry.SeqNo] = count - 1
return
}
newEpochChange.PSet = append(newEpochChange.PSet, &pb.EpochChange_SetEntry{
Epoch: *logEpoch,
SeqNo: pEntry.SeqNo,
Digest: pEntry.Digest,
})
},
onQEntry: func(qEntry *pb.QEntry) {
newEpochChange.QSet = append(newEpochChange.QSet, &pb.EpochChange_SetEntry{
Epoch: *logEpoch,
SeqNo: qEntry.SeqNo,
Digest: qEntry.Digest,
})
},
onNEntry: func(nEntry *pb.NEntry) {
logEpoch = &nEntry.EpochConfig.Number
},
onFEntry: func(fEntry *pb.FEntry) {
logEpoch = &fEntry.EndsEpochConfig.Number
},
onCEntry: func(cEntry *pb.CEntry) {
newEpochChange.Checkpoints = append(newEpochChange.Checkpoints, &pb.Checkpoint{
SeqNo: cEntry.SeqNo,
Value: cEntry.CheckpointValue,
})
},
/*
// This is actually okay, since we could be catching up and need to skip epochs
onECEntry: func(ecEntry *pb.ECEntry) {
if logEpoch != nil && *logEpoch+1 != ecEntry.EpochNumber {
panic(fmt.Sprintf("dev sanity test: expected epochChange target %d to be exactly one more than our current epoch %d", ecEntry.EpochNumber, *logEpoch))
}
},
*/
})
return newEpochChange
}