Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage: avoid per-kv allocations during consistency checks #29419

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions pkg/storage/replica_consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,11 @@ func (r *Replica) sha512(
defer iter.Close()

var alloc bufalloc.ByteAllocator
var intBuf [8]byte
var legacyTimestamp hlc.LegacyTimestamp
var timestampBuf []byte
hasher := sha512.New()

var legacyTimestamp hlc.LegacyTimestamp
visitor := func(unsafeKey engine.MVCCKey, unsafeValue []byte) error {
if snapshot != nil {
// Add (a copy of) the kv pair into the debug message.
Expand All @@ -375,24 +377,30 @@ func (r *Replica) sha512(
}

// Encode the length of the key and value.
if err := binary.Write(hasher, binary.LittleEndian, int64(len(unsafeKey.Key))); err != nil {
binary.LittleEndian.PutUint64(intBuf[:], uint64(len(unsafeKey.Key)))
if _, err := hasher.Write(intBuf[:]); err != nil {
return err
}
if err := binary.Write(hasher, binary.LittleEndian, int64(len(unsafeValue))); err != nil {
binary.LittleEndian.PutUint64(intBuf[:], uint64(len(unsafeValue)))
if _, err := hasher.Write(intBuf[:]); err != nil {
return err
}
if _, err := hasher.Write(unsafeKey.Key); err != nil {
return err
}
legacyTimestamp = hlc.LegacyTimestamp(unsafeKey.Timestamp)
timestamp, err := protoutil.Marshal(&legacyTimestamp)
if err != nil {
if size := legacyTimestamp.Size(); size > cap(timestampBuf) {
timestampBuf = make([]byte, size)
} else {
timestampBuf = timestampBuf[:size]
}
if _, err := protoutil.MarshalToWithoutFuzzing(&legacyTimestamp, timestampBuf); err != nil {
return err
}
if _, err := hasher.Write(timestamp); err != nil {
if _, err := hasher.Write(timestampBuf); err != nil {
return err
}
_, err = hasher.Write(unsafeValue)
_, err := hasher.Write(unsafeValue)
return err
}

Expand Down