Skip to content

Commit e0f8caa

Browse files
committed
server: ensure tenant servers have annotated contexts
Release note: None
1 parent ca0df0f commit e0f8caa

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

pkg/server/server_sql.go

+7
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ import (
112112
// standalone SQLServer instances per tenant (the KV layer is shared across all
113113
// tenants).
114114
type SQLServer struct {
115+
ambientCtx log.AmbientContext
115116
stopper *stop.Stopper
116117
sqlIDContainer *base.SQLIDContainer
117118
pgServer *pgwire.Server
@@ -906,6 +907,7 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) {
906907
}
907908

908909
return &SQLServer{
910+
ambientCtx: cfg.BaseConfig.AmbientCtx,
909911
stopper: cfg.stopper,
910912
sqlIDContainer: cfg.nodeIDContainer,
911913
pgServer: pgServer,
@@ -1164,3 +1166,8 @@ func (s *SQLServer) SQLInstanceID() base.SQLInstanceID {
11641166
func (s *SQLServer) StartDiagnostics(ctx context.Context) {
11651167
s.diagnosticsReporter.PeriodicallyReportDiagnostics(ctx, s.stopper)
11661168
}
1169+
1170+
// AnnotateCtx annotates the given context with the server tracer and tags.
1171+
func (s *SQLServer) AnnotateCtx(ctx context.Context) context.Context {
1172+
return s.ambientCtx.AnnotateCtx(ctx)
1173+
}

pkg/server/tenant.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func StartTenant(
6767
return nil, "", "", err
6868
}
6969

70-
args, err := makeTenantSQLServerArgs(stopper, kvClusterName, baseCfg, sqlCfg)
70+
args, err := makeTenantSQLServerArgs(ctx, stopper, kvClusterName, baseCfg, sqlCfg)
7171
if err != nil {
7272
return nil, "", "", err
7373
}
@@ -96,7 +96,18 @@ func StartTenant(
9696
// TODO(davidh): Do we need to force this to be false?
9797
baseCfg.SplitListenSQL = false
9898

99-
background := baseCfg.AmbientCtx.AnnotateCtx(context.Background())
99+
// Add the server tags to the startup context.
100+
//
101+
// We use args.BaseConfig here instead of baseCfg directly because
102+
// makeTenantSQLArgs defines its own AmbientCtx instance and it's
103+
// defined by-value.
104+
ctx = args.BaseConfig.AmbientCtx.AnnotateCtx(ctx)
105+
106+
// Add the server tags to a generic background context for use
107+
// by async goroutines.
108+
// We can only annotate the context after makeTenantSQLServerArgs
109+
// has defined the instance ID container in the AmbientCtx.
110+
background := args.BaseConfig.AmbientCtx.AnnotateCtx(context.Background())
100111

101112
// StartListenRPCAndSQL will replace the SQLAddr fields if we choose
102113
// to share the SQL and gRPC port so here, since the tenant config
@@ -164,6 +175,16 @@ func StartTenant(
164175
args.sqlStatusServer = tenantStatusServer
165176
s, err := newSQLServer(ctx, args)
166177
tenantStatusServer.sqlServer = s
178+
// Also add the SQL instance tag to the tenant status server's
179+
// ambient context.
180+
//
181+
// We use the tag "sqli" instead of just "sql" because the latter is
182+
// too generic and would be hard to search if someone was looking at
183+
// a log message and wondering what it stands for.
184+
//
185+
// TODO(knz): find a way to share common logging tags between
186+
// multiple AmbientContext instances.
187+
tenantStatusServer.AmbientContext.AddLogTag("sqli", s.sqlIDContainer)
167188

168189
if err != nil {
169190
return nil, "", "", err
@@ -355,7 +376,11 @@ func loadVarsHandler(
355376
}
356377

357378
func makeTenantSQLServerArgs(
358-
stopper *stop.Stopper, kvClusterName string, baseCfg BaseConfig, sqlCfg SQLConfig,
379+
startupCtx context.Context,
380+
stopper *stop.Stopper,
381+
kvClusterName string,
382+
baseCfg BaseConfig,
383+
sqlCfg SQLConfig,
359384
) (sqlServerArgs, error) {
360385
st := baseCfg.Settings
361386

@@ -366,6 +391,7 @@ func makeTenantSQLServerArgs(
366391
// too generic and would be hard to search if someone was looking at
367392
// a log message and wondering what it stands for.
368393
baseCfg.AmbientCtx.AddLogTag("sqli", instanceIDContainer)
394+
startupCtx = baseCfg.AmbientCtx.AnnotateCtx(startupCtx)
369395

370396
// TODO(tbg): this is needed so that the RPC heartbeats between the testcluster
371397
// and this tenant work.

pkg/testutils/serverutils/test_tenant_shim.go

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package serverutils
1515

1616
import (
17+
"context"
18+
1719
"github.com/cockroachdb/cockroach/pkg/base"
1820
"github.com/cockroachdb/cockroach/pkg/rpc"
1921
)
@@ -56,4 +58,7 @@ type TestTenantInterface interface {
5658

5759
// RPCContext returns the *rpc.Context
5860
RPCContext() *rpc.Context
61+
62+
// AnnotateCtx annotates a context.
63+
AnnotateCtx(context.Context) context.Context
5964
}

0 commit comments

Comments
 (0)