Browse Source

Merge pull request #9736 from gyuho/snap

raftsnap: use zap logger
Gyuho Lee 7 years ago
parent
commit
c772599c0f
4 changed files with 55 additions and 22 deletions
  1. 1 0
      raftsnap/db.go
  2. 45 13
      raftsnap/snapshotter.go
  3. 2 2
      raftsnap/snapshotter_test.go
  4. 7 7
      tools/etcd-dump-logs/main.go

+ 1 - 0
raftsnap/db.go

@@ -61,6 +61,7 @@ func (s *Snapshotter) SaveDBFrom(r io.Reader, id uint64) (int64, error) {
 	if s.lg != nil {
 		s.lg.Info(
 			"saved database snapshot to disk",
+			zap.String("path", fn),
 			zap.Int64("bytes", n),
 			zap.String("size", humanize.Bytes(uint64(n))),
 		)

+ 45 - 13
raftsnap/snapshotter.go

@@ -105,7 +105,7 @@ func (s *Snapshotter) Load() (*raftpb.Snapshot, error) {
 	}
 	var snap *raftpb.Snapshot
 	for _, name := range names {
-		if snap, err = loadSnap(s.dir, name); err == nil {
+		if snap, err = loadSnap(s.lg, s.dir, name); err == nil {
 			break
 		}
 	}
@@ -115,9 +115,9 @@ func (s *Snapshotter) Load() (*raftpb.Snapshot, error) {
 	return snap, nil
 }
 
-func loadSnap(dir, name string) (*raftpb.Snapshot, error) {
+func loadSnap(lg *zap.Logger, dir, name string) (*raftpb.Snapshot, error) {
 	fpath := filepath.Join(dir, name)
-	snap, err := Read(fpath)
+	snap, err := Read(lg, fpath)
 	if err != nil {
 		renameBroken(fpath)
 	}
@@ -125,38 +125,66 @@ func loadSnap(dir, name string) (*raftpb.Snapshot, error) {
 }
 
 // Read reads the snapshot named by snapname and returns the snapshot.
-func Read(snapname string) (*raftpb.Snapshot, error) {
+func Read(lg *zap.Logger, snapname string) (*raftpb.Snapshot, error) {
 	b, err := ioutil.ReadFile(snapname)
 	if err != nil {
-		plog.Errorf("cannot read file %v: %v", snapname, err)
+		if lg != nil {
+			lg.Warn("failed to read snapshot file", zap.String("path", snapname), zap.Error(err))
+		} else {
+			plog.Errorf("cannot read file %v: %v", snapname, err)
+		}
 		return nil, err
 	}
 
 	if len(b) == 0 {
-		plog.Errorf("unexpected empty snapshot")
+		if lg != nil {
+			lg.Warn("failed to read empty snapshot file", zap.String("path", snapname))
+		} else {
+			plog.Errorf("unexpected empty snapshot")
+		}
 		return nil, ErrEmptySnapshot
 	}
 
 	var serializedSnap snappb.Snapshot
 	if err = serializedSnap.Unmarshal(b); err != nil {
-		plog.Errorf("corrupted snapshot file %v: %v", snapname, err)
+		if lg != nil {
+			lg.Warn("failed to unmarshal snappb.Snapshot", zap.String("path", snapname), zap.Error(err))
+		} else {
+			plog.Errorf("corrupted snapshot file %v: %v", snapname, err)
+		}
 		return nil, err
 	}
 
 	if len(serializedSnap.Data) == 0 || serializedSnap.Crc == 0 {
-		plog.Errorf("unexpected empty snapshot")
+		if lg != nil {
+			lg.Warn("failed to read empty snapshot data", zap.String("path", snapname))
+		} else {
+			plog.Errorf("unexpected empty snapshot")
+		}
 		return nil, ErrEmptySnapshot
 	}
 
 	crc := crc32.Update(0, crcTable, serializedSnap.Data)
 	if crc != serializedSnap.Crc {
-		plog.Errorf("corrupted snapshot file %v: crc mismatch", snapname)
+		if lg != nil {
+			lg.Warn("found corrupted snapshot file",
+				zap.String("path", snapname),
+				zap.Uint32("prev-crc", serializedSnap.Crc),
+				zap.Uint32("new-crc", crc),
+			)
+		} else {
+			plog.Errorf("corrupted snapshot file %v: crc mismatch", snapname)
+		}
 		return nil, ErrCRCMismatch
 	}
 
 	var snap raftpb.Snapshot
 	if err = snap.Unmarshal(serializedSnap.Data); err != nil {
-		plog.Errorf("corrupted snapshot file %v: %v", snapname, err)
+		if lg != nil {
+			lg.Warn("failed to unmarshal raftpb.Snapshot", zap.String("path", snapname), zap.Error(err))
+		} else {
+			plog.Errorf("corrupted snapshot file %v: %v", snapname, err)
+		}
 		return nil, err
 	}
 	return &snap, nil
@@ -174,7 +202,7 @@ func (s *Snapshotter) snapNames() ([]string, error) {
 	if err != nil {
 		return nil, err
 	}
-	snaps := checkSuffix(names)
+	snaps := checkSuffix(s.lg, names)
 	if len(snaps) == 0 {
 		return nil, ErrNoSnapshot
 	}
@@ -182,7 +210,7 @@ func (s *Snapshotter) snapNames() ([]string, error) {
 	return snaps, nil
 }
 
-func checkSuffix(names []string) []string {
+func checkSuffix(lg *zap.Logger, names []string) []string {
 	snaps := []string{}
 	for i := range names {
 		if strings.HasSuffix(names[i], snapSuffix) {
@@ -191,7 +219,11 @@ func checkSuffix(names []string) []string {
 			// If we find a file which is not a snapshot then check if it's
 			// a vaild file. If not throw out a warning.
 			if _, ok := validFiles[names[i]]; !ok {
-				plog.Warningf("skipped unexpected non snapshot file %v", names[i])
+				if lg != nil {
+					lg.Warn("found unexpected non-snapshot file; skipping", zap.String("path", names[i]))
+				} else {
+					plog.Warningf("skipped unexpected non snapshot file %v", names[i])
+				}
 			}
 		}
 	}

+ 2 - 2
raftsnap/snapshotter_test.go

@@ -78,7 +78,7 @@ func TestBadCRC(t *testing.T) {
 	// fake a crc mismatch
 	crcTable = crc32.MakeTable(crc32.Koopman)
 
-	_, err = Read(filepath.Join(dir, fmt.Sprintf("%016x-%016x.snap", 1, 1)))
+	_, err = Read(zap.NewExample(), filepath.Join(dir, fmt.Sprintf("%016x-%016x.snap", 1, 1)))
 	if err == nil || err != ErrCRCMismatch {
 		t.Errorf("err = %v, want %v", err, ErrCRCMismatch)
 	}
@@ -203,7 +203,7 @@ func TestEmptySnapshot(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	_, err = Read(filepath.Join(dir, "1.snap"))
+	_, err = Read(zap.NewExample(), filepath.Join(dir, "1.snap"))
 	if err != ErrEmptySnapshot {
 		t.Errorf("err = %v, want %v", err, ErrEmptySnapshot)
 	}

+ 7 - 7
tools/etcd-dump-logs/main.go

@@ -36,9 +36,9 @@ import (
 func main() {
 	snapfile := flag.String("start-snap", "", "The base name of snapshot file to start dumping")
 	index := flag.Uint64("start-index", 0, "The index to start dumping")
-	entrytype := flag.String("entry-type", "", `If set, filters output by entry type. Must be one or more than one of: 
-	ConfigChange, Normal, Request, InternalRaftRequest, 
-	IRRRange, IRRPut, IRRDeleteRange, IRRTxn, 
+	entrytype := flag.String("entry-type", "", `If set, filters output by entry type. Must be one or more than one of:
+	ConfigChange, Normal, Request, InternalRaftRequest,
+	IRRRange, IRRPut, IRRDeleteRange, IRRTxn,
 	IRRCompaction, IRRLeaseGrant, IRRLeaseRevoke`)
 
 	flag.Parse()
@@ -68,7 +68,7 @@ func main() {
 			ss := raftsnap.New(zap.NewExample(), snapDir(dataDir))
 			snapshot, err = ss.Load()
 		} else {
-			snapshot, err = raftsnap.Read(filepath.Join(snapDir(dataDir), *snapfile))
+			snapshot, err = raftsnap.Read(zap.NewExample(), filepath.Join(snapDir(dataDir), *snapfile))
 		}
 
 		switch err {
@@ -269,9 +269,9 @@ func evaluateEntrytypeFlag(entrytype string) []EntryFilter {
 		if f, ok := validRequest[et]; ok {
 			filters = append(filters, f...)
 		} else {
-			log.Printf(`[%+v] is not a valid entry-type, ignored. 
-Please set entry-type to one or more of the following: 
-ConfigChange, Normal, Request, InternalRaftRequest, 
+			log.Printf(`[%+v] is not a valid entry-type, ignored.
+Please set entry-type to one or more of the following:
+ConfigChange, Normal, Request, InternalRaftRequest,
 IRRRange, IRRPut, IRRDeleteRange, IRRTxn,
 IRRCompaction, IRRLeaseGrant, IRRLeaseRevoke`, et)
 		}