/
wal.go
354 lines (309 loc) · 10.5 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
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
// Package wal implements the similar write-ahead logging for pbft consensus.
package wal
import (
"encoding/binary"
"errors"
"os"
"path/filepath"
"sync/atomic"
"time"
"github.com/PhoenixGlobal/Phoenix-Chain-Core/consensus/pbft/protocols"
ctypes "github.com/PhoenixGlobal/Phoenix-Chain-Core/consensus/pbft/types"
"github.com/PhoenixGlobal/Phoenix-Chain-Core/ethereum/node"
"github.com/PhoenixGlobal/Phoenix-Chain-Core/libs/log"
"github.com/PhoenixGlobal/Phoenix-Chain-Core/libs/rlp"
)
const (
// Wal working directory
walDir = "wal"
// Wal database name
metaDBName = "wal_meta"
)
var (
chainStateKey = []byte("chain-state") // Key of chainState to store leveldb
viewChangeKey = []byte("view-change") // Key of viewChange to store leveldb
viewChangeQCSplit = []byte("qs")
viewChangeQCPrefix = []byte("view-change-qc") // viewChangeQCPrefix + epoch (uint64 big endian) + viewChangeQCSplit + blockNumber (uint64 big endian) + viewChangeQCSplit + viewNumber (uint64 big endian) -> viewChangeQC
)
var (
errCreateWalDir = errors.New("failed to create wal directory")
errUpdateViewChangeMeta = errors.New("failed to update viewChange meta")
errGetViewChangeMeta = errors.New("failed to get viewChange meta")
errGetChainState = errors.New("failed to get chainState")
errGetViewChangeQC = errors.New("failed to get viewChangeQC")
)
// recoveryChainStateFn is a callback type for recovery chainState to consensus.
type recoveryChainStateFn func(chainState *protocols.ChainState) error
// recoveryConsensusMsgFn is a callback type for recovery message to consensus.
type recoveryConsensusMsgFn func(msg interface{}) error
type ViewChangeMessage struct {
Epoch uint64
ViewNumber uint64
BlockNumber uint64
FileID uint32
Seq uint64
}
// Wal encapsulates functions required to update and load consensus state.
type Wal interface {
UpdateChainState(chainState *protocols.ChainState) error
LoadChainState(fn recoveryChainStateFn) error
Write(msg interface{}) error
WriteSync(msg interface{}) error
UpdateViewChange(info *ViewChangeMessage) error
UpdateViewChangeQC(epoch uint64, blockNumber uint64,viewNumber uint64, viewChangeQC *ctypes.ViewChangeQC) error
GetViewChangeQC(epoch uint64, blockNumber uint64,viewNumber uint64) (*ctypes.ViewChangeQC, error)
Load(fn recoveryConsensusMsgFn) error
Close()
SetMockJournalLimitSize(limit uint64)
}
// emptyWal is a empty implementation for wal
type emptyWal struct {
}
func WalDir(ctx *node.ServiceContext) string {
return ctx.ResolvePath(walDir)
}
func (w *emptyWal) UpdateChainState(chainState *protocols.ChainState) error {
return nil
}
func (w *emptyWal) LoadChainState(fn recoveryChainStateFn) error {
return nil
}
func (w *emptyWal) Write(msg interface{}) error {
return nil
}
func (w *emptyWal) WriteSync(msg interface{}) error {
return nil
}
func (w *emptyWal) UpdateViewChange(info *ViewChangeMessage) error {
return nil
}
func (w *emptyWal) UpdateViewChangeQC(epoch uint64, blockNumber uint64,viewNumber uint64, viewChangeQC *ctypes.ViewChangeQC) error {
return nil
}
func (w *emptyWal) GetViewChangeQC(epoch uint64, blockNumber uint64,viewNumber uint64) (*ctypes.ViewChangeQC, error) {
return nil, nil
}
func (w *emptyWal) Load(fn recoveryConsensusMsgFn) error {
return nil
}
func (w *emptyWal) Close() {
}
func (w *emptyWal) SetMockJournalLimitSize(limit uint64) {
}
// baseWal is a default implementation for wal
type baseWal struct {
path string // Wal working directory
metaDB IWALDatabase
journal *journal
cachedChainState atomic.Value //*protocols.ChainState
}
// NewWal creates a new wal to update and load consensus state.
func NewWal(ctx *node.ServiceContext, specifiedPath string) (Wal, error) {
if ctx == nil && len(specifiedPath) == 0 {
return &emptyWal{}, nil
}
var (
originPath = specifiedPath
metaDB IWALDatabase
walPath string
journal *journal
err error
)
if originPath == "" {
originPath = WalDir(ctx)
}
// Make sure the wal directory exists,If not exist create it.
ensureWalDir := func(path string) (string, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.MkdirAll(path, 0700)
if err != nil {
return "", errCreateWalDir
}
}
return path, nil
}
// Open or create WAL Database
createMetaDB := func(path, name string) (IWALDatabase, error) {
db, err := createWalDB(filepath.Join(path, name))
if err != nil {
return nil, err
}
return db, nil
}
if walPath, err = ensureWalDir(originPath); err != nil {
log.Error("Failed to create wal directory", "err", err)
return nil, err
}
if journal, err = newJournal(walPath); err != nil {
return nil, err
}
if metaDB, err = createMetaDB(walPath, metaDBName); err != nil {
log.Error("Failed to create wal database", "err", err)
return nil, err
}
wal := &baseWal{
path: walPath,
metaDB: metaDB,
journal: journal,
}
return wal, nil
}
// UpdateChainState tries to update consensus state to leveldb
func (wal *baseWal) UpdateChainState(chainState *protocols.ChainState) error {
data, err := rlp.EncodeToBytes(chainState)
if err != nil {
return err
}
// Write the chainState to the WAL database
err = wal.metaDB.Put(chainStateKey, data, nil)
if err != nil {
return err
}
wal.cachedChainState.Store(chainState)
log.Debug("Success to update chainState")
return nil
}
// LoadChainState tries to load consensus state from leveldb
func (wal *baseWal) LoadChainState(recovery recoveryChainStateFn) error {
if wal.cachedChainState.Load() != nil {
return recovery(wal.cachedChainState.Load().(*protocols.ChainState))
}
// open wal database
data, err := wal.metaDB.Get(chainStateKey)
if err != nil {
log.Warn("Failed to get chainState from db, may be the first time to run phoenixchain")
return nil
}
var cs protocols.ChainState
err = rlp.DecodeBytes(data, &cs)
if err != nil {
log.Error("Failed to decode chainState")
return errGetChainState
}
wal.cachedChainState.Store(&cs)
return recovery(&cs)
}
// Write adds the specified consensus msg to the local disk journal.
// the mode is asynchronous write,the msg will cache in bufio.Writer
func (wal *baseWal) Write(msg interface{}) error {
return wal.journal.Insert(&Message{
Timestamp: uint64(time.Now().UnixNano()),
Data: msg,
}, false)
}
// WriteSync adds the specified consensus msg to the local disk journal.
// the mode is synchronous write,the msg will flush to disk immediately
func (wal *baseWal) WriteSync(msg interface{}) error {
return wal.journal.Insert(&Message{
Timestamp: uint64(time.Now().UnixNano()),
Data: msg,
}, true)
}
// UpdateViewChange tries to update consensus confirm viewChange to leveldb
func (wal *baseWal) UpdateViewChange(info *ViewChangeMessage) error {
return wal.updateViewChangeMeta(info)
}
// updateViewChangeMeta update the ViewChange Meta Data to the database.
func (wal *baseWal) updateViewChangeMeta(vc *ViewChangeMessage) error {
fileID, seq, err := wal.journal.CurrentJournal()
if err != nil {
log.Error("Failed to update viewChange meta", "epoch", vc.Epoch, "viewNumber", vc.ViewNumber,"blockNumber", vc.BlockNumber, "err", err)
return errUpdateViewChangeMeta
}
vc.FileID = fileID
vc.Seq = seq
data, err := rlp.EncodeToBytes(vc)
if err != nil {
return err
}
// Write the ViewChangeMeta to the WAL database
err = wal.metaDB.Put(viewChangeKey, data, nil)
if err != nil {
return err
}
log.Debug("Success to update viewChange meta", "epoch", vc.Epoch, "viewNumber", vc.ViewNumber, "fileID", fileID, "seq", seq)
// Delete previous journal logs
go wal.journal.ExpireJournalFile(fileID)
return nil
}
// Load tries to load consensus msg from the local disk journal.
// recovery is the callback function
func (wal *baseWal) Load(recovery recoveryConsensusMsgFn) error {
// open wal database
data, err := wal.metaDB.Get(viewChangeKey)
if err != nil {
log.Warn("Failed to get viewChange meta from db, may be the first time to run phoenixchain")
return nil
}
var vc ViewChangeMessage
err = rlp.DecodeBytes(data, &vc)
if err != nil {
log.Error("Failed to decode viewChange meta")
return errGetViewChangeMeta
}
return wal.journal.LoadJournal(vc.FileID, vc.Seq, recovery)
}
// epochKey = viewChangeQCPrefix + epoch (uint64 big endian) + viewChangeQCSplit
func epochKey(epoch uint64) []byte {
e := make([]byte, 8)
binary.BigEndian.PutUint64(e, epoch)
return append(append(viewChangeQCPrefix, e...), viewChangeQCSplit...)
}
// viewChangeQCKey = viewChangeQCPrefix + epoch (uint64 big endian) + viewChangeQCSplit + viewNumber (uint64 big endian)
func viewChangeQCKey(epoch uint64, blockNumber uint64,viewNumber uint64) []byte {
e := make([]byte, 8)
binary.BigEndian.PutUint64(e, epoch)
b:= make([]byte, 8)
binary.BigEndian.PutUint64(b, blockNumber)
v := make([]byte, 8)
binary.BigEndian.PutUint64(v, viewNumber)
return append(append(append(append(append(viewChangeQCPrefix, e...), viewChangeQCSplit...), b...),viewChangeQCSplit...),v...)
}
// UpdateViewChangeQC tries to save consensus confirm viewChangeQC to leveldb
func (wal *baseWal) UpdateViewChangeQC(epoch uint64, blockNumber uint64,viewNumber uint64, viewChangeQC *ctypes.ViewChangeQC) error {
data, err := rlp.EncodeToBytes(viewChangeQC)
if err != nil {
return err
}
// Write the ViewChangeQC to the WAL database
err = wal.metaDB.Put(viewChangeQCKey(epoch, blockNumber,viewNumber), data, nil)
if err != nil {
return err
}
log.Debug("Success to update viewChangeQC", "epoch", epoch, "blockNumber", blockNumber,"viewNumber", viewNumber, "viewChangeQC", viewChangeQC.String())
go wal.deleteViewChangeQC(epoch - 1)
return nil
}
// deleteViewChangeQC tries to delete viewChangeQC by gaving epoch
// we keep viewChangeQC only one epoch
// if the higher epoch comes, the lower epoch will be deleted
func (wal *baseWal) deleteViewChangeQC(epoch uint64) {
it := wal.metaDB.NewIterator(epochKey(epoch), nil)
for it.Next() {
key := it.Key()
wal.metaDB.Delete(key)
}
}
// GetViewChangeQC retrieves a viewChangeQC from the database by
// epoch, viewNumber if found.
func (wal *baseWal) GetViewChangeQC(epoch uint64, blockNumber uint64,viewNumber uint64) (*ctypes.ViewChangeQC, error) {
// open wal database
data, err := wal.metaDB.Get(viewChangeQCKey(epoch, blockNumber,viewNumber))
if err != nil {
return nil, err
}
var qc ctypes.ViewChangeQC
err = rlp.DecodeBytes(data, &qc)
if err != nil {
log.Error("Failed to decode viewChangeQC")
return nil, errGetViewChangeQC
}
return &qc, nil
}
func (wal *baseWal) Close() {
wal.metaDB.Close()
wal.journal.Close()
}
func (wal *baseWal) SetMockJournalLimitSize(limit uint64) {
wal.journal.mockJournalLimitSize = limit
}