Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
74865: kvcoord: break dependency on localtestcluster r=irfansharif a=irfansharif

The earlier form prevented localtestcluster to transitively depend on
kvcoord, something we want in cockroachdb#73876 through the rangefeed package.
This commit moves a few tests to package external ones; to do so it
exports a few symbols (cluster setting variables) and defines some
(public) testing methods to mutate/retrieve internal state.

Release note: None

Co-authored-by: irfan sharif <[email protected]>
  • Loading branch information
craig[bot] and irfansharif committed Jan 17, 2022
2 parents 4e50204 + d0b0d45 commit 7675946
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 249 deletions.
1 change: 1 addition & 0 deletions pkg/kv/kvclient/kvcoord/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ go_test(
"txn_coord_sender_server_test.go",
"txn_coord_sender_test.go",
"txn_correctness_test.go",
"txn_intercepter_pipeliner_client_test.go",
"txn_interceptor_committer_test.go",
"txn_interceptor_heartbeater_test.go",
"txn_interceptor_pipeliner_test.go",
Expand Down
2 changes: 2 additions & 0 deletions pkg/kv/kvclient/kvcoord/dist_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ var rangeDescriptorCacheSize = settings.RegisterIntSetting(
1e6,
)

// senderConcurrencyLimit controls the maximum number of asynchronous send
// requests.
var senderConcurrencyLimit = settings.RegisterIntSetting(
settings.TenantWritable,
"kv.dist_sender.concurrency_limit",
Expand Down
62 changes: 35 additions & 27 deletions pkg/kv/kvclient/kvcoord/dist_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var (
//
testMetaEndKey = roachpb.RKey(keys.SystemPrefix)
// single meta1 and meta2 range with one replica.
testMetaRangeDescriptor = roachpb.RangeDescriptor{
TestMetaRangeDescriptor = roachpb.RangeDescriptor{
RangeID: 1,
StartKey: roachpb.RKeyMin,
EndKey: testMetaEndKey,
Expand Down Expand Up @@ -142,6 +142,10 @@ func adaptSimpleTransport(fn simpleSendFn) TransportFactory {
}
}

// TestingAdaptSimpleTransport exports adaptSimpleTransport for package external
// tests.
var TestingAdaptSimpleTransport = adaptSimpleTransport

type simpleTransportAdapter struct {
fn simpleSendFn
replicas []roachpb.ReplicaDescriptor
Expand Down Expand Up @@ -502,12 +506,16 @@ func mockRangeDescriptorDBForDescs(descs ...roachpb.RangeDescriptor) MockRangeDe
})
}

// TestingMockRangeDescriptorDBForDescs exports a testing helper for package
// external tests.
var TestingMockRangeDescriptorDBForDescs = mockRangeDescriptorDBForDescs

var defaultMockRangeDescriptorDB = mockRangeDescriptorDBForDescs(
testMetaRangeDescriptor,
TestMetaRangeDescriptor,
testUserRangeDescriptor,
)
var threeReplicaMockRangeDescriptorDB = mockRangeDescriptorDBForDescs(
testMetaRangeDescriptor,
TestMetaRangeDescriptor,
testUserRangeDescriptor3Replicas,
)

Expand Down Expand Up @@ -1199,7 +1207,7 @@ func TestRetryOnDescriptorLookupError(t *testing.T) {
RangeDescriptorDB: MockRangeDescriptorDB(func(key roachpb.RKey, _ bool) ([]roachpb.RangeDescriptor, []roachpb.RangeDescriptor, error) {
// Don't return an error on the FirstRange lookup.
if key.Equal(roachpb.KeyMin) {
return []roachpb.RangeDescriptor{testMetaRangeDescriptor}, nil, nil
return []roachpb.RangeDescriptor{TestMetaRangeDescriptor}, nil, nil
}

// Return next error and truncate the prefix of the errors array.
Expand Down Expand Up @@ -1522,7 +1530,7 @@ func TestRetryOnWrongReplicaError(t *testing.T) {
clock := hlc.NewClock(hlc.UnixNano, time.Nanosecond)
rpcContext := rpc.NewInsecureTestingContext(ctx, clock, stopper)
g := makeGossip(t, stopper, rpcContext)
if err := g.AddInfoProto(gossip.KeyFirstRangeDescriptor, &testMetaRangeDescriptor, time.Hour); err != nil {
if err := g.AddInfoProto(gossip.KeyFirstRangeDescriptor, &TestMetaRangeDescriptor, time.Hour); err != nil {
t.Fatal(err)
}

Expand All @@ -1543,7 +1551,7 @@ func TestRetryOnWrongReplicaError(t *testing.T) {
br := &roachpb.BatchResponse{}
r := &roachpb.ScanResponse{}
var kv roachpb.KeyValue
if err := kv.Value.SetProto(&testMetaRangeDescriptor); err != nil {
if err := kv.Value.SetProto(&TestMetaRangeDescriptor); err != nil {
t.Fatal(err)
}
r.Rows = append(r.Rows, kv)
Expand Down Expand Up @@ -1619,7 +1627,7 @@ func TestRetryOnWrongReplicaErrorWithSuggestion(t *testing.T) {
clock := hlc.NewClock(hlc.UnixNano, time.Nanosecond)
rpcContext := rpc.NewInsecureTestingContext(ctx, clock, stopper)
g := makeGossip(t, stopper, rpcContext)
if err := g.AddInfoProto(gossip.KeyFirstRangeDescriptor, &testMetaRangeDescriptor, time.Hour); err != nil {
if err := g.AddInfoProto(gossip.KeyFirstRangeDescriptor, &TestMetaRangeDescriptor, time.Hour); err != nil {
t.Fatal(err)
}

Expand All @@ -1646,7 +1654,7 @@ func TestRetryOnWrongReplicaErrorWithSuggestion(t *testing.T) {
br := &roachpb.BatchResponse{}
r := &roachpb.ScanResponse{}
var kv roachpb.KeyValue
if err := kv.Value.SetProto(&testMetaRangeDescriptor); err != nil {
if err := kv.Value.SetProto(&TestMetaRangeDescriptor); err != nil {
panic(err)
}
r.Rows = append(r.Rows, kv)
Expand Down Expand Up @@ -1799,7 +1807,7 @@ func TestSendRPCRetry(t *testing.T) {
})
}
descDB := mockRangeDescriptorDBForDescs(
testMetaRangeDescriptor,
TestMetaRangeDescriptor,
descriptor,
)

Expand Down Expand Up @@ -1923,7 +1931,7 @@ func TestDistSenderDescriptorUpdatesOnSuccessfulRPCs(t *testing.T) {
},
} {
t.Run("", func(t *testing.T) {
descDB := mockRangeDescriptorDBForDescs(testMetaRangeDescriptor, desc)
descDB := mockRangeDescriptorDBForDescs(TestMetaRangeDescriptor, desc)
var testFn simpleSendFn = func(ctx context.Context, args roachpb.BatchRequest) (*roachpb.BatchResponse, error) {
batchReply := &roachpb.BatchResponse{}
reply := &roachpb.GetResponse{}
Expand Down Expand Up @@ -2012,7 +2020,7 @@ func TestSendRPCRangeNotFoundError(t *testing.T) {
descriptor.AddReplica(roachpb.NodeID(i), roachpb.StoreID(i), roachpb.VOTER_FULL)
}
descDB := mockRangeDescriptorDBForDescs(
testMetaRangeDescriptor,
TestMetaRangeDescriptor,
descriptor,
)

Expand Down Expand Up @@ -2277,8 +2285,8 @@ func TestMultiRangeMergeStaleDescriptor(t *testing.T) {
TransportFactory: adaptSimpleTransport(testFn),
},
RangeDescriptorDB: MockRangeDescriptorDB(func(key roachpb.RKey, _ bool) ([]roachpb.RangeDescriptor, []roachpb.RangeDescriptor, error) {
if key.Less(testMetaRangeDescriptor.EndKey) {
return []roachpb.RangeDescriptor{testMetaRangeDescriptor}, nil, nil
if key.Less(TestMetaRangeDescriptor.EndKey) {
return []roachpb.RangeDescriptor{TestMetaRangeDescriptor}, nil, nil
}
if !merged {
// Assume a range merge operation happened.
Expand Down Expand Up @@ -2329,8 +2337,8 @@ func TestRangeLookupOptionOnReverseScan(t *testing.T) {
if !key.Equal(roachpb.KeyMin) && !useReverseScan {
t.Fatalf("expected UseReverseScan to be set")
}
if key.Less(testMetaRangeDescriptor.EndKey) {
return []roachpb.RangeDescriptor{testMetaRangeDescriptor}, nil, nil
if key.Less(TestMetaRangeDescriptor.EndKey) {
return []roachpb.RangeDescriptor{TestMetaRangeDescriptor}, nil, nil
}
return []roachpb.RangeDescriptor{testUserRangeDescriptor}, nil, nil
}),
Expand Down Expand Up @@ -2458,8 +2466,8 @@ func TestTruncateWithSpanAndDescriptor(t *testing.T) {
Generation: 2,
}
descDB := MockRangeDescriptorDB(func(key roachpb.RKey, _ bool) ([]roachpb.RangeDescriptor, []roachpb.RangeDescriptor, error) {
if key.Less(testMetaRangeDescriptor.EndKey) {
return []roachpb.RangeDescriptor{testMetaRangeDescriptor}, nil, nil
if key.Less(TestMetaRangeDescriptor.EndKey) {
return []roachpb.RangeDescriptor{TestMetaRangeDescriptor}, nil, nil
}
desc := descriptor1
if key.Equal(roachpb.RKey("b")) {
Expand Down Expand Up @@ -2591,7 +2599,7 @@ func TestTruncateWithLocalSpanAndDescriptor(t *testing.T) {
},
}
descDB := mockRangeDescriptorDBForDescs(
testMetaRangeDescriptor,
TestMetaRangeDescriptor,
descriptor1,
descriptor2,
descriptor3,
Expand Down Expand Up @@ -2793,7 +2801,7 @@ func TestMultiRangeWithEndTxn(t *testing.T) {
},
}
descDB := mockRangeDescriptorDBForDescs(
testMetaRangeDescriptor,
TestMetaRangeDescriptor,
descriptor1,
descriptor2,
)
Expand Down Expand Up @@ -3156,7 +3164,7 @@ func TestCountRanges(t *testing.T) {
}

// Mock out descriptor DB and sender function.
descDB := mockRangeDescriptorDBForDescs(append(descriptors[:], testMetaRangeDescriptor)...)
descDB := mockRangeDescriptorDBForDescs(append(descriptors[:], TestMetaRangeDescriptor)...)
cfg := DistSenderConfig{
AmbientCtx: log.MakeTestingAmbientCtxWithNewTracer(),
Clock: clock,
Expand Down Expand Up @@ -3321,7 +3329,7 @@ func TestMultipleErrorsMerged(t *testing.T) {
},
}
descDB := mockRangeDescriptorDBForDescs(
testMetaRangeDescriptor,
TestMetaRangeDescriptor,
descriptor1,
descriptor2,
)
Expand Down Expand Up @@ -3575,7 +3583,7 @@ func TestErrorIndexAlignment(t *testing.T) {
}

descDB := mockRangeDescriptorDBForDescs(
testMetaRangeDescriptor,
TestMetaRangeDescriptor,
descriptor1,
descriptor2,
descriptor3,
Expand Down Expand Up @@ -3776,14 +3784,14 @@ func TestEvictMetaRange(t *testing.T) {
testutils.RunTrueAndFalse(t, "hasSuggestedRange", func(t *testing.T, hasSuggestedRange bool) {
splitKey := keys.RangeMetaKey(roachpb.RKey("b"))

testMeta1RangeDescriptor := testMetaRangeDescriptor
testMeta1RangeDescriptor := TestMetaRangeDescriptor
testMeta1RangeDescriptor.EndKey = roachpb.RKey(keys.Meta2Prefix)

testMeta2RangeDescriptor1 := testMetaRangeDescriptor
testMeta2RangeDescriptor1 := TestMetaRangeDescriptor
testMeta2RangeDescriptor1.RangeID = 2
testMeta2RangeDescriptor1.StartKey = roachpb.RKey(keys.Meta2Prefix)

testMeta2RangeDescriptor2 := testMetaRangeDescriptor
testMeta2RangeDescriptor2 := TestMetaRangeDescriptor
testMeta2RangeDescriptor2.RangeID = 3
testMeta2RangeDescriptor2.StartKey = roachpb.RKey(keys.Meta2Prefix)

Expand Down Expand Up @@ -4076,7 +4084,7 @@ func TestEvictionTokenCoalesce(t *testing.T) {
clock := hlc.NewClock(hlc.UnixNano, time.Nanosecond)
rpcContext := rpc.NewInsecureTestingContext(ctx, clock, stopper)
g := makeGossip(t, stopper, rpcContext)
if err := g.AddInfoProto(gossip.KeyFirstRangeDescriptor, &testMetaRangeDescriptor, time.Hour); err != nil {
if err := g.AddInfoProto(gossip.KeyFirstRangeDescriptor, &TestMetaRangeDescriptor, time.Hour); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -4105,7 +4113,7 @@ func TestEvictionTokenCoalesce(t *testing.T) {
br = &roachpb.BatchResponse{}
r := &roachpb.ScanResponse{}
var kv roachpb.KeyValue
if err := kv.Value.SetProto(&testMetaRangeDescriptor); err != nil {
if err := kv.Value.SetProto(&TestMetaRangeDescriptor); err != nil {
br.Error = roachpb.NewError(err)
return br, nil
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/kv/kvclient/kvcoord/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,32 @@ func (s *condensableSpanSet) asSortedSlice() []roachpb.Span {
sort.Sort(cpy)
return cpy
}

// TestingSenderConcurrencyLimit exports the cluster setting for testing
// purposes.
var TestingSenderConcurrencyLimit = senderConcurrencyLimit

// TestingGetLockFootprint returns the internal lock footprint for testing
// purposes.
func (tc *TxnCoordSender) TestingGetLockFootprint(mergeAndSort bool) []roachpb.Span {
if mergeAndSort {
tc.interceptorAlloc.txnPipeliner.lockFootprint.mergeAndSort()
}
return tc.interceptorAlloc.txnPipeliner.lockFootprint.asSlice()
}

// TestingGetRefreshFootprint returns the internal refresh footprint for testing
// purposes.
func (tc *TxnCoordSender) TestingGetRefreshFootprint() []roachpb.Span {
return tc.interceptorAlloc.txnSpanRefresher.refreshFootprint.asSlice()
}

// TestingSetLinearizable allows tests to enable linearizable behavior.
func (tcf *TxnCoordSenderFactory) TestingSetLinearizable(linearizable bool) {
tcf.linearizable = linearizable
}

// TestingSetMetrics allows tests to override the factory's metrics struct.
func (tcf *TxnCoordSenderFactory) TestingSetMetrics(metrics TxnMetrics) {
tcf.metrics = metrics
}
2 changes: 1 addition & 1 deletion pkg/kv/kvclient/kvcoord/range_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
lastKey = key
}
alphaRangeDescriptorDB = mockRangeDescriptorDBForDescs(
append(alphaRangeDescriptors, testMetaRangeDescriptor)...,
append(alphaRangeDescriptors, TestMetaRangeDescriptor)...,
)
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/kv/kvclient/kvcoord/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package kvcoord
package kvcoord_test

import (
"context"
Expand All @@ -21,6 +21,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage"
Expand Down Expand Up @@ -188,7 +189,7 @@ func TestRangeSplitsWithWritePressure(t *testing.T) {
DisableScanner: true,
},
}
s.Start(t, testutils.NewNodeTestBaseContext(), InitFactoryForLocalTestCluster)
s.Start(t, testutils.NewNodeTestBaseContext(), kvcoord.InitFactoryForLocalTestCluster)

// This is purely to silence log spam.
config.TestingSetupZoneConfigHook(s.Stopper())
Expand Down
Loading

0 comments on commit 7675946

Please sign in to comment.