Skip to content

Commit

Permalink
demo: Add a flag to automatically apply geo-partitioned replicas.
Browse files Browse the repository at this point in the history
Work for cockroachdb#39945.

Release note (cli change): Add an option for cockroach demo to
automatically apply the geo-partitioned replicas topology to the movr
dataset.
  • Loading branch information
rohany committed Sep 1, 2019
1 parent 4927a3d commit 31b9132
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 10 deletions.
1 change: 1 addition & 0 deletions pkg/ccl/cliccl/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func getLicense(clusterID uuid.UUID) (string, error) {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", errors.New("unable to connect to licensing endpoint")
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/cli/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,18 @@ to us-east1 and availability zone to 3.
`,
}

DemoGeoPartitionedReplicas = FlagInfo{
Name: "geo-partitioned-replicas",
Description: `
When used with the Movr dataset, reate a 9 node cluster and automatically apply
the geo-partitioned replicas topology across the regions us-east1, us-west1, and
europe-west1. This command will fail with an error if an enterprise license could not
be acquired, or if the Movr dataset is not used.More information about the geo-partitioned
replicas topology can be found at this URL:
https://www.cockroachlabs.com/docs/v19.1/topology-geo-partitioned-replicas.html
`,
}

UseEmptyDatabase = FlagInfo{
Name: "empty",
Description: `
Expand Down
10 changes: 6 additions & 4 deletions pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func initCLIDefaults() {
demoCtx.useEmptyDatabase = false
demoCtx.runWorkload = false
demoCtx.localities = nil
demoCtx.geoPartitionedReplicas = false

initPreFlagsDefaults()

Expand Down Expand Up @@ -336,8 +337,9 @@ var sqlfmtCtx struct {
// demoCtx captures the command-line parameters of the `demo` command.
// Defaults set by InitCLIDefaults() above.
var demoCtx struct {
nodes int
useEmptyDatabase bool
runWorkload bool
localities demoLocalityList
nodes int
useEmptyDatabase bool
runWorkload bool
localities demoLocalityList
geoPartitionedReplicas bool
}
158 changes: 155 additions & 3 deletions pkg/cli/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ 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.
cockroach demo attempts to connect to a Cockroach Labs server to obtain a
temporary enterprise license for demoing enterprise features and enable
telemetry back to Cockroach Labs. In order to disable this behavior, set the
cockroach demo attempts to connect to a Cockroach Labs server to obtain a
temporary enterprise license for demoing enterprise features and enable
telemetry back to Cockroach Labs. In order to disable this behavior, set the
environment variable "COCKROACH_SKIP_ENABLING_DIAGNOSTIC_REPORTING".
`,
Example: ` cockroach demo`,
Expand Down Expand Up @@ -124,6 +124,13 @@ func setupTransientServers(
return "", "", cleanup, errors.Errorf("must have a positive number of nodes")
}

// This demo only works on a 9 node cluster, so set the node count as such.
// Ignore input user localities.
if demoCtx.geoPartitionedReplicas {
demoCtx.nodes = 9
demoCtx.localities = nil
}

// The user specified some localities for their nodes.
if len(demoCtx.localities) != 0 {
// Error out of localities don't line up with requested node
Expand Down Expand Up @@ -207,6 +214,10 @@ func setupTransientServers(
}
urlStr := url.String()

// Communicate information about license acquisition to services
// that depend on it.
licenseSuccess := make(chan bool, 1)

// Start up the update check loop.
// We don't do this in (*server.Server).Start() because we don't want it
// in tests.
Expand All @@ -230,8 +241,13 @@ func setupTransientServers(
const msg = "Unable to acquire demo license. Enterprise features are not enabled in this session.\n"
fmt.Fprint(stderr, msg)
}
licenseSuccess <- success
}()
}
} else {
// If we aren't supposed to check for a license, then automatically
// notify failure.
licenseSuccess <- false
}

// If there is a load generator, create its database and load its
Expand All @@ -253,6 +269,30 @@ func setupTransientServers(
return ``, ``, cleanup, err
}

// If we are requested to prepartition our data, and the
// specified dataset is Movr, spawn a goroutine to do the partitioning.
if demoCtx.geoPartitionedReplicas && gen.Meta().Name == "movr" {
go func() {
success := <-licenseSuccess
// Only try partitioning if license acquisition was successful.
if success {
db, err := gosql.Open("postgres", urlStr)
if err != nil {
exitWithError("demo", err)
}
defer db.Close()
if err := setupGeoPartitionedReplicas(db); err != nil {
exitWithError("demo", err)
}
} else {
const msg = "license acquisition was unsuccessful. Enterprise features are needed to partition data"
exitWithError("demo", errors.New(msg))
}
}()
}

// TODO (rohany): Should we wait until partitioning is done
// to run the workload?
if demoCtx.runWorkload {
if err := runWorkload(ctx, gen, urlStr, stopper); err != nil {
return ``, ``, cleanup, err
Expand All @@ -263,6 +303,108 @@ func setupTransientServers(
return urlStr, s.AdminURL(), cleanup, nil
}

func setupGeoPartitionedReplicas(db *gosql.DB) error {
// Create us-west, us-east and europe-west partitions.
q := `
ALTER TABLE users PARTITION BY LIST (city) (
PARTITION us_west VALUES IN ('seattle', 'san francisco', 'los angeles'),
PARTITION us_east VALUES IN ('new york', 'boston', 'washington dc'),
PARTITION europe_west VALUES IN ('amsterdam', 'paris', 'rome')
);
ALTER TABLE vehicles PARTITION BY LIST (city) (
PARTITION us_west VALUES IN ('seattle', 'san francisco', 'los angeles'),
PARTITION us_east VALUES IN ('new york', 'boston', 'washington dc'),
PARTITION europe_west VALUES IN ('amsterdam', 'paris', 'rome')
);
ALTER INDEX vehicles_auto_index_fk_city_ref_users PARTITION BY LIST (city) (
PARTITION us_west VALUES IN ('seattle', 'san francisco', 'los angeles'),
PARTITION us_east VALUES IN ('new york', 'boston', 'washington dc'),
PARTITION europe_west VALUES IN ('amsterdam', 'paris', 'rome')
);
ALTER TABLE rides PARTITION BY LIST (city) (
PARTITION us_west VALUES IN ('seattle', 'san francisco', 'los angeles'),
PARTITION us_east VALUES IN ('new york', 'boston', 'washington dc'),
PARTITION europe_west VALUES IN ('amsterdam', 'paris', 'rome')
);
ALTER INDEX rides_auto_index_fk_city_ref_users PARTITION BY LIST (city) (
PARTITION us_west VALUES IN ('seattle', 'san francisco', 'los angeles'),
PARTITION us_east VALUES IN ('new york', 'boston', 'washington dc'),
PARTITION europe_west VALUES IN ('amsterdam', 'paris', 'rome')
);
ALTER INDEX rides_auto_index_fk_vehicle_city_ref_vehicles PARTITION BY LIST (vehicle_city) (
PARTITION us_west VALUES IN ('seattle', 'san francisco', 'los angeles'),
PARTITION us_east VALUES IN ('new york', 'boston', 'washington dc'),
PARTITION europe_west VALUES IN ('amsterdam', 'paris', 'rome')
);
ALTER TABLE user_promo_codes PARTITION BY LIST (city) (
PARTITION us_west VALUES IN ('seattle', 'san francisco', 'los angeles'),
PARTITION us_east VALUES IN ('new york', 'boston', 'washington dc'),
PARTITION europe_west VALUES IN ('amsterdam', 'paris', 'rome')
);
ALTER TABLE vehicle_location_histories PARTITION BY LIST (city) (
PARTITION us_west VALUES IN ('seattle', 'san francisco', 'los angeles'),
PARTITION us_east VALUES IN ('new york', 'boston', 'washington dc'),
PARTITION europe_west VALUES IN ('amsterdam', 'paris', 'rome')
);
`
if _, err := db.Exec(q); err != nil {
return err
}

// Alter the partitions to place replicas in the appropriate zones.
q = `
ALTER PARTITION us_west OF INDEX users@* CONFIGURE ZONE USING CONSTRAINTS='["+region=us-west1"]';
ALTER PARTITION us_east OF INDEX users@* CONFIGURE ZONE USING CONSTRAINTS='["+region=us-east1"]';
ALTER PARTITION europe_west OF INDEX users@* CONFIGURE ZONE USING CONSTRAINTS='["+region=europe-west1"]';
ALTER PARTITION us_west OF INDEX vehicles@* CONFIGURE ZONE USING CONSTRAINTS='["+region=us-west1"]';
ALTER PARTITION us_east OF INDEX vehicles@* CONFIGURE ZONE USING CONSTRAINTS='["+region=us-east1"]';
ALTER PARTITION europe_west OF INDEX vehicles@* CONFIGURE ZONE USING CONSTRAINTS='["+region=europe-west1"]';
ALTER PARTITION us_west OF INDEX rides@* CONFIGURE ZONE USING CONSTRAINTS='["+region=us-west1"]';
ALTER PARTITION us_east OF INDEX rides@* CONFIGURE ZONE USING CONSTRAINTS='["+region=us-east1"]';
ALTER PARTITION europe_west OF INDEX rides@* CONFIGURE ZONE USING CONSTRAINTS='["+region=europe-west1"]';
ALTER PARTITION us_west OF INDEX user_promo_codes@* CONFIGURE ZONE USING CONSTRAINTS='["+region=us-west1"]';
ALTER PARTITION us_east OF INDEX user_promo_codes@* CONFIGURE ZONE USING CONSTRAINTS='["+region=us-east1"]';
ALTER PARTITION europe_west OF INDEX user_promo_codes@* CONFIGURE ZONE USING CONSTRAINTS='["+region=europe-west1"]';
ALTER PARTITION us_west OF INDEX vehicle_location_histories@* CONFIGURE ZONE USING CONSTRAINTS='["+region=us-west1"]';
ALTER PARTITION us_east OF INDEX vehicle_location_histories@* CONFIGURE ZONE USING CONSTRAINTS='["+region=us-east1"]';
ALTER PARTITION europe_west OF INDEX vehicle_location_histories@* CONFIGURE ZONE USING CONSTRAINTS='["+region=europe-west1"]';
`
if _, err := db.Exec(q); err != nil {
return err
}

// Create some duplicate indexes for the promo_codes table.
q = `
CREATE INDEX promo_codes_idx_us_west ON promo_codes (code) STORING (description, creation_time, expiration_time, rules);
CREATE INDEX promo_codes_idx_europe_west ON promo_codes (code) STORING (description, creation_time, expiration_time, rules);
`
if _, err := db.Exec(q); err != nil {
return err
}

// Apply configurations to the index for fast reads.
q = `
ALTER TABLE promo_codes CONFIGURE ZONE USING num_replicas = 3,
constraints = '{"+region=us-east1": 1}',
lease_preferences = '[[+region=us-east1]]';
ALTER INDEX promo_codes@promo_codes_idx_us_west CONFIGURE ZONE USING
constraints = '{"+region=us-west1": 1}',
lease_preferences = '[[+region=us-west1]]';
ALTER INDEX promo_codes@promo_codes_idx_europe_west CONFIGURE ZONE USING
constraints = '{"+region=europe-west1": 1}',
lease_preferences = '[[+region=europe-west1]]';
`
if _, err := db.Exec(q); err != nil {
return err
}

return nil
}

func runWorkload(
ctx context.Context, gen workload.Generator, dbURL string, stopper *stop.Stopper,
) error {
Expand Down Expand Up @@ -319,6 +461,16 @@ func runDemo(cmd *cobra.Command, gen workload.Generator) error {
return errors.New("cannot run a workload against an empty database")
}

// Make sure that the user didn't request to have a topology and an empty database.
if demoCtx.geoPartitionedReplicas && demoCtx.useEmptyDatabase {
return errors.New("cannot setup geo-partitioned replicas topology on an empty database")
}

// Make sure that the Movr database is selected when automatically partitioning.
if demoCtx.geoPartitionedReplicas && (gen == nil || gen.Meta().Name != "movr") {
return errors.New("geo-partitioned replicas must be used with the Movr dataset")
}

connURL, adminURL, cleanup, err := setupTransientServers(cmd, gen)
defer cleanup()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ func init() {
// so we use the regular flag set.
BoolFlag(demoCmd.Flags(), &demoCtx.useEmptyDatabase, cliflags.UseEmptyDatabase, false)
BoolFlag(demoCmd.Flags(), &demoCtx.runWorkload, cliflags.RunDemoWorkload, false)
BoolFlag(demoCmd.Flags(), &demoCtx.geoPartitionedReplicas, cliflags.DemoGeoPartitionedReplicas, false)
VarFlag(demoFlags, &demoCtx.localities, cliflags.DemoNodeLocality)

// sqlfmt command.
Expand Down
3 changes: 0 additions & 3 deletions pkg/workload/movr/movr.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ var cities = []struct {
{city: "seattle", locality: "us_west"},
{city: "san francisco", locality: "us_west"},
{city: "los angeles", locality: "us_west"},
{city: "chicago", locality: "us_central"},
{city: "detroit", locality: "us_central"},
{city: "minneapolis", locality: "us_central"},
{city: "amsterdam", locality: "eu_west"},
{city: "paris", locality: "eu_west"},
{city: "rome", locality: "eu_west"},
Expand Down

0 comments on commit 31b9132

Please sign in to comment.