Skip to content

Commit

Permalink
statistics: do not record historical stats meta if the table is locked (
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Dec 10, 2024
1 parent 9ce7e0e commit 732fe1a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/statistics/handle/handletest/lockstats/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_test(
"//pkg/parser/model",
"//pkg/testkit",
"//pkg/testkit/testsetup",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_stretchr_testify//require",
"@org_uber_go_goleak//:goleak",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/pkg/config"
"github.com/pingcap/tidb/pkg/domain"
"github.com/pingcap/tidb/pkg/kv"
Expand Down Expand Up @@ -56,14 +57,25 @@ func TestLockAndUnlockTableStats(t *testing.T) {
require.Nil(t, err)
require.Equal(t, 1, len(lockedTables))

// Insert a new row to the table.
tk.MustExec("insert into t(a, b) values(3,'c')")
// Enable the failpoint to test the historical stats meta is not recorded.
err = failpoint.Enable(
"github.com/pingcap/tidb/pkg/statistics/handle/usage/panic-when-record-historical-stats-meta",
"1*return(true)",
)
require.NoError(t, err)
// Dump stats delta to KV.
require.NotPanics(t, func() { handle.DumpStatsDeltaToKV(true) })

tk.MustExec("unlock stats t")
rows = tk.MustQuery(selectTableLockSQL).Rows()
num, _ = strconv.Atoi(rows[0][0].(string))
require.Equal(t, num, 0)

tk.MustExec("analyze table test.t")
tblStats2 := handle.GetTableStats(tbl)
require.Equal(t, int64(2), tblStats2.RealtimeCount)
require.Equal(t, int64(3), tblStats2.RealtimeCount)
}

func TestLockAndUnlockPartitionedTableStats(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/statistics/handle/usage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ go_library(
"//pkg/util/logutil",
"//pkg/util/sqlexec",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@org_uber_go_zap//:zap",
],
)
Expand Down
10 changes: 9 additions & 1 deletion pkg/statistics/handle/usage/session_stats_collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/pkg/infoschema"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser/model"
Expand Down Expand Up @@ -124,8 +125,13 @@ func (s *statsUsageImpl) DumpStatsDeltaToKV(dumpAll bool) error {
// For a partitioned table, we will update its global-stats as well.
func (s *statsUsageImpl) dumpTableStatCountToKV(is infoschema.InfoSchema, physicalTableID int64, delta variable.TableDelta) (updated bool, err error) {
statsVersion := uint64(0)
isLocked := false
defer func() {
if err == nil && statsVersion != 0 {
// Only record the historical stats meta when the table is not locked because all stats meta are stored in the locked table.
if err == nil && statsVersion != 0 && !isLocked {
failpoint.Inject("panic-when-record-historical-stats-meta", func() {
panic("panic when record historical stats meta")
})
s.statsHandle.RecordHistoricalStatsMeta(physicalTableID, statsVersion, "flush stats", false)
}
}()
Expand Down Expand Up @@ -166,6 +172,7 @@ func (s *statsUsageImpl) dumpTableStatCountToKV(is infoschema.InfoSchema, physic
isPartitionLocked = true
}
tableOrPartitionLocked := isTableLocked || isPartitionLocked
isLocked = tableOrPartitionLocked
if err = storage.UpdateStatsMeta(sctx, statsVersion, delta,
physicalTableID, tableOrPartitionLocked); err != nil {
return err
Expand Down Expand Up @@ -196,6 +203,7 @@ func (s *statsUsageImpl) dumpTableStatCountToKV(is infoschema.InfoSchema, physic
if _, ok := lockedTables[physicalTableID]; ok {
isTableLocked = true
}
isLocked = isTableLocked
if err = storage.UpdateStatsMeta(sctx, statsVersion, delta,
physicalTableID, isTableLocked); err != nil {
return err
Expand Down

0 comments on commit 732fe1a

Please sign in to comment.