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: add status vars for 'last_plan_binding_update_time' #26340

Merged
merged 15 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2132,3 +2132,31 @@ func (s *testSuite) TestTemporaryTable(c *C) {
tk.MustGetErrCode("create binding for update t set a = 1 where b = 1 and c > 1 using update /*+ use_index(t, c) */ t set a = 1 where b = 1 and c > 1", errno.ErrOptOnTemporaryTable)
tk.MustGetErrCode("create binding for delete from t where b = 1 and c > 1 using delete /*+ use_index(t, c) */ from t where b = 1 and c > 1", errno.ErrOptOnTemporaryTable)
}

func (s *testSuite) TestBindingLastUpdateTime(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t0;")
tk.MustExec("create table t0(a int, key(a));")
tk.MustExec("create global binding for select * from t0 using select * from t0 use index(a);")
tk.MustExec("admin reload bindings;")

bindHandle := bindinfo.NewBindHandle(tk.Se)
err := bindHandle.Update(true)
c.Check(err, IsNil)
sql, hash := parser.NormalizeDigest("select * from test . t0")
bindData := bindHandle.GetBindRecord(hash.String(), sql, "test")
c.Assert(len(bindData.Bindings), Equals, 1)
bind := bindData.Bindings[0]
updateTime := bind.UpdateTime.String()

rows1 := tk.MustQuery("show status like 'last_plan_binding_update_time';").Rows()
updateTime1 := rows1[0][1]
c.Assert(updateTime1, Equals, updateTime)

rows2 := tk.MustQuery("show session status like 'last_plan_binding_update_time';").Rows()
updateTime2 := rows2[0][1]
c.Assert(updateTime2, Equals, updateTime)
tk.MustQuery(`show global status like 'last_plan_binding_update_time';`).Check(testkit.Rows())
}
1 change: 1 addition & 0 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func NewBindHandle(ctx sessionctx.Context) *BindHandle {
// BindSQL has already been validated when coming here, so we use nil sctx parameter.
return handle.AddBindRecord(nil, record)
}
variable.RegisterStatistics(handle)
return handle
}

Expand Down
39 changes: 39 additions & 0 deletions bindinfo/stat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package bindinfo

import (
"github.com/pingcap/tidb/sessionctx/variable"
)

var (
lastPlanBindingUpdateTime = "last_plan_binding_update_time"
)

// GetScope gets the status variables scope.
func (h *BindHandle) GetScope(status string) variable.ScopeFlag {
return variable.ScopeSession
}

// Stats returns the server statistics.
func (h *BindHandle) Stats(vars *variable.SessionVars) (map[string]interface{}, error) {
h.bindInfo.Lock()
defer func() {
h.bindInfo.Unlock()
}()
m := make(map[string]interface{})
m[lastPlanBindingUpdateTime] = h.bindInfo.lastUpdateTime.String()
eurekaka marked this conversation as resolved.
Show resolved Hide resolved

return m, nil
}