Skip to content

Commit

Permalink
raft: make unstable.entries immutable; copy the entries at bad path
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Nov 28, 2014
1 parent d244e3b commit d214e87
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
3 changes: 1 addition & 2 deletions raft/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ func (l *raftLog) unstableEntries() []pb.Entry {
if len(l.unstable.entries) == 0 {
return nil
}
// copy unstable entries to an empty slice
return append([]pb.Entry{}, l.unstable.entries...)
return l.unstable.entries
}

// nextEnts returns all the available entries for execution.
Expand Down
25 changes: 15 additions & 10 deletions raft/log_unstable.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,23 @@ func (u *unstable) restore(s pb.Snapshot) {
u.snapshot = &s
}

func (u *unstable) resetEntries(offset uint64) {
u.entries = nil
u.offset = offset
}

func (u *unstable) truncateAndAppend(after uint64, ents []pb.Entry) {
if after < u.offset {
// The log is being truncated to before our current unstable
// portion, so discard it and reset unstable.
u.resetEntries(after + 1)
switch {
case after < u.offset:
// The log is being truncated to before our current offset
// portion, so set the offset and replace the entries
u.offset = after + 1
u.entries = ents
case after == u.offset+uint64(len(u.entries))-1:
// after is the last index in the u.entries
// directly append
u.entries = append(u.entries, ents...)
default:
// truncate to after and copy to u.entries
// then append
u.entries = append([]pb.Entry{}, u.slice(u.offset, after+1)...)
u.entries = append(u.entries, ents...)
}
u.entries = append(u.slice(u.offset, after+1), ents...)
}

func (u *unstable) slice(lo uint64, hi uint64) []pb.Entry {
Expand Down
4 changes: 2 additions & 2 deletions raft/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ func (ms *MemoryStorage) Append(entries []pb.Entry) {
if offset < 0 {
return
}
if uint64(len(ms.ents)) >= offset {
ms.ents = ms.ents[:offset]
if uint64(len(ms.ents)) > offset {
ms.ents = append([]pb.Entry{}, ms.ents[:offset]...)
}
ms.ents = append(ms.ents, entries...)
}

0 comments on commit d214e87

Please sign in to comment.