Skip to content

Commit

Permalink
store: Add status variables for tikv statistics (#22286)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgo authored Mar 10, 2021
1 parent bdfcaf8 commit 5f1e205
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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 @@ -93,6 +94,7 @@ func NewGCWorker(store kv.Storage, pdClient pd.Client) (*GCWorker, error) {
lastFinish: time.Now(),
done: make(chan error),
}
variable.RegisterStatistics(worker)
return worker, nil
}

Expand Down Expand Up @@ -155,6 +157,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 @@ -223,6 +232,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
}
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 @@ -390,6 +390,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

0 comments on commit 5f1e205

Please sign in to comment.