mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: handling of applying old snapshots
There was a TODO requirement to handle ErrorSnapshotOutOfDate for the function ApplySnapshot. The same has been implemented #6090
This commit is contained in:
parent
eb36d0dbba
commit
fd757756f5
@ -168,7 +168,13 @@ func (ms *MemoryStorage) ApplySnapshot(snap pb.Snapshot) error {
|
|||||||
ms.Lock()
|
ms.Lock()
|
||||||
defer ms.Unlock()
|
defer ms.Unlock()
|
||||||
|
|
||||||
// TODO: return ErrSnapOutOfDate?
|
//handle check for old snapshot being applied
|
||||||
|
msIndex := ms.snapshot.Metadata.Index
|
||||||
|
snapIndex := snap.Metadata.Index
|
||||||
|
if msIndex >= snapIndex {
|
||||||
|
return ErrSnapOutOfDate
|
||||||
|
}
|
||||||
|
|
||||||
ms.snapshot = snap
|
ms.snapshot = snap
|
||||||
ms.ents = []pb.Entry{{Term: snap.Metadata.Term, Index: snap.Metadata.Index}}
|
ms.ents = []pb.Entry{{Term: snap.Metadata.Term, Index: snap.Metadata.Index}}
|
||||||
return nil
|
return nil
|
||||||
|
@ -256,3 +256,30 @@ func TestStorageAppend(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStorageApplySnapshot(t *testing.T) {
|
||||||
|
cs := &pb.ConfState{Nodes: []uint64{1, 2, 3}}
|
||||||
|
data := []byte("data")
|
||||||
|
|
||||||
|
tests := []pb.Snapshot{{Data: data, Metadata: pb.SnapshotMetadata{Index: 4, Term: 4, ConfState: *cs}},
|
||||||
|
{Data: data, Metadata: pb.SnapshotMetadata{Index: 3, Term: 3, ConfState: *cs}},
|
||||||
|
}
|
||||||
|
|
||||||
|
s := NewMemoryStorage()
|
||||||
|
|
||||||
|
//Apply Snapshot successful
|
||||||
|
i := 0
|
||||||
|
tt := tests[i]
|
||||||
|
err := s.ApplySnapshot(tt)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("#%d: err = %v, want %v", i, err, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
//Apply Snapshot fails due to ErrSnapOutOfDate
|
||||||
|
i = 1
|
||||||
|
tt = tests[i]
|
||||||
|
err = s.ApplySnapshot(tt)
|
||||||
|
if err != ErrSnapOutOfDate {
|
||||||
|
t.Errorf("#%d: err = %v, want %v", i, err, ErrSnapOutOfDate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user