Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Reminiscent committed Mar 15, 2022
1 parent d199115 commit ae5dc5b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
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

0 comments on commit ae5dc5b

Please sign in to comment.