Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
78044: sql: introduce multi-region zone config extension descriptor changes r=nvanbenschoten a=nvanbenschoten

This PR introduces lightweight zone configuration extensions, which provide flexibility to [multi-region abstractions](https://www.cockroachlabs.com/docs/stable/multiregion-overview.html) and are loosely based on the design described in [this doc](https://docs.google.com/document/d/1EiAT1BUOTaXoJy3CMzrKiP73bMfOAFiPrOPne5dtadU/edit#).

```sql
ALTER DATABASE db ALTER LOCALITY REGIONAL CONFIGURE ZONE USING ...

ALTER DATABASE db ALTER LOCALITY REGIONAL IN "us-east1" CONFIGURE ZONE USING ...

ALTER DATABASE db ALTER LOCALITY GLOBAL CONFIGURE ZONE USING ...
```

Zone config extensions represents per-locality zone configurations that influence the zone configurations derived for corresponding objects. A locality type's associated extension acts as a targeted set of rewrite rules for its associated objects' (database, table, partition) derived zone configurations.

"Extending" a zone config means having the extension inherit any missing fields from the zone config while replacing any set fields. This uses the existing zone configuration inheritance rules.

### Rules (implemented in pkg/sql/region_util.go):
```
1. database-level zone configs are extended first using the regional zone
   config extension and then with the regional_in[<primary_region>] zone
   config extension. In doing so, REGIONAL [ IN PRIMARY REGION ] tables can
   continue to inherit from the database level zone config.
2. table-level zone configs use the extension associated with their locality
   type.
 2a. GLOBAL tables are extended with the global zone config extension.
 2b. REGIONAL IN <region> tables are extended first using the regional zone
     config extension and then with the regional_in[<affinity_region>] zone
     config extension.
3. partition-level zone configs are extended first using the regional zone
   config extension and then with the regional_in[<partition_region>] zone
   config extension.
```

The PR contains only the descriptor changes and the logic to interpret zone config extensions during multi-region zone config synthesis. It does not contain the logic to set the extensions. As a result, it may be contained enough to fit into the v22.1.0 release, allowing the rest of the code to land in a later point release.

cc. `@ajstorm` `@awoods187` 

Co-authored-by: Nathan VanBenschoten <[email protected]>
  • Loading branch information
craig[bot] and nvanbenschoten committed Apr 9, 2022
2 parents ea4c75e + 918b22b commit 5d94c57
Show file tree
Hide file tree
Showing 15 changed files with 2,019 additions and 731 deletions.
7 changes: 6 additions & 1 deletion pkg/ccl/backupccl/restore_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,11 @@ func createImportingDescriptors(
if err != nil {
return err
}
superRegions, err := t.SuperRegions()
superRegions, err := typeDesc.SuperRegions()
if err != nil {
return err
}
zoneCfgExtensions, err := typeDesc.ZoneConfigExtensions()
if err != nil {
return err
}
Expand All @@ -886,6 +890,7 @@ func createImportingDescriptors(
desc.RegionConfig.RegionEnumID,
desc.RegionConfig.Placement,
superRegions,
zoneCfgExtensions,
)
if err := sql.ApplyZoneConfigFromDatabaseRegionConfig(
ctx,
Expand Down
1 change: 1 addition & 0 deletions pkg/ccl/backupccl/restore_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,7 @@ func planDatabaseModifiersForRestore(
regionEnumID,
descpb.DataPlacement_DEFAULT,
nil,
descpb.ZoneConfigExtensions{},
)
if err := multiregion.ValidateRegionConfig(regionConfig); err != nil {
return nil, nil, err
Expand Down
1 change: 1 addition & 0 deletions pkg/ccl/multiregionccl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
"//pkg/sql",
"//pkg/sql/catalog/catpb",
"//pkg/sql/catalog/descidgen",
"//pkg/sql/catalog/descpb",
"//pkg/sql/catalog/multiregion",
"//pkg/sql/catalog/typedesc",
"//pkg/sql/pgwire/pgcode",
Expand Down
9 changes: 8 additions & 1 deletion pkg/ccl/multiregionccl/multiregion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/multiregion"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
Expand Down Expand Up @@ -97,7 +98,13 @@ func initializeMultiRegionMetadata(
return nil, err
}
regionConfig := multiregion.MakeRegionConfig(
regionNames, primaryRegion, survivalGoal, regionEnumID, placement, nil,
regionNames,
primaryRegion,
survivalGoal,
regionEnumID,
placement,
nil,
descpb.ZoneConfigExtensions{},
)
if err := multiregion.ValidateRegionConfig(regionConfig); err != nil {
return nil, err
Expand Down
8 changes: 5 additions & 3 deletions pkg/ccl/schemachangerccl/testdata/end_to_end/drop_multiregion
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ upsert descriptor #105
- - 108
regionConfig:
primaryRegion: us-east1
zoneConfigExtensions: {}
- version: "2"
+ version: "3"
upsert descriptor #107
Expand Down Expand Up @@ -379,8 +380,8 @@ upsert descriptor #105
enumMembers:
- logicalRepresentation: us-east1
...
regionConfig:
primaryRegion: us-east1
zoneConfigExtensions: {}
- version: "3"
+ version: "4"
upsert descriptor #107
Expand Down Expand Up @@ -738,6 +739,7 @@ upsert descriptor #105
- - 109
regionConfig:
primaryRegion: us-east1
zoneConfigExtensions: {}
- version: "5"
+ version: "6"
upsert descriptor #109
Expand Down Expand Up @@ -968,8 +970,8 @@ upsert descriptor #105
enumMembers:
- logicalRepresentation: us-east1
...
regionConfig:
primaryRegion: us-east1
zoneConfigExtensions: {}
- version: "6"
+ version: "7"
upsert descriptor #109
Expand Down Expand Up @@ -1407,8 +1409,8 @@ upsert descriptor #105
enumMembers:
- logicalRepresentation: us-east1
...
regionConfig:
primaryRegion: us-east1
zoneConfigExtensions: {}
- version: "7"
+ state: DROP
+ version: "8"
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/catalog/descpb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ proto_library(
strip_import_prefix = "/pkg",
visibility = ["//visibility:public"],
deps = [
"//pkg/config/zonepb:zonepb_proto",
"//pkg/geo/geoindex:geoindex_proto",
"//pkg/roachpb:roachpb_proto",
"//pkg/sql/catalog/catpb:catpb_proto",
Expand All @@ -76,6 +77,7 @@ go_proto_library(
proto = ":descpb_proto",
visibility = ["//visibility:public"],
deps = [
"//pkg/config/zonepb",
"//pkg/geo/geoindex",
"//pkg/roachpb", # keep
"//pkg/sql/catalog/catpb",
Expand Down
43 changes: 43 additions & 0 deletions pkg/sql/catalog/descpb/structured.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ syntax = "proto2";
package cockroach.sql.sqlbase;
option go_package = "descpb";

import "config/zonepb/zone.proto";
import "util/hlc/timestamp.proto";
import "sql/catalog/catpb/catalog.proto";
import "sql/catalog/catpb/privilege.proto";
Expand Down Expand Up @@ -1306,6 +1307,44 @@ message SuperRegion {
repeated string regions = 2 [(gogoproto.casttype)="github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb.RegionName"];
}

// ZoneConfigExtensions represents per-locality zone configurations that
// influence the zone configurations derived for corresponding objects. A
// locality type's associated extension acts as a targeted set of rewrite rules
// for its associated objects' (database, table, partition) derived zone
// configurations.
//
// "extending" a zone config means having the extension inherit any missing
// fields from the zone config while replacing any set fields. This uses the
// existing zone configuration inheritance rules.
//
// Rules (implemented in pkg/sql/region_util.go):
// 1. database-level zone configs are extended first using the regional zone
// config extension and then with the regional_in[<primary_region>] zone
// config extension. In doing so, REGIONAL [ IN PRIMARY REGION ] tables can
// continue to inherit from the database level zone config.
// 2. table-level zone configs use the extension associated with their locality
// type.
// 2a. GLOBAL tables are extended with the global zone config extension.
// 2b. REGIONAL IN <region> tables are extended first using the regional zone
// config extension and then with the regional_in[<affinity_region>] zone
// config extension.
// 3. partition-level zone configs are extended first using the regional zone
// config extension and then with the regional_in[<partition_region>] zone
// config extension.
message ZoneConfigExtensions {
option (gogoproto.equal) = true;

// Global extends the zone config applied to GLOBAL tables.
optional config.zonepb.ZoneConfig global = 1;
// Regional extends the zone config applied to REGIONAL [ BY ROW ] tables and
// partitions, regardless of affinity region.
optional config.zonepb.ZoneConfig regional = 2;
// RegionalIn further extends the zone configs applied to REGIONAL [ BY ROW ]
// tables and partitions, according to affinity region.
map<string, config.zonepb.ZoneConfig> regional_in = 3 [(gogoproto.nullable) = false,
(gogoproto.castkey)="github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb.RegionName"];
}

// TypeDescriptor represents a user defined type and is stored in a structured
// metadata key. The TypeDescriptor has a globally-unique ID shared with other
// Descriptors.
Expand Down Expand Up @@ -1425,6 +1464,10 @@ message TypeDescriptor {

// SuperRegions represents the super regions defined on the database.
repeated SuperRegion super_regions = 2 [(gogoproto.nullable) = false];

// ZoneConfigExtensions represents per-locality zone configurations that
// influence the zone configurations derived for corresponding objects.
optional ZoneConfigExtensions zone_config_extensions = 3 [(gogoproto.nullable) = false];
}

optional RegionConfig region_config = 16;
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/catalog/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,9 @@ type TypeDescriptor interface {
TransitioningRegionNames() (catpb.RegionNames, error)
// SuperRegions returns the list of super regions.
SuperRegions() ([]descpb.SuperRegion, error)
// ZoneConfigExtensions returns the zone configuration extensions on the
// multi-region enum.
ZoneConfigExtensions() (descpb.ZoneConfigExtensions, error)

// The following fields are set if the type is an enum or a multi-region enum.

Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/catalog/multiregion/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/multiregion",
visibility = ["//visibility:public"],
deps = [
"//pkg/config/zonepb",
"//pkg/sql/catalog",
"//pkg/sql/catalog/catpb",
"//pkg/sql/catalog/descpb",
Expand All @@ -24,10 +25,12 @@ go_test(
srcs = ["region_config_test.go"],
deps = [
":multiregion",
"//pkg/config/zonepb",
"//pkg/sql/catalog/catpb",
"//pkg/sql/catalog/descpb",
"//pkg/testutils",
"//pkg/util/leaktest",
"@com_github_stretchr_testify//require",
"@org_golang_google_protobuf//proto",
],
)
Loading

0 comments on commit 5d94c57

Please sign in to comment.