This repository has been archived by the owner on Jan 17, 2018. It is now read-only.
/
log.go
462 lines (400 loc) · 12.2 KB
/
log.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package raft
import (
"encoding/binary"
"errors"
"fmt"
"hash/crc32"
"io"
"sync"
"time"
)
var (
errTermTooSmall = errors.New("term too small")
errIndexTooSmall = errors.New("index too small")
errIndexTooBig = errors.New("commit index too big")
errInvalidChecksum = errors.New("invalid checksum")
errNoCommand = errors.New("no command")
errBadIndex = errors.New("bad index")
errBadTerm = errors.New("bad term")
)
type raftLog struct {
sync.RWMutex
store io.Writer
entries []logEntry
commitPos int
apply func(uint64, []byte) []byte
}
func newRaftLog(store io.ReadWriter, apply func(uint64, []byte) []byte) *raftLog {
l := &raftLog{
store: store,
entries: []logEntry{},
commitPos: -1, // no commits to begin with
apply: apply,
}
l.recover(store)
return l
}
// recover reads from the log's store, to populate the log with log entries
// from persistent storage. It should be called once, at log instantiation.
func (l *raftLog) recover(r io.Reader) error {
for {
var entry logEntry
switch err := entry.decode(r); err {
case io.EOF:
return nil // successful completion
case nil:
if err := l.appendEntry(entry); err != nil {
return err
}
l.commitPos++
l.apply(entry.Index, entry.Command)
default:
return err // unsuccessful completion
}
}
}
// entriesAfter returns a slice of log entries after (i.e. not including) the
// passed index, and the term of the log entry specified by index, as a
// convenience to the caller. (This function is only used by a leader attempting
// to flush log entries to its followers.)
//
// This function is called to populate an AppendEntries RPC. That implies they
// are destined for a follower, which implies the application of the commands
// should have the response thrown away, which implies we shouldn't pass a
// commandResponse channel (see: commitTo implementation). In the normal case,
// the raftLogEntries we return here will get serialized as they pass thru their
// transport, and lose their commandResponse channel anyway. But in the case of
// a LocalPeer (or equivalent) this doesn't happen. So, we must make sure to
// proactively strip commandResponse channels.
func (l *raftLog) entriesAfter(index uint64) ([]logEntry, uint64) {
l.RLock()
defer l.RUnlock()
pos := 0
lastTerm := uint64(0)
for ; pos < len(l.entries); pos++ {
if l.entries[pos].Index > index {
break
}
lastTerm = l.entries[pos].Term
}
a := l.entries[pos:]
if len(a) == 0 {
return []logEntry{}, lastTerm
}
return stripResponseChannels(a), lastTerm
}
func stripResponseChannels(a []logEntry) []logEntry {
stripped := make([]logEntry, len(a))
for i, entry := range a {
stripped[i] = logEntry{
Index: entry.Index,
Term: entry.Term,
Command: entry.Command,
commandResponse: nil,
}
}
return stripped
}
// contains returns true if a log entry with the given index and term exists in
// the log.
func (l *raftLog) contains(index, term uint64) bool {
l.RLock()
defer l.RUnlock()
// It's not necessarily true that l.entries[i] has index == i.
for _, entry := range l.entries {
if entry.Index == index && entry.Term == term {
return true
}
if entry.Index > index || entry.Term > term {
break
}
}
return false
}
// ensureLastIs deletes all non-committed log entries after the given index and
// term. It will fail if the given index doesn't exist, has already been
// committed, or doesn't match the given term.
//
// This method satisfies the requirement that a log entry in an AppendEntries
// call precisely follows the accompanying LastraftLogTerm and LastraftLogIndex.
func (l *raftLog) ensureLastIs(index, term uint64) error {
l.Lock()
defer l.Unlock()
// Taken loosely from benbjohnson's impl
if index < l.getCommitIndexWithLock() {
return errIndexTooSmall
}
if index > l.lastIndexWithLock() {
return errIndexTooBig
}
// It's possible that the passed index is 0. It means the leader has come to
// decide we need a complete log rebuild. Of course, that's only valid if we
// haven't committed anything, so this check comes after that one.
if index == 0 {
for pos := 0; pos < len(l.entries); pos++ {
if l.entries[pos].commandResponse != nil {
close(l.entries[pos].commandResponse)
l.entries[pos].commandResponse = nil
}
if l.entries[pos].committed != nil {
l.entries[pos].committed <- false
close(l.entries[pos].committed)
l.entries[pos].committed = nil
}
}
l.entries = []logEntry{}
return nil
}
// Normal case: find the position of the matching log entry.
pos := 0
for ; pos < len(l.entries); pos++ {
if l.entries[pos].Index < index {
continue // didn't find it yet
}
if l.entries[pos].Index > index {
return errBadIndex // somehow went past it
}
if l.entries[pos].Index != index {
panic("not <, not >, but somehow !=")
}
if l.entries[pos].Term != term {
return errBadTerm
}
break // good
}
// Sanity check.
if pos < l.commitPos {
panic("index >= commitIndex, but pos < commitPos")
}
// `pos` is the position of log entry matching index and term.
// We want to truncate everything after that.
truncateFrom := pos + 1
if truncateFrom >= len(l.entries) {
return nil // nothing to truncate
}
// If we blow away log entries that haven't yet sent responses to clients,
// signal the clients to stop waiting, by closing the channel without a
// response value.
for pos = truncateFrom; pos < len(l.entries); pos++ {
if l.entries[pos].commandResponse != nil {
close(l.entries[pos].commandResponse)
l.entries[pos].commandResponse = nil
}
if l.entries[pos].committed != nil {
l.entries[pos].committed <- false
close(l.entries[pos].committed)
l.entries[pos].committed = nil
}
}
// Truncate the log.
l.entries = l.entries[:truncateFrom]
// Done.
return nil
}
// getCommitIndex returns the commit index of the log. That is, the index of the
// last log entry which can be considered committed.
func (l *raftLog) getCommitIndex() uint64 {
l.RLock()
defer l.RUnlock()
return l.getCommitIndexWithLock()
}
func (l *raftLog) getCommitIndexWithLock() uint64 {
if l.commitPos < 0 {
return 0
}
if l.commitPos >= len(l.entries) {
panic(fmt.Sprintf("commitPos %d > len(l.entries) %d; bad bookkeeping in raftLog", l.commitPos, len(l.entries)))
}
return l.entries[l.commitPos].Index
}
// lastIndex returns the index of the most recent log entry.
func (l *raftLog) lastIndex() uint64 {
l.RLock()
defer l.RUnlock()
return l.lastIndexWithLock()
}
func (l *raftLog) lastIndexWithLock() uint64 {
if len(l.entries) <= 0 {
return 0
}
return l.entries[len(l.entries)-1].Index
}
// lastTerm returns the term of the most recent log entry.
func (l *raftLog) lastTerm() uint64 {
l.RLock()
defer l.RUnlock()
return l.lastTermWithLock()
}
func (l *raftLog) lastTermWithLock() uint64 {
if len(l.entries) <= 0 {
return 0
}
return l.entries[len(l.entries)-1].Term
}
// appendEntry appends the passed log entry to the log. It will return an error
// if the entry's term is smaller than the log's most recent term, or if the
// entry's index is too small relative to the log's most recent entry.
func (l *raftLog) appendEntry(entry logEntry) error {
l.Lock()
defer l.Unlock()
if len(l.entries) > 0 {
lastTerm := l.lastTermWithLock()
if entry.Term < lastTerm {
return errTermTooSmall
}
lastIndex := l.lastIndexWithLock()
if entry.Term == lastTerm && entry.Index <= lastIndex {
return errIndexTooSmall
}
}
l.entries = append(l.entries, entry)
return nil
}
// commitTo commits all log entries up to and including the passed commitIndex.
// Commit means: synchronize the log entry to persistent storage, and call the
// state machine apply function for the log entry's command.
func (l *raftLog) commitTo(commitIndex uint64) error {
if commitIndex == 0 {
panic("commitTo(0)")
}
l.Lock()
defer l.Unlock()
// Reject old commit indexes
if commitIndex < l.getCommitIndexWithLock() {
return errIndexTooSmall
}
// Reject new commit indexes
if commitIndex > l.lastIndexWithLock() {
return errIndexTooBig
}
// If we've already committed to the commitIndex, great!
if commitIndex == l.getCommitIndexWithLock() {
return nil
}
// We should start committing at precisely the last commitPos + 1
pos := l.commitPos + 1
if pos < 0 {
panic("pending commit pos < 0")
}
// Commit entries between our existing commit index and the passed index.
// Remember to include the passed index.
for {
// Sanity checks. TODO replace with plain `for` when this is stable.
if pos >= len(l.entries) {
panic(fmt.Sprintf("commitTo pos=%d advanced past all log entries (%d)", pos, len(l.entries)))
}
if l.entries[pos].Index > commitIndex {
panic("commitTo advanced past the desired commitIndex")
}
// Encode the entry to persistent storage.
if err := l.entries[pos].encode(l.store); err != nil {
return err
}
// Forward non-configuration commands to the state machine.
// Send the responses to the waiting client, if applicable.
if !l.entries[pos].isConfiguration {
resp := l.apply(l.entries[pos].Index, l.entries[pos].Command)
if l.entries[pos].commandResponse != nil {
select {
case l.entries[pos].commandResponse <- resp:
break
case <-time.After(maximumElectionTimeout()): // << ElectionInterval
panic("uncoöperative command response receiver")
}
close(l.entries[pos].commandResponse)
l.entries[pos].commandResponse = nil
}
}
// Signal the entry has been committed, if applicable.
if l.entries[pos].committed != nil {
l.entries[pos].committed <- true
close(l.entries[pos].committed)
l.entries[pos].committed = nil
}
// Mark our commit position cursor.
l.commitPos = pos
// If that was the last one, we're done.
if l.entries[pos].Index == commitIndex {
break
}
if l.entries[pos].Index > commitIndex {
panic(fmt.Sprintf(
"current entry Index %d is beyond our desired commitIndex %d",
l.entries[pos].Index,
commitIndex,
))
}
// Otherwise, advance!
pos++
}
// Done.
return nil
}
// logEntry is the atomic unit being managed by the distributed log. A log entry
// always has an index (monotonically increasing), a term in which the Raft
// network leader first sees the entry, and a command. The command is what gets
// executed against the node state machine when the log entry is successfully
// replicated.
type logEntry struct {
Index uint64 `json:"index"`
Term uint64 `json:"term"` // when received by leader
Command []byte `json:"command,omitempty"`
committed chan bool `json:"-"`
commandResponse chan<- []byte `json:"-"` // only non-nil on receiver's log
isConfiguration bool `json:"-"` // for configuration change entries
}
// encode serializes the log entry to the passed io.Writer.
//
// Entries are serialized in a simple binary format:
//
// ---------------------------------------------
// | uint32 | uint64 | uint64 | uint32 | []byte |
// ---------------------------------------------
// | CRC | TERM | INDEX | SIZE | COMMAND |
// ---------------------------------------------
//
func (e *logEntry) encode(w io.Writer) error {
if len(e.Command) <= 0 {
return errNoCommand
}
if e.Index <= 0 {
return errBadIndex
}
if e.Term <= 0 {
return errBadTerm
}
commandSize := len(e.Command)
buf := make([]byte, 24+commandSize)
binary.LittleEndian.PutUint64(buf[4:12], e.Term)
binary.LittleEndian.PutUint64(buf[12:20], e.Index)
binary.LittleEndian.PutUint32(buf[20:24], uint32(commandSize))
copy(buf[24:], e.Command)
binary.LittleEndian.PutUint32(
buf[0:4],
crc32.ChecksumIEEE(buf[4:]),
)
_, err := w.Write(buf)
return err
}
// decode deserializes one log entry from the passed io.Reader.
func (e *logEntry) decode(r io.Reader) error {
header := make([]byte, 24)
if _, err := r.Read(header); err != nil {
return err
}
command := make([]byte, binary.LittleEndian.Uint32(header[20:24]))
if _, err := r.Read(command); err != nil {
return err
}
crc := binary.LittleEndian.Uint32(header[:4])
check := crc32.NewIEEE()
check.Write(header[4:])
check.Write(command)
if crc != check.Sum32() {
return errInvalidChecksum
}
e.Term = binary.LittleEndian.Uint64(header[4:12])
e.Index = binary.LittleEndian.Uint64(header[12:20])
e.Command = command
return nil
}