Skip to content

Commit

Permalink
log,server: avoid global variables to log/trace server IDs
Browse files Browse the repository at this point in the history
Prior to this change, the server identifiers (cluster ID, node ID etc)
were stored in global variables in the `log` package.
This was problematic when a single process contains multiple servers,
e.g. in tests, `demo` and multi-tenant CockroachDB.

This change switches the mechanism to use identifiers stored in the
go context. The disadvantage is that the server IDs are not any
more logged at the beginning of each log file (since a given log file
could report data from multiple servers).

Release note (cli change): The server identifiers (cluster ID, node
ID, tenant ID, instance ID) are not any more duplicated at the start
of every new log file (during log file rotations). They are now only
logged when known during server start-up.
(The copy of the identifiers is still included in per-event envelopes
for the various `json` output logging formats.)
  • Loading branch information
knz committed Nov 30, 2021
1 parent 7097a90 commit 3b5fc23
Show file tree
Hide file tree
Showing 28 changed files with 86 additions and 193 deletions.
22 changes: 0 additions & 22 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6857,9 +6857,6 @@ func TestBackupRestoreInsideTenant(t *testing.T) {
defer log.Scope(t).Close(t)

makeTenant := func(srv serverutils.TestServerInterface, tenant uint64) (*sqlutils.SQLRunner, func()) {
// Prevent a logging assertion that the server ID is initialized multiple times.
log.TestingClearServerIdentifiers()

_, conn := serverutils.StartTenant(t, srv, base.TestTenantArgs{TenantID: roachpb.MakeTenantID(tenant)})
cleanup := func() { conn.Close() }
return sqlutils.MakeSQLRunner(conn), cleanup
Expand Down Expand Up @@ -7015,16 +7012,11 @@ func TestBackupRestoreTenant(t *testing.T) {
systemDB.Exec(t, `BACKUP system.users TO 'nodelocal://1/users'`)
systemDB.CheckQueryResults(t, `SELECT manifest->>'tenants' FROM [SHOW BACKUP 'nodelocal://1/users' WITH as_json]`, [][]string{{"[]"}})

// Prevent a logging assertion that the server ID is initialized multiple times.
log.TestingClearServerIdentifiers()

_, conn11 := serverutils.StartTenant(t, srv, base.TestTenantArgs{TenantID: roachpb.MakeTenantID(11)})
defer conn11.Close()
tenant11 := sqlutils.MakeSQLRunner(conn11)
tenant11.Exec(t, `CREATE DATABASE foo; CREATE TABLE foo.baz(i int primary key); INSERT INTO foo.baz VALUES (111), (211)`)

log.TestingClearServerIdentifiers()

_, conn20 := serverutils.StartTenant(t, srv, base.TestTenantArgs{TenantID: roachpb.MakeTenantID(20)})
defer conn20.Close()
tenant20 := sqlutils.MakeSQLRunner(conn20)
Expand Down Expand Up @@ -7084,8 +7076,6 @@ func TestBackupRestoreTenant(t *testing.T) {
[][]string{{`100`, `0`, `0`, `0`}},
)

log.TestingClearServerIdentifiers()

ten10Stopper := stop.NewStopper()
_, restoreConn10 := serverutils.StartTenant(
t, restoreTC.Server(0), base.TestTenantArgs{
Expand Down Expand Up @@ -7130,8 +7120,6 @@ func TestBackupRestoreTenant(t *testing.T) {
[][]string{{`10`, `true`, `{"id": "10", "state": "ACTIVE"}`}},
)

log.TestingClearServerIdentifiers()

_, restoreConn10 = serverutils.StartTenant(
t, restoreTC.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10), Existing: true},
)
Expand Down Expand Up @@ -7171,8 +7159,6 @@ func TestBackupRestoreTenant(t *testing.T) {
[][]string{{`10`, `true`, `{"id": "10", "state": "ACTIVE"}`}},
)

log.TestingClearServerIdentifiers()

_, restoreConn10 := serverutils.StartTenant(
t, restoreTC.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10), Existing: true},
)
Expand Down Expand Up @@ -7202,8 +7188,6 @@ func TestBackupRestoreTenant(t *testing.T) {
},
)

log.TestingClearServerIdentifiers()

_, restoreConn10 := serverutils.StartTenant(
t, restoreTC.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10), Existing: true},
)
Expand All @@ -7213,8 +7197,6 @@ func TestBackupRestoreTenant(t *testing.T) {
restoreTenant10.CheckQueryResults(t, `select * from foo.bar`, tenant10.QueryStr(t, `select * from foo.bar`))
restoreTenant10.CheckQueryResults(t, `select * from foo.bar2`, tenant10.QueryStr(t, `select * from foo.bar2`))

log.TestingClearServerIdentifiers()

_, restoreConn11 := serverutils.StartTenant(
t, restoreTC.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(11), Existing: true},
)
Expand All @@ -7233,8 +7215,6 @@ func TestBackupRestoreTenant(t *testing.T) {

restoreDB.Exec(t, `RESTORE TENANT 10 FROM 'nodelocal://1/t10' AS OF SYSTEM TIME `+ts1)

log.TestingClearServerIdentifiers()

_, restoreConn10 := serverutils.StartTenant(
t, restoreTC.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10), Existing: true},
)
Expand All @@ -7253,8 +7233,6 @@ func TestBackupRestoreTenant(t *testing.T) {

restoreDB.Exec(t, `RESTORE TENANT 20 FROM 'nodelocal://1/t20'`)

log.TestingClearServerIdentifiers()

_, restoreConn20 := serverutils.StartTenant(
t, restoreTC.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(20), Existing: true},
)
Expand Down
1 change: 0 additions & 1 deletion pkg/ccl/changefeedccl/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ func startTestCluster(t testing.TB) (serverutils.TestClusterInterface, *gosql.DB
func startTestTenant(
t testing.TB, options feedTestOptions,
) (serverutils.TestServerInterface, *gosql.DB, func()) {
log.TestingClearServerIdentifiers()
ctx := context.Background()

kvServer, _, cleanup := startTestFullServer(t, options)
Expand Down
3 changes: 0 additions & 3 deletions pkg/ccl/importccl/import_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6868,9 +6868,6 @@ func TestImportInTenant(t *testing.T) {
defer conn10.Close()
t10 := sqlutils.MakeSQLRunner(conn10)

// Prevent a logging assertion that the server ID is initialized multiple times.
log.TestingClearServerIdentifiers()

// Setup a few tenants, each with a different table.
_, conn11 := serverutils.StartTenant(t, tc.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(11)})
defer conn11.Close()
Expand Down
8 changes: 0 additions & 8 deletions pkg/ccl/kvccl/kvtenantccl/tenant_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -90,8 +89,6 @@ func TestTenantUpgrade(t *testing.T) {
TestingKnobs: base.TestingKnobs{},
Settings: settings,
}
// Prevent a logging assertion that the server ID is initialized multiple times.
log.TestingClearServerIdentifiers()
tenant, err := tc.Server(0).StartTenant(ctx, tenantArgs)
require.NoError(t, err)
return connectToTenant(t, tenant.SQLAddr())
Expand Down Expand Up @@ -130,7 +127,6 @@ func TestTenantUpgrade(t *testing.T) {
// Restart the tenant and ensure that the version is correct.
cleanup()
{
log.TestingClearServerIdentifiers()
tenantServer, err := tc.Server(0).StartTenant(ctx, base.TestTenantArgs{
TenantID: roachpb.MakeTenantID(initialTenantID),
Existing: true,
Expand All @@ -156,7 +152,6 @@ func TestTenantUpgrade(t *testing.T) {
// Restart the new tenant and ensure it has the right version.
cleanup()
{
log.TestingClearServerIdentifiers()
tenantServer, err := tc.Server(0).StartTenant(ctx, base.TestTenantArgs{
TenantID: roachpb.MakeTenantID(postUpgradeTenantID),
Existing: true,
Expand Down Expand Up @@ -286,8 +281,6 @@ func TestTenantUpgradeFailure(t *testing.T) {
},
Settings: settings,
}
// Prevent a logging assertion that the server ID is initialized multiple times.
log.TestingClearServerIdentifiers()
return &tenantInfo{tenantArgs: &tenantArgs,
v2onMigrationStopper: v2onMigrationStopper}
}
Expand Down Expand Up @@ -334,7 +327,6 @@ func TestTenantUpgradeFailure(t *testing.T) {
// Restart the tenant and ensure that the version is correct.
cleanup()
{
log.TestingClearServerIdentifiers()
tca, cleanup := startAndConnectToTenant(t, tenantInfo)
defer cleanup()
initialTenantRunner = sqlutils.MakeSQLRunner(tca)
Expand Down
1 change: 0 additions & 1 deletion pkg/ccl/serverccl/statusccl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ go_library(
"//pkg/testutils/serverutils",
"//pkg/testutils/sqlutils",
"//pkg/util/httputil",
"//pkg/util/log",
"//pkg/util/protoutil",
"@com_github_stretchr_testify//require",
],
Expand Down
2 changes: 0 additions & 2 deletions pkg/ccl/serverccl/statusccl/tenant_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func TestTenantGRPCServices(t *testing.T) {
sqlRunner.Exec(t, "CREATE TABLE test (id int)")
sqlRunner.Exec(t, "INSERT INTO test VALUES (1)")

log.TestingClearServerIdentifiers()
tenant2, connTenant2 := serverutils.StartTenant(t, server, base.TestTenantArgs{
TenantID: tenantID,
Existing: true,
Expand All @@ -110,7 +109,6 @@ func TestTenantGRPCServices(t *testing.T) {
require.Contains(t, string(body), "INSERT INTO test VALUES")
})

log.TestingClearServerIdentifiers()
tenant3, connTenant3 := serverutils.StartTenant(t, server, base.TestTenantArgs{
TenantID: roachpb.MakeTenantID(11),
TestingKnobs: testingKnobs,
Expand Down
2 changes: 0 additions & 2 deletions pkg/ccl/serverccl/statusccl/tenant_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/httputil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/stretchr/testify/require"
)
Expand All @@ -50,7 +49,6 @@ func newTestTenant(
tenantParams.Existing = existing
tenantParams.TestingKnobs = knobs

log.TestingClearServerIdentifiers()
tenant, tenantConn := serverutils.StartTenant(t, server, tenantParams)
sqlDB := sqlutils.MakeSQLRunner(tenantConn)
status := tenant.StatusServer().(serverpb.SQLStatusServer)
Expand Down
1 change: 0 additions & 1 deletion pkg/ccl/sqlproxyccl/proxy_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,6 @@ func newDirectoryServer(
return nil, status.Error(codes.NotFound, "tenant not found")
}

log.TestingClearServerIdentifiers()
tenantStopper := tenantdirsvr.NewSubStopper(tdsStopper)
ten, err := srv.StartTenant(ctx, base.TestTenantArgs{
Existing: true,
Expand Down
1 change: 0 additions & 1 deletion pkg/ccl/sqlproxyccl/tenant/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ func destroyTenant(tc serverutils.TestClusterInterface, id roachpb.TenantID) err
func startTenant(
ctx context.Context, srv serverutils.TestServerInterface, id uint64,
) (*tenantdirsvr.Process, error) {
log.TestingClearServerIdentifiers()
tenantStopper := tenantdirsvr.NewSubStopper(srv.Stopper())
t, err := srv.StartTenant(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ SET CLUSTER SETTING changefeed.experimental_poll_interval = '10ms'
`)
require.NoError(t, err)

// Prevent a logging assertion that the server ID is initialized multiple times.
log.TestingClearServerIdentifiers()

// Start the destination server.
hDest, cleanupDest := streamingtest.NewReplicationHelper(t, base.TestServerArgs{})
defer cleanupDest()
Expand Down
8 changes: 1 addition & 7 deletions pkg/ccl/testccl/sqlccl/temp_table_clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
)
Expand Down Expand Up @@ -100,7 +99,6 @@ func TestTenantTempTableCleanup(t *testing.T) {
},
},
)
log.TestingClearServerIdentifiers()
tenantStoppers := []*stop.Stopper{stop.NewStopper(), stop.NewStopper()}
_, tenantPrimaryDB := serverutils.StartTenant(t, tc.Server(0),
base.TestTenantArgs{
Expand All @@ -118,16 +116,14 @@ func TestTenantTempTableCleanup(t *testing.T) {
tenantSQL.Exec(t, "SET experimental_enable_temp_tables = 'on'")
tenantSQL.Exec(t, "set cluster setting sql.temp_object_cleaner.cleanup_interval='1 seconds'")
tenantSQL.Exec(t, "CREATE TEMP TABLE temp_table (x INT PRIMARY KEY, y INT);")
// Prevent a logging assertion that the server ID is initialized multiple times.
log.TestingClearServerIdentifiers()

_, tenantSecondDB := serverutils.StartTenant(t, tc.Server(1),
base.TestTenantArgs{
Existing: true,
TenantID: serverutils.TestTenantID(),
Settings: settings,
Stopper: tenantStoppers[1],
})
log.TestingClearServerIdentifiers()
tenantSecondSQL := sqlutils.MakeSQLRunner(tenantSecondDB)
tenantSecondSQL.CheckQueryResults(t, "SELECT table_name FROM [SHOW TABLES]",
[][]string{
Expand Down Expand Up @@ -160,8 +156,6 @@ func TestTenantTempTableCleanup(t *testing.T) {
// Enable our hook to allow the database to be
// brought up.
pause()
// Prevent a logging assertion that the server ID is initialized multiple times.
log.TestingClearServerIdentifiers()
// Once we restart the tenant, no sessions should exist
// so all temporary tables should be cleaned up.
tenantStoppers[0] = stop.NewStopper()
Expand Down
1 change: 0 additions & 1 deletion pkg/cli/democluster/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ go_library(
"//pkg/server/serverpb",
"//pkg/server/status",
"//pkg/settings",
"//pkg/sql",
"//pkg/sql/catalog/catalogkeys",
"//pkg/sql/catalog/catconstants",
"//pkg/sql/distsql",
Expand Down
2 changes: 0 additions & 2 deletions pkg/cli/democluster/demo_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/server/status"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/distsql"
Expand Down Expand Up @@ -406,7 +405,6 @@ func (c *transientCluster) Start(
StartingHTTPPort: c.demoCtx.HTTPPort - 2,
Locality: c.demoCtx.Localities[i],
TestingKnobs: base.TestingKnobs{
TenantTestingKnobs: &sql.TenantTestingKnobs{DisableLogTags: true},
Server: &server.TestingKnobs{
ContextTestingKnobs: rpc.ContextTestingKnobs{
ArtificialLatencyMap: latencyMap,
Expand Down
3 changes: 1 addition & 2 deletions pkg/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,8 @@ If problems persist, please see %s.`
buf.Printf("cluster name:\t%s\n", baseCfg.ClusterName)
}

// Remember the cluster ID for log file rotation.
// Report the server identifiers.
clusterID := s.ClusterID().String()
log.SetNodeIDs(clusterID, int32(nodeID))
buf.Printf("clusterID:\t%s\n", clusterID)
buf.Printf("nodeID:\t%d\n", nodeID)

Expand Down
11 changes: 0 additions & 11 deletions pkg/server/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,6 @@ func StartTenant(
// The InstanceID subsystem is not available until `preStart`.
args.rpcContext.NodeID.Set(ctx, roachpb.NodeID(s.SQLInstanceID()))

if knobs, ok := baseCfg.TestingKnobs.TenantTestingKnobs.(*sql.TenantTestingKnobs); !ok || !knobs.DisableLogTags {
// Register the server's identifiers so that log events are
// decorated with the server's identity. This helps when gathering
// log events from multiple servers into the same log collector.
//
// We do this only here, as the identifiers may not be known before this point.
clusterID := args.rpcContext.ClusterID.Get().String()
log.SetNodeIDs(clusterID, 0 /* nodeID is not known for a SQL-only server. */)
log.SetTenantIDs(args.TenantID.String(), int32(s.SQLInstanceID()))
}

externalUsageFn := func(ctx context.Context) multitenant.ExternalUsage {
userTimeMillis, _, err := status.GetCPUTime(ctx)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,10 +1344,6 @@ type TenantTestingKnobs struct {
// OverrideTokenBucketProvider allows a test-only TokenBucketProvider (which
// can optionally forward requests to the real provider).
OverrideTokenBucketProvider func(origProvider kvtenant.TokenBucketProvider) kvtenant.TokenBucketProvider

// DisableLogTags can be set to true to cause the tenant server to avoid
// setting any global log tags for cluster id or node id.
DisableLogTags bool
}

var _ base.ModuleTestingKnobs = &TenantTestingKnobs{}
Expand Down
3 changes: 0 additions & 3 deletions pkg/sql/logictest/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -1542,9 +1542,6 @@ func (t *logicTest) newCluster(serverArgs TestServerArgs, opts []clusterOpt) {
Existing: i > 0,
}

// Prevent a logging assertion that the server ID is initialized multiple times.
log.TestingClearServerIdentifiers()

tenant, err := t.cluster.Server(i).StartTenant(context.Background(), tenantArgs)
if err != nil {
t.rootT.Fatalf("%+v", err)
Expand Down
1 change: 0 additions & 1 deletion pkg/sql/sqltestutils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ go_library(
"//pkg/testutils/skip",
"//pkg/testutils/sqlutils",
"//pkg/util/cloudinfo",
"//pkg/util/log",
"//pkg/util/protoutil",
"//pkg/util/treeprinter",
"@com_github_cockroachdb_datadriven//:datadriven",
Expand Down
4 changes: 0 additions & 4 deletions pkg/sql/sqltestutils/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/cloudinfo"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/treeprinter"
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/errors"
Expand Down Expand Up @@ -156,9 +155,6 @@ func (tt *telemetryTest) Start(t *testing.T, serverArgs []base.TestServerArgs) {
tt.serverDB = tt.cluster.ServerConn(0)
tt.prepareCluster(tt.serverDB)

// Prevent a logging assertion that the server ID is initialized multiple times.
log.TestingClearServerIdentifiers()

tt.tenant, tt.tenantDB = serverutils.StartTenant(tt.t, tt.server, base.TestTenantArgs{
TenantID: serverutils.TestTenantID(),
AllowSettingClusterSettings: true,
Expand Down
1 change: 1 addition & 0 deletions pkg/util/log/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ go_library(
"log_flush.go",
"redact.go",
"registry.go",
"server_ident.go",
"sinks.go",
"stderr_redirect.go",
"stderr_redirect_unix.go",
Expand Down
Loading

0 comments on commit 3b5fc23

Please sign in to comment.