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

sessionctx, executor: Add correctness check when set system variables #7117

Merged
merged 19 commits into from
Jul 24, 2018

Conversation

spongedu
Copy link
Contributor

What have you changed? (mandatory)

Add correctness check when set system variables

What is the type of the changes? (mandatory)

  • Improvement (non-breaking change which is an improvement to an existing feature)

How has this PR been tested? (mandatory)

Unittest

@shenli
Copy link
Member

shenli commented Jul 22, 2018

@spongedu Thanks! Is it the same behaviour with MySQL?

@shenli
Copy link
Member

shenli commented Jul 22, 2018

/run-all-tests

@shenli shenli added the contribution This PR is from a community contributor. label Jul 22, 2018
@spongedu
Copy link
Contributor Author

@shenli Yes,this pr add the same type & value check as MySQL do.
Not all system variables are checked because there are too much of them, but after this PR we can fix the others concurrently.
Currently, if we give wrong value to TiDB specific system variable, a default value will be set and no warning or error is raised. I suggest that we deal with TiDB specific system variable the same way as MySQL system variable, that would be less confusing if the user use wrong value.

@winkyao
Copy link
Contributor

winkyao commented Jul 23, 2018

Please fix ci.

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

Great job~
Does all the mysql system vars has been tested in MySQL5.7?

@@ -156,6 +165,181 @@ func ValidateGetSystemVar(name string, isGlobal bool) error {
return nil
}

// ValidateSetSystemVar checks if system variable satisfies specific restriction.
func ValidateSetSystemVar(name string, value string) (string, error, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We may not need to return warn?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, If we take the SessionVars as a parameter into function ValidateSetSystemVar, we can directly append warning in this function. I'll fix this

@XuHuaiyu XuHuaiyu added this to the 2.1 milestone Jul 23, 2018
if val := GetSysVar(name); val != nil {
return val.Value, nil, nil
}
return value, nil, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Any case cover this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In set_test.go, line 93 - line 97 covers this:

	tk.MustQuery(`select @@global.low_priority_updates;`).Check(testkit.Rows("0"))
	tk.MustExec(`set @@global.low_priority_updates="ON";`)
	tk.MustQuery(`select @@global.low_priority_updates;`).Check(testkit.Rows("1"))
	tk.MustExec(`set @@global.low_priority_updates=DEFAULT;`) // It will be set to compiled-in default value.
	tk.MustQuery(`select @@global.low_priority_updates;`).Check(testkit.Rows("0"))
	

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, this case covers line 172, but seems there is no case that covers line 174?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems so. I'll add some related tests..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@XuHuaiyu the condition that we fail to GetSysVar should never happen. We'd better panic here ?

@spongedu
Copy link
Contributor Author

@XuHuaiyu Yes, these checks are according to https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#

return val.Value, nil
}
// should never happen
panic(fmt.Sprintf("Error happened when ValidateSetSystemVar. Invalid system variable: %s", name))
Copy link
Contributor

Choose a reason for hiding this comment

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

Will it be more friendly if we return UnknownSystemVar.GenByArgs(name) here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

Addressing the comment will make a LGTM ^_^

@@ -156,6 +162,197 @@ func ValidateGetSystemVar(name string, isGlobal bool) error {
return nil
}

// ValidateSetSystemVar checks if system variable satisfies specific restriction.
func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string, error) {
if strings.EqualFold(value, "DEFAULT") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we need to consider System? Currently, set time_zone='System' does not work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zhexuany it's not a common check, I think we'd better by refine parseTimeZone in varsutil.go to :

func parseTimeZone(s string) (*time.Location, error) {
	if strings.EqualFold(s, "SYSTEM") {
	.......
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that we should validate the result format in ValidateSetSystemVar. Only modify parseTimeZone is not enough. I'll fix here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tidb> set time_zone='System';                                                                                                                                               Query OK, 0 rows affected (0.00 sec)

tidb> select @@time_zone;
+-------------+
| @@time_zone |
+-------------+
| System      |
+-------------+
1 row in set (0.00 sec)

In MySQL:

mysql> set time_zone='System';                                                                                                                                              Query OK, 0 rows affected (0.00 sec)

mysql> select @@time_zone;                                                                                                                                                  
+-------------+
| @@time_zone |
+-------------+
| SYSTEM      |
+-------------+
1 row in set (0.01 sec)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zhexuany Done here. PTAL

@shenli
Copy link
Member

shenli commented Jul 24, 2018

@zhexuany @XuHuaiyu PTAL

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@zhexuany zhexuany left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor. type/compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants