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 7, 2022
1 parent 60fc156 commit 3d90060
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 28 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
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 @@ -1892,6 +1892,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 @@ -1934,7 +1935,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 @@ -3030,6 +3031,7 @@ func (sc *SchemaChanger) applyZoneConfigChangeForMutation(
ctx,
txn,
sc.execCfg,
descsCol,
regionConfig,
tableDesc,
opts...,
Expand All @@ -3042,7 +3044,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
41 changes: 24 additions & 17 deletions pkg/sql/set_zone_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/descmetadata"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
Expand Down Expand Up @@ -790,7 +792,7 @@ func (n *setZoneConfigNode) startExec(params runParams) error {
// TODO(ajwerner): This is extremely fragile because we accept a nil table
// all the way down here.
n.run.numAffected, err = writeZoneConfig(params.ctx, params.p.txn,
targetID, table, zoneToWrite, execConfig, hasNewSubzones)
targetID, table, zoneToWrite, execConfig, params.p.Descriptors(), hasNewSubzones)
if err != nil {
return err
}
Expand Down Expand Up @@ -1054,8 +1056,8 @@ func validateZoneLocalitiesForSecondaryTenants(
const MultitenancyZoneCfgIssueNo = 49854

type zoneConfigUpdate struct {
id descpb.ID
value []byte
id descpb.ID
zoneConfig *zonepb.ZoneConfig
}

func prepareZoneConfigWrites(
Expand All @@ -1080,11 +1082,7 @@ func prepareZoneConfigWrites(
if zone.IsSubzonePlaceholder() && len(zone.Subzones) == 0 {
return &zoneConfigUpdate{id: targetID}, nil
}
buf, err := protoutil.Marshal(zone)
if err != nil {
return nil, pgerror.Wrap(err, pgcode.CheckViolation, "could not marshal zone config")
}
return &zoneConfigUpdate{id: targetID, value: buf}, nil
return &zoneConfigUpdate{id: targetID, zoneConfig: zone}, nil
}

func writeZoneConfig(
Expand All @@ -1094,24 +1092,33 @@ func writeZoneConfig(
table catalog.TableDescriptor,
zone *zonepb.ZoneConfig,
execCfg *ExecutorConfig,
descriptors *descs.Collection,
hasNewSubzones bool,
) (numAffected int, err error) {
update, err := prepareZoneConfigWrites(ctx, execCfg, targetID, table, zone, hasNewSubzones)
if err != nil {
return 0, err
}
return writeZoneConfigUpdate(ctx, txn, execCfg, update)
return writeZoneConfigUpdate(ctx, txn, execCfg, descriptors, update)
}

func writeZoneConfigUpdate(
ctx context.Context, txn *kv.Txn, execCfg *ExecutorConfig, update *zoneConfigUpdate,
ctx context.Context,
txn *kv.Txn,
execCfg *ExecutorConfig,
descriptors *descs.Collection,
update *zoneConfigUpdate,
) (numAffected int, _ error) {
if update.value == nil {
return execCfg.InternalExecutor.Exec(ctx, "delete-zone", txn,
"DELETE FROM system.zones WHERE id = $1", update.id)
metaDataUpdater := descmetadata.NewMetadataUpdater(ctx,
execCfg.InternalExecutorFactory,
descriptors,
execCfg.SV(),
txn,
NewFakeSessionData(execCfg.SV()))
if update.zoneConfig == nil {
return metaDataUpdater.DeleteZoneConfig(ctx, update.id)
}
return execCfg.InternalExecutor.Exec(ctx, "update-zone", txn,
"UPSERT INTO system.zones (id, config) VALUES ($1, $2)", update.id, update.value)
return metaDataUpdater.UpsertZoneConfig(ctx, update.id, update.zoneConfig)
}

// getZoneConfigRaw looks up the zone config with the given ID. Unlike
Expand Down Expand Up @@ -1180,6 +1187,7 @@ func RemoveIndexZoneConfigs(
ctx context.Context,
txn *kv.Txn,
execCfg *ExecutorConfig,
descriptors *descs.Collection,
tableDesc catalog.TableDescriptor,
indexIDs []uint32,
) error {
Expand Down Expand Up @@ -1210,11 +1218,10 @@ func RemoveIndexZoneConfigs(

if zcRewriteNecessary {
// Ignore CCL required error to allow schema change to progress.
_, err = writeZoneConfig(ctx, txn, tableDesc.GetID(), tableDesc, zone, execCfg, false /* hasNewSubzones */)
_, err = writeZoneConfig(ctx, txn, tableDesc.GetID(), tableDesc, zone, execCfg, descriptors, false /* hasNewSubzones */)
if err != nil && !sqlerrors.IsCCLRequiredError(err) {
return err
}
}

return nil
}
Loading

0 comments on commit 3d90060

Please sign in to comment.