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

     1  // Copyright 2016 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 kvserver
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/keys"
    17  	"github.com/cockroachdb/cockroach/pkg/kv"
    18  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/abortspan"
    19  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/batcheval"
    20  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency"
    21  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverbase"
    22  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/spanset"
    23  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    24  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    25  	"github.com/cockroachdb/cockroach/pkg/storage"
    26  	"github.com/cockroachdb/cockroach/pkg/storage/cloud"
    27  	"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
    28  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    29  	"github.com/cockroachdb/cockroach/pkg/util/uuid"
    30  )
    31  
    32  // SpanSetReplicaEvalContext is a testing-only implementation of
    33  // ReplicaEvalContext which verifies that access to state is registered in the
    34  // SpanSet if one is given.
    35  type SpanSetReplicaEvalContext struct {
    36  	i  batcheval.EvalContext
    37  	ss spanset.SpanSet
    38  }
    39  
    40  var _ batcheval.EvalContext = &SpanSetReplicaEvalContext{}
    41  
    42  // AbortSpan returns the abort span.
    43  func (rec *SpanSetReplicaEvalContext) AbortSpan() *abortspan.AbortSpan {
    44  	return rec.i.AbortSpan()
    45  }
    46  
    47  // EvalKnobs returns the batch evaluation Knobs.
    48  func (rec *SpanSetReplicaEvalContext) EvalKnobs() kvserverbase.BatchEvalTestingKnobs {
    49  	return rec.i.EvalKnobs()
    50  }
    51  
    52  // StoreID returns the StoreID.
    53  func (rec *SpanSetReplicaEvalContext) StoreID() roachpb.StoreID {
    54  	return rec.i.StoreID()
    55  }
    56  
    57  // GetRangeID returns the RangeID.
    58  func (rec *SpanSetReplicaEvalContext) GetRangeID() roachpb.RangeID {
    59  	return rec.i.GetRangeID()
    60  }
    61  
    62  // ClusterSettings returns the cluster settings.
    63  func (rec *SpanSetReplicaEvalContext) ClusterSettings() *cluster.Settings {
    64  	return rec.i.ClusterSettings()
    65  }
    66  
    67  // Clock returns the Replica's clock.
    68  func (rec *SpanSetReplicaEvalContext) Clock() *hlc.Clock {
    69  	return rec.i.Clock()
    70  }
    71  
    72  // DB returns the Replica's client DB.
    73  func (rec *SpanSetReplicaEvalContext) DB() *kv.DB {
    74  	return rec.i.DB()
    75  }
    76  
    77  // GetConcurrencyManager returns the concurrency.Manager.
    78  func (rec *SpanSetReplicaEvalContext) GetConcurrencyManager() concurrency.Manager {
    79  	return rec.i.GetConcurrencyManager()
    80  }
    81  
    82  // NodeID returns the NodeID.
    83  func (rec *SpanSetReplicaEvalContext) NodeID() roachpb.NodeID {
    84  	return rec.i.NodeID()
    85  }
    86  
    87  // GetNodeLocality returns the node locality.
    88  func (rec *SpanSetReplicaEvalContext) GetNodeLocality() roachpb.Locality {
    89  	return rec.i.GetNodeLocality()
    90  }
    91  
    92  // Engine returns the engine.
    93  func (rec *SpanSetReplicaEvalContext) Engine() storage.Engine {
    94  	return rec.i.Engine()
    95  }
    96  
    97  // GetFirstIndex returns the first index.
    98  func (rec *SpanSetReplicaEvalContext) GetFirstIndex() (uint64, error) {
    99  	return rec.i.GetFirstIndex()
   100  }
   101  
   102  // GetTerm returns the term for the given index in the Raft log.
   103  func (rec *SpanSetReplicaEvalContext) GetTerm(i uint64) (uint64, error) {
   104  	return rec.i.GetTerm(i)
   105  }
   106  
   107  // GetLeaseAppliedIndex returns the lease index of the last applied command.
   108  func (rec *SpanSetReplicaEvalContext) GetLeaseAppliedIndex() uint64 {
   109  	return rec.i.GetLeaseAppliedIndex()
   110  }
   111  
   112  // IsFirstRange returns true iff the replica belongs to the first range.
   113  func (rec *SpanSetReplicaEvalContext) IsFirstRange() bool {
   114  	return rec.i.IsFirstRange()
   115  }
   116  
   117  // Desc returns the Replica's RangeDescriptor.
   118  func (rec SpanSetReplicaEvalContext) Desc() *roachpb.RangeDescriptor {
   119  	desc := rec.i.Desc()
   120  	rec.ss.AssertAllowed(spanset.SpanReadOnly,
   121  		roachpb.Span{Key: keys.RangeDescriptorKey(desc.StartKey)},
   122  	)
   123  	return desc
   124  }
   125  
   126  // ContainsKey returns true if the given key is within the Replica's range.
   127  //
   128  // TODO(bdarnell): Replace this method with one on Desc(). See comment
   129  // on Replica.ContainsKey.
   130  func (rec SpanSetReplicaEvalContext) ContainsKey(key roachpb.Key) bool {
   131  	desc := rec.Desc() // already asserts
   132  	return kvserverbase.ContainsKey(desc, key)
   133  }
   134  
   135  // GetMVCCStats returns the Replica's MVCCStats.
   136  func (rec SpanSetReplicaEvalContext) GetMVCCStats() enginepb.MVCCStats {
   137  	// Thanks to commutativity, the spanlatch manager does not have to serialize
   138  	// on the MVCCStats key. This means that the key is not included in SpanSet
   139  	// declarations, so there's nothing to assert here.
   140  	return rec.i.GetMVCCStats()
   141  }
   142  
   143  // GetSplitQPS returns the Replica's queries/s rate for splitting purposes.
   144  func (rec SpanSetReplicaEvalContext) GetSplitQPS() float64 {
   145  	return rec.i.GetSplitQPS()
   146  }
   147  
   148  // CanCreateTxnRecord determines whether a transaction record can be created
   149  // for the provided transaction information. See Replica.CanCreateTxnRecord
   150  // for details about its arguments, return values, and preconditions.
   151  func (rec SpanSetReplicaEvalContext) CanCreateTxnRecord(
   152  	txnID uuid.UUID, txnKey []byte, txnMinTS hlc.Timestamp,
   153  ) (bool, hlc.Timestamp, roachpb.TransactionAbortedReason) {
   154  	rec.ss.AssertAllowed(spanset.SpanReadOnly,
   155  		roachpb.Span{Key: keys.TransactionKey(txnKey, txnID)},
   156  	)
   157  	return rec.i.CanCreateTxnRecord(txnID, txnKey, txnMinTS)
   158  }
   159  
   160  // GetGCThreshold returns the GC threshold of the Range, typically updated when
   161  // keys are garbage collected. Reads and writes at timestamps <= this time will
   162  // not be served.
   163  func (rec SpanSetReplicaEvalContext) GetGCThreshold() hlc.Timestamp {
   164  	rec.ss.AssertAllowed(spanset.SpanReadOnly,
   165  		roachpb.Span{Key: keys.RangeLastGCKey(rec.GetRangeID())},
   166  	)
   167  	return rec.i.GetGCThreshold()
   168  }
   169  
   170  // String implements Stringer.
   171  func (rec SpanSetReplicaEvalContext) String() string {
   172  	return rec.i.String()
   173  }
   174  
   175  // GetLastReplicaGCTimestamp returns the last time the Replica was
   176  // considered for GC.
   177  func (rec SpanSetReplicaEvalContext) GetLastReplicaGCTimestamp(
   178  	ctx context.Context,
   179  ) (hlc.Timestamp, error) {
   180  	if err := rec.ss.CheckAllowed(spanset.SpanReadOnly,
   181  		roachpb.Span{Key: keys.RangeLastReplicaGCTimestampKey(rec.GetRangeID())},
   182  	); err != nil {
   183  		return hlc.Timestamp{}, err
   184  	}
   185  	return rec.i.GetLastReplicaGCTimestamp(ctx)
   186  }
   187  
   188  // GetLease returns the Replica's current and next lease (if any).
   189  func (rec SpanSetReplicaEvalContext) GetLease() (roachpb.Lease, roachpb.Lease) {
   190  	rec.ss.AssertAllowed(spanset.SpanReadOnly,
   191  		roachpb.Span{Key: keys.RangeLeaseKey(rec.GetRangeID())},
   192  	)
   193  	return rec.i.GetLease()
   194  }
   195  
   196  // GetLimiters returns the per-store limiters.
   197  func (rec *SpanSetReplicaEvalContext) GetLimiters() *batcheval.Limiters {
   198  	return rec.i.GetLimiters()
   199  }
   200  
   201  // GetExternalStorage returns an ExternalStorage object, based on
   202  // information parsed from a URI, stored in `dest`.
   203  func (rec *SpanSetReplicaEvalContext) GetExternalStorage(
   204  	ctx context.Context, dest roachpb.ExternalStorage,
   205  ) (cloud.ExternalStorage, error) {
   206  	return rec.i.GetExternalStorage(ctx, dest)
   207  }
   208  
   209  // GetExternalStorageFromURI returns an ExternalStorage object, based on the given URI.
   210  func (rec *SpanSetReplicaEvalContext) GetExternalStorageFromURI(
   211  	ctx context.Context, uri string,
   212  ) (cloud.ExternalStorage, error) {
   213  	return rec.i.GetExternalStorageFromURI(ctx, uri)
   214  }