Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
98466: cli,server: static configuration profiles r=stevendanna a=knz

Epic: CRDB-23559
Informs #98431. 
Fixes #94856.
(Based off #98459)
Supersedes #98380.

This change introduces a mechanism through which an operator can
select a "configuration profile" via the command-line flag
`--config-profile` or env var `COCKROACH_CONFIG_PROFILE`. The SQL
initialization defined by the profile is applied during server
start-up.

The profiles are (currently) hardcoded inside CockroachDB.

The following profiles are predefined:

- `default`: no configuration.

- `multitenant+noapp`: no pre-defined `application` tenant, but with a
  predefined application tenant template that is used whenever a new
  tenant is defined. This config profile is meant for use for C2C
  replication target clusters.

- `multitenant+app+sharedservice`: shared-process multitenancy with
  pre-defined `application` tenant, based off the same configuration as
  `multitenant+noapp`.

Release note: None

101907: multitenant: misc fixes related to tenant capabilities r=arulajmani a=knz

See individual commits for details.

The last commit in particular probably addresses #99087.

Epic: CRDB-23559

101935: sql: add issue number to todo r=rharding6373 a=rharding6373

Epic: none
Informs: #101934

Release note: none

Co-authored-by: Raphael 'kena' Poss <[email protected]>
Co-authored-by: rharding6373 <[email protected]>
  • Loading branch information
3 people committed Apr 20, 2023
4 parents 5a819ef + f3547ec + aa0e26f + 62b5bf9 commit 17331ef
Show file tree
Hide file tree
Showing 52 changed files with 1,359 additions and 171 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
/pkg/server/api_v2*.go @cockroachdb/obs-inf-prs @cockroachdb/server-prs
/pkg/server/api_v2_auth*.go @cockroachdb/obs-inf-prs @cockroachdb/server-prs @cockroachdb/prodsec
/pkg/server/authentication*.go @cockroachdb/server-prs @cockroachdb/prodsec
/pkg/server/autoconfig/ @cockroachdb/jobs-prs @cockroachdb/multi-tenant
/pkg/server/auto_tls_init*go @cockroachdb/server-prs @cockroachdb/prodsec
/pkg/server/clock_monotonicity.go @cockroachdb/kv-prs
/pkg/server/combined_statement_stats*.go @cockroachdb/cluster-observability @cockroachdb/obs-inf-prs
Expand Down Expand Up @@ -174,6 +175,7 @@
/pkg/server/testserver*.go @cockroachdb/test-eng @cockroachdb/server-prs
/pkg/server/tracedumper/ @cockroachdb/obs-inf-prs @cockroachdb/server-prs
/pkg/server/user*.go @cockroachdb/obs-inf-prs @cockroachdb/server-prs @cockroachdb/prodsec
/pkg/configprofiles/ @cockroachdb/multi-tenant @cockroachdb/server-prs


/pkg/ccl/jobsccl/ @cockroachdb/jobs-prs @cockroachdb/disaster-recovery
Expand Down
4 changes: 4 additions & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ ALL_TESTS = [
"//pkg/config/zonepb:zonepb_test",
"//pkg/config:config_disallowed_imports_test",
"//pkg/config:config_test",
"//pkg/configprofiles:configprofiles_test",
"//pkg/geo/geogen:geogen_test",
"//pkg/geo/geogfn:geogfn_test",
"//pkg/geo/geographiclib:geographiclib_test",
Expand Down Expand Up @@ -1119,6 +1120,8 @@ GO_TARGETS = [
"//pkg/config/zonepb:zonepb_test",
"//pkg/config:config",
"//pkg/config:config_test",
"//pkg/configprofiles:configprofiles",
"//pkg/configprofiles:configprofiles_test",
"//pkg/docs:docs",
"//pkg/featureflag:featureflag",
"//pkg/gen/genbzl:genbzl",
Expand Down Expand Up @@ -2644,6 +2647,7 @@ GET_X_DATA_TARGETS = [
"//pkg/compose/compare/compare:get_x_data",
"//pkg/config:get_x_data",
"//pkg/config/zonepb:get_x_data",
"//pkg/configprofiles:get_x_data",
"//pkg/docs:get_x_data",
"//pkg/featureflag:get_x_data",
"//pkg/gen/genbzl:get_x_data",
Expand Down
1 change: 1 addition & 0 deletions pkg/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ go_library(
"//pkg/cli/cliflags",
"//pkg/roachpb",
"//pkg/security/username",
"//pkg/server/autoconfig/acprovider",
"//pkg/settings/cluster",
"//pkg/util",
"//pkg/util/envutil",
Expand Down
5 changes: 5 additions & 0 deletions pkg/base/test_server_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/autoconfig/acprovider"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/retry"
Expand Down Expand Up @@ -168,6 +169,10 @@ type TestServerArgs struct {
// ObsServiceAddr is the address to which events will be exported over OTLP.
// If empty, exporting events is inhibited.
ObsServiceAddr string

// AutoConfigProvider provides auto-configuration tasks to apply on
// the cluster during server initialization.
AutoConfigProvider acprovider.Provider
}

// TestClusterArgs contains the parameters one can set when creating a test
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ go_library(
"//pkg/cloud/userfile",
"//pkg/clusterversion",
"//pkg/config",
"//pkg/configprofiles",
"//pkg/docs",
"//pkg/geo/geos",
"//pkg/gossip",
Expand All @@ -143,6 +144,7 @@ go_library(
"//pkg/security/securitytest",
"//pkg/security/username",
"//pkg/server",
"//pkg/server/autoconfig/acprovider",
"//pkg/server/pgurl",
"//pkg/server/profiler",
"//pkg/server/serverpb",
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,12 @@ Disable the creation of a default dataset in the demo shell.
This makes 'cockroach demo' faster to start.`,
}

ConfigProfile = FlagInfo{
Name: "config-profile",
EnvVar: "COCKROACH_CONFIG_PROFILE",
Description: `Select a configuration profile to apply.`,
}

GeoLibsDir = FlagInfo{
Name: "spatial-libs",
Description: `
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/security/clientsecopts"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/server/autoconfig/acprovider"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/storage"
Expand Down Expand Up @@ -639,6 +640,7 @@ func setDemoContextDefaults() {
demoCtx.Multitenant = true
demoCtx.DisableServerController = false
demoCtx.DefaultEnableRangefeeds = true
demoCtx.AutoConfigProvider = acprovider.NoTaskProvider{}

demoCtx.pidFile = ""
demoCtx.disableEnterpriseFeatures = false
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/democluster/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_library(
"//pkg/security/certnames",
"//pkg/security/username",
"//pkg/server",
"//pkg/server/autoconfig/acprovider",
"//pkg/server/pgurl",
"//pkg/server/serverpb",
"//pkg/server/status",
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/democluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/cli/clicfg"
"github.com/cockroachdb/cockroach/pkg/server/autoconfig/acprovider"
"github.com/cockroachdb/cockroach/pkg/workload"
)

Expand Down Expand Up @@ -110,6 +111,10 @@ type Context struct {
// DisableServerController is true if we want to avoid the server
// controller to instantiate tenant secondary servers.
DisableServerController bool

// AutoConfigProvider provides auto-configuration tasks to apply on
// the cluster during server initialization.
AutoConfigProvider acprovider.Provider
}

// IsInteractive returns true if the demo cluster configuration
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/democluster/demo_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ func (demoCtx *Context) testServerArgsForTransientCluster(
StoreSpecs: []base.StoreSpec{storeSpec},
SQLMemoryPoolSize: demoCtx.SQLPoolMemorySize,
CacheSize: demoCtx.CacheSize,
AutoConfigProvider: demoCtx.AutoConfigProvider,
NoAutoInitializeCluster: true,
EnableDemoLoginEndpoint: true,
// Demo clusters by default will create their own tenants, so we
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/cli/clienturl"
"github.com/cockroachdb/cockroach/pkg/cli/cliflagcfg"
"github.com/cockroachdb/cockroach/pkg/cli/cliflags"
"github.com/cockroachdb/cockroach/pkg/configprofiles"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/security/username"
Expand Down Expand Up @@ -393,6 +394,10 @@ func init() {
// planning?
if cmd != connectInitCmd && cmd != connectJoinCmd {
cliflagcfg.StringFlag(f, &serverCfg.Attrs, cliflags.Attrs)
// Cluster initialization. We only do this for a regular start command;
// SQL-only servers get their initialization payload from their tenant
// configuration.
cliflagcfg.VarFlag(f, configprofiles.NewProfileSetter(&serverCfg.AutoConfigProvider), cliflags.ConfigProfile)
}
}

Expand Down Expand Up @@ -828,6 +833,7 @@ func init() {
cliflagcfg.IntFlag(f, &demoCtx.HTTPPort, cliflags.DemoHTTPPort)
cliflagcfg.StringFlag(f, &demoCtx.ListeningURLFile, cliflags.ListeningURLFile)
cliflagcfg.StringFlag(f, &demoCtx.pidFile, cliflags.PIDFile)
cliflagcfg.VarFlag(f, configprofiles.NewProfileSetter(&demoCtx.AutoConfigProvider), cliflags.ConfigProfile)
}

{
Expand Down
54 changes: 54 additions & 0 deletions pkg/configprofiles/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
load("//build/bazelutil/unused_checker:unused.bzl", "get_x_data")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "configprofiles",
srcs = [
"doc.go",
"profiles.go",
"provider.go",
"setter.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/configprofiles",
visibility = ["//visibility:public"],
deps = [
"//pkg/clusterversion",
"//pkg/server/autoconfig/acprovider",
"//pkg/server/autoconfig/autoconfigpb",
"//pkg/util/log",
"//pkg/util/syncutil",
"@com_github_cockroachdb_errors//:errors",
"@com_github_spf13_pflag//:pflag",
],
)

go_test(
name = "configprofiles_test",
srcs = [
"datadriven_test.go",
"main_test.go",
"profiles_test.go",
],
args = ["-test.timeout=295s"],
data = glob(["testdata/**"]) + ["//c-deps:libgeos"],
embed = [":configprofiles"],
deps = [
"//pkg/base",
"//pkg/build",
"//pkg/ccl",
"//pkg/security/securityassets",
"//pkg/security/securitytest",
"//pkg/server",
"//pkg/server/autoconfig/acprovider",
"//pkg/server/autoconfig/autoconfigpb",
"//pkg/testutils",
"//pkg/testutils/serverutils",
"//pkg/testutils/sqlutils",
"//pkg/testutils/testcluster",
"//pkg/util/leaktest",
"//pkg/util/log",
"@com_github_cockroachdb_datadriven//:datadriven",
],
)

get_x_data(name = "get_x_data")
130 changes: 130 additions & 0 deletions pkg/configprofiles/datadriven_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// 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 configprofiles_test

import (
"context"
"fmt"
"strings"
"testing"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/configprofiles"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/server/autoconfig/acprovider"
"github.com/cockroachdb/cockroach/pkg/testutils"
"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/datadriven"
)

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

ctx := context.Background()

datadriven.Walk(t, "testdata", func(t *testing.T, path string) {
var alreadyStarted bool
var provider acprovider.Provider
var s serverutils.TestServerInterface
var db *sqlutils.SQLRunner
defer func() {
if s == nil {
return
}
s.Stopper().Stop(ctx)
}()

datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string {
switch d.Cmd {
case "profile":
if alreadyStarted {
t.Fatalf("%s: cannot use profile more than once", d.Pos)
}
setter := configprofiles.NewProfileSetter(&provider)
if err := setter.Set(d.Input); err != nil {
t.Fatalf("%s: %v", d.Pos, err)
}
var res strings.Builder
fmt.Fprintf(&res, "canonical profile name: %s\n", setter.String())

numExpectedTasks := len(configprofiles.TestingGetProfiles()[setter.String()])

s, _, _ = serverutils.StartServer(t, base.TestServerArgs{
AutoConfigProvider: provider,
// This test does not exercise security parameters, so we
// keep the configuration simpler to keep the test code also
// simple.
Insecure: true,
// The test controls secondary tenants manually.
DefaultTestTenant: base.TestTenantDisabled,
})
// We need to force the connection to the system tenant,
// because at least one of the config profiles changes the
// default tenant.
sysTenantDB := serverutils.OpenDBConn(t, s.SQLAddr(), "cluster:system/defaultdb",
true /* insecure */, s.Stopper())
db = sqlutils.MakeSQLRunner(sysTenantDB)
res.WriteString("server started\n")

testutils.SucceedsSoon(t, func() error {
var numTasksCompleted int
db.QueryRow(t, `SELECT count(*)
FROM [SHOW AUTOMATIC JOBS]
WHERE job_type = 'AUTO CONFIG TASK'
AND status = 'succeeded'`).Scan(&numTasksCompleted)
if numTasksCompleted < numExpectedTasks {
return fmt.Errorf("expected %d tasks to be completed, got %d", numExpectedTasks, numTasksCompleted)
}
return nil
})

alreadyStarted = true

return res.String()

case "system-sql":
if !alreadyStarted {
t.Fatalf("%s: must use profile before sql", d.Pos)
}
var res strings.Builder
rows := db.QueryStr(t, d.Input)
if len(rows) == 0 {
res.WriteString("<no rows>\n")
} else {
for _, row := range rows {
res.WriteString(strings.Join(row, " "))
res.WriteString("\n")
}
}
return res.String()

case "connect-tenant":
if !alreadyStarted {
t.Fatalf("%s: must use profile before sql", d.Pos)
}
sqlAddr := s.(*server.TestServer).SQLAddr()
testutils.SucceedsSoon(t, func() error {
goDB := serverutils.OpenDBConn(t, sqlAddr, "cluster:"+d.Input+"/defaultdb", true /* insecure */, s.Stopper())
return goDB.Ping()
})
return "ok"

default:
t.Fatalf("unknown command: %s", d.Cmd)
}
return ""
})
})
}
16 changes: 16 additions & 0 deletions pkg/configprofiles/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 configprofiles contain static configuration profiles embedded
// inside the CockroachDB binary.
//
// Configuration profiles are ways to initialize CockroachDB clusters
// differently (upon cluster creation) depending on purpose.
package configprofiles
Loading

0 comments on commit 17331ef

Please sign in to comment.