Browse Source

clientv3: add integrity check in snapshot status

Add snapshot file integrity check in snapshot status. If the file is
corrupted, return with error message.
Jingyi Hu 7 years ago
parent
commit
422f867f6b
1 changed files with 9 additions and 0 deletions
  1. 9 0
      clientv3/snapshot/v3_snapshot.go

+ 9 - 0
clientv3/snapshot/v3_snapshot.go

@@ -25,6 +25,7 @@ import (
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
 	"reflect"
 	"reflect"
+	"strings"
 	"time"
 	"time"
 
 
 	"go.etcd.io/etcd/clientv3"
 	"go.etcd.io/etcd/clientv3"
@@ -166,6 +167,14 @@ func (s *v3Manager) Status(dbPath string) (ds Status, err error) {
 	h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
 	h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
 
 
 	if err = db.View(func(tx *bolt.Tx) error {
 	if err = db.View(func(tx *bolt.Tx) error {
+		// check snapshot file integrity first
+		var dbErrStrings []string
+		for dbErr := range tx.Check() {
+			dbErrStrings = append(dbErrStrings, dbErr.Error())
+		}
+		if len(dbErrStrings) > 0 {
+			return fmt.Errorf("snapshot file integrity check failed. %d errors found.\n"+strings.Join(dbErrStrings, "\n"), len(dbErrStrings))
+		}
 		ds.TotalSize = tx.Size()
 		ds.TotalSize = tx.Size()
 		c := tx.Cursor()
 		c := tx.Cursor()
 		for next, _ := c.First(); next != nil; next, _ = c.Next() {
 		for next, _ := c.First(); next != nil; next, _ = c.Next() {