-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.go
133 lines (119 loc) · 3.07 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
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
/*
reference: https://eli.thegreenplace.net/2020/implementing-raft-part-3-persistence-and-optimizations/
*/
package raft
import (
"bytes"
"encoding/gob"
"log"
"github.com/boltdb/bolt"
)
const (
currentTermKey = "currentTerm"
votedForKey = "votedFor"
logKey = "log"
bucketName = "raft"
)
// Persistence is an interface implemented by stable storage providers.
type Persistence interface {
Set(key string, value []byte)
Get(key string) ([]byte, bool)
HasData() bool
}
// BoltDBStorage is an implementation of the Persistence interface using BoltDB.
type BoltDBStorage struct {
db *bolt.DB
}
// NewBoltDBStorage creates a new BoltDBStorage instance.
func NewBoltDBStorage(dbPath string) (*BoltDBStorage, error) {
db, err := bolt.Open(dbPath, 0600, nil)
if err != nil {
return nil, err
}
return &BoltDBStorage{db: db}, nil
}
// Set writes a key-value pair to the database.
func (s *BoltDBStorage) Set(key string, value []byte) {
err := s.db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(bucketName))
if err != nil {
return err
}
return b.Put([]byte(key), value)
})
if err != nil {
log.Fatal(err)
}
}
// Get retrieves a value for a given key from the database.
func (s *BoltDBStorage) Get(key string) ([]byte, bool) {
var value []byte
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
if b == nil {
return nil
}
value = b.Get([]byte(key))
return nil
})
if err != nil {
log.Fatal(err)
}
return value, value != nil
}
// HasData checks if there is any data in the persistence.
func (s *BoltDBStorage) HasData() bool {
if v, found := s.Get(currentTermKey); found {
return v != nil
}
if v, found := s.Get(votedForKey); found {
return v != nil
}
if v, found := s.Get(logKey); found {
return v != nil
}
return false
}
func (n *Node) restoreFromStorage() {
if termData, found := n.persistence.Get(currentTermKey); found {
d := gob.NewDecoder(bytes.NewBuffer(termData))
if err := d.Decode(&n.currentTerm); err != nil {
log.Fatal(err)
}
} else {
log.Fatal("currentTerm not found in storage")
}
if votedData, found := n.persistence.Get(votedForKey); found {
d := gob.NewDecoder(bytes.NewBuffer(votedData))
if err := d.Decode(&n.votedFor); err != nil {
log.Fatal(err)
}
} else {
log.Fatal("votedFor not found in storage")
}
if logData, found := n.persistence.Get(logKey); found {
d := gob.NewDecoder(bytes.NewBuffer(logData))
if err := d.Decode(&n.log); err != nil {
log.Fatal(err)
}
} else {
log.Fatal("log not found in storage")
}
}
func (n *Node) persist() {
var termData bytes.Buffer
if err := gob.NewEncoder(&termData).Encode(n.currentTerm); err != nil {
log.Fatal(err)
}
n.persistence.Set(currentTermKey, termData.Bytes())
var votedData bytes.Buffer
if err := gob.NewEncoder(&votedData).Encode(n.votedFor); err != nil {
log.Fatal(err)
}
n.persistence.Set(votedForKey, votedData.Bytes())
var logData bytes.Buffer
if err := gob.NewEncoder(&logData).Encode(n.log); err != nil {
log.Fatal(err)
}
n.persistence.Set(logKey, logData.Bytes())
}