Skip to content

Commit

Permalink
kv: test eviction on first range gossip
Browse files Browse the repository at this point in the history
I briefly stress tested this to make sure it isn't obviously flaky, and also
verified that it fails without cockroachdb#8163.

Fixes cockroachdb#8164.
  • Loading branch information
tbg committed Aug 3, 2016
1 parent f542487 commit 44b02f4
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions kv/dist_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"fmt"
"reflect"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -587,6 +588,93 @@ func TestRetryOnDescriptorLookupError(t *testing.T) {
}
}

// TestEvictOnFirstRangeGossip verifies that we evict the first range
// descriptor from the descriptor cache when a gossip update is received for
// the first range.
func TestEvictOnFirstRangeGossip(t *testing.T) {
defer leaktest.AfterTest(t)()

n := simulation.NewNetwork(1, true)
n.Start()
defer n.Stop()
g := n.Nodes[0].Gossip

var sender client.SenderFunc = func(_ context.Context, ba roachpb.BatchRequest) (
*roachpb.BatchResponse, *roachpb.Error,
) {
return ba.CreateReply(), nil
}

desc := roachpb.RangeDescriptor{
RangeID: 1,
StartKey: roachpb.RKeyMin,
EndKey: roachpb.RKeyMax,
Replicas: []roachpb.ReplicaDescriptor{
{
NodeID: 1,
StoreID: 1,
},
},
}

var numFirstRange int32

rDB := MockRangeDescriptorDB(func(key roachpb.RKey, _, _ bool) (
[]roachpb.RangeDescriptor, []roachpb.RangeDescriptor, *roachpb.Error,
) {
if len(key) == 0 {
fmt.Println("lookup")
atomic.AddInt32(&numFirstRange, 1)
}
return []roachpb.RangeDescriptor{desc}, nil, nil
})

ctx := &DistSenderContext{
TransportFactory: SenderTransportFactory(tracing.NewTracer(), sender),
RangeDescriptorDB: rDB,
}

ds := NewDistSender(ctx, g)

anyKey := roachpb.Key("anything")

getArg := roachpb.NewIncrement(anyKey, 0)
if _, pErr := client.SendWrapped(ds, context.Background(), getArg); pErr != nil {
t.Fatal(pErr)
}

call := func() {
if _, _, err := ds.rangeCache.LookupRangeDescriptor(
context.Background(), keys.MustAddr(anyKey), nil, false, false,
); err != nil {
t.Fatal(err)
}
}

call()
call()
call()

if num := atomic.LoadInt32(&numFirstRange); num != 1 {
t.Fatalf("expected one first range lookup, got %d", num)
}

if err := g.AddInfoProto(gossip.KeyFirstRangeDescriptor, &desc, 0); err != nil {
t.Fatal(err)
}

// Once Gossip fires the callbacks, we should see a cache eviction and thus,
// a new cache hit.
util.SucceedsSoon(t, func() error {
call()
if exp, act := int32(2), atomic.LoadInt32(&numFirstRange); exp != act {
return errors.Errorf("expected %d first range lookups, got %d", exp, act)
}
return nil
})

}

func TestEvictCacheOnError(t *testing.T) {
defer leaktest.AfterTest(t)()
// if rpcError is true, the first attempt gets an RPC error, otherwise
Expand Down

0 comments on commit 44b02f4

Please sign in to comment.