forked from Consensys/quorum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.go
75 lines (63 loc) · 1.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package raft
import (
"encoding/binary"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
var (
noFsync = &opt.WriteOptions{
NoWriteMerge: false,
Sync: false,
}
mustFsync = &opt.WriteOptions{
NoWriteMerge: false,
Sync: true,
}
)
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 {
glog.Fatalln(err)
} else {
lastAppliedIndex = binary.LittleEndian.Uint64(dat)
}
glog.V(logger.Info).Infof("Persistent applied index load: %d", lastAppliedIndex)
pm.appliedIndex = lastAppliedIndex
return lastAppliedIndex
}
func (pm *ProtocolManager) writeAppliedIndex(index uint64) {
glog.V(logger.Info).Infof("Persistent applied index write: %d", index)
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, index)
pm.quorumRaftDb.Put(appliedDbKey, buf, noFsync)
}
func (pm *ProtocolManager) loadPeerUrl(nodeId uint64) string {
peerUrlKey := []byte(peerUrlKeyPrefix + string(nodeId))
value, err := pm.quorumRaftDb.Get(peerUrlKey, nil)
if err != nil {
glog.Fatalf("failed to read peer url for peer %d from leveldb: %v", nodeId, err)
}
return string(value)
}
func (pm *ProtocolManager) writePeerUrl(nodeId uint64, url string) {
key := []byte(peerUrlKeyPrefix + string(nodeId))
value := []byte(url)
pm.quorumRaftDb.Put(key, value, mustFsync)
}