Skip to content

Commit

Permalink
stats: fix converting duration to timestamp (pingcap#8174) (pingcap#8184
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alivxxx authored and ngaut committed Nov 6, 2018
1 parent fd2d0bb commit 386ea13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion statistics/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ import (
"golang.org/x/net/context"
)

// DurationToTS converts duration to timestamp.
func DurationToTS(d time.Duration) uint64 {
return oracle.ComposeTS(d.Nanoseconds()/int64(time.Millisecond), 0)
}

// GCStats will garbage collect the useless stats info. For dropped tables, we will first update their version so that
// other tidb could know that table is deleted.
func (h *Handle) GCStats(is infoschema.InfoSchema, ddlLease time.Duration) error {
// To make sure that all the deleted tables' schema and stats info have been acknowledged to all tidb,
// we only garbage collect version before 10 lease.
lease := mathutil.MaxInt64(int64(h.Lease), int64(ddlLease))
offset := oracle.ComposeTS(10*lease, 0)
offset := DurationToTS(10 * time.Duration(lease))
if h.PrevLastVersion < offset {
return nil
}
Expand Down
9 changes: 9 additions & 0 deletions statistics/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
Expand Down Expand Up @@ -235,6 +236,14 @@ func (s *testStatsCacheSuite) TestAvgColLen(c *C) {
c.Assert(statsTbl.Columns[tableInfo.Columns[3].ID].AvgColSize(statsTbl.Count), Equals, 16.0)
}

func (s *testStatsCacheSuite) TestDurationToTS(c *C) {
tests := []time.Duration{time.Millisecond, time.Second, time.Minute, time.Hour}
for _, t := range tests {
ts := statistics.DurationToTS(t)
c.Assert(oracle.ExtractPhysical(ts)*int64(time.Millisecond), Equals, int64(t))
}
}

func (s *testStatsCacheSuite) TestVersion(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
Expand Down

0 comments on commit 386ea13

Please sign in to comment.