Skip to content

Commit

Permalink
kv: Don't sort replicas by latency if we know the leaseholder
Browse files Browse the repository at this point in the history
This avoids a bunch of unnecessary latency lookups and sorting in the
common case.

Release note: None
  • Loading branch information
a-robinson committed Sep 13, 2018
1 parent 778586d commit 1be2119
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
19 changes: 11 additions & 8 deletions pkg/kv/dist_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,23 +433,26 @@ func (ds *DistSender) sendSingleRange(
// Try to send the call.
replicas := NewReplicaSlice(ds.gossip, desc)

// Rearrange the replicas so that they're ordered in expectation of
// request latency.
var latencyFn LatencyFunc
if ds.rpcContext != nil {
latencyFn = ds.rpcContext.RemoteClocks.Latency
}
replicas.OptimizeReplicaOrder(ds.getNodeDescriptor(), latencyFn)

// If this request needs to go to a lease holder and we know who that is, move
// it to the front.
var knowLeaseholder bool
if !ba.IsReadOnly() || ba.ReadConsistency.RequiresReadLease() {
if storeID, ok := ds.leaseHolderCache.Lookup(ctx, desc.RangeID); ok {
if i := replicas.FindReplica(storeID); i >= 0 {
replicas.MoveToFront(i)
knowLeaseholder = true
}
}
}
if !knowLeaseholder {
// Rearrange the replicas so that they're ordered in expectation of
// request latency.
var latencyFn LatencyFunc
if ds.rpcContext != nil {
latencyFn = ds.rpcContext.RemoteClocks.Latency
}
replicas.OptimizeReplicaOrder(ds.getNodeDescriptor(), latencyFn)
}

br, err := ds.sendRPC(ctx, desc.RangeID, replicas, ba)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/kv/dist_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ func TestSendRPCOrder(t *testing.T) {
{
args: &roachpb.PutRequest{},
tiers: append(nodeTiers[5], roachpb.Tier{Key: "irrelevant", Value: ""}),
// Compare only the first resulting addresses as we have a lease holder
// Compare only the first resulting address as we have a lease holder
// and that means we're only trying to send there.
expReplica: []roachpb.NodeID{2, 5, 4, 0, 0},
expReplica: []roachpb.NodeID{2, 0, 0, 0, 0},
leaseHolder: 2,
},
// Inconsistent Get without matching attributes but lease holder (node 3). Should just
Expand Down Expand Up @@ -335,6 +335,7 @@ func TestSendRPCOrder(t *testing.T) {
ds := NewDistSender(cfg, g)

for n, tc := range testCases {
log.Infof(context.TODO(), "testcase %d", n)
verifyCall = makeVerifier(tc.expReplica)

{
Expand Down

0 comments on commit 1be2119

Please sign in to comment.