mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Verification package: Verified given data-dir.
For now verifies whete Backend.cindex is consistent with WAL log, but should get expanded to cover memberships & revisions.
This commit is contained in:
@@ -618,10 +618,11 @@ func ValidSnapshotEntries(lg *zap.Logger, walDir string) ([]walpb.Snapshot, erro
|
||||
// If it cannot read out the expected snap, it will return ErrSnapshotNotFound.
|
||||
// If the loaded snap doesn't match with the expected one, it will
|
||||
// return error ErrSnapshotMismatch.
|
||||
func Verify(lg *zap.Logger, walDir string, snap walpb.Snapshot) error {
|
||||
func Verify(lg *zap.Logger, walDir string, snap walpb.Snapshot) (*raftpb.HardState, error) {
|
||||
var metadata []byte
|
||||
var err error
|
||||
var match bool
|
||||
var state raftpb.HardState
|
||||
|
||||
rec := &walpb.Record{}
|
||||
|
||||
@@ -630,14 +631,14 @@ func Verify(lg *zap.Logger, walDir string, snap walpb.Snapshot) error {
|
||||
}
|
||||
names, nameIndex, err := selectWALFiles(lg, walDir, snap)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// open wal files in read mode, so that there is no conflict
|
||||
// when the same WAL is opened elsewhere in write mode
|
||||
rs, _, closer, err := openWALFiles(lg, walDir, names, nameIndex, false)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
if closer != nil {
|
||||
@@ -652,7 +653,7 @@ func Verify(lg *zap.Logger, walDir string, snap walpb.Snapshot) error {
|
||||
switch rec.Type {
|
||||
case metadataType:
|
||||
if metadata != nil && !bytes.Equal(metadata, rec.Data) {
|
||||
return ErrMetadataConflict
|
||||
return nil, ErrMetadataConflict
|
||||
}
|
||||
metadata = rec.Data
|
||||
case crcType:
|
||||
@@ -660,7 +661,7 @@ func Verify(lg *zap.Logger, walDir string, snap walpb.Snapshot) error {
|
||||
// Current crc of decoder must match the crc of the record.
|
||||
// We need not match 0 crc, since the decoder is a new one at this point.
|
||||
if crc != 0 && rec.Validate(crc) != nil {
|
||||
return ErrCRCMismatch
|
||||
return nil, ErrCRCMismatch
|
||||
}
|
||||
decoder.updateCRC(rec.Crc)
|
||||
case snapshotType:
|
||||
@@ -668,7 +669,7 @@ func Verify(lg *zap.Logger, walDir string, snap walpb.Snapshot) error {
|
||||
pbutil.MustUnmarshal(&loadedSnap, rec.Data)
|
||||
if loadedSnap.Index == snap.Index {
|
||||
if loadedSnap.Term != snap.Term {
|
||||
return ErrSnapshotMismatch
|
||||
return nil, ErrSnapshotMismatch
|
||||
}
|
||||
match = true
|
||||
}
|
||||
@@ -676,22 +677,23 @@ func Verify(lg *zap.Logger, walDir string, snap walpb.Snapshot) error {
|
||||
// are not necessary for validating the WAL contents
|
||||
case entryType:
|
||||
case stateType:
|
||||
pbutil.MustUnmarshal(&state, rec.Data)
|
||||
default:
|
||||
return fmt.Errorf("unexpected block type %d", rec.Type)
|
||||
return nil, fmt.Errorf("unexpected block type %d", rec.Type)
|
||||
}
|
||||
}
|
||||
|
||||
// We do not have to read out all the WAL entries
|
||||
// as the decoder is opened in read mode.
|
||||
if err != io.EOF && err != io.ErrUnexpectedEOF {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !match {
|
||||
return ErrSnapshotNotFound
|
||||
return nil, ErrSnapshotNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
return &state, nil
|
||||
}
|
||||
|
||||
// cut closes current file written and creates a new one ready to append.
|
||||
|
||||
@@ -27,10 +27,12 @@ import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.etcd.io/etcd/client/pkg/v3/fileutil"
|
||||
"go.etcd.io/etcd/pkg/v3/pbutil"
|
||||
"go.etcd.io/etcd/raft/v3/raftpb"
|
||||
"go.etcd.io/etcd/server/v3/wal/walpb"
|
||||
"go.uber.org/zap/zaptest"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -231,14 +233,14 @@ func TestOpenAtIndex(t *testing.T) {
|
||||
// The test creates a WAL directory and cuts out multiple WAL files. Then
|
||||
// it corrupts one of the files by completely truncating it.
|
||||
func TestVerify(t *testing.T) {
|
||||
lg := zaptest.NewLogger(t)
|
||||
walDir, err := ioutil.TempDir(t.TempDir(), "waltest")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(walDir)
|
||||
|
||||
// create WAL
|
||||
w, err := Create(zap.NewExample(), walDir, nil)
|
||||
w, err := Create(lg, walDir, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -255,11 +257,15 @@ func TestVerify(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
hs := raftpb.HardState{Term: 1, Vote: 3, Commit: 5}
|
||||
assert.NoError(t, w.Save(hs, nil))
|
||||
|
||||
// to verify the WAL is not corrupted at this point
|
||||
err = Verify(zap.NewExample(), walDir, walpb.Snapshot{})
|
||||
hardstate, err := Verify(lg, walDir, walpb.Snapshot{})
|
||||
if err != nil {
|
||||
t.Errorf("expected a nil error, got %v", err)
|
||||
}
|
||||
assert.Equal(t, hs, *hardstate)
|
||||
|
||||
walFiles, err := ioutil.ReadDir(walDir)
|
||||
if err != nil {
|
||||
@@ -272,7 +278,7 @@ func TestVerify(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = Verify(zap.NewExample(), walDir, walpb.Snapshot{})
|
||||
_, err = Verify(lg, walDir, walpb.Snapshot{})
|
||||
if err == nil {
|
||||
t.Error("expected a non-nil error, got nil")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user