Skip to content

Commit

Permalink
sql: show the virtual cluster name via a session var
Browse files Browse the repository at this point in the history
Release note (cluster virtualization): The name of the virtual cluster
that the SQL client is connected to can now be inspected via the SQL
session var `virtual_cluster_name`.
  • Loading branch information
knz committed Oct 2, 2023
1 parent f6f355b commit 779f59c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,7 @@ session_var ::=
| 'LC_COLLATE'
| 'LC_CTYPE'
| 'TIME' 'ZONE'
| 'VIRTUAL_CLUSTER_NAME'

var_name ::=
name
Expand Down
6 changes: 6 additions & 0 deletions pkg/server/server_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/scheduledlogging"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scdeps"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
Expand Down Expand Up @@ -1040,6 +1041,10 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) {
AutoConfigProvider: cfg.AutoConfigProvider,
}

if codec.ForSystemTenant() {
execCfg.VirtualClusterName = catconstants.SystemTenantName
}

if sqlSchemaChangerTestingKnobs := cfg.TestingKnobs.SQLSchemaChanger; sqlSchemaChangerTestingKnobs != nil {
execCfg.SchemaChangerTestingKnobs = sqlSchemaChangerTestingKnobs.(*sql.SchemaChangerTestingKnobs)
} else {
Expand Down Expand Up @@ -1438,6 +1443,7 @@ func (s *SQLServer) preStart(
// from KV (or elsewhere).
if entry, _ := s.tenantConnect.TenantInfo(); entry.Name != "" {
s.cfg.idProvider.SetTenantName(entry.Name)
s.execCfg.VirtualClusterName = entry.Name
}
if err := s.startCheckService(ctx, stopper); err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,10 @@ type ExecutorConfig struct {
// AutoConfigProvider informs the auto config runner job of new
// tasks to run.
AutoConfigProvider acprovider.Provider

// VirtualClusterName contains the name of the virtual cluster
// (tenant).
VirtualClusterName roachpb.TenantName
}

// UpdateVersionSystemSettingHook provides a callback that allows us
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -7486,6 +7486,7 @@ session_var:
// TIME ZONE is special: it is two tokens, but is really the identifier "TIME ZONE".
| TIME ZONE { $$ = "timezone" }
| TIME error // SHOW HELP: SHOW SESSION
| VIRTUAL_CLUSTER_NAME

session_var_parts:
'.' IDENT
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ go_library(
"command_filters.go",
"data.go",
"explain_test_util.go",
"virtual_cluster_name.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/sql/tests",
visibility = ["//visibility:public"],
deps = [
"//pkg/base",
"//pkg/internal/sqlsmith",
"//pkg/kv",
"//pkg/kv/kvpb",
Expand All @@ -18,7 +20,10 @@ go_library(
"//pkg/sql/sem/tree",
"//pkg/storage",
"//pkg/testutils/serverutils",
"//pkg/testutils/sqlutils",
"//pkg/testutils/storageutils",
"//pkg/util/leaktest",
"//pkg/util/log",
"//pkg/util/syncutil",
"@com_github_cockroachdb_errors//:errors",
"@org_golang_x_text//cases",
Expand Down
51 changes: 51 additions & 0 deletions pkg/sql/tests/virtual_cluster_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2023 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 tests

import (
"context"
gosql "database/sql"
"testing"

"github.com/cockroachdb/cockroach/pkg/base"
"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"
)

func TestClusterName(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

ctx := context.Background()
s, db, _ := serverutils.StartServer(t, base.TestServerArgs{
DefaultTestTenant: base.TestControlsTenantsExplicitly,
})
defer s.Stopper().Stop(ctx)

checkName := func(t *testing.T, db *gosql.DB, expected string) {
sql := sqlutils.MakeSQLRunner(db)
sql.CheckQueryResults(t, `SHOW virtual_cluster_name`, [][]string{{expected}})
}

t.Run("system", func(t *testing.T) {
checkName(t, db, "system")
})

t.Run("virtual", func(t *testing.T) {
_, db2 := serverutils.StartTenant(t, s, base.TestTenantArgs{
TenantID: serverutils.TestTenantID(),
TenantName: "virtual",
})
checkName(t, db2, "virtual")
})
}
8 changes: 8 additions & 0 deletions pkg/sql/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,14 @@ var varGen = map[string]sessionVar{
},
},

// CockroachDB extension.
`virtual_cluster_name`: {
Hidden: true,
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return string(evalCtx.ExecCfg.VirtualClusterName), nil
},
},

// CockroachDB extension.
`allow_prepare_as_opt_plan`: {
Hidden: true,
Expand Down

0 comments on commit 779f59c

Please sign in to comment.