Skip to content

Commit f131785

Browse files
ashwinpvgSkCQ
authored andcommitted
Update maintenance service to be compatible with spanner
- Add support to perform schema checks for spanner - The unit tests currently only support CDB. Will be adding support for running tests against a local spanner db in a separate CL. Change-Id: Ida0790a6c9b02816cc45025ebeb5c1776ec88bb6 Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/934278 Reviewed-by: Farid (Mojtaba) Faridzad <[email protected]> Commit-Queue: Ashwin Verleker <[email protected]>
1 parent 633e13c commit f131785

File tree

20 files changed

+407
-48
lines changed

20 files changed

+407
-48
lines changed

go/sql/schema/exportschema/exportschema.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package exportschema
55
import (
66
"context"
77
"encoding/json"
8-
"flag"
98
"fmt"
109
"io"
1110
"math/rand"
@@ -24,16 +23,7 @@ import (
2423
// Note that this works by requiring a local instance of the CockroachDB
2524
// emulator to be running, so this is not appropriate to call in a go:generate
2625
// statement.
27-
func Main(args []string, tables interface{}, schemaAsString string) error {
28-
var out string
29-
fs := flag.NewFlagSet("exportschema", flag.ExitOnError)
30-
fs.StringVar(&out, "out", "", "Filename of the schema Description to write.")
31-
32-
err := fs.Parse(args[1:])
33-
if err != nil {
34-
return skerr.Wrap(err)
35-
}
36-
26+
func Main(out string, dbType string, tables interface{}, schemaAsString string) error {
3727
if out == "" {
3828
return skerr.Fmt("--out flag must be supplied")
3929
}
@@ -42,18 +32,23 @@ func Main(args []string, tables interface{}, schemaAsString string) error {
4232
rand.Seed(time.Now().UnixNano())
4333
databaseName := fmt.Sprintf("%s_%d", "export", rand.Uint64())
4434
host := emulators.GetEmulatorHostEnvVar(emulators.CockroachDB)
35+
if dbType == schema.SpannerDBType {
36+
host = "localhost:5432"
37+
}
4538
connectionString := fmt.Sprintf("postgresql://root@%s/%s?sslmode=disable", host, databaseName)
4639
db, err := pgxpool.Connect(ctx, connectionString)
4740
if err != nil {
4841
sklog.Fatal(err)
4942
}
5043

51-
// Create the database in cockroachdb.
52-
_, err = db.Exec(ctx, fmt.Sprintf(`
53-
CREATE DATABASE %s;
54-
SET DATABASE = %s;`, databaseName, databaseName))
55-
if err != nil {
56-
sklog.Fatal(err)
44+
if dbType != schema.SpannerDBType {
45+
// Create the database in cockroachdb.
46+
_, err = db.Exec(ctx, fmt.Sprintf(`
47+
CREATE DATABASE %s;
48+
SET DATABASE = %s;`, databaseName, databaseName))
49+
if err != nil {
50+
sklog.Fatal(err)
51+
}
5752
}
5853

5954
// Create the tables.
@@ -62,7 +57,7 @@ func Main(args []string, tables interface{}, schemaAsString string) error {
6257
sklog.Fatal(err)
6358
}
6459

65-
sch, err := schema.GetDescription(ctx, db, tables)
60+
sch, err := schema.GetDescription(ctx, db, tables, dbType)
6661
if err != nil {
6762
sklog.Fatal(err)
6863
}

go/sql/schema/schema.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313

1414
const (
1515
// Timeout used on Contexts when making SQL requests.
16-
sqlTimeout = time.Minute
16+
sqlTimeout = time.Minute
17+
SpannerDBType = "spanner"
18+
CockroachDBType = "cockroachdb"
1719
)
1820

1921
// TableNames takes in a "table type", that is a table whose fields are slices.
@@ -48,6 +50,17 @@ WHERE
4850
table_name = $1;
4951
`
5052

53+
const typesQuerySpanner = `
54+
SELECT
55+
column_name,
56+
data_type || ' def:' || COALESCE(column_default, '') || ' nullable:' || COALESCE(is_nullable, '')
57+
FROM
58+
information_schema.columns
59+
WHERE
60+
table_name = $1;
61+
62+
`
63+
5164
// Query to return the index names for each table.
5265
const indexNameQuery = `
5366
SELECT DISTINCT
@@ -60,16 +73,31 @@ ORDER BY
6073
index_name DESC
6174
`
6275

76+
const spannerIndexNameQuery = `
77+
SELECT DISTINCT
78+
index_name
79+
FROM
80+
information_schema.indexes
81+
WHERE
82+
table_name = $1
83+
ORDER BY
84+
index_name DESC
85+
`
86+
6387
// GetDescription returns a Description populated for every table listed in
6488
// `tables`.
65-
func GetDescription(ctx context.Context, db pool.Pool, tables interface{}) (*Description, error) {
89+
func GetDescription(ctx context.Context, db pool.Pool, tables interface{}, databaseType string) (*Description, error) {
6690
ctx, cancel := context.WithTimeout(ctx, sqlTimeout)
6791
defer cancel()
6892
colNameAndType := map[string]string{}
6993
indexNames := []string{}
7094
for _, tableName := range TableNames(tables) {
7195
// Fill in colNameAndType.
72-
rows, err := db.Query(ctx, typesQuery, tableName)
96+
typeQuery := typesQuery
97+
if databaseType == SpannerDBType {
98+
typeQuery = typesQuerySpanner
99+
}
100+
rows, err := db.Query(ctx, typeQuery, tableName)
73101
if err != nil {
74102
return nil, skerr.Wrap(err)
75103
}
@@ -84,7 +112,11 @@ func GetDescription(ctx context.Context, db pool.Pool, tables interface{}) (*Des
84112
}
85113

86114
// Fill in indexNames.
87-
rows, err = db.Query(ctx, indexNameQuery, tableName)
115+
indexQuery := indexNameQuery
116+
if databaseType == SpannerDBType {
117+
indexQuery = spannerIndexNameQuery
118+
}
119+
rows, err = db.Query(ctx, indexQuery, tableName)
88120
if err != nil {
89121
return nil, skerr.Wrap(err)
90122
}

go/sql/schema/schema_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ func setupForTest(t *testing.T) *pgxpool.Pool {
8080

8181
}
8282

83-
func TestGetDescription(t *testing.T) {
83+
func TestGetDescriptionCDB(t *testing.T) {
8484
db := setupForTest(t)
85-
desc, err := GetDescription(context.Background(), db, Tables{})
85+
desc, err := GetDescription(context.Background(), db, Tables{}, CockroachDBType)
8686
require.NoError(t, err)
8787

8888
expected := Description{

machine/go/machine/store/cdb/cdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func New(ctx context.Context, db pool.Pool, pools *pools.Pools) (*Store, error)
136136
return nil, skerr.Wrap(err)
137137
}
138138

139-
actual, err := schema.GetDescription(ctx, db, Tables{})
139+
actual, err := schema.GetDescription(ctx, db, Tables{}, schema.CockroachDBType)
140140
if err != nil {
141141
return nil, skerr.Wrap(err)
142142
}

machine/go/machine/store/cdb/cdb_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ CREATE TABLE IF NOT EXISTS TaskResult (
356356
`
357357

358358
func getSchema(t *testing.T, db pool.Pool) *schema.Description {
359-
ret, err := schema.GetDescription(context.Background(), db, cdb.Tables{})
359+
ret, err := schema.GetDescription(context.Background(), db, cdb.Tables{}, schema.CockroachDBType)
360360
require.NoError(t, err)
361361
require.NotEmpty(t, ret.ColumnNameAndType)
362362
return ret

machine/go/machine/store/cdb/exportschema/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go_library(
99
"//go/sklog",
1010
"//go/sql/schema/exportschema:exportschema_lib",
1111
"//machine/go/machine/store/cdb",
12+
"//machine/go/machine/store/cdb/spanner",
1213
],
1314
)
1415

machine/go/machine/store/cdb/exportschema/exportschema.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@
22
package main
33

44
import (
5+
"flag"
56
"os"
67

78
"go.skia.org/infra/go/sklog"
89
"go.skia.org/infra/go/sql/schema/exportschema"
910
"go.skia.org/infra/machine/go/machine/store/cdb"
11+
"go.skia.org/infra/machine/go/machine/store/cdb/spanner"
1012
)
1113

1214
func main() {
13-
err := exportschema.Main(os.Args, cdb.Tables{}, cdb.Schema)
15+
var dbType string
16+
var out string
17+
fs := flag.NewFlagSet("exportschema", flag.ExitOnError)
18+
fs.StringVar(&dbType, "databaseType", "", "Database type for the schema.")
19+
fs.StringVar(&out, "out", "", "Filename of the schema Description to write.")
20+
21+
err := fs.Parse(os.Args[1:])
22+
if err != nil {
23+
sklog.Fatalf("Error parsing arguments: %v", err)
24+
}
25+
26+
schema := cdb.Schema
27+
if dbType == "spanner" {
28+
schema = spanner.Schema
29+
}
30+
err = exportschema.Main(out, dbType, cdb.Tables{}, schema)
1431
if err != nil {
1532
sklog.Fatal(err)
1633
}

perf/Makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ generate_sql_schema:
99
cd ./go/sql; go run ./tosql
1010
go run ./go/sql/exportschema --out ./go/sql/expectedschema/schema.json
1111

12+
generate_sql_schema_spanner:
13+
cd ./go/sql; go run ./tosql
14+
go run ./go/sql/exportschema --out ./go/sql/expectedschema/schema_spanner.json --databaseType spanner
15+
1216
run_cockroachdb_emulators:
1317
../scripts/run_emulators/run_emulators start
1418

@@ -246,11 +250,22 @@ run_frontend:
246250
run-demo-instance: run-spanner-emulator initdemo ingest_demo run_frontend
247251
run-demo-instance-db-persist: initdemo run_frontend
248252

253+
run_maintenance:
254+
../_bazel_bin/perf/go/perfserver/perfserver_/perfserver maintenance \
255+
--local \
256+
--config_filename=./configs/demo_spanner.json \
257+
--connection_string=postgresql://[email protected]:5432/demo?sslmode=disable \
258+
--migrate_regressions \
259+
--refresh_query_cache \
260+
--tiles_for_query_cache=5
261+
262+
run-demo-maintenance: run-spanner-emulator initdemo ingest_demo run_maintenance
263+
249264
# Before running this target you need to have a local single-instance of
250265
# cockroachdb running, started like this:
251266
#
252267
# cd /tmp cockroach start-single-node --insecure --listen-addr=127.0.0.1
253-
run-demo-maintenance:
268+
run-demo-maintenance-cdb:
254269
$(BAZEL) build --config=mayberemote -c dbg //perf/...
255270
$(BAZEL) run --config=mayberemote -c dbg //perf/go/initdemo:initdemo
256271
../_bazel_bin/perf/go/perfserver/perfserver_/perfserver ingest \

perf/go/builders/builders.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ func NewCockroachDBFromConfig(ctx context.Context, instanceConfig *config.Instan
117117

118118
if checkSchema {
119119
// Confirm the database has the right schema.
120-
expectedSchema, err := expectedschema.Load()
120+
expectedSchema, err := expectedschema.Load(instanceConfig.DataStoreConfig.DataStoreType)
121121
if err != nil {
122122
return nil, skerr.Wrap(err)
123123
}
124124

125-
actual, err := schema.GetDescription(ctx, singletonPool, sql.Tables{})
125+
actual, err := schema.GetDescription(ctx, singletonPool, sql.Tables{}, string(instanceConfig.DataStoreConfig.DataStoreType))
126126
if err != nil {
127127
return nil, skerr.Wrap(err)
128128
}

perf/go/maintenance/maintenance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Start(ctx context.Context, flags config.MaintenanceFlags, instanceConfig *c
5454
if err != nil {
5555
return skerr.Wrapf(err, "Failed to create CockroachDB instance.")
5656
}
57-
err = expectedschema.ValidateAndMigrateNewSchema(ctx, db)
57+
err = expectedschema.ValidateAndMigrateNewSchema(ctx, db, instanceConfig.DataStoreConfig.DataStoreType)
5858
if err != nil {
5959
return skerr.Wrapf(err, "Failed to migrate schema.")
6060
}

0 commit comments

Comments
 (0)