Skip to content

Commit

Permalink
cleanup WAL incase of KVValue decode error before panic
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonwang371 committed Nov 24, 2021
1 parent 9643999 commit 759e6c2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions server/etcdserver/corrupt.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func (s *EtcdServer) monitorKVDecoding() {
return
case <-s.kv.ErrorC():
s.sendCorruptionAlarmRequest(uint64(s.ID()))
s.r.storage.Cleanup()
panic("watchable kv decoding error")
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions server/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Storage interface {
Release(snap raftpb.Snapshot) error
// Sync WAL
Sync() error
// CLean up WAL
Cleanup() error
// MinimalEtcdVersion returns minimal etcd storage able to interpret WAL log.
MinimalEtcdVersion() *semver.Version
}
Expand Down Expand Up @@ -87,6 +89,13 @@ func (st *storage) Release(snap raftpb.Snapshot) error {
return st.s.ReleaseSnapDBs(snap)
}

func (st *storage) Cleanup() error {
st.mux.RLock()
defer st.mux.RUnlock()
st.w.CleanupWAL(st.lg)
return nil
}

func (st *storage) Save(s raftpb.HardState, ents []raftpb.Entry) error {
st.mux.RLock()
defer st.mux.RUnlock()
Expand Down
4 changes: 2 additions & 2 deletions server/storage/wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) {
var perr error
defer func() {
if perr != nil {
w.cleanupWAL(lg)
w.CleanupWAL(lg)
}
}()

Expand Down Expand Up @@ -246,7 +246,7 @@ func (w *WAL) SetUnsafeNoFsync() {
w.unsafeNoSync = true
}

func (w *WAL) cleanupWAL(lg *zap.Logger) {
func (w *WAL) CleanupWAL(lg *zap.Logger) {
var err error
if err = w.Close(); err != nil {
lg.Panic("failed to close WAL during cleanup", zap.Error(err))
Expand Down
2 changes: 1 addition & 1 deletion server/storage/wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestWalCleanup(t *testing.T) {
if err != nil {
t.Fatalf("err = %v, want nil", err)
}
w.cleanupWAL(logger)
w.CleanupWAL(logger)
fnames, err := fileutil.ReadDir(testRoot)
if err != nil {
t.Fatalf("err = %v, want nil", err)
Expand Down

0 comments on commit 759e6c2

Please sign in to comment.