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: add validation to cascading zone configs #32861

Merged
merged 3 commits into from
Dec 12, 2018

Conversation

ridwanmsharif
Copy link

@ridwanmsharif ridwanmsharif commented Dec 5, 2018

I found a few hours free and so took a second stab at #32059. Thought it'd be fun and new to try adding something to SQL for this, so @knz please feel free to tell me if what I've done here makes no sense.

There are to be two changes to be made:

  1. Allow for the COPY FROM PARENT command so users can do something like:
    ALTER ... CONFIGURE ZONE USING range_min_bytes = ..., range_max_bytes = COPY FROM PARENT

  2. Add validation to the zone configs so certain fields are written in tandem. The fields are:

    • NumReplicas and Constraints (Only when per-replica constraints are in use)
    • RangeMinBytes and RangeMaxBytes
    • LeasePreferences and Constraints

Each of the changes are contained in their own commits.

@ridwanmsharif ridwanmsharif requested review from a team December 5, 2018 19:14
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

Code LGTM. Well done. A few comments below.

Reviewed 10 of 10 files at r1, 6 of 6 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained


pkg/cmd/roachtest/split.go, line 143 at r2 (raw file):

		setRangeMaxBytes := func(maxBytes int) {
			stmtZone := fmt.Sprintf(
				"ALTER RANGE default CONFIGURE ZONE USING range_max_bytes = %d, range_min_bytes = COPY FROM PARENT",

please check with the powers that be that this test code is only used with 2.2/master; if it is also used with 2.1 you'll need to backport the syntax change to 2.1 before you can merge the test update.


pkg/sql/set_zone_config.go, line 472 at r2 (raw file):

	// Per-replica constraints cannot be set unless num_replicas is explicitly set
	if err := zoneToWrite.ValidateTandemFields(); err != nil {
		return pgerror.NewErrorf(pgerror.CodeInternalError,

This is not an internal error. Internal errors are bugs in CockroachDB; also internal errors get uploaded to our telemetry data.

Here you are simply reporting an error to the client/user. You can use one of the other error codes instead.

Copy link
Contributor

@knz knz 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


pkg/sql/parser/sql.y, line 1416 at r1 (raw file):

var_set_list:
  var_name '=' COPY FROM PARENT

Also add the missing tests to parse_test.go.

Copy link
Author

@ridwanmsharif ridwanmsharif 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


pkg/cmd/roachtest/split.go, line 143 at r2 (raw file):

Previously, knz (kena) wrote…

please check with the powers that be that this test code is only used with 2.2/master; if it is also used with 2.1 you'll need to backport the syntax change to 2.1 before you can merge the test update.

This test uses 2.2 but good note, changes the other one to use the min bytes explicitly.


pkg/sql/set_zone_config.go, line 472 at r2 (raw file):

Previously, knz (kena) wrote…

This is not an internal error. Internal errors are bugs in CockroachDB; also internal errors get uploaded to our telemetry data.

Here you are simply reporting an error to the client/user. You can use one of the other error codes instead.

Done.


pkg/sql/parser/sql.y, line 1416 at r1 (raw file):

Previously, knz (kena) wrote…

Also add the missing tests to parse_test.go.

Done.

@benesch
Copy link
Contributor

benesch commented Dec 6, 2018

Approach looks fantastic to me! If @knz is happy, I'm happy.

ctx.FormatNode(&node.Options)
kvOptions := node.Options
for i, kv := range kvOptions {
if i > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

comma := ""
for ... {
   WriteString(comma)
   ...
   comma = ", "
}

if optionStr.Len() > 0 {
optionStr.WriteString(", ")
}
fmt.Fprintf(&optionStr, "%s = COPY FROM PARENT", string(*name))
Copy link
Contributor

Choose a reason for hiding this comment

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

wait what no you should not convert the name to a string!!

it's fmt("%s = ...", name)

Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

Reviewed 7 of 7 files at r3, 5 of 5 files at r4, 1 of 3 files at r5, 2 of 2 files at r6.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained


pkg/sql/set_zone_config.go, line 473 at r3 (raw file):

	if err := zoneToWrite.ValidateTandemFields(); err != nil {
		return pgerror.NewErrorf(pgerror.CodeInvalidParameterValueError,
			fmt.Sprintf("could not validate zone config: %s", err)).SetHintf(
  1. we use %v for errors not %s

  2. NewErrorf means you can use formatting -- so it's pgerror.NewErrorf(code, "foo %v", err) not NewErrorf(code, fmt.Sprintf(...))


pkg/sql/set_zone_config.go, line 134 at r6 (raw file):

		for _, opt := range n.Options {
			if _, alreadyExists := options[opt.Key]; alreadyExists {
				return nil, fmt.Errorf("duplicate zone config parameter: %q", tree.ErrString(&opt.Key))

while you're at it please convert the errors around here to pgerrors


pkg/sql/set_zone_config.go, line 216 at r6 (raw file):

					optionStr.WriteString(", ")
				}
				fmt.Fprintf(&optionStr, "%s = COPY FROM PARENT", *name)

pass name not *name, they are not the same.


pkg/sql/set_zone_config.go, line 224 at r6 (raw file):

			}
			if datum == tree.DNull {
				return fmt.Errorf("unsupported NULL value for %q", tree.ErrString(name))

ditto pgerror


pkg/sql/set_zone_config.go, line 231 at r6 (raw file):

				optionStr.WriteString(", ")
			}
			fmt.Fprintf(&optionStr, "%s = %s", *name, datum)

ditto name vs *name

Ridwan Sharif added 3 commits December 12, 2018 10:02
Adds a `COPY FROM PARENT` command to zone configs to
solidify a field. Solidifying a field means, the field will
now be explicitly set to the value that applied to it.
Any change to a parent zone on the field will no longer
affect it.
This change will allow people to do the following:

`ALTER .... CONFIGURE ZONE USING range_min_bytes = ...,
range_max_bytes = COPY FROM PARENT` instead of figuring
out what the `RangeMaxBytes`should be and populating it manually.
This change will allow us to add a validation constraint
that tandem fields are set together (for more details see
the cascading zone config RFC).

Release note (sql change): New SQL command added.
`ALTER ... CONFIGURE ZONE USING field_name = COPY FROM PARENT`
now solidifies/explicitly sets the field.
As described in the cascading zone RFC, a few additional
constraints are required on zone config usage so no zone
is ever in an invalid state if their parent zone changes
a value.

Release note: None
The formatter is updated to now recognise the COPY FROM PARENT
command when the value in the KVOption is nil.

Release note: None
Copy link
Author

@ridwanmsharif ridwanmsharif left a comment

Choose a reason for hiding this comment

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

TFTR!

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


pkg/sql/set_zone_config.go, line 473 at r3 (raw file):

Previously, knz (kena) wrote…
  1. we use %v for errors not %s

  2. NewErrorf means you can use formatting -- so it's pgerror.NewErrorf(code, "foo %v", err) not NewErrorf(code, fmt.Sprintf(...))

Done.


pkg/sql/set_zone_config.go, line 134 at r6 (raw file):

Previously, knz (kena) wrote…

while you're at it please convert the errors around here to pgerrors

Done.


pkg/sql/set_zone_config.go, line 216 at r6 (raw file):

Previously, knz (kena) wrote…

pass name not *name, they are not the same.

Done.


pkg/sql/set_zone_config.go, line 224 at r6 (raw file):

Previously, knz (kena) wrote…

ditto pgerror

Done.


pkg/sql/set_zone_config.go, line 231 at r6 (raw file):

Previously, knz (kena) wrote…

ditto name vs *name

Done.

Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 6 of 9 files at r7, 4 of 5 files at r8, 3 of 3 files at r9.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained

@ridwanmsharif
Copy link
Author

bors r+

craig bot pushed a commit that referenced this pull request Dec 12, 2018
32861: sql: add validation to cascading zone configs r=ridwanmsharif a=ridwanmsharif

I found a few hours free and so took a second stab at #32059. Thought it'd be fun and new to try adding something to SQL for this, so @knz please feel free to tell me if what I've done here makes no sense. 

There are to be two changes to be made: 
1) Allow for the `COPY FROM PARENT` command so users can do something like:
` ALTER ... CONFIGURE ZONE USING range_min_bytes = ..., range_max_bytes = COPY FROM PARENT`

2) Add validation to the zone configs so certain fields are written in tandem. The fields are:
      - `NumReplicas` and `Constraints` (Only when per-replica constraints are in use)
      - `RangeMinBytes` and `RangeMaxBytes`
      - `LeasePreferences` and `Constraints`

Each of the changes are contained in their own commits.

Co-authored-by: Ridwan Sharif <ridwan@cockroachlabs.com>
@craig
Copy link
Contributor

craig bot commented Dec 12, 2018

Build succeeded

@craig craig bot merged commit 733cfb7 into cockroachdb:master Dec 12, 2018
@ridwanmsharif ridwanmsharif deleted the zone-validation-2 branch December 12, 2018 21:55
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.

None yet

4 participants