github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/batcheval/cmd_compute_checksum.go (about)

     1  // Copyright 2014 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package batcheval
    12  
    13  import (
    14  	"context"
    15  	"time"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/batcheval/result"
    18  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
    19  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/spanset"
    20  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    21  	"github.com/cockroachdb/cockroach/pkg/storage"
    22  	"github.com/cockroachdb/cockroach/pkg/util/uuid"
    23  )
    24  
    25  func init() {
    26  	RegisterReadOnlyCommand(roachpb.ComputeChecksum, declareKeysComputeChecksum, ComputeChecksum)
    27  }
    28  
    29  func declareKeysComputeChecksum(
    30  	_ *roachpb.RangeDescriptor, _ roachpb.Header, _ roachpb.Request, _, _ *spanset.SpanSet,
    31  ) {
    32  	// Intentionally declare no keys, as ComputeChecksum does not need to be
    33  	// serialized with any other commands. It simply needs to be committed into
    34  	// the Raft log.
    35  }
    36  
    37  // Version numbers for Replica checksum computation. Requests silently no-op
    38  // unless the versions are compatible.
    39  const (
    40  	ReplicaChecksumVersion    = 4
    41  	ReplicaChecksumGCInterval = time.Hour
    42  )
    43  
    44  // ComputeChecksum starts the process of computing a checksum on the replica at
    45  // a particular snapshot. The checksum is later verified through a
    46  // CollectChecksumRequest.
    47  func ComputeChecksum(
    48  	_ context.Context, _ storage.Reader, cArgs CommandArgs, resp roachpb.Response,
    49  ) (result.Result, error) {
    50  	args := cArgs.Args.(*roachpb.ComputeChecksumRequest)
    51  
    52  	reply := resp.(*roachpb.ComputeChecksumResponse)
    53  	reply.ChecksumID = uuid.MakeV4()
    54  
    55  	var pd result.Result
    56  	pd.Replicated.ComputeChecksum = &kvserverpb.ComputeChecksum{
    57  		Version:      args.Version,
    58  		ChecksumID:   reply.ChecksumID,
    59  		SaveSnapshot: args.Snapshot,
    60  		Mode:         args.Mode,
    61  		Checkpoint:   args.Checkpoint,
    62  		Terminate:    args.Terminate,
    63  	}
    64  	return pd, nil
    65  }