From 422a838ee95cb1accc09d0178873554e94b70edd Mon Sep 17 00:00:00 2001 From: Rohan Yadav Date: Mon, 26 Aug 2019 15:47:18 -0700 Subject: [PATCH] cli: Add location information to default demo localities. Fixes #39937. Adds default latitude and longitude information for the default localities in cockroach demo so that the web UI is pre-populated. Release note (cli change): Add location information to default localities to populate web UI for cockroach demo. --- pkg/cli/demo.go | 3 ++- pkg/roachpb/metadata.go | 34 ++++++++++++++++++++++++++++ pkg/sqlmigrations/migrations.go | 29 ++++++++++++++++++++++++ pkg/sqlmigrations/migrations_test.go | 29 ++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) diff --git a/pkg/cli/demo.go b/pkg/cli/demo.go index db9d08d6a2b2..15bda3e6f34f 100644 --- a/pkg/cli/demo.go +++ b/pkg/cli/demo.go @@ -52,7 +52,8 @@ to avoid pre-loading a dataset.`, const defaultGeneratorName = "movr" var defaultGenerator workload.Generator -var defaultLocalities = []roachpb.Locality{ + +var defaultLocalities = demoLocalityList{ // Default localities for a 3 node cluster {Tiers: []roachpb.Tier{{Key: "region", Value: "us-east1"}, {Key: "az", Value: "b"}}}, {Tiers: []roachpb.Tier{{Key: "region", Value: "us-east1"}, {Key: "az", Value: "c"}}}, diff --git a/pkg/roachpb/metadata.go b/pkg/roachpb/metadata.go index 63855311beb5..3f00fd4bd661 100644 --- a/pkg/roachpb/metadata.go +++ b/pkg/roachpb/metadata.go @@ -514,3 +514,37 @@ func (l *Locality) Set(value string) error { l.Tiers = tiers return nil } + +// DefaultLocationInformation is used to populate the system.locations +// table. The region values here are specific to GCP. +var DefaultLocationInformation = []struct { + Locality Locality + Latitude string + Longitude string +}{ + { + Locality: Locality{Tiers: []Tier{{Key: "region", Value: "us-east1"}}}, + Latitude: "33.836082", + Longitude: "-81.163727", + }, + { + Locality: Locality{Tiers: []Tier{{Key: "region", Value: "us-east4"}}}, + Latitude: "37.478397", + Longitude: "-76.453077", + }, + { + Locality: Locality{Tiers: []Tier{{Key: "region", Value: "us-central1"}}}, + Latitude: "42.032974", + Longitude: "-93.581543", + }, + { + Locality: Locality{Tiers: []Tier{{Key: "region", Value: "us-west1"}}}, + Latitude: "43.804133", + Longitude: "-120.554201", + }, + { + Locality: Locality{Tiers: []Tier{{Key: "region", Value: "europe-west1"}}}, + Latitude: "50.44816", + Longitude: "3.81886", + }, +} diff --git a/pkg/sqlmigrations/migrations.go b/pkg/sqlmigrations/migrations.go index 5a615d090c77..a17ba4dfc00b 100644 --- a/pkg/sqlmigrations/migrations.go +++ b/pkg/sqlmigrations/migrations.go @@ -206,6 +206,11 @@ var backwardCompatibleMigrations = []migrationDescriptor{ name: "propagate the ts purge interval to the new setting names", workFn: retireOldTsPurgeIntervalSettings, }, + { + // Introduced in ? TODO (rohany): what version is this? + name: "update system.locations with default location data", + workFn: updateSystemLocationData, + }, } func staticIDs(ids ...sqlbase.ID) func(ctx context.Context, db db) ([]sqlbase.ID, error) { @@ -918,3 +923,27 @@ ON CONFLICT (name) DO NOTHING`, return nil } + +func updateSystemLocationData(ctx context.Context, r runner) error { + // See if the system.locations table already has data in it. + // If so, we don't want to do anything. + row, err := r.sqlExecutor.QueryRow(ctx, "update-system-locations", + nil, `SELECT count(*) FROM system.locations`) + if err != nil { + return err + } + count := int(tree.MustBeDInt(row[0])) + if count != 0 { + return nil + } + + for _, loc := range roachpb.DefaultLocationInformation { + stmt := `UPSERT INTO system.locations VALUES ($1, $2, $3, $4)` + tier := loc.Locality.Tiers[0] + if _, err := r.sqlExecutor.Exec(ctx, "update-system-locations", nil, + stmt, tier.Key, tier.Value, loc.Latitude, loc.Longitude); err != nil { + return err + } + } + return nil +} diff --git a/pkg/sqlmigrations/migrations_test.go b/pkg/sqlmigrations/migrations_test.go index 7ef53928c1cd..0353c17e6b94 100644 --- a/pkg/sqlmigrations/migrations_test.go +++ b/pkg/sqlmigrations/migrations_test.go @@ -639,3 +639,32 @@ func TestExpectedInitialRangeCount(t *testing.T) { return nil }) } + +func TestUpdateSystemLocationData(t *testing.T) { + defer leaktest.AfterTest(t)() + ctx := context.Background() + + mt := makeMigrationTest(ctx, t) + defer mt.close(ctx) + + migration := mt.pop(t, "update system.locations with default location data") + mt.start(t, base.TestServerArgs{}) + + // Check that we don't have any data in the system.locations table without the migration. + var count int + mt.sqlDB.QueryRow(t, `SELECT count(*) FROM system.locations`).Scan(&count) + if count != 0 { + t.Fatalf("Exected to find 0 rows in system.locations. Found %d instead", count) + } + + // Run the migration to insert locations. + if err := mt.runMigration(ctx, migration); err != nil { + t.Errorf("expected success, got %q", err) + } + + // Check that we have all of the expected locations. + mt.sqlDB.QueryRow(t, `SELECT count(*) FROM system.locations`).Scan(&count) + if count != len(roachpb.DefaultLocationInformation) { + t.Fatalf("Exected to find 0 rows in system.locations. Found %d instead", count) + } +}