Skip to content

Commit

Permalink
roachtest: add API to start virtual clusters
Browse files Browse the repository at this point in the history
Previously the cluster interface only exposed a method to start storage nodes,
but that is insufficient to start virtual clusters that have a separate method
on the `roachprod` API (for starting).

This change adds a new method `StartServiceForVirtualCluster` to the cluster
interface to enable roachtests to start virtual clusters. Some refactoring was
required to enable different sets of cluster settings, depending on what service
type is going to be started.

There are now two sets of cluster settings that can be utilised in
`test_runner`. For virtual clusters `virtualClusterSettings` will be used, and
for storage clusters `clusterSettings` will be utilised.

Epic: None
Release Note: None
  • Loading branch information
herkolategan authored and DarrylWong committed Jan 16, 2024
1 parent 86ad205 commit 7045c45
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 27 deletions.
103 changes: 76 additions & 27 deletions pkg/cmd/roachtest/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,11 @@ type clusterImpl struct {
expiration time.Time
encAtRest bool // use encryption at rest

// clusterSettings are additional cluster settings set on cluster startup.
// clusterSettings are additional cluster settings set on the storage cluster startup.
clusterSettings map[string]string
// virtualClusterSettings are additional cluster settings to set on the
// virtual cluster startup.
virtualClusterSettings map[string]string
// goCoverDir is the directory for Go coverage data (if coverage is enabled).
// BAZEL_COVER_DIR will be set to this value when starting a node.
goCoverDir string
Expand Down Expand Up @@ -1894,6 +1897,38 @@ func (c *clusterImpl) clearStatusForClusterOpt(worker bool) {
}
}

func (c *clusterImpl) configureClusterSettingOptions(
defaultClusterSettings install.ClusterSettingsOption, settings install.ClusterSettings,
) []install.ClusterSettingOption {
setUnlessExists := func(name string, value interface{}) {
if !envExists(settings.Env, name) {
settings.Env = append(settings.Env, fmt.Sprintf("%s=%s", name, fmt.Sprint(value)))
}
}
// Set the same seed on every node, to be used by builds with
// runtime assertions enabled.
setUnlessExists("COCKROACH_RANDOM_SEED", c.cockroachRandomSeed())

// Panic on span use-after-Finish, so we catch such bugs.
setUnlessExists("COCKROACH_CRASH_ON_SPAN_USE_AFTER_FINISH", true)

if c.goCoverDir != "" {
settings.Env = append(settings.Env, fmt.Sprintf("BAZEL_COVER_DIR=%s", c.goCoverDir))
}

return []install.ClusterSettingOption{
install.TagOption(settings.Tag),
install.PGUrlCertsDirOption(settings.PGUrlCertsDir),
install.SecureOption(settings.Secure),
install.UseTreeDistOption(settings.UseTreeDist),
install.EnvOption(settings.Env),
install.NumRacksOption(settings.NumRacks),
install.BinaryOption(settings.Binary),
defaultClusterSettings,
install.ClusterSettingsOption(settings.ClusterSettings),
}
}

// StartE starts cockroach nodes on a subset of the cluster. The nodes parameter
// can either be a specific node, empty (to indicate all nodes), or a pair of
// nodes indicating a range.
Expand All @@ -1912,17 +1947,6 @@ func (c *clusterImpl) StartE(

startOpts.RoachprodOpts.EncryptedStores = c.encAtRest

setUnlessExists := func(name string, value interface{}) {
if !envExists(settings.Env, name) {
settings.Env = append(settings.Env, fmt.Sprintf("%s=%s", name, fmt.Sprint(value)))
}
}
// Panic on span use-after-Finish, so we catch such bugs.
setUnlessExists("COCKROACH_CRASH_ON_SPAN_USE_AFTER_FINISH", true)
// Set the same seed on every node, to be used by builds with
// runtime assertions enabled.
setUnlessExists("COCKROACH_RANDOM_SEED", c.cockroachRandomSeed())

// Needed for backward-compat on crdb_internal.ranges{_no_leases}.
// Remove in v23.2.
if !envExists(settings.Env, "COCKROACH_FORCE_DEPRECATED_SHOW_RANGE_BEHAVIOR") {
Expand All @@ -1931,21 +1955,7 @@ func (c *clusterImpl) StartE(
settings.Env = append(settings.Env, "COCKROACH_FORCE_DEPRECATED_SHOW_RANGE_BEHAVIOR=false")
}

if c.goCoverDir != "" {
settings.Env = append(settings.Env, fmt.Sprintf("BAZEL_COVER_DIR=%s", c.goCoverDir))
}

clusterSettingsOpts := []install.ClusterSettingOption{
install.TagOption(settings.Tag),
install.PGUrlCertsDirOption(settings.PGUrlCertsDir),
install.SecureOption(settings.Secure),
install.UseTreeDistOption(settings.UseTreeDist),
install.EnvOption(settings.Env),
install.NumRacksOption(settings.NumRacks),
install.BinaryOption(settings.Binary),
install.ClusterSettingsOption(c.clusterSettings),
install.ClusterSettingsOption(settings.ClusterSettings),
}
clusterSettingsOpts := c.configureClusterSettingOptions(c.clusterSettings, settings)

if err := roachprod.Start(ctx, l, c.MakeNodes(opts...), startOpts.RoachprodOpts, clusterSettingsOpts...); err != nil {
return err
Expand All @@ -1961,6 +1971,45 @@ func (c *clusterImpl) StartE(
return nil
}

func (c *clusterImpl) StartServiceForVirtualClusterE(
ctx context.Context,
l *logger.Logger,
externalNodes option.NodeListOption,
startOpts option.StartOpts,
settings install.ClusterSettings,
opts ...option.Option,
) error {

c.setStatusForClusterOpt("starting virtual cluster", startOpts.RoachtestOpts.Worker, opts...)
defer c.clearStatusForClusterOpt(startOpts.RoachtestOpts.Worker)

clusterSettingsOpts := c.configureClusterSettingOptions(c.virtualClusterSettings, settings)

if err := roachprod.StartServiceForVirtualCluster(ctx, l, c.MakeNodes(externalNodes), c.MakeNodes(opts...), startOpts.RoachprodOpts, clusterSettingsOpts...); err != nil {
return err
}

if settings.Secure {
if err := c.RefetchCertsFromNode(ctx, 1); err != nil {
return err
}
}
return nil
}

func (c *clusterImpl) StartServiceForVirtualCluster(
ctx context.Context,
l *logger.Logger,
externalNodes option.NodeListOption,
startOpts option.StartOpts,
settings install.ClusterSettings,
opts ...option.Option,
) {
if err := c.StartServiceForVirtualClusterE(ctx, l, externalNodes, startOpts, settings, opts...); err != nil {
c.t.Fatal(err)
}
}

func (c *clusterImpl) RefetchCertsFromNode(ctx context.Context, node int) error {
var err error
c.localCertsDir, err = os.MkdirTemp("", "roachtest-certs")
Expand Down
5 changes: 5 additions & 0 deletions pkg/cmd/roachtest/cluster/cluster_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ type Cluster interface {
StopCockroachGracefullyOnNode(ctx context.Context, l *logger.Logger, node int) error
NewMonitor(context.Context, ...option.Option) Monitor

// Starting virtual clusters.

StartServiceForVirtualClusterE(ctx context.Context, l *logger.Logger, externalNodes option.NodeListOption, startOpts option.StartOpts, settings install.ClusterSettings, opts ...option.Option) error
StartServiceForVirtualCluster(ctx context.Context, l *logger.Logger, externalNodes option.NodeListOption, startOpts option.StartOpts, settings install.ClusterSettings, opts ...option.Option)

// Hostnames and IP addresses of the nodes.

InternalAddr(ctx context.Context, l *logger.Logger, node option.NodeListOption) ([]string, error)
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/roachtest/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ func (r *testRunner) runWorker(

// Set initial cluster settings for this test.
c.clusterSettings = map[string]string{}
c.virtualClusterSettings = map[string]string{}

switch testSpec.Leases {
case registry.DefaultLeases:
Expand Down
2 changes: 2 additions & 0 deletions pkg/roachprod/install/cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ const (
// StartRoutingProxy starts the SQL proxy process to route
// connections to multiple virtual clusters.
StartRoutingProxy
)

const (
// startSQLTimeout identifies the COCKROACH_CONNECT_TIMEOUT to use (in seconds)
// for sql cmds within syncedCluster.Start().
startSQLTimeout = 1200
Expand Down

0 comments on commit 7045c45

Please sign in to comment.