github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/stats/gossip_invalidation_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 stats_test 12 13 import ( 14 "context" 15 "fmt" 16 "testing" 17 18 "github.com/cockroachdb/cockroach/pkg/base" 19 "github.com/cockroachdb/cockroach/pkg/gossip" 20 "github.com/cockroachdb/cockroach/pkg/keys" 21 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 22 "github.com/cockroachdb/cockroach/pkg/sql/sqlutil" 23 "github.com/cockroachdb/cockroach/pkg/sql/stats" 24 "github.com/cockroachdb/cockroach/pkg/testutils" 25 "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" 26 "github.com/cockroachdb/cockroach/pkg/testutils/testcluster" 27 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 28 ) 29 30 // TestGossipInvalidation verifies that the cache gets invalidated automatically 31 // when a new stat is generated. 32 func TestGossipInvalidation(t *testing.T) { 33 defer leaktest.AfterTest(t)() 34 35 ctx := context.Background() 36 tc := testcluster.StartTestCluster(t, 3, base.TestClusterArgs{}) 37 defer tc.Stopper().Stop(ctx) 38 39 sc := stats.NewTableStatisticsCache( 40 10, /* cacheSize */ 41 gossip.MakeExposedGossip(tc.Server(0).GossipI().(*gossip.Gossip)), 42 tc.Server(0).DB(), 43 tc.Server(0).InternalExecutor().(sqlutil.InternalExecutor), 44 keys.SystemSQLCodec, 45 ) 46 47 sr0 := sqlutils.MakeSQLRunner(tc.ServerConn(0)) 48 sr0.Exec(t, "CREATE DATABASE test") 49 sr0.Exec(t, "CREATE TABLE test.t (k INT PRIMARY KEY, v INT)") 50 sr0.Exec(t, "INSERT INTO test.t VALUES (1, 1), (2, 2), (3, 3)") 51 52 tableDesc := sqlbase.GetTableDescriptor(tc.Server(0).DB(), keys.SystemSQLCodec, "test", "t") 53 tableID := tableDesc.ID 54 55 expectNStats := func(n int) error { 56 stats, err := sc.GetTableStats(ctx, tableID) 57 if err != nil { 58 t.Fatal(err) 59 } 60 if len(stats) != n { 61 return fmt.Errorf("expected %d stats, got: %v", n, stats) 62 } 63 return nil 64 } 65 66 if err := expectNStats(0); err != nil { 67 t.Fatal(err) 68 } 69 sr1 := sqlutils.MakeSQLRunner(tc.ServerConn(1)) 70 sr1.Exec(t, "CREATE STATISTICS k ON k FROM test.t") 71 72 testutils.SucceedsSoon(t, func() error { 73 return expectNStats(1) 74 }) 75 76 sr2 := sqlutils.MakeSQLRunner(tc.ServerConn(2)) 77 sr2.Exec(t, "CREATE STATISTICS v ON v FROM test.t") 78 79 testutils.SucceedsSoon(t, func() error { 80 return expectNStats(2) 81 }) 82 }