Skip to content

Commit

Permalink
kvserver: make replica sha512 a function
Browse files Browse the repository at this point in the history
This function does not use the replica, so does not have to be a method.

Epic: None
Release note: None
  • Loading branch information
pav-kv committed Oct 17, 2022
1 parent a8a10a4 commit bbdd88c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
3 changes: 1 addition & 2 deletions pkg/kv/kvserver/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,8 @@ func WatchForDisappearingReplicas(t testing.TB, store *Store) {
func ChecksumRange(
ctx context.Context, desc roachpb.RangeDescriptor, snap storage.Reader,
) ([]byte, error) {
var r *Replica // TODO(pavelkalinnikov): make this less ugly.
lim := quotapool.NewRateLimiter("test", 1<<30, 1<<30)
res, err := r.sha512(ctx, desc, snap, roachpb.ChecksumMode_CHECK_FULL, lim)
res, err := replicaSHA512(ctx, desc, snap, roachpb.ChecksumMode_CHECK_FULL, lim)
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/kv/kvserver/replica_consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,10 @@ type replicaHash struct {
PersistedMS, RecomputedMS enginepb.MVCCStats
}

// sha512 computes the SHA512 hash of all the replica data at the snapshot.
// It will dump all the kv data into snapshot if it is provided.
func (*Replica) sha512(
// replicaSHA512 computes the SHA512 hash of the replica data at the given
// snapshot. Either the full replicated state is taken into account, or only
// RangeAppliedState (which includes MVCC stats), depending on the mode.
func replicaSHA512(
ctx context.Context,
desc roachpb.RangeDescriptor,
snap storage.Reader,
Expand Down Expand Up @@ -690,7 +691,7 @@ func (r *Replica) computeChecksumPostApply(
); err != nil {
log.Errorf(ctx, "checksum collection did not join: %v", err)
} else {
result, err := r.sha512(ctx, desc, snap, cc.Mode, r.store.consistencyLimiter)
result, err := replicaSHA512(ctx, desc, snap, cc.Mode, r.store.consistencyLimiter)
if err != nil {
log.Errorf(ctx, "checksum computation failed: %v", err)
result = nil
Expand Down
7 changes: 3 additions & 4 deletions pkg/kv/kvserver/replica_consistency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,14 @@ func TestReplicaChecksumSHA512(t *testing.T) {
eng := storage.NewDefaultInMemForTesting()
defer eng.Close()

repl := &Replica{} // We don't actually need the replica at all, just the method.
desc := roachpb.RangeDescriptor{
RangeID: 1,
StartKey: roachpb.RKey("a"),
EndKey: roachpb.RKey("z"),
}

// Hash the empty state.
rh, err := repl.sha512(ctx, desc, eng, roachpb.ChecksumMode_CHECK_FULL, lim)
rh, err := replicaSHA512(ctx, desc, eng, roachpb.ChecksumMode_CHECK_FULL, lim)
require.NoError(t, err)
fmt.Fprintf(sb, "checksum0: %x\n", rh.SHA512[:])

Expand Down Expand Up @@ -214,13 +213,13 @@ func TestReplicaChecksumSHA512(t *testing.T) {
require.NoError(t, storage.MVCCPut(ctx, eng, nil, key, ts, localTS, value, nil))
}

rh, err = repl.sha512(ctx, desc, eng, roachpb.ChecksumMode_CHECK_FULL, lim)
rh, err = replicaSHA512(ctx, desc, eng, roachpb.ChecksumMode_CHECK_FULL, lim)
require.NoError(t, err)
fmt.Fprintf(sb, "checksum%d: %x\n", i+1, rh.SHA512[:])
}

// Run another check to obtain stats for the final state.
rh, err = repl.sha512(ctx, desc, eng, roachpb.ChecksumMode_CHECK_FULL, lim)
rh, err = replicaSHA512(ctx, desc, eng, roachpb.ChecksumMode_CHECK_FULL, lim)
require.NoError(t, err)

jsonpb := protoutil.JSONPb{Indent: " "}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kv/kvserver/replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10431,7 +10431,7 @@ func TestReplicaServersideRefreshes(t *testing.T) {
// Regression test for #31870.
snap := tc.engine.NewSnapshot()
defer snap.Close()
res, err := tc.repl.sha512(ctx, *tc.repl.Desc(), tc.engine,
res, err := replicaSHA512(ctx, *tc.repl.Desc(), tc.engine,
roachpb.ChecksumMode_CHECK_FULL,
quotapool.NewRateLimiter("ConsistencyQueue", quotapool.Limit(math.MaxFloat64), math.MaxInt64))
if err != nil {
Expand Down

0 comments on commit bbdd88c

Please sign in to comment.