From 229bbaa7f6e2e5eb5589bf7c3ccfb3cc3732d6cd Mon Sep 17 00:00:00 2001 From: Dirkjan Bussink Date: Fri, 23 Feb 2024 14:51:49 +0100 Subject: [PATCH] srvtopo: Setup metrics in init() function (#15304) Signed-off-by: Dirkjan Bussink --- go/cmd/vtcombo/cli/main.go | 10 +++-- go/cmd/vtexplain/cli/vtexplain.go | 4 +- go/cmd/vtgate/cli/cli.go | 9 ++++- go/cmd/vttablet/cli/cli.go | 13 ++++-- go/vt/srvtopo/discover_test.go | 4 +- go/vt/srvtopo/resilient_server.go | 10 +---- go/vt/srvtopo/resilient_server_test.go | 31 ++++++++------ go/vt/srvtopo/resolver_test.go | 8 ++-- go/vt/vtadmin/api.go | 4 +- go/vt/vtcombo/tablet_map.go | 14 ++++--- go/vt/vtexplain/vtexplain.go | 5 ++- go/vt/vtexplain/vtexplain_test.go | 7 +++- go/vt/vtexplain/vtexplain_vtgate.go | 9 +++-- go/vt/vtexplain/vtexplain_vttablet.go | 5 ++- go/vt/vtexplain/vtexplain_vttablet_test.go | 8 ++-- go/vt/vttablet/endtoend/framework/server.go | 4 +- .../tabletserver/query_executor_test.go | 4 +- go/vt/vttablet/tabletserver/tabletserver.go | 14 ++----- .../tabletserver/tabletserver_test.go | 40 +++++++++++++------ .../tabletserver/vstreamer/testenv/testenv.go | 7 ++-- 20 files changed, 129 insertions(+), 81 deletions(-) diff --git a/go/cmd/vtcombo/cli/main.go b/go/cmd/vtcombo/cli/main.go index a86b98cc250..16e625a6ae6 100644 --- a/go/cmd/vtcombo/cli/main.go +++ b/go/cmd/vtcombo/cli/main.go @@ -32,6 +32,7 @@ import ( "vitess.io/vitess/go/acl" "vitess.io/vitess/go/mysql/replication" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/logutil" @@ -84,6 +85,8 @@ In particular, it contains: tabletTypesToWait []topodatapb.TabletType env *vtenv.Environment + + srvTopoCounts *stats.CountersWithSingleLabel ) func init() { @@ -131,6 +134,7 @@ func init() { if err != nil { log.Fatalf("unable to initialize env: %v", err) } + srvTopoCounts = stats.NewCountersWithSingleLabel("ResilientSrvTopoServer", "Resilient srvtopo server operations", "type") } func startMysqld(uid uint32) (mysqld *mysqlctl.Mysqld, cnf *mysqlctl.Mycnf, err error) { @@ -234,7 +238,7 @@ func run(cmd *cobra.Command, args []string) (err error) { // to be the "internal" protocol that InitTabletMap registers. cmd.Flags().Set("tablet_manager_protocol", "internal") cmd.Flags().Set("tablet_protocol", "internal") - uid, err := vtcombo.InitTabletMap(env, ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, startMysql) + uid, err := vtcombo.InitTabletMap(env, ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, startMysql, srvTopoCounts) if err != nil { // ensure we start mysql in the event we fail here if startMysql { @@ -260,7 +264,7 @@ func run(cmd *cobra.Command, args []string) (err error) { } wr := wrangler.New(env, logutil.NewConsoleLogger(), ts, nil) - newUID, err := vtcombo.CreateKs(ctx, env, ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, ks, true, uid, wr) + newUID, err := vtcombo.CreateKs(ctx, env, ts, &tpb, mysqld, &dbconfigs.GlobalDBConfigs, schemaDir, ks, true, uid, wr, srvTopoCounts) if err != nil { return err } @@ -297,7 +301,7 @@ func run(cmd *cobra.Command, args []string) (err error) { } // vtgate configuration and init - resilientServer = srvtopo.NewResilientServer(context.Background(), ts, "ResilientSrvTopoServer") + resilientServer = srvtopo.NewResilientServer(context.Background(), ts, srvTopoCounts) tabletTypes := make([]topodatapb.TabletType, 0, 1) if len(tabletTypesToWait) != 0 { diff --git a/go/cmd/vtexplain/cli/vtexplain.go b/go/cmd/vtexplain/cli/vtexplain.go index c8671cdb532..fe17d9af47c 100644 --- a/go/cmd/vtexplain/cli/vtexplain.go +++ b/go/cmd/vtexplain/cli/vtexplain.go @@ -22,6 +22,7 @@ import ( "os" "vitess.io/vitess/go/acl" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/servenv" "vitess.io/vitess/go/vt/topo/memorytopo" @@ -186,7 +187,8 @@ func parseAndRun() error { } ctx := context.Background() ts := memorytopo.NewServer(ctx, vtexplain.Cell) - vte, err := vtexplain.Init(ctx, env, ts, vschema, schema, ksShardMap, opts) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + vte, err := vtexplain.Init(ctx, env, ts, vschema, schema, ksShardMap, opts, srvTopoCounts) if err != nil { return err } diff --git a/go/cmd/vtgate/cli/cli.go b/go/cmd/vtgate/cli/cli.go index c348c4d8296..312396a5a3c 100644 --- a/go/cmd/vtgate/cli/cli.go +++ b/go/cmd/vtgate/cli/cli.go @@ -25,6 +25,7 @@ import ( "vitess.io/vitess/go/acl" "vitess.io/vitess/go/exit" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/discovery" "vitess.io/vitess/go/vt/servenv" "vitess.io/vitess/go/vt/srvtopo" @@ -73,8 +74,14 @@ var ( PreRunE: servenv.CobraPreRunE, RunE: run, } + + srvTopoCounts *stats.CountersWithSingleLabel ) +func init() { + srvTopoCounts = stats.NewCountersWithSingleLabel("ResilientSrvTopoServer", "Resilient srvtopo server operations", "type") +} + // CheckCellFlags will check validation of cell and cells_to_watch flag // it will help to avoid strange behaviors when vtgate runs but actually does not work func CheckCellFlags(ctx context.Context, serv srvtopo.Server, cell string, cellsToWatch string) error { @@ -139,7 +146,7 @@ func run(cmd *cobra.Command, args []string) error { ts := topo.Open() defer ts.Close() - resilientServer = srvtopo.NewResilientServer(context.Background(), ts, "ResilientSrvTopoServer") + resilientServer = srvtopo.NewResilientServer(context.Background(), ts, srvTopoCounts) tabletTypes := make([]topodatapb.TabletType, 0, 1) for _, tt := range tabletTypesToWait { diff --git a/go/cmd/vttablet/cli/cli.go b/go/cmd/vttablet/cli/cli.go index 872d7a5ef69..ab212127457 100644 --- a/go/cmd/vttablet/cli/cli.go +++ b/go/cmd/vttablet/cli/cli.go @@ -27,6 +27,7 @@ import ( "vitess.io/vitess/go/acl" "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/binlog" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/log" @@ -102,8 +103,14 @@ vttablet \ PreRunE: servenv.CobraPreRunE, RunE: run, } + + srvTopoCounts *stats.CountersWithSingleLabel ) +func init() { + srvTopoCounts = stats.NewCountersWithSingleLabel("TabletSrvTopo", "Resilient srvtopo server operations", "type") +} + func run(cmd *cobra.Command, args []string) error { servenv.Init() @@ -129,7 +136,7 @@ func run(cmd *cobra.Command, args []string) error { } ts := topo.Open() - qsc, err := createTabletServer(context.Background(), env, config, ts, tabletAlias) + qsc, err := createTabletServer(context.Background(), env, config, ts, tabletAlias, srvTopoCounts) if err != nil { ts.Close() return err @@ -249,7 +256,7 @@ func extractOnlineDDL() error { return nil } -func createTabletServer(ctx context.Context, env *vtenv.Environment, config *tabletenv.TabletConfig, ts *topo.Server, tabletAlias *topodatapb.TabletAlias) (*tabletserver.TabletServer, error) { +func createTabletServer(ctx context.Context, env *vtenv.Environment, config *tabletenv.TabletConfig, ts *topo.Server, tabletAlias *topodatapb.TabletAlias, srvTopoCounts *stats.CountersWithSingleLabel) (*tabletserver.TabletServer, error) { if tableACLConfig != "" { // To override default simpleacl, other ACL plugins must set themselves to be default ACL factory tableacl.Register("simpleacl", &simpleacl.Factory{}) @@ -258,7 +265,7 @@ func createTabletServer(ctx context.Context, env *vtenv.Environment, config *tab } // creates and registers the query service - qsc := tabletserver.NewTabletServer(ctx, env, "", config, ts, tabletAlias) + qsc := tabletserver.NewTabletServer(ctx, env, "", config, ts, tabletAlias, srvTopoCounts) servenv.OnRun(func() { qsc.Register() addStatusParts(qsc) diff --git a/go/vt/srvtopo/discover_test.go b/go/vt/srvtopo/discover_test.go index ca4774a1b84..81279b7d61a 100644 --- a/go/vt/srvtopo/discover_test.go +++ b/go/vt/srvtopo/discover_test.go @@ -23,6 +23,7 @@ import ( "testing" "time" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/topo/memorytopo" querypb "vitess.io/vitess/go/vt/proto/query" @@ -59,7 +60,8 @@ func TestFindAllTargets(t *testing.T) { srvTopoCacheTTL = 1 * time.Second }() - rs := NewResilientServer(ctx, ts, "TestFindAllKeyspaceShards") + counts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + rs := NewResilientServer(ctx, ts, counts) // No keyspace / shards. ks, err := FindAllTargets(ctx, rs, "cell1", []topodatapb.TabletType{topodatapb.TabletType_PRIMARY}) diff --git a/go/vt/srvtopo/resilient_server.go b/go/vt/srvtopo/resilient_server.go index 0cbccbdb31c..78fc9134bce 100644 --- a/go/vt/srvtopo/resilient_server.go +++ b/go/vt/srvtopo/resilient_server.go @@ -70,7 +70,6 @@ const ( // - return the last known value of the data if there is an error type ResilientServer struct { topoServer *topo.Server - counts *stats.CountersWithSingleLabel *SrvKeyspaceWatcher *SrvVSchemaWatcher @@ -79,20 +78,13 @@ type ResilientServer struct { // NewResilientServer creates a new ResilientServer // based on the provided topo.Server. -func NewResilientServer(ctx context.Context, base *topo.Server, counterPrefix string) *ResilientServer { +func NewResilientServer(ctx context.Context, base *topo.Server, counts *stats.CountersWithSingleLabel) *ResilientServer { if srvTopoCacheRefresh > srvTopoCacheTTL { log.Fatalf("srv_topo_cache_refresh must be less than or equal to srv_topo_cache_ttl") } - metric := "" - if counterPrefix != "" { - metric = counterPrefix + "Counts" - } - counts := stats.NewCountersWithSingleLabel(metric, "Resilient srvtopo server operations", "type") - return &ResilientServer{ topoServer: base, - counts: counts, SrvKeyspaceWatcher: NewSrvKeyspaceWatcher(ctx, base, counts, srvTopoCacheRefresh, srvTopoCacheTTL), SrvVSchemaWatcher: NewSrvVSchemaWatcher(ctx, base, counts, srvTopoCacheRefresh, srvTopoCacheTTL), SrvKeyspaceNamesQuery: NewSrvKeyspaceNamesQuery(base, counts, srvTopoCacheRefresh, srvTopoCacheTTL), diff --git a/go/vt/srvtopo/resilient_server_test.go b/go/vt/srvtopo/resilient_server_test.go index fe248f56087..dbcf48ce176 100644 --- a/go/vt/srvtopo/resilient_server_test.go +++ b/go/vt/srvtopo/resilient_server_test.go @@ -28,6 +28,7 @@ import ( "github.com/google/safehtml/template" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/key" "github.com/stretchr/testify/assert" @@ -53,7 +54,8 @@ func TestGetSrvKeyspace(t *testing.T) { srvTopoCacheRefresh = 1 * time.Second }() - rs := NewResilientServer(ctx, ts, "TestGetSrvKeyspace") + counts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + rs := NewResilientServer(ctx, ts, counts) // Ask for a not-yet-created keyspace _, err := rs.GetSrvKeyspace(context.Background(), "test_cell", "test_ks") @@ -175,7 +177,7 @@ func TestGetSrvKeyspace(t *testing.T) { // Now simulate a topo service error and see that the last value is // cached for at least half of the expected ttl. errorTestStart := time.Now() - errorReqsBefore := rs.counts.Counts()[errorCategory] + errorReqsBefore := counts.Counts()[errorCategory] forceErr := topo.NewError(topo.Timeout, "test topo error") factory.SetError(forceErr) @@ -271,7 +273,7 @@ func TestGetSrvKeyspace(t *testing.T) { // Check that the expected number of errors were counted during the // interval - errorReqs := rs.counts.Counts()[errorCategory] + errorReqs := counts.Counts()[errorCategory] expectedErrors := int64(time.Since(errorTestStart) / srvTopoCacheRefresh) if errorReqs-errorReqsBefore > expectedErrors { t.Errorf("expected <= %v error requests got %d", expectedErrors, errorReqs-errorReqsBefore) @@ -370,7 +372,8 @@ func TestSrvKeyspaceCachedError(t *testing.T) { srvTopoCacheTTL = 1 * time.Second srvTopoCacheRefresh = 1 * time.Second }() - rs := NewResilientServer(ctx, ts, "TestSrvKeyspaceCachedErrors") + counts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + rs := NewResilientServer(ctx, ts, counts) // Ask for an unknown keyspace, should get an error. _, err := rs.GetSrvKeyspace(ctx, "test_cell", "unknown_ks") @@ -401,7 +404,8 @@ func TestGetSrvKeyspaceCreated(t *testing.T) { defer cancel() ts := memorytopo.NewServer(ctx, "test_cell") defer ts.Close() - rs := NewResilientServer(ctx, ts, "TestGetSrvKeyspaceCreated") + counts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + rs := NewResilientServer(ctx, ts, counts) // Set SrvKeyspace with value. want := &topodatapb.SrvKeyspace{} @@ -435,7 +439,8 @@ func TestWatchSrvVSchema(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "test_cell") - rs := NewResilientServer(ctx, ts, "TestWatchSrvVSchema") + counts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + rs := NewResilientServer(ctx, ts, counts) // mu protects watchValue and watchErr. mu := sync.Mutex{} @@ -529,7 +534,8 @@ func TestGetSrvKeyspaceNames(t *testing.T) { srvTopoCacheTTL = 1 * time.Second srvTopoCacheRefresh = 1 * time.Second }() - rs := NewResilientServer(ctx, ts, "TestGetSrvKeyspaceNames") + counts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + rs := NewResilientServer(ctx, ts, counts) // Set SrvKeyspace with value want := &topodatapb.SrvKeyspace{} @@ -614,7 +620,7 @@ func TestGetSrvKeyspaceNames(t *testing.T) { // Check that we only checked the topo service 1 or 2 times during the // period where we got the cached error. - cachedReqs, ok := rs.counts.Counts()[cachedCategory] + cachedReqs, ok := counts.Counts()[cachedCategory] if !ok || cachedReqs > 2 { t.Errorf("expected <= 2 cached requests got %v", cachedReqs) } @@ -640,7 +646,7 @@ func TestGetSrvKeyspaceNames(t *testing.T) { t.Errorf("GetSrvKeyspaceNames got %v want %v", names, wantNames) } - errorReqs, ok := rs.counts.Counts()[errorCategory] + errorReqs, ok := counts.Counts()[errorCategory] if !ok || errorReqs == 0 { t.Errorf("expected non-zero error requests got %v", errorReqs) } @@ -684,8 +690,8 @@ func TestSrvKeyspaceWatcher(t *testing.T) { srvTopoCacheTTL = 1 * time.Second srvTopoCacheRefresh = 1 * time.Second }() - - rs := NewResilientServer(ctx, ts, "TestGetSrvKeyspaceWatcher") + counts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + rs := NewResilientServer(ctx, ts, counts) var wmu sync.Mutex var wseen []watched @@ -811,7 +817,8 @@ func TestSrvKeyspaceListener(t *testing.T) { srvTopoCacheRefresh = 1 * time.Second }() - rs := NewResilientServer(ctx, ts, "TestGetSrvKeyspaceListener") + counts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + rs := NewResilientServer(ctx, ts, counts) cancelCtx, cancelFunc := context.WithCancel(context.Background()) var callbackCount atomic.Int32 diff --git a/go/vt/srvtopo/resolver_test.go b/go/vt/srvtopo/resolver_test.go index 95e6dbe620c..fae8bef1fb2 100644 --- a/go/vt/srvtopo/resolver_test.go +++ b/go/vt/srvtopo/resolver_test.go @@ -23,6 +23,7 @@ import ( "github.com/stretchr/testify/require" "vitess.io/vitess/go/sqltypes" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/key" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/topo/memorytopo" @@ -33,10 +34,11 @@ import ( topodatapb "vitess.io/vitess/go/vt/proto/topodata" ) -func initResolver(t *testing.T, ctx context.Context, name string) *Resolver { +func initResolver(t *testing.T, ctx context.Context) *Resolver { cell := "cell1" ts := memorytopo.NewServer(ctx, cell) - rs := NewResilientServer(ctx, ts, name) + counts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + rs := NewResilientServer(ctx, ts, counts) // Create sharded keyspace and shards. if err := ts.CreateKeyspace(ctx, "sks", &topodatapb.Keyspace{}); err != nil { @@ -97,7 +99,7 @@ func initResolver(t *testing.T, ctx context.Context, name string) *Resolver { func TestResolveDestinations(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - resolver := initResolver(t, ctx, "TestResolveDestinations") + resolver := initResolver(t, ctx) id1 := &querypb.Value{ Type: sqltypes.VarChar, diff --git a/go/vt/vtadmin/api.go b/go/vt/vtadmin/api.go index dce4ff041c9..1e83875de35 100644 --- a/go/vt/vtadmin/api.go +++ b/go/vt/vtadmin/api.go @@ -32,6 +32,7 @@ import ( "github.com/gorilla/mux" "github.com/patrickmn/go-cache" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/sets" @@ -2384,7 +2385,8 @@ func (api *API) VTExplain(ctx context.Context, req *vtadminpb.VTExplainRequest) } ts := memorytopo.NewServer(ctx, vtexplain.Cell) - vte, err := vtexplain.Init(ctx, api.env, ts, srvVSchema, schema, shardMap, &vtexplain.Options{ReplicationMode: "ROW"}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + vte, err := vtexplain.Init(ctx, api.env, ts, srvVSchema, schema, shardMap, &vtexplain.Options{ReplicationMode: "ROW"}, srvTopoCounts) if err != nil { return nil, fmt.Errorf("error initilaizing vtexplain: %w", err) } diff --git a/go/vt/vtcombo/tablet_map.go b/go/vt/vtcombo/tablet_map.go index 369a8138b5a..25cc28d366d 100644 --- a/go/vt/vtcombo/tablet_map.go +++ b/go/vt/vtcombo/tablet_map.go @@ -24,6 +24,7 @@ import ( "time" "vitess.io/vitess/go/sqltypes" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/grpcclient" "vitess.io/vitess/go/vt/hook" @@ -84,6 +85,7 @@ func CreateTablet( tabletType topodatapb.TabletType, mysqld mysqlctl.MysqlDaemon, dbcfgs *dbconfigs.DBConfigs, + srvTopoCounts *stats.CountersWithSingleLabel, ) error { alias := &topodatapb.TabletAlias{ Cell: cell, @@ -91,7 +93,7 @@ func CreateTablet( } log.Infof("Creating %v tablet %v for %v/%v", tabletType, topoproto.TabletAliasString(alias), keyspace, shard) - controller := tabletserver.NewServer(ctx, env, topoproto.TabletAliasString(alias), ts, alias) + controller := tabletserver.NewServer(ctx, env, topoproto.TabletAliasString(alias), ts, alias, srvTopoCounts) initTabletType := tabletType if tabletType == topodatapb.TabletType_PRIMARY { initTabletType = topodatapb.TabletType_REPLICA @@ -173,6 +175,7 @@ func InitTabletMap( dbcfgs *dbconfigs.DBConfigs, schemaDir string, ensureDatabase bool, + srvTopoCounts *stats.CountersWithSingleLabel, ) (uint32, error) { tabletMap = make(map[uint32]*comboTablet) @@ -192,7 +195,7 @@ func InitTabletMap( var uid uint32 = 1 for _, kpb := range tpb.Keyspaces { var err error - uid, err = CreateKs(ctx, env, ts, tpb, mysqld, dbcfgs, schemaDir, kpb, ensureDatabase, uid, wr) + uid, err = CreateKs(ctx, env, ts, tpb, mysqld, dbcfgs, schemaDir, kpb, ensureDatabase, uid, wr, srvTopoCounts) if err != nil { return 0, err } @@ -293,6 +296,7 @@ func CreateKs( ensureDatabase bool, uid uint32, wr *wrangler.Wrangler, + srvTopoCounts *stats.CountersWithSingleLabel, ) (uint32, error) { keyspace := kpb.Name @@ -342,7 +346,7 @@ func CreateKs( replicas-- // create the primary - if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_PRIMARY, mysqld, dbcfgs.Clone()); err != nil { + if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_PRIMARY, mysqld, dbcfgs.Clone(), srvTopoCounts); err != nil { return 0, err } uid++ @@ -350,7 +354,7 @@ func CreateKs( for i := 0; i < replicas; i++ { // create a replica tablet - if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_REPLICA, mysqld, dbcfgs.Clone()); err != nil { + if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_REPLICA, mysqld, dbcfgs.Clone(), srvTopoCounts); err != nil { return 0, err } uid++ @@ -358,7 +362,7 @@ func CreateKs( for i := 0; i < rdonlys; i++ { // create a rdonly tablet - if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_RDONLY, mysqld, dbcfgs.Clone()); err != nil { + if err := CreateTablet(ctx, env, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_RDONLY, mysqld, dbcfgs.Clone(), srvTopoCounts); err != nil { return 0, err } uid++ diff --git a/go/vt/vtexplain/vtexplain.go b/go/vt/vtexplain/vtexplain.go index e16dea89d2b..64f1ca3cea1 100644 --- a/go/vt/vtexplain/vtexplain.go +++ b/go/vt/vtexplain/vtexplain.go @@ -28,6 +28,7 @@ import ( "github.com/spf13/pflag" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/vtenv" @@ -184,7 +185,7 @@ type TabletActions struct { } // Init sets up the fake execution environment -func Init(ctx context.Context, env *vtenv.Environment, ts *topo.Server, vSchemaStr, sqlSchema, ksShardMapStr string, opts *Options) (*VTExplain, error) { +func Init(ctx context.Context, env *vtenv.Environment, ts *topo.Server, vSchemaStr, sqlSchema, ksShardMapStr string, opts *Options, srvTopoCounts *stats.CountersWithSingleLabel) (*VTExplain, error) { // Verify options if opts.ReplicationMode != "ROW" && opts.ReplicationMode != "STATEMENT" { return nil, fmt.Errorf("invalid replication mode \"%s\"", opts.ReplicationMode) @@ -207,7 +208,7 @@ func Init(ctx context.Context, env *vtenv.Environment, ts *topo.Server, vSchemaS env: env, } vte.setGlobalTabletEnv(tabletEnv) - err = vte.initVtgateExecutor(ctx, ts, vSchemaStr, ksShardMapStr, opts) + err = vte.initVtgateExecutor(ctx, ts, vSchemaStr, ksShardMapStr, opts, srvTopoCounts) if err != nil { return nil, fmt.Errorf("initVtgateExecutor: %v", err.Error()) } diff --git a/go/vt/vtexplain/vtexplain_test.go b/go/vt/vtexplain/vtexplain_test.go index e7a6f4bdfc8..49bb94fedb1 100644 --- a/go/vt/vtexplain/vtexplain_test.go +++ b/go/vt/vtexplain/vtexplain_test.go @@ -28,6 +28,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/require" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/vtenv" @@ -68,7 +69,8 @@ func initTest(ctx context.Context, ts *topo.Server, mode string, opts *Options, } opts.ExecutionMode = mode - vte, err := Init(ctx, vtenv.NewTestEnv(), ts, string(vSchema), string(schema), shardmap, opts) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + vte, err := Init(ctx, vtenv.NewTestEnv(), ts, string(vSchema), string(schema), shardmap, opts, srvTopoCounts) require.NoError(t, err, "vtexplain Init error\n%s", string(schema)) return vte } @@ -349,7 +351,8 @@ func TestInit(t *testing.T) { }` schema := "create table table_missing_primary_vindex (id int primary key)" ts := memorytopo.NewServer(ctx, Cell) - _, err := Init(ctx, vtenv.NewTestEnv(), ts, vschema, schema, "", defaultTestOpts()) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + _, err := Init(ctx, vtenv.NewTestEnv(), ts, vschema, schema, "", defaultTestOpts(), srvTopoCounts) require.Error(t, err) require.Contains(t, err.Error(), "missing primary col vindex") } diff --git a/go/vt/vtexplain/vtexplain_vtgate.go b/go/vt/vtexplain/vtexplain_vtgate.go index a5d9677f02a..22939efde20 100644 --- a/go/vt/vtexplain/vtexplain_vtgate.go +++ b/go/vt/vtexplain/vtexplain_vtgate.go @@ -28,6 +28,7 @@ import ( "vitess.io/vitess/go/cache/theine" "vitess.io/vitess/go/json2" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/streamlog" "vitess.io/vitess/go/vt/discovery" "vitess.io/vitess/go/vt/key" @@ -47,14 +48,14 @@ import ( vtgatepb "vitess.io/vitess/go/vt/proto/vtgate" ) -func (vte *VTExplain) initVtgateExecutor(ctx context.Context, ts *topo.Server, vSchemaStr, ksShardMapStr string, opts *Options) error { +func (vte *VTExplain) initVtgateExecutor(ctx context.Context, ts *topo.Server, vSchemaStr, ksShardMapStr string, opts *Options, srvTopoCounts *stats.CountersWithSingleLabel) error { vte.explainTopo = &ExplainTopo{NumShards: opts.NumShards} vte.explainTopo.TopoServer = ts vte.healthCheck = discovery.NewFakeHealthCheck(nil) resolver := vte.newFakeResolver(ctx, opts, vte.explainTopo, Cell) - err := vte.buildTopology(ctx, ts, opts, vSchemaStr, ksShardMapStr, opts.NumShards) + err := vte.buildTopology(ctx, ts, opts, vSchemaStr, ksShardMapStr, opts.NumShards, srvTopoCounts) if err != nil { return err } @@ -92,7 +93,7 @@ func (vte *VTExplain) newFakeResolver(ctx context.Context, opts *Options, serv s return vtgate.NewResolver(srvResolver, serv, cell, sc) } -func (vte *VTExplain) buildTopology(ctx context.Context, ts *topo.Server, opts *Options, vschemaStr string, ksShardMapStr string, numShardsPerKeyspace int) error { +func (vte *VTExplain) buildTopology(ctx context.Context, ts *topo.Server, opts *Options, vschemaStr string, ksShardMapStr string, numShardsPerKeyspace int, srvTopoCounts *stats.CountersWithSingleLabel) error { vte.explainTopo.Lock.Lock() defer vte.explainTopo.Lock.Unlock() @@ -170,7 +171,7 @@ func (vte *VTExplain) buildTopology(ctx context.Context, ts *topo.Server, opts * log.Infof("registering test tablet %s for keyspace %s shard %s", hostname, ks, shard.Name) tablet := vte.healthCheck.AddFakeTablet(Cell, hostname, 1, ks, shard.Name, topodatapb.TabletType_PRIMARY, true, 1, nil, func(t *topodatapb.Tablet) queryservice.QueryService { - return vte.newTablet(ctx, vte.env, opts, t, ts) + return vte.newTablet(ctx, vte.env, opts, t, ts, srvTopoCounts) }) vte.explainTopo.TabletConns[hostname] = tablet.(*explainTablet) vte.explainTopo.KeyspaceShards[ks][shard.Name] = shard diff --git a/go/vt/vtexplain/vtexplain_vttablet.go b/go/vt/vtexplain/vtexplain_vttablet.go index b573fe29774..53e09445c17 100644 --- a/go/vt/vtexplain/vtexplain_vttablet.go +++ b/go/vt/vtexplain/vtexplain_vttablet.go @@ -23,6 +23,7 @@ import ( "strings" "sync" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/sidecardb" "vitess.io/vitess/go/vt/topo" "vitess.io/vitess/go/vt/vtenv" @@ -105,7 +106,7 @@ type explainTablet struct { var _ queryservice.QueryService = (*explainTablet)(nil) -func (vte *VTExplain) newTablet(ctx context.Context, env *vtenv.Environment, opts *Options, t *topodatapb.Tablet, ts *topo.Server) *explainTablet { +func (vte *VTExplain) newTablet(ctx context.Context, env *vtenv.Environment, opts *Options, t *topodatapb.Tablet, ts *topo.Server, srvTopoCounts *stats.CountersWithSingleLabel) *explainTablet { db := fakesqldb.New(nil) sidecardb.AddSchemaInitQueries(db, true, env.Parser()) @@ -120,7 +121,7 @@ func (vte *VTExplain) newTablet(ctx context.Context, env *vtenv.Environment, opt config.EnableTableGC = false // XXX much of this is cloned from the tabletserver tests - tsv := tabletserver.NewTabletServer(ctx, env, topoproto.TabletAliasString(t.Alias), config, ts, t.Alias) + tsv := tabletserver.NewTabletServer(ctx, env, topoproto.TabletAliasString(t.Alias), config, ts, t.Alias, srvTopoCounts) tablet := explainTablet{db: db, tsv: tsv, vte: vte, collationEnv: env.CollationEnv()} db.Handler = &tablet diff --git a/go/vt/vtexplain/vtexplain_vttablet_test.go b/go/vt/vtexplain/vtexplain_vttablet_test.go index a4350316d82..ddf24b90afa 100644 --- a/go/vt/vtexplain/vtexplain_vttablet_test.go +++ b/go/vt/vtexplain/vtexplain_vttablet_test.go @@ -26,6 +26,7 @@ import ( "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql/collations" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/topo/memorytopo" "vitess.io/vitess/go/vt/vtenv" @@ -76,7 +77,8 @@ create table t2 ( ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, Cell) - vte, err := Init(ctx, vtenv.NewTestEnv(), ts, testVSchema, testSchema, "", opts) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + vte, err := Init(ctx, vtenv.NewTestEnv(), ts, testVSchema, testSchema, "", opts, srvTopoCounts) require.NoError(t, err) defer vte.Stop() @@ -141,14 +143,14 @@ create table test_partitioned ( tabletEnv, _ := newTabletEnvironment(ddls, defaultTestOpts(), env.CollationEnv()) vte.setGlobalTabletEnv(tabletEnv) - + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") tablet := vte.newTablet(ctx, env, defaultTestOpts(), &topodatapb.Tablet{ Keyspace: "ks_sharded", Shard: "-80", Alias: &topodatapb.TabletAlias{ Cell: Cell, }, - }, ts) + }, ts, srvTopoCounts) time.Sleep(10 * time.Millisecond) se := tablet.tsv.SchemaEngine() diff --git a/go/vt/vttablet/endtoend/framework/server.go b/go/vt/vttablet/endtoend/framework/server.go index 0258dee9186..42f2994a22c 100644 --- a/go/vt/vttablet/endtoend/framework/server.go +++ b/go/vt/vttablet/endtoend/framework/server.go @@ -23,6 +23,7 @@ import ( "net/http" "time" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/servenv" "vitess.io/vitess/go/vt/topo" @@ -78,7 +79,8 @@ func StartCustomServer(ctx context.Context, connParams, connAppDebugParams mysql } TopoServer = memorytopo.NewServer(ctx, "") - Server = tabletserver.NewTabletServer(ctx, vtenv.NewTestEnv(), "", cfg, TopoServer, &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + Server = tabletserver.NewTabletServer(ctx, vtenv.NewTestEnv(), "", cfg, TopoServer, &topodatapb.TabletAlias{}, srvTopoCounts) Server.Register() err := Server.StartService(Target, dbcfgs, nil /* mysqld */) if err != nil { diff --git a/go/vt/vttablet/tabletserver/query_executor_test.go b/go/vt/vttablet/tabletserver/query_executor_test.go index 3466a55133d..afc42113522 100644 --- a/go/vt/vttablet/tabletserver/query_executor_test.go +++ b/go/vt/vttablet/tabletserver/query_executor_test.go @@ -28,6 +28,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/vtenv" @@ -1528,7 +1529,8 @@ func newTestTabletServer(ctx context.Context, flags executorFlags, db *fakesqldb } dbconfigs := newDBConfigs(db) cfg.DB = dbconfigs - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) target := &querypb.Target{TabletType: topodatapb.TabletType_PRIMARY} err := tsv.StartService(target, dbconfigs, nil /* mysqld */) if cfg.TwoPCEnable { diff --git a/go/vt/vttablet/tabletserver/tabletserver.go b/go/vt/vttablet/tabletserver/tabletserver.go index 6ecc46c68ab..dad637a6daf 100644 --- a/go/vt/vttablet/tabletserver/tabletserver.go +++ b/go/vt/vttablet/tabletserver/tabletserver.go @@ -28,7 +28,6 @@ import ( "sort" "strconv" "strings" - "sync" "sync/atomic" "syscall" "time" @@ -140,18 +139,13 @@ var _ queryservice.QueryService = (*TabletServer)(nil) var RegisterFunctions []func(Controller) // NewServer creates a new TabletServer based on the command line flags. -func NewServer(ctx context.Context, env *vtenv.Environment, name string, topoServer *topo.Server, alias *topodatapb.TabletAlias) *TabletServer { - return NewTabletServer(ctx, env, name, tabletenv.NewCurrentConfig(), topoServer, alias) +func NewServer(ctx context.Context, env *vtenv.Environment, name string, topoServer *topo.Server, alias *topodatapb.TabletAlias, srvTopoCounts *stats.CountersWithSingleLabel) *TabletServer { + return NewTabletServer(ctx, env, name, tabletenv.NewCurrentConfig(), topoServer, alias, srvTopoCounts) } -var ( - tsOnce sync.Once - srvTopoServer srvtopo.Server -) - // NewTabletServer creates an instance of TabletServer. Only the first // instance of TabletServer will expose its state variables. -func NewTabletServer(ctx context.Context, env *vtenv.Environment, name string, config *tabletenv.TabletConfig, topoServer *topo.Server, alias *topodatapb.TabletAlias) *TabletServer { +func NewTabletServer(ctx context.Context, env *vtenv.Environment, name string, config *tabletenv.TabletConfig, topoServer *topo.Server, alias *topodatapb.TabletAlias, srvTopoCounts *stats.CountersWithSingleLabel) *TabletServer { exporter := servenv.NewExporter(name, "Tablet") tsv := &TabletServer{ exporter: exporter, @@ -166,7 +160,7 @@ func NewTabletServer(ctx context.Context, env *vtenv.Environment, name string, c } tsv.QueryTimeout.Store(config.Oltp.QueryTimeout.Nanoseconds()) - tsOnce.Do(func() { srvTopoServer = srvtopo.NewResilientServer(ctx, topoServer, "TabletSrvTopo") }) + srvTopoServer := srvtopo.NewResilientServer(ctx, topoServer, srvTopoCounts) tabletTypeFunc := func() topodatapb.TabletType { if tsv.sm == nil || tsv.sm.Target() == nil { diff --git a/go/vt/vttablet/tabletserver/tabletserver_test.go b/go/vt/vttablet/tabletserver/tabletserver_test.go index 97777c0245f..9744b971946 100644 --- a/go/vt/vttablet/tabletserver/tabletserver_test.go +++ b/go/vt/vttablet/tabletserver/tabletserver_test.go @@ -32,6 +32,7 @@ import ( "vitess.io/vitess/go/mysql/config" "vitess.io/vitess/go/mysql/sqlerror" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/callerid" "vitess.io/vitess/go/vt/sidecardb" "vitess.io/vitess/go/vt/vtenv" @@ -1560,7 +1561,8 @@ func TestHandleExecUnknownError(t *testing.T) { defer cancel() logStats := tabletenv.NewLogStats(ctx, "TestHandleExecError") cfg := tabletenv.NewDefaultConfig() - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) defer tsv.handlePanicAndSendLogStats("select * from test_table", nil, logStats) panic("unknown exec error") } @@ -1730,7 +1732,8 @@ func TestHandleExecTabletError(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() cfg := tabletenv.NewDefaultConfig() - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError( @@ -1755,7 +1758,8 @@ func TestTerseErrors(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = true cfg.SanitizeLogMessages = false - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) tl := newTestLogger() defer tl.Close() @@ -1789,7 +1793,8 @@ func TestSanitizeLogMessages(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = false cfg.SanitizeLogMessages = true - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) tl := newTestLogger() defer tl.Close() @@ -1822,7 +1827,8 @@ func TestTerseErrorsNonSQLError(t *testing.T) { defer cancel() cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = true - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError( @@ -1847,7 +1853,8 @@ func TestSanitizeLogMessagesNonSQLError(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = false cfg.SanitizeLogMessages = true - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError( @@ -1872,7 +1879,8 @@ func TestSanitizeMessagesBindVars(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = true cfg.SanitizeLogMessages = true - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) tl := newTestLogger() defer tl.Close() @@ -1903,7 +1911,8 @@ func TestSanitizeMessagesNoBindVars(t *testing.T) { cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = true cfg.SanitizeLogMessages = true - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError(ctx, "", nil, vterrors.Errorf(vtrpcpb.Code_DEADLINE_EXCEEDED, "sensitive message"), nil) @@ -1921,7 +1930,8 @@ func TestTruncateErrorLen(t *testing.T) { defer cancel() cfg := tabletenv.NewDefaultConfig() cfg.TruncateErrorLen = 32 - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError( @@ -1952,7 +1962,8 @@ func TestTruncateMessages(t *testing.T) { TruncateErrLen: 52, }) require.NoError(t, err) - tsv := NewTabletServer(ctx, env, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, env, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) tl := newTestLogger() defer tl.Close() @@ -2006,7 +2017,8 @@ func TestTerseErrorsIgnoreFailoverInProgress(t *testing.T) { defer cancel() cfg := tabletenv.NewDefaultConfig() cfg.TerseErrors = true - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) tl := newTestLogger() defer tl.Close() err := tsv.convertAndLogError(ctx, "select * from test_table where id = :a", @@ -2048,7 +2060,8 @@ func TestACLHUP(t *testing.T) { defer cancel() tableacl.Register("simpleacl", &simpleacl.Factory{}) cfg := tabletenv.NewDefaultConfig() - tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, vtenv.NewTestEnv(), "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) f, err := os.CreateTemp("", "tableacl") require.NoError(t, err) @@ -2564,7 +2577,8 @@ func setupTabletServerTest(t testing.TB, ctx context.Context, keyspaceName strin func setupTabletServerTestCustom(t testing.TB, ctx context.Context, cfg *tabletenv.TabletConfig, keyspaceName string, env *vtenv.Environment) (*fakesqldb.DB, *TabletServer) { db := setupFakeDB(t) sidecardb.AddSchemaInitQueries(db, true, env.Parser()) - tsv := NewTabletServer(ctx, env, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}) + srvTopoCounts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + tsv := NewTabletServer(ctx, env, "TabletServerTest", cfg, memorytopo.NewServer(ctx, ""), &topodatapb.TabletAlias{}, srvTopoCounts) require.Equal(t, StateNotConnected, tsv.sm.State()) dbcfgs := newDBConfigs(db) target := &querypb.Target{ diff --git a/go/vt/vttablet/tabletserver/vstreamer/testenv/testenv.go b/go/vt/vttablet/tabletserver/vstreamer/testenv/testenv.go index 9c77ca18594..c09f73c0354 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/testenv/testenv.go +++ b/go/vt/vttablet/tabletserver/vstreamer/testenv/testenv.go @@ -20,12 +20,12 @@ package testenv import ( "context" "fmt" - "math/rand" "os" "regexp" "strings" "vitess.io/vitess/go/json2" + "vitess.io/vitess/go/stats" "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/mysqlctl" "vitess.io/vitess/go/vt/srvtopo" @@ -79,9 +79,8 @@ func Init(ctx context.Context) (*Env, error) { if err := te.TopoServ.CreateShard(ctx, te.KeyspaceName, te.ShardName); err != nil { panic(err) } - // Add a random suffix to metric name to avoid panic. Another option would have been to generate a random string. - suffix := rand.Int() - te.SrvTopo = srvtopo.NewResilientServer(ctx, te.TopoServ, "TestTopo"+fmt.Sprint(suffix)) + counts := stats.NewCountersWithSingleLabel("", "Resilient srvtopo server operations", "type") + te.SrvTopo = srvtopo.NewResilientServer(ctx, te.TopoServ, counts) cfg := vttest.Config{ Topology: &vttestpb.VTTestTopology{