github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/physicalplan/fake_span_resolver_test.go (about) 1 // Copyright 2017 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 physicalplan_test 12 13 import ( 14 "context" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/base" 18 "github.com/cockroachdb/cockroach/pkg/keys" 19 "github.com/cockroachdb/cockroach/pkg/kv" 20 "github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord" 21 "github.com/cockroachdb/cockroach/pkg/roachpb" 22 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 23 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 24 "github.com/cockroachdb/cockroach/pkg/testutils/physicalplanutils" 25 "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" 26 "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" 27 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 28 ) 29 30 func TestFakeSpanResolver(t *testing.T) { 31 defer leaktest.AfterTest(t)() 32 ctx := context.Background() 33 34 tc := serverutils.StartTestCluster(t, 3, base.TestClusterArgs{}) 35 defer tc.Stopper().Stop(ctx) 36 37 sqlutils.CreateTable( 38 t, tc.ServerConn(0), "t", 39 "k INT PRIMARY KEY, v INT", 40 100, 41 func(row int) []tree.Datum { 42 return []tree.Datum{ 43 tree.NewDInt(tree.DInt(row)), 44 tree.NewDInt(tree.DInt(row * row)), 45 } 46 }, 47 ) 48 49 resolver := physicalplanutils.FakeResolverForTestCluster(tc) 50 51 db := tc.Server(0).DB() 52 53 txn := kv.NewTxn(ctx, db, tc.Server(0).NodeID()) 54 it := resolver.NewSpanResolverIterator(txn) 55 56 tableDesc := sqlbase.GetTableDescriptor(db, keys.SystemSQLCodec, "test", "t") 57 primIdxValDirs := sqlbase.IndexKeyValDirs(&tableDesc.PrimaryIndex) 58 59 span := tableDesc.PrimaryIndexSpan(keys.SystemSQLCodec) 60 61 // Make sure we see all the nodes. It will not always happen (due to 62 // randomness) but it should happen most of the time. 63 for attempt := 0; attempt < 10; attempt++ { 64 nodesSeen := make(map[roachpb.NodeID]struct{}) 65 it.Seek(ctx, span, kvcoord.Ascending) 66 lastKey := span.Key 67 for { 68 if !it.Valid() { 69 t.Fatal(it.Error()) 70 } 71 desc := it.Desc() 72 rinfo, err := it.ReplicaInfo(ctx) 73 if err != nil { 74 t.Fatal(err) 75 } 76 77 prettyStart := keys.PrettyPrint(primIdxValDirs, desc.StartKey.AsRawKey()) 78 prettyEnd := keys.PrettyPrint(primIdxValDirs, desc.EndKey.AsRawKey()) 79 t.Logf("%d %s %s", rinfo.NodeID, prettyStart, prettyEnd) 80 81 if !lastKey.Equal(desc.StartKey.AsRawKey()) { 82 t.Errorf("unexpected start key %s, should be %s", prettyStart, keys.PrettyPrint(primIdxValDirs, span.Key)) 83 } 84 if !desc.StartKey.Less(desc.EndKey) { 85 t.Errorf("invalid range %s to %s", prettyStart, prettyEnd) 86 } 87 lastKey = desc.EndKey.AsRawKey() 88 nodesSeen[rinfo.NodeID] = struct{}{} 89 90 if !it.NeedAnother() { 91 break 92 } 93 it.Next(ctx) 94 } 95 96 if !lastKey.Equal(span.EndKey) { 97 t.Errorf("last key %s, should be %s", keys.PrettyPrint(primIdxValDirs, lastKey), keys.PrettyPrint(primIdxValDirs, span.EndKey)) 98 } 99 100 if len(nodesSeen) == tc.NumServers() { 101 // Saw all the nodes. 102 break 103 } 104 t.Logf("not all nodes seen; retrying") 105 } 106 }