Fix checksum hex padding

This commit is contained in:
Ben Johnson
2021-01-17 09:52:09 -07:00
parent 4b65e6a88f
commit 04d75507e3
3 changed files with 31 additions and 3 deletions

28
db.go
View File

@@ -1324,6 +1324,13 @@ func (db *DB) Restore(ctx context.Context, opt RestoreOptions) error {
if err := db.restoreSnapshot(ctx, r, pos.Generation, pos.Index, tmpPath); err != nil { if err := db.restoreSnapshot(ctx, r, pos.Generation, pos.Index, tmpPath); err != nil {
return fmt.Errorf("cannot restore snapshot: %w", err) return fmt.Errorf("cannot restore snapshot: %w", err)
} }
chksum, err := checksumFile(tmpPath)
if err != nil {
log.Printf("%s(%s): cannot checksum snapshot: %s", db.path, r.Name(), err)
} else {
log.Printf("%s(%s): restored snapshot checksum: %016x", db.path, r.Name(), chksum)
}
} }
// Restore each WAL file until we reach our maximum index. // Restore each WAL file until we reach our maximum index.
@@ -1352,6 +1359,20 @@ func (db *DB) Restore(ctx context.Context, opt RestoreOptions) error {
return nil return nil
} }
func checksumFile(filename string) (uint64, error) {
f, err := os.Open(filename)
if err != nil {
return 0, err
}
defer f.Close()
h := crc64.New(crc64.MakeTable(crc64.ISO))
if _, err := io.Copy(h, f); err != nil {
return 0, err
}
return h.Sum64(), nil
}
func (db *DB) restoreTarget(ctx context.Context, opt RestoreOptions, logger *log.Logger) (Replica, string, error) { func (db *DB) restoreTarget(ctx context.Context, opt RestoreOptions, logger *log.Logger) (Replica, string, error) {
var target struct { var target struct {
replica Replica replica Replica
@@ -1460,6 +1481,13 @@ func (db *DB) restoreWAL(ctx context.Context, r Replica, generation string, inde
return err return err
} }
chksum, err := checksumFile(dbPath + "-wal")
if err != nil {
log.Printf("%s(%s): cannot checksum wal: %s", db.path, r.Name(), err)
} else {
log.Printf("%s(%s): restored wal checksum: %016x", db.path, r.Name(), chksum)
}
// Open SQLite database and force a truncating checkpoint. // Open SQLite database and force a truncating checkpoint.
d, err := sql.Open("sqlite3", dbPath) d, err := sql.Open("sqlite3", dbPath)
if err != nil { if err != nil {

View File

@@ -98,7 +98,7 @@ func (p Pos) String() string {
if p.IsZero() { if p.IsZero() {
return "<>" return "<>"
} }
return fmt.Sprintf("<%s,%d,%d>", p.Generation, p.Index, p.Offset) return fmt.Sprintf("<%s,%08x,%d>", p.Generation, p.Index, p.Offset)
} }
// IsZero returns true if p is the zero value. // IsZero returns true if p is the zero value.

View File

@@ -996,7 +996,7 @@ func ValidateReplica(ctx context.Context, r Replica) error {
if err != nil { if err != nil {
return fmt.Errorf("cannot compute checksum: %w", err) return fmt.Errorf("cannot compute checksum: %w", err)
} }
log.Printf("%s(%s): primary checksum computed: %08x @ %s", db.Path(), r.Name(), chksum0, pos) log.Printf("%s(%s): primary checksum computed: %016x @ %s", db.Path(), r.Name(), chksum0, pos)
// Wait until replica catches up to position. // Wait until replica catches up to position.
log.Printf("%s(%s): waiting for replica", db.Path(), r.Name()) log.Printf("%s(%s): waiting for replica", db.Path(), r.Name())
@@ -1039,7 +1039,7 @@ func ValidateReplica(ctx context.Context, r Replica) error {
} }
chksum1 := h.Sum64() chksum1 := h.Sum64()
log.Printf("%s(%s): replica checksum computed: %08x", db.Path(), r.Name(), chksum1) log.Printf("%s(%s): replica checksum computed: %016x", db.Path(), r.Name(), chksum1)
// Validate checksums match. // Validate checksums match.
if chksum0 != chksum1 { if chksum0 != chksum1 {