Skip to content

Commit

Permalink
workloadccl: inject table statistics during fixtures load
Browse files Browse the repository at this point in the history
See #39103.

Release note: None
  • Loading branch information
nvanbenschoten committed Jul 25, 2019
1 parent a678fea commit 0045120
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/ccl/workloadccl/cliccl/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (l restoreDataLoader) InitialDataLoad(
) (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)
bytes, err := workloadccl.RestoreFixture(ctx, db, l.fixture, l.database, true /* injectStats */)
if err != nil {
return 0, errors.Wrap(err, `restoring fixture`)
}
Expand Down
52 changes: 44 additions & 8 deletions pkg/ccl/workloadccl/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func ImportFixture(
var bytesAtomic int64
g := ctxgroup.WithContext(ctx)
tables := gen.Tables()
if injectStats && len(tables) > 0 && len(tables[0].Stats) > 0 {
if injectStats && tablesHaveStats(tables) {
// Turn off automatic stats temporarily so we don't trigger stats creation
// after the IMPORT. We will inject stats inside importFixtureTable.
// TODO(rytaft): It would be better if the automatic statistics code would
Expand Down Expand Up @@ -402,12 +402,7 @@ func importFixtureTable(
start := timeutil.Now()
var buf bytes.Buffer
var params []interface{}
var qualifiedTableName string
if dbName != `` {
qualifiedTableName = fmt.Sprintf(`"%s"."%s"`, dbName, table.Name)
} else {
qualifiedTableName = fmt.Sprintf(`"%s"`, table.Name)
}
qualifiedTableName := makeQualifiedTableName(dbName, &table)
fmt.Fprintf(&buf, `IMPORT TABLE %s %s CSV DATA (`, qualifiedTableName, table.Schema)
// Generate $1,...,$N-1, where N is the number of csv paths.
for _, path := range paths {
Expand Down Expand Up @@ -448,6 +443,17 @@ func importFixtureTable(
return tableBytes, nil
}

// tablesHaveStats returns whether any of the provided tables have associated
// table statistics to inject.
func tablesHaveStats(tables []workload.Table) bool {
for _, t := range tables {
if len(t.Stats) > 0 {
return true
}
}
return false
}

// disableAutoStats disables automatic stats if they are enabled and returns
// a function to re-enable them later. If automatic stats are already disabled,
// disableAutoStats does nothing and returns an empty function.
Expand Down Expand Up @@ -494,14 +500,33 @@ func injectStatistics(qualifiedTableName string, table *workload.Table, sqlDB *g
return err
}

// makeQualifiedTableName constructs a qualified table name from the specified
// database name and table.
func makeQualifiedTableName(dbName string, table *workload.Table) string {
if dbName == "" {
return fmt.Sprintf(`"%s"`, table.Name)
}
return fmt.Sprintf(`"%s"."%s"`, dbName, table.Name)
}

// RestoreFixture loads a fixture into a CockroachDB cluster. An enterprise
// license is required to have been set in the cluster.
func RestoreFixture(
ctx context.Context, sqlDB *gosql.DB, fixture Fixture, database string,
ctx context.Context, sqlDB *gosql.DB, fixture Fixture, database string, injectStats bool,
) (int64, error) {
var bytesAtomic int64
g := ctxgroup.WithContext(ctx)
genName := fixture.Generator.Meta().Name
tables := fixture.Generator.Tables()
if injectStats && tablesHaveStats(tables) {
// Turn off automatic stats temporarily so we don't trigger stats creation
// after the RESTORE.
// TODO(rytaft): It would be better if the automatic statistics code would
// just trigger a no-op if there are new stats available so we wouldn't
// have to disable and re-enable automatic stats here.
enableFn := disableAutoStats(ctx, sqlDB)
defer enableFn()
}
for _, table := range fixture.Tables {
table := table
g.GoCtx(func(ctx context.Context) error {
Expand All @@ -525,6 +550,17 @@ func RestoreFixture(
if err := g.Wait(); err != nil {
return 0, err
}
if injectStats {
for i := range tables {
t := &tables[i]
if len(t.Stats) > 0 {
qualifiedTableName := makeQualifiedTableName(genName, t)
if err := injectStatistics(qualifiedTableName, t, sqlDB); err != nil {
return 0, err
}
}
}
}
return atomic.LoadInt64(&bytesAtomic), nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/workloadccl/fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestFixture(t *testing.T) {
}

sqlDB.Exec(t, `CREATE DATABASE test`)
if _, err := RestoreFixture(ctx, db, fixture, `test`); err != nil {
if _, err := RestoreFixture(ctx, db, fixture, `test`, false); err != nil {
t.Fatalf(`%+v`, err)
}
sqlDB.CheckQueryResults(t,
Expand Down

0 comments on commit 0045120

Please sign in to comment.