-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
fixtures.go
376 lines (332 loc) · 11.8 KB
/
fixtures.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2018 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package cliccl
import (
"context"
gosql "database/sql"
"fmt"
"strings"
"cloud.google.com/go/storage"
"github.com/cockroachdb/cockroach/pkg/ccl/workloadccl"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/workload"
workloadcli "github.com/cockroachdb/cockroach/pkg/workload/cli"
"github.com/cockroachdb/cockroach/pkg/workload/workloadsql"
"github.com/cockroachdb/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/api/option"
)
var useast1bFixtures = workloadccl.FixtureConfig{
// TODO(dan): Keep fixtures in more than one region to better support
// geo-distributed clusters.
GCSBucket: `cockroach-fixtures`,
GCSPrefix: `workload`,
}
func config() workloadccl.FixtureConfig {
config := useast1bFixtures
if len(*gcsBucketOverride) > 0 {
config.GCSBucket = *gcsBucketOverride
}
if len(*gcsPrefixOverride) > 0 {
config.GCSPrefix = *gcsPrefixOverride
}
if len(*gcsBillingProjectOverride) > 0 {
config.BillingProject = *gcsBillingProjectOverride
}
config.CSVServerURL = *fixturesMakeImportCSVServerURL
config.TableStats = *fixturesMakeTableStats
return config
}
var fixturesCmd = workloadcli.SetCmdDefaults(&cobra.Command{
Use: `fixtures`,
Short: `tools for quickly synthesizing and loading large datasets`,
})
var fixturesListCmd = workloadcli.SetCmdDefaults(&cobra.Command{
Use: `list`,
Short: `list all fixtures stored on GCS`,
Run: workloadcli.HandleErrs(fixturesList),
})
var fixturesMakeCmd = workloadcli.SetCmdDefaults(&cobra.Command{
Use: `make`,
Short: `IMPORT a fixture and then store a BACKUP of it on GCS`,
})
var fixturesLoadCmd = workloadcli.SetCmdDefaults(&cobra.Command{
Use: `load`,
Short: `load a fixture into a running cluster. An enterprise license is required.`,
})
var fixturesImportCmd = workloadcli.SetCmdDefaults(&cobra.Command{
Use: `import`,
Short: `import a fixture into a running cluster. An enterprise license is NOT required.`,
})
var fixturesURLCmd = workloadcli.SetCmdDefaults(&cobra.Command{
Use: `url`,
Short: `generate the GCS URL for a fixture`,
})
var fixturesLoadImportShared = pflag.NewFlagSet(`load/import`, pflag.ContinueOnError)
var fixturesMakeImportShared = pflag.NewFlagSet(`load/import`, pflag.ContinueOnError)
var fixturesMakeImportCSVServerURL = fixturesMakeImportShared.String(
`csv-server`, ``,
`Skip saving CSVs to cloud storage, instead get them from a 'csv-server' running at this url`)
var fixturesMakeOnlyTable = fixturesMakeCmd.PersistentFlags().String(
`only-tables`, ``,
`Only load the tables with the given comma-separated names`)
var fixturesMakeFilesPerNode = fixturesMakeCmd.PersistentFlags().Int(
`files-per-node`, 1,
`number of file URLs to generate per node when using csv-server`)
var fixturesMakeTableStats = fixturesMakeCmd.PersistentFlags().Bool(
`table-stats`, true,
`generate full table statistics for all tables`)
var fixturesImportFilesPerNode = fixturesImportCmd.PersistentFlags().Int(
`files-per-node`, 1,
`number of file URLs to generate per node`)
var fixturesRunChecks = fixturesLoadImportShared.Bool(
`checks`, true, `Run validity checks on the loaded fixture`)
var fixturesImportInjectStats = fixturesImportCmd.PersistentFlags().Bool(
`inject-stats`, true, `Inject pre-calculated statistics if they are available`)
var gcsBucketOverride, gcsPrefixOverride, gcsBillingProjectOverride *string
func init() {
gcsBucketOverride = fixturesCmd.PersistentFlags().String(`gcs-bucket-override`, ``, ``)
gcsPrefixOverride = fixturesCmd.PersistentFlags().String(`gcs-prefix-override`, ``, ``)
_ = fixturesCmd.PersistentFlags().MarkHidden(`gcs-bucket-override`)
_ = fixturesCmd.PersistentFlags().MarkHidden(`gcs-prefix-override`)
gcsBillingProjectOverride = fixturesCmd.PersistentFlags().String(
`gcs-billing-project`, ``,
`Google Cloud project to use for storage billing; `+
`required to be non-empty if the bucket is requestor pays`)
}
const storageError = `failed to create google cloud client ` +
`(You may need to setup the GCS application default credentials: ` +
`'gcloud auth application-default login --project=cockroach-shared')`
// getStorage returns a GCS client using "application default" credentials. The
// caller is responsible for closing it.
func getStorage(ctx context.Context) (*storage.Client, error) {
// TODO(dan): Right now, we don't need all the complexity of
// cloud.ExternalStorage, but if we start supporting more than just GCS,
// this should probably be switched to it.
g, err := storage.NewClient(ctx, option.WithScopes(storage.ScopeReadWrite))
return g, errors.Wrap(err, storageError)
}
func init() {
workloadcli.AddSubCmd(func(userFacing bool) *cobra.Command {
for _, meta := range workload.Registered() {
gen := meta.New()
var genFlags *pflag.FlagSet
if f, ok := gen.(workload.Flagser); ok {
genFlags = f.Flags().FlagSet
// Hide runtime-only flags so they don't clutter up the help text,
// but don't remove them entirely so if someone switches from
// `./workload run` to `./workload fixtures` they don't have to
// remove them from the invocation.
for flagName, meta := range f.Flags().Meta {
if meta.RuntimeOnly || meta.CheckConsistencyOnly {
_ = genFlags.MarkHidden(flagName)
}
}
}
genMakeCmd := workloadcli.SetCmdDefaults(&cobra.Command{
Use: meta.Name + ` [CRDB URI]`,
Args: cobra.RangeArgs(0, 1),
})
genMakeCmd.Flags().AddFlagSet(genFlags)
genMakeCmd.Flags().AddFlagSet(fixturesMakeImportShared)
genMakeCmd.Run = workloadcli.CmdHelper(gen, fixturesMake)
fixturesMakeCmd.AddCommand(genMakeCmd)
genLoadCmd := workloadcli.SetCmdDefaults(&cobra.Command{
Use: meta.Name + ` [CRDB URI]`,
Args: cobra.RangeArgs(0, 1),
})
genLoadCmd.Flags().AddFlagSet(genFlags)
genLoadCmd.Flags().AddFlagSet(fixturesLoadImportShared)
genLoadCmd.Run = workloadcli.CmdHelper(gen, fixturesLoad)
fixturesLoadCmd.AddCommand(genLoadCmd)
genImportCmd := workloadcli.SetCmdDefaults(&cobra.Command{
Use: meta.Name + ` [CRDB URI]`,
Args: cobra.RangeArgs(0, 1),
})
genImportCmd.Flags().AddFlagSet(genFlags)
genImportCmd.Flags().AddFlagSet(fixturesLoadImportShared)
genImportCmd.Flags().AddFlagSet(fixturesMakeImportShared)
genImportCmd.Run = workloadcli.CmdHelper(gen, fixturesImport)
fixturesImportCmd.AddCommand(genImportCmd)
genURLCmd := workloadcli.SetCmdDefaults(&cobra.Command{
Use: meta.Name,
Args: cobra.NoArgs,
})
genURLCmd.Flags().AddFlagSet(genFlags)
genURLCmd.Run = fixturesURL(gen)
fixturesURLCmd.AddCommand(genURLCmd)
}
fixturesCmd.AddCommand(fixturesListCmd)
fixturesCmd.AddCommand(fixturesMakeCmd)
fixturesCmd.AddCommand(fixturesLoadCmd)
fixturesCmd.AddCommand(fixturesImportCmd)
fixturesCmd.AddCommand(fixturesURLCmd)
return fixturesCmd
})
}
func fixturesList(_ *cobra.Command, _ []string) error {
ctx := context.Background()
gcs, err := getStorage(ctx)
if err != nil {
return err
}
defer func() { _ = gcs.Close() }()
fixtures, err := workloadccl.ListFixtures(ctx, gcs, config())
if err != nil {
return err
}
for _, fixture := range fixtures {
fmt.Println(fixture)
}
return nil
}
type filteringGenerator struct {
gen workload.Generator
filter map[string]struct{}
}
func (f filteringGenerator) Meta() workload.Meta {
return f.gen.Meta()
}
func (f filteringGenerator) Tables() []workload.Table {
ret := make([]workload.Table, 0)
for _, t := range f.gen.Tables() {
if _, ok := f.filter[t.Name]; ok {
ret = append(ret, t)
}
}
return ret
}
func fixturesMake(gen workload.Generator, urls []string, _ string) error {
ctx := context.Background()
gcs, err := getStorage(ctx)
if err != nil {
return err
}
defer func() { _ = gcs.Close() }()
sqlDB, err := gosql.Open(`cockroach`, strings.Join(urls, ` `))
if err != nil {
return err
}
if *fixturesMakeOnlyTable != "" {
tableNames := strings.Split(*fixturesMakeOnlyTable, ",")
if len(tableNames) == 0 {
return errors.New("no table names specified")
}
filter := make(map[string]struct{}, len(tableNames))
for _, tableName := range tableNames {
filter[tableName] = struct{}{}
}
gen = filteringGenerator{
gen: gen,
filter: filter,
}
}
filesPerNode := *fixturesMakeFilesPerNode
fixture, err := workloadccl.MakeFixture(ctx, sqlDB, gcs, config(), gen, filesPerNode)
if err != nil {
return err
}
for _, table := range fixture.Tables {
log.Infof(ctx, `stored backup %s`, table.BackupURI)
}
return nil
}
// restoreDataLoader is an InitialDataLoader implementation that loads data with
// RESTORE.
type restoreDataLoader struct {
fixture workloadccl.Fixture
database string
}
// InitialDataLoad implements the InitialDataLoader interface.
func (l restoreDataLoader) InitialDataLoad(
ctx context.Context, db *gosql.DB, gen workload.Generator,
) (int64, error) {
log.Infof(ctx, "starting restore of %d tables", len(gen.Tables()))
start := timeutil.Now()
bytes, err := workloadccl.RestoreFixture(ctx, db, l.fixture, l.database, true /* injectStats */)
if err != nil {
return 0, errors.Wrap(err, `restoring fixture`)
}
elapsed := timeutil.Since(start)
log.Infof(ctx, "restored %s bytes in %d tables (took %s, %s)",
humanizeutil.IBytes(bytes), len(gen.Tables()), elapsed, humanizeutil.DataRate(bytes, elapsed))
return bytes, nil
}
func fixturesLoad(gen workload.Generator, urls []string, dbName string) error {
ctx := context.Background()
gcs, err := getStorage(ctx)
if err != nil {
return err
}
defer func() { _ = gcs.Close() }()
sqlDB, err := gosql.Open(`cockroach`, strings.Join(urls, ` `))
if err != nil {
return err
}
if _, err := sqlDB.Exec(`CREATE DATABASE IF NOT EXISTS ` + dbName); err != nil {
return err
}
fixture, err := workloadccl.GetFixture(ctx, gcs, config(), gen)
if err != nil {
return errors.Wrap(err, `finding fixture`)
}
l := restoreDataLoader{fixture: fixture, database: dbName}
if _, err := workloadsql.Setup(ctx, sqlDB, gen, l); err != nil {
return err
}
if hooks, ok := gen.(workload.Hookser); *fixturesRunChecks && ok {
if consistencyCheckFn := hooks.Hooks().CheckConsistency; consistencyCheckFn != nil {
log.Info(ctx, "fixture is imported; now running consistency checks (ctrl-c to abort)")
if err := consistencyCheckFn(ctx, sqlDB); err != nil {
return err
}
}
}
return nil
}
func fixturesImport(gen workload.Generator, urls []string, dbName string) error {
ctx := context.Background()
sqlDB, err := gosql.Open(`cockroach`, strings.Join(urls, ` `))
if err != nil {
return err
}
if _, err := sqlDB.Exec(`CREATE DATABASE IF NOT EXISTS ` + dbName); err != nil {
return err
}
l := workloadccl.ImportDataLoader{
FilesPerNode: *fixturesImportFilesPerNode,
InjectStats: *fixturesImportInjectStats,
CSVServer: *fixturesMakeImportCSVServerURL,
}
if _, err := workloadsql.Setup(ctx, sqlDB, gen, l); err != nil {
return err
}
if hooks, ok := gen.(workload.Hookser); *fixturesRunChecks && ok {
if consistencyCheckFn := hooks.Hooks().CheckConsistency; consistencyCheckFn != nil {
log.Info(ctx, "fixture is restored; now running consistency checks (ctrl-c to abort)")
if err := consistencyCheckFn(ctx, sqlDB); err != nil {
return err
}
}
}
return nil
}
func fixturesURL(gen workload.Generator) func(*cobra.Command, []string) {
return workloadcli.HandleErrs(func(*cobra.Command, []string) error {
if h, ok := gen.(workload.Hookser); ok {
if err := h.Hooks().Validate(); err != nil {
return err
}
}
fmt.Println(workloadccl.FixtureURL(config(), gen))
return nil
})
}