-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
executor: fix drop user bug #6624
Conversation
zhexuany
commented
May 23, 2018
•
edited
Loading
edited
After drop user, mysql.db does not have any row related with user which was intended to drop but tidb has such row. This PR fixes this. |
executor/simple.go
Outdated
} else { | ||
sql = fmt.Sprintf(`DELETE FROM %s.%s WHERE Host = "%s" and User = "%s";`, mysql.SystemDB, mysql.DBTable, user.Hostname, user.Username) | ||
} | ||
sql = fmt.Sprintf(`DELETE FROM %s.%s WHERE Host = "%s" and User = "%s";`, mysql.SystemDB, mysql.DBTable, user.Hostname, user.Username) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this line?
7a806fc
to
0af59be
Compare
executor/simple.go
Outdated
@@ -232,6 +232,13 @@ func (e *SimpleExec) executeDropUser(s *ast.DropUserStmt) error { | |||
} | |||
sql := fmt.Sprintf(`DELETE FROM %s.%s WHERE Host = "%s" and User = "%s";`, mysql.SystemDB, mysql.UserTable, user.Hostname, user.Username) | |||
_, _, err = e.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(e.ctx, sql) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This err will be overwritten.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address comment, reset LGTM
/run-all-tests |
@zhexuany We need to clean up data in the tables_priv and columns_priv. /cc @tiancaiamao |
@tiancaiamao PTAL |
executor/simple.go
Outdated
@@ -235,6 +235,22 @@ func (e *SimpleExec) executeDropUser(s *ast.DropUserStmt) error { | |||
if err != nil { | |||
failedUsers = append(failedUsers, user.String()) | |||
} | |||
|
|||
// delete privileges from mysql.db | |||
sql = fmt.Sprintf(`DELETE FROM %s.%s WHERE Host = "%s" and User = "%s";`, mysql.SystemDB, mysql.DBTable, user.Hostname, user.Username) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about use a transaction to run the three delete statements? Or it will leave some grabege when meet something wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test MySQL's behavior:
begin
insert into t value (1)
drop user tidb
rollback
select * from t
_, _, err = e.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(e.ctx, sql) | ||
if err != nil { | ||
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), sql); err != nil { | ||
failedUsers = append(failedUsers, user.String()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unlikely to meet a error here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? For example all the tikv nodes are down.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before commit, all the operation is cached in union store @shenli
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete statement need to get data from tikv first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the err is not nil, we should return error.
executor/simple.go
Outdated
|
||
//TODO: (tidb-team) need delete columns_priv once we implement columns_priv functionality. | ||
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), "commit"); err != nil { | ||
return errors.Trace(err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
failedUsers = append(failedUsers, user.String())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha.
executor/simple.go
Outdated
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
// err := e.ctx.Txn().Commit(sessionctx.SetCommitCtx(context.Background(), e.ctx)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clean up the code
@shenli @tiancaiamao PTAL |
executor/simple.go
Outdated
|
||
// begin a transaction to delete a user. | ||
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), "begin"); err != nil { | ||
fmt.Println(err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove debug log
_, _, err = e.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(e.ctx, sql) | ||
if err != nil { | ||
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), sql); err != nil { | ||
failedUsers = append(failedUsers, user.String()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before commit, all the operation is cached in union store @shenli
Will MySQL |
I will be auto committed. @tiancaiamao |
@shenli @tiancaiamao PTAL again. |
64c3cd0
to
89e5973
Compare
LGTM |
|
||
// delete privileges from mysql.db | ||
sql = fmt.Sprintf(`DELETE FROM %s.%s WHERE Host = "%s" and User = "%s";`, mysql.SystemDB, mysql.DBTable, user.Hostname, user.Username) | ||
if _, err := e.ctx.(sqlexec.SQLExecutor).Execute(context.Background(), sql); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
@shenli PTAL |
executor/simple.go
Outdated
failedUsers = append(failedUsers, user.String()) | ||
} | ||
|
||
//TODO: (tidb-team) need delete columns_priv once we implement columns_priv functionality. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to add "(tidb-team)" in todo
@zz-jason PTAL again. |
err := e.ctx.Txn().Commit(sessionctx.SetCommitCtx(context.Background(), e.ctx)) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
errMsg := "Operation DROP USER failed for " + strings.Join(failedUsers, ",") | ||
return terror.ClassExecutor.New(CodeCannotUser, errMsg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to execute rollback
when len(failedUsers) > 0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need. I just update accordingly.
"localhost test testDB1 Y Y Y Y Y Y Y N Y Y N N N N N N Y N N] [% dddb_% dduser Y Y Y Y Y Y Y N Y Y N N N N N N Y N N", | ||
"% test test Y N N N N N N N N N N N N N N N N N N", | ||
"localhost test testDBRevoke N N N N N N N N N N N N N N N N N N N", | ||
)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we need another test like dropping a user which not exists?
LGTM |
21f4494
to
5cbbc29
Compare
d3b5b93
to
1f05eef
Compare
/run-all-test |
executor/simple_test.go
Outdated
|
||
createUserSQL = `CREATE USER 'test1'@'localhost'` | ||
createUserSQL = `CREATE USER 'test2'@'localhost'` | ||
tk.Exec(createUserSQL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use MustExec
?
c29daae
to
ad10072
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM