Test for stale indices.
parent
6988dd4d6a
commit
7106fe616e
4
log.go
4
log.go
|
@ -311,9 +311,9 @@ func (l *Log) appendEntry(entry *LogEntry) error {
|
|||
if len(l.entries) > 0 {
|
||||
lastEntry := l.entries[len(l.entries)-1]
|
||||
if entry.term < lastEntry.term {
|
||||
return fmt.Errorf("raft.Log: Cannot append entry with earlier term (%x:%x < %x:%x)", entry.term, entry.index, lastEntry.term, lastEntry.index)
|
||||
return fmt.Errorf("raft.Log: Cannot append entry with earlier term (%x:%x <= %x:%x)", entry.term, entry.index, lastEntry.term, lastEntry.index)
|
||||
} else if entry.index == lastEntry.index && entry.index <= lastEntry.index {
|
||||
return fmt.Errorf("raft.Log: Cannot append entry with earlier index in the same term (%x:%x < %x:%x)", entry.term, entry.index, lastEntry.term, lastEntry.index)
|
||||
return fmt.Errorf("raft.Log: Cannot append entry with earlier index in the same term (%x:%x <= %x:%x)", entry.term, entry.index, lastEntry.term, lastEntry.index)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -149,7 +149,29 @@ func TestServerAppendEntriesWithStaleTermsAreRejected(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: Reject new entries to log if entries already exist.
|
||||
// Ensure that we reject entries if the commit log is different.
|
||||
func TestServerAppendEntriesRejectedIfAlreadyCommitted(t *testing.T) {
|
||||
server := newTestServer("1")
|
||||
server.Start()
|
||||
|
||||
// Append single entry + commit.
|
||||
entries := []*LogEntry{
|
||||
NewLogEntry(nil, 1, 1, &TestCommand1{"foo", 10}),
|
||||
NewLogEntry(nil, 2, 1, &TestCommand1{"foo", 15}),
|
||||
}
|
||||
resp, err := server.AppendEntries(NewAppendEntriesRequest(1, "ldr", 0, 0, entries, 2))
|
||||
if !(resp.Term == 1 && resp.Success && err == nil) {
|
||||
t.Fatalf("AppendEntries failed: %v/%v : %v", resp.Term, resp.Success, err)
|
||||
}
|
||||
|
||||
// Append entry again (post-commit).
|
||||
entries = []*LogEntry{NewLogEntry(nil, 2, 1, &TestCommand1{"bar", 20})}
|
||||
resp, err = server.AppendEntries(NewAppendEntriesRequest(1, "ldr", 2, 1, entries, 1))
|
||||
if !(resp.Term == 1 && !resp.Success && err != nil && err.Error() == "raft.Log: Cannot append entry with earlier index in the same term (1:2 <= 1:2)") {
|
||||
t.Fatalf("AppendEntries should have failed: %v/%v : %v", resp.Term, resp.Success, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Reject entries from earlier index or term.
|
||||
// TODO: Test rollback of uncommitted entries.
|
||||
|
||||
|
|
Loading…
Reference in New Issue