forked from Consensys/quorum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.go
57 lines (47 loc) · 1.38 KB
/
persistence.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
package raft
import (
"encoding/binary"
"github.com/ethereum/go-ethereum/log"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/opt"
)
var (
noFsync = &opt.WriteOptions{
NoWriteMerge: false,
Sync: false,
}
)
func openQuorumRaftDb(path string) (db *leveldb.DB, err error) {
// Open the db and recover any potential corruptions
db, err = leveldb.OpenFile(path, &opt.Options{
OpenFilesCacheCapacity: -1, // -1 means 0??
BlockCacheCapacity: -1,
})
if _, corrupted := err.(*errors.ErrCorrupted); corrupted {
db, err = leveldb.RecoverFile(path, nil)
}
return
}
func (pm *ProtocolManager) loadAppliedIndex() uint64 {
dat, err := pm.quorumRaftDb.Get(appliedDbKey, nil)
var lastAppliedIndex uint64
if err == errors.ErrNotFound {
lastAppliedIndex = 0
} else if err != nil {
fatalf("loadAppliedIndex error: %s", err)
} else {
lastAppliedIndex = binary.LittleEndian.Uint64(dat)
}
pm.mu.Lock()
pm.appliedIndex = lastAppliedIndex
pm.mu.Unlock()
log.Info("loaded the latest applied index", "lastAppliedIndex", lastAppliedIndex)
return lastAppliedIndex
}
func (pm *ProtocolManager) writeAppliedIndex(index uint64) {
log.Info("persisted the latest applied index", "index", index)
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, index)
pm.quorumRaftDb.Put(appliedDbKey, buf, noFsync)
}