Skip to content

Commit

Permalink
Merge pull request #16371 from adamgee/5974_revisit_cluster_init
Browse files Browse the repository at this point in the history
server,cli: Support Init Command
  • Loading branch information
bdarnell authored Jul 27, 2017
2 parents 136e445 + 639bdc0 commit 1a69b50
Show file tree
Hide file tree
Showing 16 changed files with 1,065 additions and 193 deletions.
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

0 comments on commit 1a69b50

Please sign in to comment.