Skip to content

sql/multiregion: Add basic validation for zone config extension setting#86538

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
ZhouXing19:validate-zone-config-ext
Sep 6, 2022
Merged

sql/multiregion: Add basic validation for zone config extension setting#86538
craig[bot] merged 1 commit intocockroachdb:masterfrom
ZhouXing19:validate-zone-config-ext

Conversation

@ZhouXing19
Copy link
Copy Markdown
Collaborator

We added constraints on setting the zone config extension so that it won't break
the original region configs (such as home region and survival goal requirement).

We didn't exhaust all rules but added the following:

  1. REGIONAL extensions don't set lease_preferences
  2. REGIONAL IN extensions don't set lease_preferences that disagree with their home
    region. I.e. After the extension gets applied, the home region is unchanged.
  3. REGIONAL IN extensions don't set replica constraints or voter contains that disagree
    with their home region.
  4. Extensions can't set the number of replicas and voters lower than the ones
    required by the database's survival goal.

Note that the validation for zone config extension is different from other
zone config settings in that it will influence the derived zone config for not
just the database, but also the tables and partitions under it. So we need to
ensure the validation pass for all these schemas before truly write the zone
config updates.

fixes #85015

Release justification: important validation for a multiregion funtionality.
Release note: add basic validation for zone config extension setting

@ZhouXing19 ZhouXing19 requested a review from a team August 22, 2022 02:19
@ZhouXing19 ZhouXing19 requested a review from a team as a code owner August 22, 2022 02:19
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@ZhouXing19 ZhouXing19 changed the title sql/multiretion: Add basic validation for zone config extension setting sql/multiregion: Add basic validation for zone config extension setting Aug 22, 2022
Copy link
Copy Markdown
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering, should some of this validation go into validateMultiRegion in pkg/sql/catalog/dbdesc/database_desc.go. this is validation that's lower-level at the descriptor level, so it prevents any other change to the descriptor from doing something invalid.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained

Copy link
Copy Markdown
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we might want the validation in both places. the ones in this PR should show user-friendly errors, and the ones in validateMultiRegion should show assertion errors.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained

Copy link
Copy Markdown
Collaborator Author

@ZhouXing19 ZhouXing19 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's necessary to add the validation in the descriptor level, as the validation we have here are mostly for the zone config itself. The logic here is similar to the existing validations for the set zone config logic, which IIUC are mostly done in the sql package rather than the descriptor level (example).

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained

@ZhouXing19 ZhouXing19 force-pushed the validate-zone-config-ext branch from 41a20e0 to 3397fa2 Compare August 31, 2022 05:24
@ZhouXing19 ZhouXing19 requested review from a team as code owners August 31, 2022 05:24
@ZhouXing19 ZhouXing19 force-pushed the validate-zone-config-ext branch from 3397fa2 to f780d6d Compare August 31, 2022 05:27
@ZhouXing19
Copy link
Copy Markdown
Collaborator Author

For the requirement that Extensions can't break a database's survivability goal, I'm not sure how to ensure that, so I translate it into Extensions can't set the number of replicas and voters lower than the ones required by the database's survival goal. Not sure if that makes sense or is too limited.

@ZhouXing19 ZhouXing19 requested review from nvb and rafiss August 31, 2022 15:59
@rafiss rafiss requested a review from RichardJCai September 1, 2022 15:24
@rafiss
Copy link
Copy Markdown
Collaborator

rafiss commented Sep 1, 2022

this lgtm, but i could also use an opinion from @RichardJCai

Copy link
Copy Markdown
Contributor

@RichardJCai RichardJCai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM good checks. Just some nits

// tables in it, we now follow these steps:
// 1. Generate and Validate the newly created zone config for the database.
// 2. For each table under this database, validate the newly derived zone
// config. After the validation passed for all table, we are now sure that the
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: all tables

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// replicas. We may want to consider adding constraint to their distribution
// across zones/regions as well.
if zc.NumVoters != nil && *zc.NumVoters < numVoters {
return zonepb.ZoneConfig{}, errors.Newf("zone config extension "+
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: cannot set num_voters lower than ...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
if zc.NumReplicas != nil && *zc.NumReplicas < numReplicas {
return zonepb.ZoneConfig{}, errors.Newf("zone config extension "+
"cannot set a replica number %v that is lower than the one required for the "+
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

num_replicas just to be explicit about the setting name

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

oriHomeRegionLeasePreference, idx1 := getHomeRegionLeasePreference(zc.LeasePreferences, homeRegion)
extHomeRegionLeasePreference, idx2 := getHomeRegionLeasePreference(ext.LeasePreferences, homeRegion)
if idx1 != -1 {
// If can' find a lease preference for the home region in the zone config
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if we can't

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// field.
// Lease preferences.
if !(ext.ShouldInheritLeasePreferences(&zc)) && len(zc.LeasePreferences) > 0 && len(ext.LeasePreferences) > 0 {
oriHomeRegionLeasePreference, idx1 := getHomeRegionLeasePreference(zc.LeasePreferences, homeRegion)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather we just use originalHomeRegionLeasePreference took me a second to understand what ori meant

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if !(ext.ShouldInheritConstraints(&zc)) && len(zc.Constraints) > 0 && len(ext.Constraints) > 0 {
oriHomeRegionReplicaConstraint, ok1 := getHomeRegionConstraintConjunction(zc.Constraints, homeRegion)
extHomeRegionReplicaConstraint, ok2 := getHomeRegionConstraintConjunction(ext.Constraints, homeRegion)
if ok1 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little bit hard to read.

Can we do if ok1 && !ok2 { ... } and split out the logic below
Also probably better to do name ok1 as homeRegionConstraintExistsForOldZC and ok2 as homeRegionConstraintExistsForExt as something like, I prefer verbosity here to make it clear.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to return the reference of the constraint, so the second return value is not very much needed.

@ZhouXing19 ZhouXing19 force-pushed the validate-zone-config-ext branch from f780d6d to 70d0ac7 Compare September 1, 2022 21:38
Copy link
Copy Markdown
Contributor

@RichardJCai RichardJCai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @nvanbenschoten, @rafiss, @RichardJCai, and @ZhouXing19)


pkg/sql/catalog/multiregion/region_config.go line 527 at r3 (raw file):

// If we found the constraint for the region, we return the corresponding
// ConstraintsConjunction and "true" as the second boolean returned value.
// If not found in the list, it returns an empty lease preference and "false".

This comment is out of date now

We added constraints on setting the zone config extension so that it won't break
the original region configs (such as home region and survival goal requirement).

We didn't exhaust all rules but added the following:

1. REGIONAL extensions don't set lease_preferences
2. REGIONAL IN extensions don't set lease_preferences that disagree with their home
region. I.e. After the extension gets applied, the home region is unchanged.
3. REGIONAL IN extensions don't set replica constraints or voter contains that disagree
with their home region.
4. Extensions can't set the number of replicas and voters lower than the ones
required by the database's survival goal.

Note that the validation for zone config extension is different from other
zone config settings in that it will influence the derived zone config for not
just the database, but also the tables and partitions under it. So we need to
ensure the validation pass for all these schemas before truly write the zone
config updates.

fixes cockroachdb#85015

Release justification: important validation for a multiregion funtionality.
Release note: add basic validation for zone config extension setting
@ZhouXing19 ZhouXing19 force-pushed the validate-zone-config-ext branch from 70d0ac7 to 292736e Compare September 5, 2022 22:00
Copy link
Copy Markdown
Collaborator Author

@ZhouXing19 ZhouXing19 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @nvanbenschoten, @rafiss, and @RichardJCai)


pkg/sql/catalog/multiregion/region_config.go line 527 at r3 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

This comment is out of date now

Done.

@ZhouXing19
Copy link
Copy Markdown
Collaborator Author

TFTR!
bors r=RichardJCai

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Sep 6, 2022

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

multiregion: add validation for zone config extension

4 participants