From 718034b150732cf3fc99e9c6c5b2bff0c3efc733 Mon Sep 17 00:00:00 2001 From: Oliver Tan Date: Fri, 19 Feb 2021 06:30:04 +1100 Subject: [PATCH] sql: fix dangling zone config on REGIONAL BY ROW -> REGIONAL BY TABLE 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 --- .../testdata/logic_test/alter_table_locality | 72 +++++++++---------- pkg/sql/region_util.go | 24 +++---- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/pkg/ccl/logictestccl/testdata/logic_test/alter_table_locality b/pkg/ccl/logictestccl/testdata/logic_test/alter_table_locality index 02cd9db3f722..8266714eaf41 100644 --- a/pkg/ccl/logictestccl/testdata/logic_test/alter_table_locality +++ b/pkg/ccl/logictestccl/testdata/logic_test/alter_table_locality @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/pkg/sql/region_util.go b/pkg/sql/region_util.go index bd68ccf20d53..2a27063ee4a7 100644 --- a/pkg/sql/region_util.go +++ b/pkg/sql/region_util.go @@ -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 @@ -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 } } @@ -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 { @@ -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(