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

store: Add status variables for tikv statistics #22286

Merged
merged 4 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 35 additions & 0 deletions store/gcworker/gc_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/logutil"
"github.com/pingcap/tidb/store/tikv/oracle"
Expand Down Expand Up @@ -87,6 +88,7 @@ func NewGCWorker(store tikv.Storage, pdClient pd.Client) (*GCWorker, error) {
lastFinish: time.Now(),
done: make(chan error),
}
variable.RegisterStatistics(worker)
return worker, nil
}

Expand Down Expand Up @@ -149,6 +151,13 @@ const (
gcDefaultAutoConcurrency = true

gcWorkerServiceSafePointID = "gc_worker"

// Status var names start with tidb_%
tidbGCLastRunTime = "tidb_gc_last_run_time"
tidbGCLeaderDesc = "tidb_gc_leader_desc"
tidbGCLeaderLease = "tidb_gc_leader_lease"
tidbGCLeaderUUID = "tidb_gc_leader_uuid"
tidbGCSafePoint = "tidb_gc_safe_point"
)

var gcSafePointCacheInterval = tikv.GcSafePointCacheInterval
Expand Down Expand Up @@ -217,6 +226,32 @@ func createSession(store kv.Storage) session.Session {
}
}

// GetScope gets the status variables scope.
func (w *GCWorker) GetScope(status string) variable.ScopeFlag {
return variable.DefaultStatusVarScopeFlag
}

// Stats returns the server statistics.
func (w *GCWorker) Stats(vars *variable.SessionVars) (map[string]interface{}, error) {
m := make(map[string]interface{})
if v, err := w.loadValueFromSysTable(gcLeaderUUIDKey); err == nil {
m[tidbGCLeaderUUID] = v
}
if v, err := w.loadValueFromSysTable(gcLeaderDescKey); err == nil {
m[tidbGCLeaderDesc] = v
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The leader desc usually contains the leader node's hostname or IP address. Since mysql.tidb will be hidden for security concern, can this be exposed directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will have to add filtering of SHOW [GLOBAL] STATUS to security enhanced mode. The proposal is closed for now because it was moved out of this sprint. cc @SunRunAway

Moving to SHOW STATUS is still beneficial though, since filtering from here has a cleaner semantic than patching out results from SELECT * FROM mysql.tidb.

}
if v, err := w.loadValueFromSysTable(gcLeaderLeaseKey); err == nil {
m[tidbGCLeaderLease] = v
}
if v, err := w.loadValueFromSysTable(gcLastRunTimeKey); err == nil {
m[tidbGCLastRunTime] = v
}
if v, err := w.loadValueFromSysTable(gcSafePointKey); err == nil {
m[tidbGCSafePoint] = v
}
return m, nil
}

func (w *GCWorker) tick(ctx context.Context) {
isLeader, err := w.checkLeader()
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions store/gcworker/gc_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,26 @@ func (s *testGCWorkerSuite) TestPrepareGC(c *C) {
c.Assert(safepoint, Equals, uint64(0))
}

func (s *testGCWorkerSuite) TestStatusVars(c *C) {
// Status variables should now exist for:
// tidb_gc_safe_point, tidb_gc_last_run_time
se := createSession(s.gcWorker.store)
defer se.Close()

safePoint, err := s.gcWorker.loadValueFromSysTable(gcSafePointKey)
c.Assert(err, IsNil)
lastRunTime, err := s.gcWorker.loadValueFromSysTable(gcLastRunTimeKey)
c.Assert(err, IsNil)

statusVars, _ := s.gcWorker.Stats(se.GetSessionVars())
val, ok := statusVars[tidbGCSafePoint]
c.Assert(ok, IsTrue)
c.Assert(val, Equals, safePoint)
val, ok = statusVars[tidbGCLastRunTime]
c.Assert(ok, IsTrue)
c.Assert(val, Equals, lastRunTime)
}

func (s *testGCWorkerSuite) TestDoGCForOneRegion(c *C) {
ctx := context.Background()
bo := tikv.NewBackofferWithVars(ctx, tikv.GcOneRegionMaxBackoff, nil)
Expand Down