-
Notifications
You must be signed in to change notification settings - Fork 2
/
wal.go
221 lines (192 loc) · 5.42 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
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package raftwal
import (
"bytes"
"encoding/binary"
"github.com/coreos/etcd/raft"
"github.com/coreos/etcd/raft/raftpb"
"github.com/journeymidnight/badger"
"github.com/journeymidnight/nentropy/helper"
"github.com/journeymidnight/nentropy/storage/engine"
)
type Wal struct {
eng engine.Engine
id uint64
}
func Init(eng engine.Engine, id uint64) *Wal {
return &Wal{eng: eng, id: id}
}
func (w *Wal) snapshotKey(gid uint32) []byte {
b := make([]byte, 14)
binary.BigEndian.PutUint64(b[0:8], w.id)
copy(b[8:10], []byte("ss"))
binary.BigEndian.PutUint32(b[10:14], gid)
return b
}
func (w *Wal) hardStateKey(gid uint32) []byte {
b := make([]byte, 14)
binary.BigEndian.PutUint64(b[0:8], w.id)
copy(b[8:10], []byte("hs"))
binary.BigEndian.PutUint32(b[10:14], gid)
return b
}
func (w *Wal) entryKey(gid uint32, term, idx uint64) []byte {
b := make([]byte, 28)
binary.BigEndian.PutUint64(b[0:8], w.id)
binary.BigEndian.PutUint32(b[8:12], gid)
binary.BigEndian.PutUint64(b[12:20], term)
binary.BigEndian.PutUint64(b[20:28], idx)
return b
}
func (w *Wal) prefix(gid uint32) []byte {
b := make([]byte, 12)
binary.BigEndian.PutUint64(b[0:8], w.id)
binary.BigEndian.PutUint32(b[8:12], gid)
return b
}
func (w *Wal) StoreSnapshot(gid uint32, s raftpb.Snapshot) error {
if raft.IsEmptySnap(s) {
return nil
}
data, err := s.Marshal()
if err != nil {
return helper.Wrapf(err, "wal.Store: While marshal snapshot")
}
if err := w.eng.Put(w.snapshotKey(gid), data); err != nil {
return err
}
helper.Printf(10, "Writing snapshot to WAL: %+v\n", s)
// Delete all entries before this snapshot to save disk space.
start := w.entryKey(gid, 0, 0)
last := w.entryKey(gid, s.Metadata.Term, s.Metadata.Index)
batch := w.eng.NewBatch(true)
defer batch.Close()
it := batch.NewIterator()
//defer it.Close()
for it.Seek(start); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()
if bytes.Compare(k, last) > 0 {
break
}
err = batch.Clear(k)
if err != nil {
break
}
}
it.Close()
if err != nil {
batch.Close()
return err
}
if err = batch.Commit(); err != nil {
batch.Close()
return err
}
return nil
}
// Store stores the snapshot, hardstate and entries for a given RAFT group.
func (w *Wal) Store(gid uint32, h raftpb.HardState, es []raftpb.Entry) error {
batch := w.eng.NewBatch(true)
if !raft.IsEmptyHardState(h) {
data, err := h.Marshal()
if err != nil {
return helper.Wrapf(err, "wal.Store: While marshal hardstate")
}
err = batch.Put(w.hardStateKey(gid), data)
if err != nil {
batch.Close()
return err
}
}
var t, i uint64
for _, e := range es {
t, i = e.Term, e.Index
data, err := e.Marshal()
if err != nil {
return helper.Wrapf(err, "wal.Store: While marshal entry")
}
k := w.entryKey(gid, e.Term, e.Index)
err = batch.Put(k, data)
if err != nil {
batch.Close()
return err
}
}
// If we get no entries, then the default value of t and i would be zero. That would
// end up deleting all the previous valid raft entry logs. This check avoids that.
if t > 0 || i > 0 {
// When writing an Entry with Index i, any previously-persisted entries
// with Index >= i must be discarded.
start := w.entryKey(gid, t, i+1)
prefix := w.prefix(gid)
itr := batch.NewIterator()
for itr.Seek(start); itr.ValidForPrefix(prefix); itr.Next() {
key := itr.Item().Key()
newk := make([]byte, len(key))
copy(newk, key)
err := batch.Clear(newk)
if err != nil {
break
}
}
itr.Close()
}
if err := batch.Commit(); err != nil {
batch.Close()
return err
}
return nil
}
func (w *Wal) Snapshot(gid uint32) (snap raftpb.Snapshot, rerr error) {
val, err := w.eng.Get(w.snapshotKey(gid))
if err != nil && err != badger.ErrKeyNotFound {
rerr = helper.Wrapf(err, "While getting snapshot")
return
}
rerr = helper.Wrapf(snap.Unmarshal(val), "While unmarshal snapshot")
return
}
func (w *Wal) HardState(gid uint32) (hd raftpb.HardState, rerr error) {
val, err := w.eng.Get(w.hardStateKey(gid))
if err != nil && err != badger.ErrKeyNotFound {
rerr = helper.Wrapf(err, "While getting hardstate")
return
}
rerr = helper.Wrapf(hd.Unmarshal(val), "While unmarshal hardstate")
return
}
func (w *Wal) Entries(gid uint32, fromTerm, fromIndex uint64) (es []raftpb.Entry, rerr error) {
it := w.eng.NewIterator()
defer it.Close()
prefix := w.prefix(gid)
start := w.entryKey(gid, fromTerm, fromIndex)
for it.Seek(start); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
v, err := item.Value()
if err != nil {
return es, helper.Wrapf(err, "While unmarshal raftpb.Entry")
}
var e raftpb.Entry
if err := e.Unmarshal(v); err != nil {
return es, helper.Wrapf(err, "While unmarshal raftpb.Entry")
}
es = append(es, e)
}
return
}