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

server,cli: Support Init Command #16371

Merged
merged 3 commits into from
Jul 27, 2017
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
48 changes: 39 additions & 9 deletions pkg/acceptance/cluster/localcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,20 @@ func (l *LocalCluster) initCluster(ctx context.Context) {
maybePanic(c.Wait(ctx))
}

// cockroachEntryPoint returns the value to be used as
// container.Config.Entrypoint for a container running the cockroach
// binary under test.
// TODO(bdarnell): refactor this to minimize globals
func cockroachEntrypoint() []string {
var entrypoint []string
if *cockroachImage == defaultImage {
entrypoint = append(entrypoint, CockroachBinaryInContainer)
} else if *cockroachEntry != "" {
entrypoint = append(entrypoint, *cockroachEntry)
}
return entrypoint
}

// createRoach creates the docker container for a testNode. It may be called in
// parallel to start many nodes at once, and thus should remain threadsafe.
func (l *LocalCluster) createRoach(
Expand All @@ -441,12 +455,6 @@ func (l *LocalCluster) createRoach(
hostname = fmt.Sprintf("roach-%s-%d", l.clusterID, node.index)
}
log.Infof(ctx, "creating docker container with name: %s", hostname)
var entrypoint []string
if *cockroachImage == defaultImage {
entrypoint = append(entrypoint, CockroachBinaryInContainer)
} else if *cockroachEntry != "" {
entrypoint = append(entrypoint, *cockroachEntry)
}
var err error
node.Container, err = createContainer(
ctx,
Expand All @@ -458,7 +466,7 @@ func (l *LocalCluster) createRoach(
DefaultTCP: {},
defaultHTTP: {},
},
Entrypoint: entrypoint,
Entrypoint: cockroachEntrypoint(),
Env: env,
Cmd: cmd,
Labels: map[string]string{
Expand Down Expand Up @@ -514,8 +522,8 @@ func (l *LocalCluster) startNode(ctx context.Context, node *testNode) {
}
cmd = append(cmd, fmt.Sprintf("--store=%s", storeSpec))
}
// Append --join flag for all nodes except first.
if node.index > 0 {
// Append --join flag (for all nodes except first in bootstrap-node-zero mode)
if node.index > 0 || l.config.InitMode != INIT_BOOTSTRAP_NODE_ZERO {
cmd = append(cmd, "--join="+net.JoinHostPort(l.Nodes[0].nodeStr, base.DefaultPort))
}

Expand Down Expand Up @@ -552,6 +560,24 @@ func (l *LocalCluster) startNode(ctx context.Context, node *testNode) {
base.DefaultHTTPPort, cmd, l.CertsDir, httpAddr.IP, httpAddr.Port)
}

// RunInitCommand runs the `cockroach init` command. Normally called
// automatically, but exposed for tests that use INIT_NONE. nodeIdx
// may designate any node in the cluster as the target of the command.
func (l *LocalCluster) RunInitCommand(ctx context.Context, nodeIdx int) {
containerConfig := container.Config{
Image: *cockroachImage,
Entrypoint: cockroachEntrypoint(),
Cmd: []string{
"init",
"--certs-dir=/certs/",
"--host=" + l.Nodes[nodeIdx].nodeStr,
"--logtostderr",
},
}
maybePanic(l.OneShot(ctx, defaultImage, types.ImagePullOptions{},
containerConfig, container.HostConfig{}, "init-command"))
}

// returns false is the event
func (l *LocalCluster) processEvent(ctx context.Context, event events.Message) bool {
l.mu.Lock()
Expand Down Expand Up @@ -671,6 +697,10 @@ func (l *LocalCluster) Start(ctx context.Context) {
}(node)
}
wg.Wait()

if l.config.InitMode == INIT_COMMAND && len(l.Nodes) > 0 {
l.RunInitCommand(ctx, 0)
}
}

// Assert drains the Events channel and compares the actual events with those
Expand Down
115 changes: 95 additions & 20 deletions pkg/acceptance/cluster/testconfig.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion pkg/acceptance/cluster/testconfig.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ option go_package = "cluster";

import "gogoproto/gogo.proto";

// InitMode specifies different ways to initialize the cluster.
enum InitMode {
option (gogoproto.goproto_enum_prefix) = false;

// INIT_COMMAND starts every node with a join flag and issues the
// init command.
INIT_COMMAND = 0;

// INIT_BOOTSTRAP_NODE_ZERO uses the legacy protocol of omitting the
// join flag from node zero.
INIT_BOOTSTRAP_NODE_ZERO = 1;

// INIT_NONE starts every node with a join flag and leaves the
// cluster uninitialized.
INIT_NONE = 2;
}

// StoreConfig holds the configuration of a collection of similar stores.
message StoreConfig {
optional int32 count = 1 [(gogoproto.nullable) = false];
Expand All @@ -38,5 +55,7 @@ message TestConfig {
// Duration is the total time that the test should run for. Important for
// tests such as TestPut that will run indefinitely.
optional int64 duration = 3 [(gogoproto.nullable) = false, (gogoproto.casttype) = "time.Duration"];
// TODO(bram): #4559 once defined, add in a collection of chaos agents here.
optional InitMode init_mode = 4 [(gogoproto.nullable) = false];

// TODO(bram): #4559 once defined, add in a collection of chaos agents here.
}
Loading