Skip to content

Commit

Permalink
sql: fix dangling zone config on REGIONAL BY ROW -> REGIONAL BY TABLE
Browse files Browse the repository at this point in the history
We previously did not set NumReplicas to 0 for RBR -> RBT conversions.
This means the zone configuration do not cleanup and inherit properly
after the async job completes for dropping old indexes since it no
longer thinks the RBR zone config is a placeholder, leaving a
dangling reference. Patch this up correctly.

N.B.: In theory there's nothing wrong with this. But this patch makes
SHOW ZONE CONFIGURATION consistent when you convert RBR to RBT
compared to making a RBT table directly.

Release note: None
  • Loading branch information
otan committed Feb 18, 2021
1 parent 86a44f7 commit 718034b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
72 changes: 36 additions & 36 deletions pkg/ccl/logictestccl/testdata/logic_test/alter_table_locality
Original file line number Diff line number Diff line change
Expand Up @@ -1465,15 +1465,15 @@ regional_by_row CREATE TABLE public.regional_by_
query TT
SHOW ZONE CONFIGURATION FOR TABLE regional_by_row
----
TABLE regional_by_row ALTER TABLE regional_by_row CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 90000,
num_replicas = 5,
num_voters = 3,
constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
voter_constraints = '[+region=ca-central-1]',
lease_preferences = '[[+region=ca-central-1]]'
DATABASE alter_locality_test ALTER DATABASE alter_locality_test CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 90000,
num_replicas = 5,
num_voters = 3,
constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
voter_constraints = '[+region=ca-central-1]',
lease_preferences = '[[+region=ca-central-1]]'

statement ok
DROP TABLE regional_by_row;
Expand Down Expand Up @@ -1537,15 +1537,15 @@ regional_by_row CREATE TABLE public.regional_by_
query TT
SHOW ZONE CONFIGURATION FOR TABLE regional_by_row
----
TABLE regional_by_row ALTER TABLE regional_by_row CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 90000,
num_replicas = 5,
num_voters = 3,
constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
voter_constraints = '[+region=ca-central-1]',
lease_preferences = '[[+region=ca-central-1]]'
DATABASE alter_locality_test ALTER DATABASE alter_locality_test CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 90000,
num_replicas = 5,
num_voters = 3,
constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
voter_constraints = '[+region=ca-central-1]',
lease_preferences = '[[+region=ca-central-1]]'

statement ok
DROP TABLE regional_by_row;
Expand Down Expand Up @@ -1792,15 +1792,15 @@ regional_by_row_as CREATE TABLE public.regional_by_
query TT
SHOW ZONE CONFIGURATION FOR TABLE regional_by_row_as
----
TABLE regional_by_row_as ALTER TABLE regional_by_row_as CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 90000,
num_replicas = 5,
num_voters = 3,
constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
voter_constraints = '[+region=ca-central-1]',
lease_preferences = '[[+region=ca-central-1]]'
DATABASE alter_locality_test ALTER DATABASE alter_locality_test CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 90000,
num_replicas = 5,
num_voters = 3,
constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
voter_constraints = '[+region=ca-central-1]',
lease_preferences = '[[+region=ca-central-1]]'

statement ok
DROP TABLE regional_by_row_as;
Expand Down Expand Up @@ -1866,15 +1866,15 @@ regional_by_row_as CREATE TABLE public.regional_by_
query TT
SHOW ZONE CONFIGURATION FOR TABLE regional_by_row_as
----
TABLE regional_by_row_as ALTER TABLE regional_by_row_as CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 90000,
num_replicas = 5,
num_voters = 3,
constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
voter_constraints = '[+region=ca-central-1]',
lease_preferences = '[[+region=ca-central-1]]'
DATABASE alter_locality_test ALTER DATABASE alter_locality_test CONFIGURE ZONE USING
range_min_bytes = 134217728,
range_max_bytes = 536870912,
gc.ttlseconds = 90000,
num_replicas = 5,
num_voters = 3,
constraints = '{+region=ap-southeast-2: 1, +region=ca-central-1: 1, +region=us-east-1: 1}',
voter_constraints = '[+region=ca-central-1]',
lease_preferences = '[[+region=ca-central-1]]'

statement ok
DROP TABLE regional_by_row_as;
Expand Down
24 changes: 11 additions & 13 deletions pkg/sql/region_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,10 @@ func applyZoneConfigForMultiRegionTableOptionNewIndexes(
}
}

// isRegionalByRowPlaceholderZoneConfig returns whether a given zone config
// should be marked as a placeholder config if the zone config belongs to
// a REGIONAL BY ROW table.
// isPlaceholderZoneConfigForMultiRegion returns whether a given zone config
// should be marked as a placeholder config for a multi-region object.
// See zonepb.IsSubzonePlaceholder for why this is necessary.
func isRegionalByRowPlaceholderZoneConfig(zc zonepb.ZoneConfig) bool {
func isPlaceholderZoneConfigForMultiRegion(zc zonepb.ZoneConfig) bool {
// Strip Subzones / SubzoneSpans, as these may contain items if migrating
// from one REGIONAL BY ROW table to another.
strippedZC := zc
Expand All @@ -484,11 +483,6 @@ func applyZoneConfigForMultiRegionTableOptionTableNewConfig(
return false, zonepb.ZoneConfig{}, err
}
zc.CopyFromZone(*localityZoneConfig, multiRegionZoneConfigFields)
if newConfig.GetRegionalByRow() != nil {
if isRegionalByRowPlaceholderZoneConfig(zc) {
zc.NumReplicas = proto.Int32(0)
}
}
return false, zc, nil
}
}
Expand All @@ -513,10 +507,6 @@ var ApplyZoneConfigForMultiRegionTableOptionTableAndIndexes = func(

hasNewSubzones := table.IsLocalityRegionalByRow()
if hasNewSubzones {
if isRegionalByRowPlaceholderZoneConfig(zc) {
zc.NumReplicas = proto.Int32(0)
}

for _, region := range regionConfig.Regions {
subzoneConfig, err := zoneConfigForMultiRegionPartition(region, regionConfig)
if err != nil {
Expand Down Expand Up @@ -568,6 +558,14 @@ func ApplyZoneConfigForMultiRegionTable(
zoneConfig = newZoneConfig
}

// Mark the NumReplicas as 0 if we have subzones but no other features
// in the zone config. This signifies a placeholder.
// Note we do not use hasNewSubzones here as there may be existing subzones
// on the zone config which may still be a placeholder.
if len(zoneConfig.Subzones) > 0 && isPlaceholderZoneConfigForMultiRegion(zoneConfig) {
zoneConfig.NumReplicas = proto.Int32(0)
}

if !zoneConfig.Equal(zonepb.NewZoneConfig()) {
if err := zoneConfig.Validate(); err != nil {
return pgerror.Newf(
Expand Down

0 comments on commit 718034b

Please sign in to comment.