github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/distsql_plan_backfill_test.go (about) 1 // Copyright 2016 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 sql 12 13 import ( 14 "context" 15 "testing" 16 "time" 17 18 "github.com/cockroachdb/cockroach/pkg/base" 19 "github.com/cockroachdb/cockroach/pkg/keys" 20 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 21 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 22 "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" 23 "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" 24 "github.com/cockroachdb/cockroach/pkg/util" 25 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 26 ) 27 28 func TestDistBackfill(t *testing.T) { 29 defer leaktest.AfterTest(t)() 30 if testing.Short() { 31 t.Skip("short flag #13645") 32 } 33 34 // This test sets up various queries using these tables: 35 // - a NumToSquare table of size N that maps integers from 1 to n to their 36 // squares 37 // - a NumToStr table of size N^2 that maps integers to their string 38 // representations. This table is split and distributed to all the nodes. 39 n := 100 40 if util.RaceEnabled { 41 // Race builds are a lot slower, so use a smaller number of rows. 42 n = 10 43 } 44 const numNodes = 5 45 46 tc := serverutils.StartTestCluster(t, numNodes, 47 base.TestClusterArgs{ 48 ReplicationMode: base.ReplicationManual, 49 ServerArgs: base.TestServerArgs{ 50 UseDatabase: "test", 51 Knobs: base.TestingKnobs{ 52 SQLSchemaChanger: &SchemaChangerTestingKnobs{ 53 // Aggressively write checkpoints, so that 54 // we test checkpointing functionality while 55 // a schema change backfill is progressing. 56 WriteCheckpointInterval: time.Nanosecond, 57 }, 58 }, 59 }, 60 }) 61 defer tc.Stopper().Stop(context.Background()) 62 cdb := tc.Server(0).DB() 63 64 sqlutils.CreateTable( 65 t, tc.ServerConn(0), "numtosquare", "x INT PRIMARY KEY, xsquared INT", 66 n, 67 sqlutils.ToRowFn(sqlutils.RowIdxFn, func(row int) tree.Datum { 68 return tree.NewDInt(tree.DInt(row * row)) 69 }), 70 ) 71 72 sqlutils.CreateTable( 73 t, tc.ServerConn(0), "numtostr", "y INT PRIMARY KEY, str STRING", 74 n*n, 75 sqlutils.ToRowFn(sqlutils.RowIdxFn, sqlutils.RowEnglishFn), 76 ) 77 // Split the table into multiple ranges. 78 descNumToStr := sqlbase.GetTableDescriptor(cdb, keys.SystemSQLCodec, "test", "numtostr") 79 var sps []SplitPoint 80 //for i := 1; i <= numNodes-1; i++ { 81 for i := numNodes - 1; i > 0; i-- { 82 sps = append(sps, SplitPoint{i, []interface{}{n * n / numNodes * i}}) 83 } 84 SplitTable(t, tc, descNumToStr, sps) 85 86 db := tc.ServerConn(0) 87 db.SetMaxOpenConns(1) 88 r := sqlutils.MakeSQLRunner(db) 89 r.Exec(t, "SET DISTSQL = OFF") 90 if _, err := tc.ServerConn(0).Exec(`CREATE INDEX foo ON numtostr (str)`); err != nil { 91 t.Fatal(err) 92 } 93 r.Exec(t, "SET DISTSQL = ALWAYS") 94 res := r.QueryStr(t, `SELECT str FROM numtostr@foo`) 95 if len(res) != n*n { 96 t.Errorf("expected %d entries, got %d", n*n, len(res)) 97 } 98 // Check res is sorted. 99 curr := "" 100 for i, str := range res { 101 if curr > str[0] { 102 t.Errorf("unexpected unsorted %s > %s at %d", curr, str[0], i) 103 } 104 curr = str[0] 105 } 106 }