Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-20.2: server: add diagnostics support for tenants #58517

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pkg/base/test_server_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,10 @@ type TestTenantArgs struct {
// cluster settings.
AllowSettingClusterSettings bool

// TenantIDCodecOverride overrides the tenant ID used to construct the SQL
// server's codec, but nothing else (e.g. its certs). Used for testing.
TenantIDCodecOverride roachpb.TenantID

// Stopper, if not nil, is used to stop the tenant manually otherwise the
// TestServer stopper will be used.
Stopper *stop.Stopper

// TestingKnobs for the test server.
TestingKnobs TestingKnobs
}
22 changes: 11 additions & 11 deletions pkg/ccl/backupccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3465,7 +3465,7 @@ func TestBackupTenantsWithRevisionHistory(t *testing.T) {
ctx, tc, sqlDB, _, cleanupFn := BackupRestoreTestSetup(t, singleNode, numAccounts, InitNone)
defer cleanupFn()

_, _, err := tc.Servers[0].StartTenant(base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10)})
_, err := tc.Servers[0].StartTenant(base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10)})
require.NoError(t, err)

const msg = "can not backup tenants with revision history"
Expand Down Expand Up @@ -5819,17 +5819,17 @@ func TestBackupRestoreTenant(t *testing.T) {
_ = security.EmbeddedTenantIDs()

// Setup a few tenants, each with a different table.
conn10 := serverutils.StartTenant(t, srv, base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10)})
_, conn10 := serverutils.StartTenant(t, srv, base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10)})
defer conn10.Close()
tenant10 := sqlutils.MakeSQLRunner(conn10)
tenant10.Exec(t, `CREATE DATABASE foo; CREATE TABLE foo.bar(i int primary key); INSERT INTO foo.bar VALUES (110), (210)`)

conn11 := serverutils.StartTenant(t, srv, base.TestTenantArgs{TenantID: roachpb.MakeTenantID(11)})
_, 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)`)

conn20 := serverutils.StartTenant(t, srv, base.TestTenantArgs{TenantID: roachpb.MakeTenantID(20)})
_, conn20 := serverutils.StartTenant(t, srv, base.TestTenantArgs{TenantID: roachpb.MakeTenantID(20)})
defer conn20.Close()
tenant20 := sqlutils.MakeSQLRunner(conn20)
tenant20.Exec(t, `CREATE DATABASE foo; CREATE TABLE foo.qux(i int primary key); INSERT INTO foo.qux VALUES (120), (220)`)
Expand Down Expand Up @@ -5881,7 +5881,7 @@ func TestBackupRestoreTenant(t *testing.T) {
)

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

restoreConn10 = serverutils.StartTenant(
_, restoreConn10 = serverutils.StartTenant(
t, restoreTC.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10), Existing: true},
)
defer restoreConn10.Close()
Expand All @@ -5938,7 +5938,7 @@ func TestBackupRestoreTenant(t *testing.T) {
[][]string{{`10`, `true`, `{"id": "10", "state": "ACTIVE"}`}},
)

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

restoreConn10 := serverutils.StartTenant(
_, restoreConn10 := serverutils.StartTenant(
t, restoreTC.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10), Existing: true},
)
defer restoreConn10.Close()
Expand All @@ -5976,7 +5976,7 @@ 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`))

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

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

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

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

restoreConn20 := serverutils.StartTenant(
_, restoreConn20 := serverutils.StartTenant(
t, restoreTC.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(20), Existing: true},
)
defer restoreConn20.Close()
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/backupccl/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func TestShowBackupTenants(t *testing.T) {
// NB: tenant certs for 10, 11, 20 are embedded. See:
_ = security.EmbeddedTenantIDs()

conn10 := serverutils.StartTenant(t, srv, base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10)})
_, conn10 := serverutils.StartTenant(t, srv, base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10)})
defer conn10.Close()
tenant10 := sqlutils.MakeSQLRunner(conn10)
tenant10.Exec(t, `CREATE DATABASE foo; CREATE TABLE foo.bar(i int primary key); INSERT INTO foo.bar VALUES (110), (210)`)
Expand Down
21 changes: 13 additions & 8 deletions pkg/ccl/serverccl/server_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestSQLServer(t *testing.T) {
tc := serverutils.StartNewTestCluster(t, 3, base.TestClusterArgs{})
defer tc.Stopper().Stop(ctx)

db := serverutils.StartTenant(
_, db := serverutils.StartTenant(
t,
tc.Server(0),
base.TestTenantArgs{TenantID: roachpb.MakeTenantID(security.EmbeddedTenantIDs()[0])},
Expand All @@ -72,7 +73,7 @@ func TestTenantCannotSetClusterSetting(t *testing.T) {
defer tc.Stopper().Stop(ctx)

// StartTenant with the default permissions to
db := serverutils.StartTenant(t, tc.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10), AllowSettingClusterSettings: false})
_, db := serverutils.StartTenant(t, tc.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10), AllowSettingClusterSettings: false})
defer db.Close()
_, err := db.Exec(`SET CLUSTER SETTING sql.defaults.vectorize=off`)
var pqErr *pq.Error
Expand All @@ -89,10 +90,14 @@ func TestTenantUnauthenticatedAccess(t *testing.T) {
tc := serverutils.StartNewTestCluster(t, 1, base.TestClusterArgs{})
defer tc.Stopper().Stop(ctx)

_, _, err := tc.Server(0).StartTenant(base.TestTenantArgs{
_, err := tc.Server(0).StartTenant(base.TestTenantArgs{
TenantID: roachpb.MakeTenantID(security.EmbeddedTenantIDs()[0]),
// Configure the SQL server to access the wrong tenant keyspace.
TenantIDCodecOverride: roachpb.MakeTenantID(security.EmbeddedTenantIDs()[1]),
TestingKnobs: base.TestingKnobs{
TenantTestingKnobs: &sql.TenantTestingKnobs{
// Configure the SQL server to access the wrong tenant keyspace.
TenantIDCodecOverride: roachpb.MakeTenantID(security.EmbeddedTenantIDs()[1]),
},
},
})
require.Error(t, err)
require.Regexp(t, `Unauthenticated desc = requested key /Tenant/11/System/"system-version/" not fully contained in tenant keyspace /Tenant/1{0-1}`, err)
Expand All @@ -107,12 +112,12 @@ func TestTenantHTTP(t *testing.T) {
tc := serverutils.StartNewTestCluster(t, 1, base.TestClusterArgs{})
defer tc.Stopper().Stop(ctx)

_, httpAddr, err := tc.Server(0).StartTenant(base.TestTenantArgs{
tenant, err := tc.Server(0).StartTenant(base.TestTenantArgs{
TenantID: roachpb.MakeTenantID(security.EmbeddedTenantIDs()[0]),
})
require.NoError(t, err)
t.Run("prometheus", func(t *testing.T) {
resp, err := httputil.Get(ctx, "http://"+httpAddr+"/_status/vars")
resp, err := httputil.Get(ctx, "http://"+tenant.HTTPAddr()+"/_status/vars")
defer http.DefaultClient.CloseIdleConnections()
require.NoError(t, err)
defer resp.Body.Close()
Expand All @@ -121,7 +126,7 @@ func TestTenantHTTP(t *testing.T) {
require.Contains(t, string(body), "sql_ddl_started_count_internal")
})
t.Run("pprof", func(t *testing.T) {
resp, err := httputil.Get(ctx, "http://"+httpAddr+"/debug/pprof/goroutine?debug=2")
resp, err := httputil.Get(ctx, "http://"+tenant.HTTPAddr()+"/debug/pprof/goroutine?debug=2")
defer http.DefaultClient.CloseIdleConnections()
require.NoError(t, err)
defer resp.Body.Close()
Expand Down
11 changes: 10 additions & 1 deletion pkg/cli/mt_start_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -97,7 +98,7 @@ func runStartSQL(cmd *cobra.Command, args []string) error {
tempStorageMaxSizeBytes,
)

addr, httpAddr, err := server.StartTenant(
sqlServer, addr, httpAddr, err := server.StartTenant(
ctx,
stopper,
clusterName,
Expand All @@ -107,6 +108,14 @@ func runStartSQL(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

// Start up the diagnostics reporting loop.
// We don't do this in (*server.SQLServer).start() because we don't
// want this overhead and possible interference in tests.
if !cluster.TelemetryOptOut() {
sqlServer.StartDiagnostics(ctx)
}

log.Infof(ctx, "SQL server for tenant %s listening at %s, http at %s", serverCfg.SQLConfig.TenantID, addr, httpAddr)

// TODO(tbg): make the other goodies in `./cockroach start` reusable, such as
Expand Down
6 changes: 0 additions & 6 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,6 @@ type SQLConfig struct {
//
// Only applies when the SQL server is deployed individually.
TenantKVAddrs []string

// TenantIDCodecOverride overrides the tenant ID used to construct the SQL
// server's codec, but nothing else (e.g. its certs). Used for testing.
//
// Only applies when the SQL server is deployed individually.
TenantIDCodecOverride roachpb.TenantID
}

// MakeSQLConfig returns a SQLConfig with default values.
Expand Down
27 changes: 27 additions & 0 deletions pkg/server/diagnostics/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package diagnostics_test

import (
"os"
"testing"

"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/security/securitytest"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
)

func TestMain(m *testing.M) {
security.SetAssetLoader(securitytest.EmbeddedAssets)
serverutils.InitTestServerFactory(server.TestServerFactory)
os.Exit(m.Run())
}
Loading