-
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
ddl: migrate part of ddl package code from Execute/ExecRestricted to safe API (1) #22670
Merged
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
017c69c
migrate part of ddl package code from Execute/ExecRestricted to safe API
AilinKid d227711
Merge branch 'master' into fix_ddl_package
AilinKid d768063
address comment
AilinKid 3d25f86
Merge branch 'master' into fix_ddl_package
AilinKid 0f19818
Merge branch 'master' into fix_ddl_package
AilinKid ba73205
make fmt
AilinKid 29a6f68
.
AilinKid fad310d
restore the old api in the test
AilinKid c1f8106
Merge branch 'master' into fix_ddl_package
AilinKid 7a8f1ff
address comment
AilinKid a863e4f
Merge branch 'master' into fix_ddl_package
AilinKid f3b9380
.
AilinKid cef2347
.
AilinKid 3a680c4
Merge branch 'master' into fix_ddl_package
AilinKid 1d5c0c1
Merge branch 'master' into fix_ddl_package
AilinKid f8a6282
Merge branch 'master' into fix_ddl_package
AilinKid 65dc56f
Merge branch 'master' into fix_ddl_package
AilinKid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,12 +32,12 @@ import ( | |
const ( | ||
deleteRangesTable = `gc_delete_range` | ||
doneDeleteRangesTable = `gc_delete_range_done` | ||
loadDeleteRangeSQL = `SELECT HIGH_PRIORITY job_id, element_id, start_key, end_key FROM mysql.%s WHERE ts < %v` | ||
recordDoneDeletedRangeSQL = `INSERT IGNORE INTO mysql.gc_delete_range_done SELECT * FROM mysql.gc_delete_range WHERE job_id = %d AND element_id = %d` | ||
completeDeleteRangeSQL = `DELETE FROM mysql.gc_delete_range WHERE job_id = %d AND element_id = %d` | ||
completeDeleteMultiRangesSQL = `DELETE FROM mysql.gc_delete_range WHERE job_id = %d AND element_id in (%v)` | ||
updateDeleteRangeSQL = `UPDATE mysql.gc_delete_range SET start_key = "%s" WHERE job_id = %d AND element_id = %d AND start_key = "%s"` | ||
deleteDoneRecordSQL = `DELETE FROM mysql.gc_delete_range_done WHERE job_id = %d AND element_id = %d` | ||
loadDeleteRangeSQL = `SELECT HIGH_PRIORITY job_id, element_id, start_key, end_key FROM mysql.%n WHERE ts < %?` | ||
recordDoneDeletedRangeSQL = `INSERT IGNORE INTO mysql.gc_delete_range_done SELECT * FROM mysql.gc_delete_range WHERE job_id = %? AND element_id = %?` | ||
completeDeleteRangeSQL = `DELETE FROM mysql.gc_delete_range WHERE job_id = %? AND element_id = %?` | ||
completeDeleteMultiRangesSQL = `DELETE FROM mysql.gc_delete_range WHERE job_id = %? AND element_id in (%?)` | ||
updateDeleteRangeSQL = `UPDATE mysql.gc_delete_range SET start_key = "%?" WHERE job_id = %? AND element_id = %? AND start_key = "%?"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete the quotes around There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch |
||
deleteDoneRecordSQL = `DELETE FROM mysql.gc_delete_range_done WHERE job_id = %? AND element_id = %?` | ||
) | ||
|
||
// DelRangeTask is for run delete-range command in gc_worker. | ||
|
@@ -62,16 +62,14 @@ func LoadDoneDeleteRanges(ctx sessionctx.Context, safePoint uint64) (ranges []De | |
} | ||
|
||
func loadDeleteRangesFromTable(ctx sessionctx.Context, table string, safePoint uint64) (ranges []DelRangeTask, _ error) { | ||
sql := fmt.Sprintf(loadDeleteRangeSQL, table, safePoint) | ||
rss, err := ctx.(sqlexec.SQLExecutor).Execute(context.TODO(), sql) | ||
if len(rss) > 0 { | ||
defer terror.Call(rss[0].Close) | ||
rs, err := ctx.(sqlexec.SQLExecutor).ExecuteInternal(context.TODO(), loadDeleteRangeSQL, table, safePoint) | ||
if rs != nil { | ||
defer terror.Call(rs.Close) | ||
} | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
rs := rss[0] | ||
req := rs.NewChunk() | ||
it := chunk.NewIterator4Chunk(req) | ||
for { | ||
|
@@ -106,8 +104,7 @@ func loadDeleteRangesFromTable(ctx sessionctx.Context, table string, safePoint u | |
// CompleteDeleteRange moves a record from gc_delete_range table to gc_delete_range_done table. | ||
// NOTE: This function WILL NOT start and run in a new transaction internally. | ||
func CompleteDeleteRange(ctx sessionctx.Context, dr DelRangeTask) error { | ||
sql := fmt.Sprintf(recordDoneDeletedRangeSQL, dr.JobID, dr.ElementID) | ||
_, err := ctx.(sqlexec.SQLExecutor).Execute(context.TODO(), sql) | ||
_, err := ctx.(sqlexec.SQLExecutor).ExecuteInternal(context.TODO(), recordDoneDeletedRangeSQL, dr.JobID, dr.ElementID) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
@@ -117,8 +114,7 @@ func CompleteDeleteRange(ctx sessionctx.Context, dr DelRangeTask) error { | |
|
||
// RemoveFromGCDeleteRange is exported for ddl pkg to use. | ||
func RemoveFromGCDeleteRange(ctx sessionctx.Context, jobID, elementID int64) error { | ||
sql := fmt.Sprintf(completeDeleteRangeSQL, jobID, elementID) | ||
_, err := ctx.(sqlexec.SQLExecutor).Execute(context.TODO(), sql) | ||
_, err := ctx.(sqlexec.SQLExecutor).ExecuteInternal(context.TODO(), completeDeleteRangeSQL, jobID, elementID) | ||
return errors.Trace(err) | ||
} | ||
|
||
|
@@ -131,24 +127,21 @@ func RemoveMultiFromGCDeleteRange(ctx sessionctx.Context, jobID int64, elementID | |
} | ||
buf.WriteString(strconv.FormatInt(elementID, 10)) | ||
} | ||
sql := fmt.Sprintf(completeDeleteMultiRangesSQL, jobID, buf.String()) | ||
_, err := ctx.(sqlexec.SQLExecutor).Execute(context.TODO(), sql) | ||
_, err := ctx.(sqlexec.SQLExecutor).ExecuteInternal(context.TODO(), completeDeleteMultiRangesSQL, jobID, buf.String()) | ||
AilinKid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return errors.Trace(err) | ||
} | ||
|
||
// DeleteDoneRecord removes a record from gc_delete_range_done table. | ||
func DeleteDoneRecord(ctx sessionctx.Context, dr DelRangeTask) error { | ||
sql := fmt.Sprintf(deleteDoneRecordSQL, dr.JobID, dr.ElementID) | ||
_, err := ctx.(sqlexec.SQLExecutor).Execute(context.TODO(), sql) | ||
_, err := ctx.(sqlexec.SQLExecutor).ExecuteInternal(context.TODO(), deleteDoneRecordSQL, dr.JobID, dr.ElementID) | ||
return errors.Trace(err) | ||
} | ||
|
||
// UpdateDeleteRange is only for emulator. | ||
func UpdateDeleteRange(ctx sessionctx.Context, dr DelRangeTask, newStartKey, oldStartKey kv.Key) error { | ||
newStartKeyHex := hex.EncodeToString(newStartKey) | ||
oldStartKeyHex := hex.EncodeToString(oldStartKey) | ||
sql := fmt.Sprintf(updateDeleteRangeSQL, newStartKeyHex, dr.JobID, dr.ElementID, oldStartKeyHex) | ||
_, err := ctx.(sqlexec.SQLExecutor).Execute(context.TODO(), sql) | ||
_, err := ctx.(sqlexec.SQLExecutor).ExecuteInternal(context.TODO(), updateDeleteRangeSQL, newStartKeyHex, dr.JobID, dr.ElementID, oldStartKeyHex) | ||
return errors.Trace(err) | ||
} | ||
|
||
|
@@ -164,7 +157,7 @@ func LoadDDLVars(ctx sessionctx.Context) error { | |
return LoadGlobalVars(ctx, []string{variable.TiDBDDLErrorCountLimit}) | ||
} | ||
|
||
const loadGlobalVarsSQL = "select HIGH_PRIORITY variable_name, variable_value from mysql.global_variables where variable_name in (%s)" | ||
const loadGlobalVarsSQL = "select HIGH_PRIORITY variable_name, variable_value from mysql.global_variables where variable_name in (%?)" | ||
|
||
// LoadGlobalVars loads global variable from mysql.global_variables. | ||
func LoadGlobalVars(ctx sessionctx.Context, varNames []string) error { | ||
|
@@ -176,8 +169,11 @@ func LoadGlobalVars(ctx sessionctx.Context, varNames []string) error { | |
} | ||
nameList += fmt.Sprintf("'%s'", name) | ||
} | ||
sql := fmt.Sprintf(loadGlobalVarsSQL, nameList) | ||
rows, _, err := sctx.ExecRestrictedSQL(sql) | ||
stmt, err := sctx.ParseWithParams(context.Background(), loadGlobalVarsSQL, nameList) | ||
AilinKid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
rows, _, err := sctx.ExecRestrictedStmt(context.Background(), stmt) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is not considered safe to use concat and ParseWithParams at the same time. Though this case is safe.
fmt.Sprintf(fmt.Sprintf("%s", "%s"))
will report an error.You may want to check this PR.
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.
good~
the PR link is not right
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.
Sorry, here it is: 22674
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.
I suggest that you can use fmt.Fprintf.