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

bindinfo: support disabled status for binding #32819

Merged
merged 49 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
113f3b4
bindinfo: rename Using status to Enable status
Reminiscent Mar 3, 2022
65da03b
convert the Using status binding to Enable status binding
Reminiscent Mar 3, 2022
c16dbf7
Merge branch 'master' of github.com:pingcap/tidb into bindingStatus
Reminiscent Mar 3, 2022
5ac35cd
address comments and fix ut
Reminiscent Mar 4, 2022
2a4f21e
Merge branch 'master' of github.com:pingcap/tidb into bindingStatus
Reminiscent Mar 4, 2022
7a49f34
fix ut
Reminiscent Mar 4, 2022
9fc6cff
bindinfo: support disable status for binding
Reminiscent Mar 4, 2022
52e91bf
fixup
Reminiscent Mar 4, 2022
7dff9d8
rename enable and disable to enabled and disabled
Reminiscent Mar 4, 2022
1b3a47e
Merge branch 'master' of github.com:pingcap/tidb into bindingStatus
Reminiscent Mar 4, 2022
722045d
remove useless change
Reminiscent Mar 4, 2022
dfbe5c6
change the parser part
Reminiscent Mar 4, 2022
adaf1cd
remove useless change
Reminiscent Mar 4, 2022
053bcf2
Merge branch 'master' of github.com:pingcap/tidb into bindingStatus
Reminiscent Mar 4, 2022
1e5008d
fix ut
Reminiscent Mar 4, 2022
3e8adb7
Merge branch 'master' of github.com:pingcap/tidb into bindingStatus
Reminiscent Mar 4, 2022
0a0fa6d
Merge branch 'master' into bindingStatus
ti-chi-bot Mar 7, 2022
164bcc1
Merge branch 'master' into bindingStatus
ti-chi-bot Mar 7, 2022
feb255c
Merge branches 'bindingStatus' and 'supportDisableStatus' of github.c…
Reminiscent Mar 8, 2022
a4b024b
Merge branch 'master' of github.com:pingcap/tidb into supportDisableS…
Reminiscent Mar 8, 2022
df39a8f
rename
Reminiscent Mar 8, 2022
b6b2b11
fix typo
Reminiscent Mar 8, 2022
4134d63
remove useless change
Reminiscent Mar 8, 2022
a1e68c8
update the status from using to enabled when upgrade
Reminiscent Mar 8, 2022
479d86e
Merge branch 'master' into supportDisableStatus
qw4990 Mar 8, 2022
a514afb
Merge branch 'master' into supportDisableStatus
qw4990 Mar 8, 2022
8c45877
add more comments
Reminiscent Mar 9, 2022
6f6a355
Merge branch 'master' into supportDisableStatus
qw4990 Mar 9, 2022
6476661
planner: support 'set binding enabled/ disabled'
Reminiscent Mar 9, 2022
62aecaf
Merge branch 'master' of github.com:pingcap/tidb into supportDisableS…
Reminiscent Mar 9, 2022
f58b8d1
Merge remote-tracking branch 'origin/supportDisableStatus' into suppo…
Reminiscent Mar 9, 2022
04069bf
format the header
Reminiscent Mar 9, 2022
d199115
fix check_dev
Reminiscent Mar 9, 2022
31f315c
Merge branch 'master' into supportDisableStatus
Reminiscent Mar 10, 2022
ae5dc5b
add more tests
Reminiscent Mar 15, 2022
fb12213
Merge branch 'master' of github.com:pingcap/tidb into supportDisableS…
Reminiscent Mar 15, 2022
d80d763
Merge remote-tracking branch 'origin/supportDisableStatus' into suppo…
Reminiscent Mar 15, 2022
7c96a00
Merge branch 'master' of github.com:pingcap/tidb into supportDisableS…
Reminiscent Mar 15, 2022
27272c9
update the warning message
Reminiscent Mar 15, 2022
b601865
add more test cases
Reminiscent Mar 15, 2022
f5b59a4
remove useless change
Reminiscent Mar 15, 2022
0c11684
Merge branch 'master' into supportDisableStatus
Reminiscent Mar 15, 2022
b196859
add the test for Privilege
Reminiscent Mar 16, 2022
ecf4d52
Merge branch 'master' of github.com:pingcap/tidb into supportDisableS…
Reminiscent Mar 16, 2022
fe9e03e
Merge remote-tracking branch 'origin/supportDisableStatus' into suppo…
Reminiscent Mar 16, 2022
f09ec70
address comments
Reminiscent Mar 16, 2022
f7934c2
Merge branch 'master' of github.com:pingcap/tidb into supportDisableS…
Reminiscent Mar 16, 2022
c8f0977
address comments
Reminiscent Mar 16, 2022
e293107
Merge branch 'master' into supportDisableStatus
ti-chi-bot Mar 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add more tests
  • Loading branch information
Reminiscent committed Mar 15, 2022
commit ae5dc5b39605ce93ea18167d1406927e482ffb74
26 changes: 16 additions & 10 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,11 @@ func (h *BindHandle) SetBindRecordStatus(originalSQL, db string, binding *Bindin
if err != nil {
return err
}
var updateTs types.Time
var oldStatus0, oldStatus1 string
var (
updateTs types.Time
oldStatus0, oldStatus1 string
affectRows int
)
if newStatus == Disabled {
// For compatibility reasons, when we need to 'set binding disabled for <stmt>',
// we need to consider both the 'enabled' and 'using' status.
Expand All @@ -429,7 +432,10 @@ func (h *BindHandle) SetBindRecordStatus(originalSQL, db string, binding *Bindin
}

_, err = exec.ExecuteInternal(context.TODO(), "COMMIT")
if err != nil {
if err != nil || affectRows == 0 {
if affectRows == 0 {
logutil.BgLogger().Warn("[sql-bind] There are no bindings can be set the status")
}
return
}

Expand All @@ -452,8 +458,6 @@ func (h *BindHandle) SetBindRecordStatus(originalSQL, db string, binding *Bindin
}
if setBindingStatusSucc {
h.setBindRecord(sqlDigest.String(), record)
} else {
logutil.BgLogger().Warn("[sql-bind] There are no bindings can be set the status")
}
}()

Expand All @@ -463,14 +467,16 @@ func (h *BindHandle) SetBindRecordStatus(originalSQL, db string, binding *Bindin
}

updateTs = types.NewTime(types.FromGoTime(time.Now()), mysql.TypeTimestamp, 3)
updateTsStr := updateTs.String()

if binding == nil {
_, err = exec.ExecuteInternal(context.TODO(), `UPDATE mysql.bind_info SET status = %?, update_time = %? WHERE original_sql = %? AND update_time < %? AND (status = %? or status = %?)`,
newStatus, updateTs.String(), originalSQL, updateTs.String(), oldStatus0, oldStatus1)
_, err = exec.ExecuteInternal(context.TODO(), `UPDATE mysql.bind_info SET status = %?, update_time = %? WHERE original_sql = %? AND update_time < %? AND (status = %? OR status = %?)`,
newStatus, updateTsStr, originalSQL, updateTsStr, oldStatus0, oldStatus1)
} else {
_, err = exec.ExecuteInternal(context.TODO(), `UPDATE mysql.bind_info SET status = %?, update_time = %? WHERE original_sql = %? AND update_time < %? AND bind_sql = %? and (status = %? or status = %?)`,
newStatus, updateTs.String(), originalSQL, updateTs.String(), binding.BindSQL, oldStatus0, oldStatus1)
_, err = exec.ExecuteInternal(context.TODO(), `UPDATE mysql.bind_info SET status = %?, update_time = %? WHERE original_sql = %? AND update_time < %? AND bind_sql = %? AND (status = %? OR status = %?)`,
newStatus, updateTsStr, originalSQL, updateTsStr, binding.BindSQL, oldStatus0, oldStatus1)
}
affectRows = int(h.sctx.Context.GetSessionVars().StmtCtx.AffectedRows())
return err
}

Expand Down Expand Up @@ -656,7 +662,7 @@ func (h *BindHandle) setBindRecord(hash string, meta *BindRecord) {
updateMetrics(metrics.ScopeGlobal, oldRecord, meta, false)
}

// appendBindRecord addes the BindRecord to the cache, all the stale BindRecords are
// appendBindRecord adds the BindRecord to the cache, all the stale BindRecords are
// removed from the cache after this operation.
func (h *BindHandle) appendBindRecord(hash string, meta *BindRecord) {
newCache := h.bindInfo.Value.Load().(*bindCache).Copy()
Expand Down
44 changes: 44 additions & 0 deletions bindinfo/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,50 @@ func TestSetBindingStatus(t *testing.T) {
require.Len(t, rows, 0)
}

func TestSetBindingStatusWithoutBindingInCache(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()

tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, index idx_a(a))")
utilCleanBindingEnv(tk, dom)
tk.MustQuery("show global bindings").Check(testkit.Rows())

// Simulate creating bindings on other machines
tk.MustExec("insert into mysql.bind_info values('select * from `test` . `t` where `a` > ?', 'SELECT /*+ USE_INDEX(`t` `idx_a`)*/ * FROM `test`.`t` WHERE `a` > 10', 'test', 'deleted', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
bindinfo.Manual + "')")
tk.MustExec("insert into mysql.bind_info values('select * from `test` . `t` where `a` > ?', 'SELECT /*+ USE_INDEX(`t` `idx_a`)*/ * FROM `test`.`t` WHERE `a` > 10', 'test', 'enabled', '2000-01-02 09:00:00', '2000-01-02 09:00:00', '', '','" +
bindinfo.Manual + "')")
dom.BindHandle().Clear()
tk.MustExec("set binding disabled for select * from t where a > 10")
tk.MustExec("admin reload bindings")
rows := tk.MustQuery("show global bindings").Rows()
require.Len(t, rows, 1)
require.Equal(t, bindinfo.Disabled, rows[0][3])

// clear the mysql.bind_info
utilCleanBindingEnv(tk, dom)
dom.BindHandle().Clear()

// Simulate creating bindings on other machines
tk.MustExec("insert into mysql.bind_info values('select * from `test` . `t` where `a` > ?', 'SELECT * FROM `test`.`t` WHERE `a` > 10', 'test', 'deleted', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" +
bindinfo.Manual + "')")
tk.MustExec("insert into mysql.bind_info values('select * from `test` . `t` where `a` > ?', 'SELECT * FROM `test`.`t` WHERE `a` > 10', 'test', 'disabled', '2000-01-02 09:00:00', '2000-01-02 09:00:00', '', '','" +
bindinfo.Manual + "')")
dom.BindHandle().Clear()
tk.MustExec("set binding enabled for select * from t where a > 10")
tk.MustExec("admin reload bindings")
rows = tk.MustQuery("show global bindings").Rows()
require.Len(t, rows, 1)
require.Equal(t, bindinfo.Enabled, rows[0][3])

utilCleanBindingEnv(tk, dom)
dom.BindHandle().Clear()
}

var testSQLs = []struct {
createSQL string
overlaySQL string
Expand Down