Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
39298: cli/demo: pre-load 'movr' by default r=knz a=knz

Requested/suggested by @kenliu.
cc @piyush-singh 

Release note (cli change): `cockroach demo` without argument becomes
equivalent to `cockroach demo movr`. The previous behavior
(no pre-defined dataset) is still available via `cockroach demo
--empty`.

Co-authored-by: Raphael 'kena' Poss <[email protected]>
  • Loading branch information
craig[bot] and knz committed Aug 4, 2019
2 parents 836c28d + 6091118 commit 3b9a95b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
8 changes: 6 additions & 2 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ func Example_demo() {

testData := [][]string{
{`demo`, `-e`, `show database`},
{`demo`, `-e`, `show database`, `--empty`},
{`demo`, `-e`, `show application_name`},
{`demo`, `--format=table`, `-e`, `show database`},
{`demo`, `-e`, `select 1 as "1"`, `-e`, `select 3 as "3"`},
Expand All @@ -488,14 +489,17 @@ func Example_demo() {
// Output:
// demo -e show database
// database
// movr
// demo -e show database --empty
// database
// defaultdb
// demo -e show application_name
// application_name
// $ cockroach demo
// demo --format=table -e show database
// database
// +-----------+
// defaultdb
// +----------+
// movr
// (1 row)
// demo -e select 1 as "1" -e select 3 as "3"
// 1
Expand Down
7 changes: 7 additions & 0 deletions pkg/cli/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,13 @@ The line length where sqlfmt will try to wrap.`,
Description: `How many in-memory nodes to create for the demo.`,
}

UseEmptyDatabase = FlagInfo{
Name: "empty",
Description: `
Start with an empty database: avoid pre-loading a default dataset in
the demo shell.`,
}

LogDir = FlagInfo{
Name: "log-dir",
Description: `
Expand Down
6 changes: 5 additions & 1 deletion pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ func initCLIDefaults() {
networkBenchCtx.port = 8081
networkBenchCtx.addresses = []string{"localhost:8081"}

demoCtx.nodes = 1
demoCtx.useEmptyDatabase = false

initPreFlagsDefaults()

// Clear the "Changed" state of all the registered command-line flags.
Expand Down Expand Up @@ -339,5 +342,6 @@ var sqlfmtCtx struct {
// demoCtx captures the command-line parameters of the `demo` command.
// Defaults set by InitCLIDefaults() above.
var demoCtx struct {
nodes int
nodes int
useEmptyDatabase bool
}
34 changes: 30 additions & 4 deletions pkg/cli/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,30 @@ var demoCmd = &cobra.Command{
Start an in-memory, standalone, single-node CockroachDB instance, and open an
interactive SQL prompt to it. Various datasets are available to be preloaded as
subcommands: e.g. "cockroach demo startrek". See --help for a full list.
`,
By default, the 'movr' dataset is pre-loaded. You can also use --empty
to avoid pre-loading a dataset.`,
Example: ` cockroach demo`,
Args: cobra.NoArgs,
RunE: MaybeDecorateGRPCError(func(cmd *cobra.Command, _ []string) error {
return runDemo(cmd, nil /* gen */)
}),
}

const defaultGeneratorName = "movr"

var defaultGenerator workload.Generator

func init() {
for _, meta := range workload.Registered() {
gen := meta.New()

if meta.Name == defaultGeneratorName {
// Save the default for use in the top-level 'demo' command
// without argument.
defaultGenerator = gen
}

var genFlags *pflag.FlagSet
if f, ok := gen.(workload.Flagser); ok {
genFlags = f.Flags().FlagSet
Expand Down Expand Up @@ -168,6 +181,11 @@ func setupTransientServers(
}

func runDemo(cmd *cobra.Command, gen workload.Generator) error {
if gen == nil && !demoCtx.useEmptyDatabase {
// Use a default dataset unless prevented by --empty.
gen = defaultGenerator
}

connURL, adminURL, cleanup, err := setupTransientServers(cmd, gen)
defer cleanup()
if err != nil {
Expand All @@ -180,12 +198,20 @@ func runDemo(cmd *cobra.Command, gen workload.Generator) error {
fmt.Printf(`#
# Welcome to the CockroachDB demo database!
#
# You are connected to a temporary, in-memory CockroachDB
# cluster of %d node%s. Your changes will not be saved!
# You are connected to a temporary, in-memory CockroachDB cluster of %d node%s.
`, demoCtx.nodes, util.Pluralize(int64(demoCtx.nodes)))

if gen != nil {
fmt.Printf("# The cluster has been preloaded with the %q dataset\n# (%s).\n",
gen.Meta().Name, gen.Meta().Description)
}

fmt.Printf(`#
# Your changes will not be saved!
#
# Web UI: %s
#
`, demoCtx.nodes, util.Pluralize(int64(demoCtx.nodes)), adminURL)
`, adminURL)
}

checkTzDatabaseAvailability(context.Background())
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@ func init() {
// We add this command as a persistent flag so you can do stuff like
// ./cockroach demo movr --nodes=3.
IntFlag(demoFlags, &demoCtx.nodes, cliflags.DemoNodes, 1)
// The --empty flag is only valid for the top level demo command,
// so we use the regular flag set.
BoolFlag(demoCmd.Flags(), &demoCtx.useEmptyDatabase, cliflags.UseEmptyDatabase, false)

// sqlfmt command.
fmtFlags := sqlfmtCmd.Flags()
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/interactive_tests/test_demo.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ eexpect "Cluster ID"
eexpect "brief introduction"
# Ensure user is root.
eexpect root@
# Ensure db is defaultdb.
eexpect "defaultdb>"
# Ensure db is movr.
eexpect "movr>"
end_test

0 comments on commit 3b9a95b

Please sign in to comment.