Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: refactor SynthesizeRegionConfig usages #63512

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions pkg/sql/region_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ func (p *planner) validateAllMultiRegionZoneConfigsInDatabase(

// CurrentDatabaseRegionConfig is part of the tree.EvalDatabase interface.
// CurrentDatabaseRegionConfig uses the cache to synthesize the RegionConfig
// and as such is intended for DML use. It returns an empty DatabaseRegionConfig
// and as such is intended for DML use. It returns nil
// if the current database is not multi-region enabled.
func (p *planner) CurrentDatabaseRegionConfig(
ctx context.Context,
Expand All @@ -907,37 +907,19 @@ func (p *planner) CurrentDatabaseRegionConfig(
return nil, nil
}

// Construct a region config from leased descriptors.
regionEnumID, err := dbDesc.MultiRegionEnumID()
if err != nil {
return nil, err
}

regionEnum, err := p.Descriptors().GetImmutableTypeByID(
return SynthesizeRegionConfig(
ctx,
p.txn,
regionEnumID,
tree.ObjectLookupFlags{},
dbDesc.GetID(),
p.Descriptors(),
SynthesizeRegionConfigOptionUseCache,
)
if err != nil {
return nil, err
}
regionNames, err := regionEnum.RegionNames()
if err != nil {
return nil, err
}

return multiregion.MakeRegionConfig(
regionNames,
dbDesc.GetRegionConfig().PrimaryRegion,
dbDesc.GetRegionConfig().SurvivalGoal,
regionEnumID,
), nil
}

type synthesizeRegionConfigOptions struct {
includeOffline bool
forValidation bool
useCache bool
}

// SynthesizeRegionConfigOption is an option to pass into SynthesizeRegionConfig.
Expand All @@ -956,10 +938,16 @@ var SynthesizeRegionConfigOptionForValidation SynthesizeRegionConfigOption = fun
o.forValidation = true
}

// SynthesizeRegionConfigOptionUseCache uses a cache for synthesizing the region
// config.
var SynthesizeRegionConfigOptionUseCache SynthesizeRegionConfigOption = func(o *synthesizeRegionConfigOptions) {
o.useCache = true
}

// SynthesizeRegionConfig returns a RegionConfig representing the user
// configured state of a multi-region database by coalescing state from both
// the database descriptor and multi-region type descriptor. It avoids the cache
// and is intended for use by DDL statements.
// the database descriptor and multi-region type descriptor. By default, it
// avoids the cache and is intended for use by DDL statements.
func SynthesizeRegionConfig(
ctx context.Context,
txn *kv.Txn,
Expand All @@ -974,7 +962,7 @@ func SynthesizeRegionConfig(

regionConfig := multiregion.RegionConfig{}
_, dbDesc, err := descsCol.GetImmutableDatabaseByID(ctx, txn, dbID, tree.DatabaseLookupFlags{
AvoidCached: true,
AvoidCached: !o.useCache,
Required: true,
IncludeOffline: o.includeOffline,
})
Expand All @@ -993,7 +981,7 @@ func SynthesizeRegionConfig(
regionEnumID,
tree.ObjectLookupFlags{
CommonLookupFlags: tree.CommonLookupFlags{
AvoidCached: true,
AvoidCached: !o.useCache,
IncludeOffline: o.includeOffline,
},
},
Expand Down