forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wal.go
291 lines (265 loc) · 7.45 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
/*
* 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/dgraph-io/badger"
"github.com/dgraph-io/dgraph/x"
)
type Wal struct {
wals *badger.ManagedDB
id uint64
}
func Init(walStore *badger.ManagedDB, id uint64) *Wal {
w := &Wal{wals: walStore, id: id}
x.Check(w.StoreRaftId(id))
return w
}
var idKey = []byte("raftid")
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) StoreRaftId(id uint64) error {
txn := w.wals.NewTransactionAt(1, true)
defer txn.Discard()
var b [8]byte
binary.BigEndian.PutUint64(b[:], id)
if err := txn.Set(idKey, b[:]); err != nil {
return err
}
return txn.CommitAt(1, nil)
}
func RaftId(wals *badger.ManagedDB) (uint64, error) {
txn := wals.NewTransactionAt(1, false)
defer txn.Discard()
item, err := txn.Get(idKey)
if err == badger.ErrKeyNotFound {
return 0, nil
}
if err != nil {
return 0, err
}
val, err := item.Value()
if err != nil {
return 0, err
}
id := binary.BigEndian.Uint64(val)
return id, nil
}
func (w *Wal) StoreSnapshot(gid uint32, s raftpb.Snapshot) error {
txn := w.wals.NewTransactionAt(1, true)
defer txn.Discard()
if raft.IsEmptySnap(s) {
return nil
}
data, err := s.Marshal()
if err != nil {
return x.Wrapf(err, "wal.Store: While marshal snapshot")
}
if err := txn.Set(w.snapshotKey(gid), data); err != nil {
return err
}
x.Printf("Writing snapshot to WAL, metadata: %+v, len(data): %d\n", s.Metadata, len(s.Data))
// 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)
opt := badger.DefaultIteratorOptions
opt.PrefetchValues = false
itr := txn.NewIterator(opt)
defer itr.Close()
for itr.Seek(start); itr.Valid(); itr.Next() {
key := itr.Item().Key()
if bytes.Compare(key, last) > 0 {
break
}
newk := make([]byte, len(key))
copy(newk, key)
if err := txn.Delete(newk); err == badger.ErrTxnTooBig {
if err := txn.CommitAt(1, nil); err != nil {
return err
}
txn = w.wals.NewTransactionAt(1, true)
if err := txn.Delete(newk); err != nil {
return err
}
} else if err != nil {
return err
}
}
// Failure to delete entries is not a fatal error, so should be
// ok to ignore
if err := txn.CommitAt(1, nil); err != nil {
x.Printf("Error while storing snapshot %v\n", err)
return err
}
return nil
}
// Store stores the hardstate and entries for a given RAFT group.
func (w *Wal) Store(gid uint32, h raftpb.HardState, es []raftpb.Entry) error {
txn := w.wals.NewTransactionAt(1, true)
var t, i uint64
for _, e := range es {
t, i = e.Term, e.Index
data, err := e.Marshal()
if err != nil {
return x.Wrapf(err, "wal.Store: While marshal entry")
}
k := w.entryKey(gid, e.Term, e.Index)
if err := txn.Set(k, data); err == badger.ErrTxnTooBig {
if err := txn.CommitAt(1, nil); err != nil {
return err
}
txn = w.wals.NewTransactionAt(1, true)
if err := txn.Set(k, data); err != nil {
return err
}
} else if err != nil {
return err
}
}
if !raft.IsEmptyHardState(h) {
data, err := h.Marshal()
if err != nil {
return x.Wrapf(err, "wal.Store: While marshal hardstate")
}
if err := txn.Set(w.hardStateKey(gid), data); err != nil {
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.
// Ideally we should be deleting entries from previous term with index >= i,
// but to avoid complexity we remove them during reading from wal.
start := w.entryKey(gid, t, i+1)
prefix := w.prefix(gid)
opt := badger.DefaultIteratorOptions
opt.PrefetchValues = false
itr := txn.NewIterator(opt)
defer itr.Close()
for itr.Seek(start); itr.ValidForPrefix(prefix); itr.Next() {
key := itr.Item().Key()
newk := make([]byte, len(key))
copy(newk, key)
if err := txn.Delete(newk); err == badger.ErrTxnTooBig {
if err := txn.CommitAt(1, nil); err != nil {
return err
}
txn = w.wals.NewTransactionAt(1, true)
if err := txn.Delete(newk); err != nil {
return err
}
} else if err != nil {
return err
}
}
}
return txn.CommitAt(1, nil)
}
func (w *Wal) Snapshot(gid uint32) (snap raftpb.Snapshot, rerr error) {
txn := w.wals.NewTransactionAt(1, false)
defer txn.Discard()
item, err := txn.Get(w.snapshotKey(gid))
if err == badger.ErrKeyNotFound {
return
}
if err != nil {
return snap, x.Wrapf(err, "while fetching snapshot from wal")
}
val, err := item.Value()
if err != nil {
return
}
rerr = x.Wrapf(snap.Unmarshal(val), "While unmarshal snapshot")
return
}
func (w *Wal) HardState(gid uint32) (hd raftpb.HardState, rerr error) {
txn := w.wals.NewTransactionAt(1, false)
defer txn.Discard()
item, err := txn.Get(w.hardStateKey(gid))
if err == badger.ErrKeyNotFound {
return
}
if err != nil {
return hd, x.Wrapf(err, "while fetching hardstate from wal")
}
val, err := item.Value()
if err != nil {
return
}
rerr = x.Wrapf(hd.Unmarshal(val), "While unmarshal snapshot")
return
}
func (w *Wal) Entries(gid uint32, fromTerm, fromIndex uint64) (es []raftpb.Entry, rerr error) {
start := w.entryKey(gid, fromTerm, fromIndex)
prefix := w.prefix(gid)
txn := w.wals.NewTransactionAt(1, false)
defer txn.Discard()
itr := txn.NewIterator(badger.DefaultIteratorOptions)
defer itr.Close()
var firstIndex uint64
for itr.Seek(start); itr.ValidForPrefix(prefix); itr.Next() {
item := itr.Item()
var e raftpb.Entry
val, err := item.Value()
if err != nil {
return es, err
}
if err = e.Unmarshal(val); err != nil {
return es, err
}
if e.Index < fromIndex {
continue
}
if firstIndex == 0 {
firstIndex = e.Index
}
// When you see entry with Index i, ignore all entries with index >= i
es = append(es[:e.Index-firstIndex], e)
}
return
}