mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #3585 from xiang90/fix_hash
storage: fix hash by iterating kv
This commit is contained in:
commit
0813a0f2d1
@ -15,6 +15,8 @@
|
|||||||
package backend
|
package backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"hash/crc32"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
@ -25,6 +27,7 @@ import (
|
|||||||
type Backend interface {
|
type Backend interface {
|
||||||
BatchTx() BatchTx
|
BatchTx() BatchTx
|
||||||
Snapshot(w io.Writer) (n int64, err error)
|
Snapshot(w io.Writer) (n int64, err error)
|
||||||
|
Hash() (uint32, error)
|
||||||
ForceCommit()
|
ForceCommit()
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
@ -84,6 +87,33 @@ func (b *backend) Snapshot(w io.Writer) (n int64, err error) {
|
|||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *backend) Hash() (uint32, error) {
|
||||||
|
h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
|
||||||
|
|
||||||
|
err := b.db.View(func(tx *bolt.Tx) error {
|
||||||
|
c := tx.Cursor()
|
||||||
|
for next, _ := c.First(); next != nil; next, _ = c.Next() {
|
||||||
|
b := tx.Bucket(next)
|
||||||
|
if b == nil {
|
||||||
|
return fmt.Errorf("cannot get hash of bucket %s", string(next))
|
||||||
|
}
|
||||||
|
h.Write(next)
|
||||||
|
b.ForEach(func(k, v []byte) error {
|
||||||
|
h.Write(k)
|
||||||
|
h.Write(v)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.Sum32(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *backend) run() {
|
func (b *backend) run() {
|
||||||
defer close(b.donec)
|
defer close(b.donec)
|
||||||
|
|
||||||
|
@ -16,7 +16,6 @@ package storage
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"hash/crc32"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
@ -289,12 +288,8 @@ func (s *store) Compact(rev int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *store) Hash() (uint32, error) {
|
func (s *store) Hash() (uint32, error) {
|
||||||
h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
|
s.b.ForceCommit()
|
||||||
_, err := s.Snapshot(h)
|
return s.b.Hash()
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return h.Sum32(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *store) Snapshot(w io.Writer) (int64, error) {
|
func (s *store) Snapshot(w io.Writer) (int64, error) {
|
||||||
|
@ -722,6 +722,7 @@ type fakeBackend struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *fakeBackend) BatchTx() backend.BatchTx { return b.tx }
|
func (b *fakeBackend) BatchTx() backend.BatchTx { return b.tx }
|
||||||
|
func (b *fakeBackend) Hash() (uint32, error) { return 0, nil }
|
||||||
func (b *fakeBackend) Snapshot(w io.Writer) (n int64, err error) { return 0, errors.New("unsupported") }
|
func (b *fakeBackend) Snapshot(w io.Writer) (n int64, err error) { return 0, errors.New("unsupported") }
|
||||||
func (b *fakeBackend) ForceCommit() {}
|
func (b *fakeBackend) ForceCommit() {}
|
||||||
func (b *fakeBackend) Close() error { return nil }
|
func (b *fakeBackend) Close() error { return nil }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user