github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/batcheval/cmd_lease_info.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  
    16  	"github.com/cockroachdb/cockroach/pkg/keys"
    17  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/batcheval/result"
    18  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/spanset"
    19  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    20  	"github.com/cockroachdb/cockroach/pkg/storage"
    21  )
    22  
    23  func init() {
    24  	RegisterReadOnlyCommand(roachpb.LeaseInfo, declareKeysLeaseInfo, LeaseInfo)
    25  }
    26  
    27  func declareKeysLeaseInfo(
    28  	_ *roachpb.RangeDescriptor,
    29  	header roachpb.Header,
    30  	req roachpb.Request,
    31  	latchSpans, _ *spanset.SpanSet,
    32  ) {
    33  	latchSpans.AddNonMVCC(spanset.SpanReadOnly, roachpb.Span{Key: keys.RangeLeaseKey(header.RangeID)})
    34  }
    35  
    36  // LeaseInfo returns information about the lease holder for the range.
    37  func LeaseInfo(
    38  	ctx context.Context, reader storage.Reader, cArgs CommandArgs, resp roachpb.Response,
    39  ) (result.Result, error) {
    40  	reply := resp.(*roachpb.LeaseInfoResponse)
    41  	lease, nextLease := cArgs.EvalCtx.GetLease()
    42  	if nextLease != (roachpb.Lease{}) {
    43  		// If there's a lease request in progress, speculatively return that future
    44  		// lease.
    45  		reply.Lease = nextLease
    46  	} else {
    47  		reply.Lease = lease
    48  	}
    49  	return result.Result{}, nil
    50  }