Skip to content

Commit

Permalink
sql/schemachanger: adopt metadata updater for zone config updates
Browse files Browse the repository at this point in the history
Previously, the code to update the zone configs was separate
between the declarative schema changer and the legacy schema
changer. This was inadequate because we have a opportunity
to share logic here. To address this the logic to update and
delete zone configurations is now shared.

Release note: None
  • Loading branch information
fqazi committed Jul 5, 2022
1 parent 7da053e commit 3dd75b3
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 29 deletions.
2 changes: 2 additions & 0 deletions pkg/ccl/backupccl/restore_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ func createImportingDescriptors(
regionConfig,
txn,
p.ExecCfg(),
descsCol,
); err != nil {
return err
}
Expand Down Expand Up @@ -1045,6 +1046,7 @@ func createImportingDescriptors(
ctx,
txn,
p.ExecCfg(),
descsCol,
regionConfig,
mutTable,
sql.ApplyZoneConfigForMultiRegionTableOptionTableAndIndexes,
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/alter_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ func removeLocalityConfigFromAllTablesInDB(
ctx,
p.txn,
p.ExecCfg(),
p.Descriptors(),
multiregion.RegionConfig{}, // pass dummy config as it is not used.
tbDesc,
applyZoneConfigForMultiRegionTableOptionRemoveGlobalZoneConfig,
Expand Down Expand Up @@ -600,6 +601,7 @@ func (n *alterDatabaseDropRegionNode) startExec(params runParams) error {
n.desc.ID,
params.p.txn,
params.p.execCfg,
params.p.Descriptors(),
); err != nil {
return err
}
Expand Down Expand Up @@ -756,6 +758,7 @@ func (n *alterDatabasePrimaryRegionNode) switchPrimaryRegion(params runParams) e
updatedRegionConfig,
params.p.txn,
params.p.execCfg,
params.p.Descriptors(),
); err != nil {
return err
}
Expand Down Expand Up @@ -1129,6 +1132,7 @@ func (n *alterDatabaseSurvivalGoalNode) startExec(params runParams) error {
regionConfig,
params.p.txn,
params.p.execCfg,
params.p.Descriptors(),
); err != nil {
return err
}
Expand Down Expand Up @@ -1258,6 +1262,7 @@ func (n *alterDatabasePlacementNode) startExec(params runParams) error {
regionConfig,
params.p.txn,
params.p.execCfg,
params.p.Descriptors(),
); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/alter_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (n *alterIndexNode) startExec(params runParams) error {
params.ctx,
params.p.txn,
n.tableDesc,
params.p.Descriptors(),
n.index.GetID(),
oldPartitioning,
n.index.GetPartitioning(),
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ func (n *alterTableNode) startExec(params runParams) error {
params.ctx,
params.p.txn,
n.tableDesc,
params.p.Descriptors(),
n.tableDesc.GetPrimaryIndexID(),
oldPartitioning,
n.tableDesc.GetPrimaryIndex().GetPartitioning(),
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/alter_table_locality.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ func (n *alterTableSetLocalityNode) writeNewTableLocalityAndZoneConfig(
params.ctx,
params.p.txn,
params.p.ExecCfg(),
params.p.Descriptors(),
regionConfig,
n.tableDesc,
ApplyZoneConfigForMultiRegionTableOptionTableAndIndexes,
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -2326,7 +2326,7 @@ func runSchemaChangesInTxn(
}
} else if idx := m.AsIndex(); idx != nil {
if err := indexTruncateInTxn(
ctx, planner.Txn(), planner.ExecCfg(), planner.EvalContext(), immutDesc, idx, traceKV,
ctx, planner.Txn(), planner.ExecCfg(), planner.Descriptors(), planner.EvalContext(), immutDesc, idx, traceKV,
); err != nil {
return err
}
Expand Down Expand Up @@ -2698,6 +2698,7 @@ func indexTruncateInTxn(
ctx context.Context,
txn *kv.Txn,
execCfg *ExecutorConfig,
descriptors *descs.Collection,
evalCtx *eval.Context,
tableDesc catalog.TableDescriptor,
idx catalog.Index,
Expand All @@ -2724,7 +2725,7 @@ func indexTruncateInTxn(
}
}
// Remove index zone configs.
return RemoveIndexZoneConfigs(ctx, txn, execCfg, tableDesc, []uint32{uint32(idx.GetID())})
return RemoveIndexZoneConfigs(ctx, txn, execCfg, descriptors, tableDesc, []uint32{uint32(idx.GetID())})
}

// We note the time at the start of the merge in order to limit the set of
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ func (p *planner) configureZoneConfigForNewIndexPartitioning(
ctx,
p.txn,
p.ExecCfg(),
p.Descriptors(),
regionConfig,
tableDesc,
applyZoneConfigForMultiRegionTableOptionNewIndexes(indexIDs...),
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ func (n *createTableNode) startExec(params runParams) error {
params.ctx,
params.p.txn,
params.p.ExecCfg(),
params.p.Descriptors(),
regionConfig,
desc,
ApplyZoneConfigForMultiRegionTableOptionTableAndIndexes,
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/create_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ func (n *createViewNode) startExec(params runParams) error {
params.ctx,
params.p.txn,
params.p.ExecCfg(),
params.p.Descriptors(),
regionConfig,
newDesc,
applyZoneConfigForMultiRegionTableOptionTableNewConfig(
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/database_region_change_finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (r *databaseRegionChangeFinalizer) preDrop(ctx context.Context, txn *kv.Txn
}
for _, update := range zoneConfigUpdates {
if _, err := writeZoneConfigUpdate(
ctx, txn, r.localPlanner.ExecCfg(), update,
ctx, txn, r.localPlanner.ExecCfg(), r.localPlanner.Descriptors(), update,
); err != nil {
return err
}
Expand Down Expand Up @@ -210,6 +210,7 @@ func (r *databaseRegionChangeFinalizer) updateDatabaseZoneConfig(
regionConfig,
txn,
r.localPlanner.ExecCfg(),
r.localPlanner.Descriptors(),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/gcjob/index_garbage_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func gcIndexes(
return err
}
return sql.RemoveIndexZoneConfigs(
ctx, txn, execCfg, freshParentTableDesc, []uint32{uint32(index.IndexID)},
ctx, txn, execCfg, descriptors, freshParentTableDesc, []uint32{uint32(index.IndexID)},
)
}
if err := sql.DescsTxn(ctx, execCfg, removeIndexZoneConfigs); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/pgwire/testdata/pgtest/notice
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Query {"String": "DROP INDEX t_x_idx"}
until crdb_only
CommandComplete
----
{"Severity":"NOTICE","SeverityUnlocalized":"NOTICE","Code":"00000","Message":"the data for dropped indexes is reclaimed asynchronously","Detail":"","Hint":"The reclamation delay can be customized in the zone configuration for the table.","Position":0,"InternalPosition":0,"InternalQuery":"","Where":"","SchemaName":"","TableName":"","ColumnName":"","DataTypeName":"","ConstraintName":"","File":"drop_index.go","Line":576,"Routine":"dropIndexByName","UnknownFields":null}
{"Severity":"NOTICE","SeverityUnlocalized":"NOTICE","Code":"00000","Message":"the data for dropped indexes is reclaimed asynchronously","Detail":"","Hint":"The reclamation delay can be customized in the zone configuration for the table.","Position":0,"InternalPosition":0,"InternalQuery":"","Where":"","SchemaName":"","TableName":"","ColumnName":"","DataTypeName":"","ConstraintName":"","File":"drop_index.go","Line":577,"Routine":"dropIndexByName","UnknownFields":null}
{"Type":"CommandComplete","CommandTag":"DROP INDEX"}

until noncrdb_only
Expand Down
20 changes: 17 additions & 3 deletions pkg/sql/region_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ func ApplyZoneConfigForMultiRegionTable(
ctx context.Context,
txn *kv.Txn,
execCfg *ExecutorConfig,
descriptors *descs.Collection,
regionConfig multiregion.RegionConfig,
table catalog.TableDescriptor,
opts ...applyZoneConfigForMultiRegionTableOption,
Expand All @@ -833,7 +834,7 @@ func ApplyZoneConfigForMultiRegionTable(
if update == nil || err != nil {
return err
}
_, err = writeZoneConfigUpdate(ctx, txn, execCfg, update)
_, err = writeZoneConfigUpdate(ctx, txn, execCfg, descriptors, update)
return err
}

Expand All @@ -845,6 +846,7 @@ func ApplyZoneConfigFromDatabaseRegionConfig(
regionConfig multiregion.RegionConfig,
txn *kv.Txn,
execConfig *ExecutorConfig,
descriptors *descs.Collection,
) error {
// Build a zone config based on the RegionConfig information.
dbZoneConfig, err := zoneConfigForMultiRegionDatabase(regionConfig)
Expand All @@ -857,13 +859,18 @@ func ApplyZoneConfigFromDatabaseRegionConfig(
dbZoneConfig,
txn,
execConfig,
descriptors,
)
}

// discardMultiRegionFieldsForDatabaseZoneConfig resets the multi-region zone
// config fields for a multi-region database.
func discardMultiRegionFieldsForDatabaseZoneConfig(
ctx context.Context, dbID descpb.ID, txn *kv.Txn, execConfig *ExecutorConfig,
ctx context.Context,
dbID descpb.ID,
txn *kv.Txn,
execConfig *ExecutorConfig,
descriptors *descs.Collection,
) error {
// Merge with an empty zone config.
return applyZoneConfigForMultiRegionDatabase(
Expand All @@ -872,6 +879,7 @@ func discardMultiRegionFieldsForDatabaseZoneConfig(
*zonepb.NewZoneConfig(),
txn,
execConfig,
descriptors,
)
}

Expand All @@ -881,6 +889,7 @@ func applyZoneConfigForMultiRegionDatabase(
mergeZoneConfig zonepb.ZoneConfig,
txn *kv.Txn,
execConfig *ExecutorConfig,
descriptors *descs.Collection,
) error {
currentZoneConfig, err := getZoneConfigRaw(ctx, txn, execConfig.Codec, execConfig.Settings, dbID)
if err != nil {
Expand Down Expand Up @@ -912,6 +921,7 @@ func applyZoneConfigForMultiRegionDatabase(
nil, /* table */
&newZoneConfig,
execConfig,
descriptors,
false, /* hasNewSubzones */
); err != nil {
return err
Expand Down Expand Up @@ -951,6 +961,7 @@ func (p *planner) refreshZoneConfigsForTables(
ctx,
p.txn,
p.ExecCfg(),
p.Descriptors(),
regionConfig,
tbDesc,
ApplyZoneConfigForMultiRegionTableOptionTableAndIndexes,
Expand Down Expand Up @@ -1014,7 +1025,8 @@ func (p *planner) maybeInitializeMultiRegionDatabase(
desc.ID,
*regionConfig,
p.txn,
p.execCfg); err != nil {
p.execCfg,
p.Descriptors()); err != nil {
return err
}

Expand Down Expand Up @@ -1099,6 +1111,7 @@ func (p *planner) ResetMultiRegionZoneConfigsForTable(ctx context.Context, id in
ctx,
p.txn,
p.ExecCfg(),
p.Descriptors(),
regionConfig,
desc,
ApplyZoneConfigForMultiRegionTableOptionTableAndIndexes,
Expand Down Expand Up @@ -1138,6 +1151,7 @@ func (p *planner) ResetMultiRegionZoneConfigsForDatabase(ctx context.Context, id
regionConfig,
p.txn,
p.execCfg,
p.Descriptors(),
); err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/sql/schema_changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,7 @@ func maybeUpdateZoneConfigsForPKChange(
ctx context.Context,
txn *kv.Txn,
execCfg *ExecutorConfig,
descriptors *descs.Collection,
table *tabledesc.Mutable,
swapInfo *descpb.PrimaryKeySwap,
) error {
Expand Down Expand Up @@ -1935,7 +1936,7 @@ func maybeUpdateZoneConfigsForPKChange(

// Write the zone back. This call regenerates the index spans that apply
// to each partition in the index.
_, err = writeZoneConfig(ctx, txn, table.ID, table, zone, execCfg, false)
_, err = writeZoneConfig(ctx, txn, table.ID, table, zone, execCfg, descriptors, false)
if err != nil && !sqlerrors.IsCCLRequiredError(err) {
return err
}
Expand Down Expand Up @@ -3031,6 +3032,7 @@ func (sc *SchemaChanger) applyZoneConfigChangeForMutation(
ctx,
txn,
sc.execCfg,
descsCol,
regionConfig,
tableDesc,
opts...,
Expand All @@ -3043,7 +3045,7 @@ func (sc *SchemaChanger) applyZoneConfigChangeForMutation(
// Note this is done even for isDone = true, though not strictly
// necessary.
return maybeUpdateZoneConfigsForPKChange(
ctx, txn, sc.execCfg, tableDesc, pkSwap.PrimaryKeySwapDesc(),
ctx, txn, sc.execCfg, descsCol, tableDesc, pkSwap.PrimaryKeySwapDesc(),
)
}
return nil
Expand Down
Loading

0 comments on commit 3dd75b3

Please sign in to comment.