Skip to content

Commit

Permalink
Fix Raft Panic
Browse files Browse the repository at this point in the history
The lastIndex cache I had introduced, caused Raft panics because it was
incorrect. This PR fixes that by updating it correctly in two places:
addEntries and setSnapshot.

With this PR, blockade runs OK.
  • Loading branch information
manishrjain committed Mar 9, 2019
1 parent 60d9ef0 commit 8cb69ea
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions raftwal/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,16 @@ func (w *DiskStorage) setSnapshot(batch *badger.WriteBatch, s pb.Snapshot) error
return err
}

// Cache it.
// Update the last index cache here. This is useful so when a follower gets a jump due to
// receiving a snapshot and Save is called, addEntries wouldn't have much. So, the last index
// cache would need to rely upon this update here.
if val, ok := w.cache.Load(lastKey); ok {
le := val.(uint64)
if le < e.Index {
w.cache.Store(lastKey, e.Index)
}
}
// Cache snapshot.
w.cache.Store(snapshotKey, proto.Clone(&s))
return nil
}
Expand Down Expand Up @@ -614,9 +623,9 @@ func (w *DiskStorage) addEntries(batch *badger.WriteBatch, entries []pb.Entry) e
}
}
laste := entries[len(entries)-1].Index
w.cache.Store(lastKey, laste) // Update the last index cache.
if laste < last {
return w.deleteFrom(batch, laste+1)
}
w.cache.Store(lastKey, laste)
return nil
}

1 comment on commit 8cb69ea

@skitzofrenic
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Please sign in to comment.