Skip to content

Commit 2b4f86e

Browse files
0xPoeti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#57636
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 3fd908a commit 2b4f86e

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

pkg/statistics/handle/handletest/lockstats/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ go_test(
1717
"//pkg/parser/model",
1818
"//pkg/testkit",
1919
"//pkg/testkit/testsetup",
20+
"@com_github_pingcap_failpoint//:failpoint",
2021
"@com_github_stretchr_testify//require",
2122
"@org_uber_go_goleak//:goleak",
2223
],

pkg/statistics/handle/handletest/lockstats/lock_table_stats_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121
"time"
2222

23+
"github.com/pingcap/failpoint"
2324
"github.com/pingcap/tidb/pkg/config"
2425
"github.com/pingcap/tidb/pkg/domain"
2526
"github.com/pingcap/tidb/pkg/kv"
@@ -56,14 +57,25 @@ func TestLockAndUnlockTableStats(t *testing.T) {
5657
require.Nil(t, err)
5758
require.Equal(t, 1, len(lockedTables))
5859

60+
// Insert a new row to the table.
61+
tk.MustExec("insert into t(a, b) values(3,'c')")
62+
// Enable the failpoint to test the historical stats meta is not recorded.
63+
err = failpoint.Enable(
64+
"github.com/pingcap/tidb/pkg/statistics/handle/usage/panic-when-record-historical-stats-meta",
65+
"1*return(true)",
66+
)
67+
require.NoError(t, err)
68+
// Dump stats delta to KV.
69+
require.NotPanics(t, func() { handle.DumpStatsDeltaToKV(true) })
70+
5971
tk.MustExec("unlock stats t")
6072
rows = tk.MustQuery(selectTableLockSQL).Rows()
6173
num, _ = strconv.Atoi(rows[0][0].(string))
6274
require.Equal(t, num, 0)
6375

6476
tk.MustExec("analyze table test.t")
6577
tblStats2 := handle.GetTableStats(tbl)
66-
require.Equal(t, int64(2), tblStats2.RealtimeCount)
78+
require.Equal(t, int64(3), tblStats2.RealtimeCount)
6779
}
6880

6981
func TestLockAndUnlockPartitionedTableStats(t *testing.T) {

pkg/statistics/handle/usage/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ go_library(
2424
"//pkg/util/logutil",
2525
"//pkg/util/sqlexec",
2626
"@com_github_pingcap_errors//:errors",
27+
<<<<<<< HEAD
2728
"@org_uber_go_zap//:zap",
29+
=======
30+
"@com_github_pingcap_failpoint//:failpoint",
31+
>>>>>>> 5e73267e719 (statistics: do not record historical stats meta if the table is locked (#57636))
2832
],
2933
)
3034

pkg/statistics/handle/usage/session_stats_collect.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/pingcap/errors"
25+
"github.com/pingcap/failpoint"
2526
"github.com/pingcap/tidb/pkg/infoschema"
2627
"github.com/pingcap/tidb/pkg/metrics"
2728
"github.com/pingcap/tidb/pkg/parser/model"
@@ -124,8 +125,13 @@ func (s *statsUsageImpl) DumpStatsDeltaToKV(dumpAll bool) error {
124125
// For a partitioned table, we will update its global-stats as well.
125126
func (s *statsUsageImpl) dumpTableStatCountToKV(is infoschema.InfoSchema, physicalTableID int64, delta variable.TableDelta) (updated bool, err error) {
126127
statsVersion := uint64(0)
128+
isLocked := false
127129
defer func() {
128-
if err == nil && statsVersion != 0 {
130+
// Only record the historical stats meta when the table is not locked because all stats meta are stored in the locked table.
131+
if err == nil && statsVersion != 0 && !isLocked {
132+
failpoint.Inject("panic-when-record-historical-stats-meta", func() {
133+
panic("panic when record historical stats meta")
134+
})
129135
s.statsHandle.RecordHistoricalStatsMeta(physicalTableID, statsVersion, "flush stats", false)
130136
}
131137
}()
@@ -166,7 +172,12 @@ func (s *statsUsageImpl) dumpTableStatCountToKV(is infoschema.InfoSchema, physic
166172
isPartitionLocked = true
167173
}
168174
tableOrPartitionLocked := isTableLocked || isPartitionLocked
175+
<<<<<<< HEAD
169176
if err = storage.UpdateStatsMeta(sctx, statsVersion, delta,
177+
=======
178+
isLocked = tableOrPartitionLocked
179+
if err = storage.UpdateStatsMeta(utilstats.StatsCtx, sctx, statsVersion, delta,
180+
>>>>>>> 5e73267e719 (statistics: do not record historical stats meta if the table is locked (#57636))
170181
physicalTableID, tableOrPartitionLocked); err != nil {
171182
return err
172183
}
@@ -196,7 +207,12 @@ func (s *statsUsageImpl) dumpTableStatCountToKV(is infoschema.InfoSchema, physic
196207
if _, ok := lockedTables[physicalTableID]; ok {
197208
isTableLocked = true
198209
}
210+
<<<<<<< HEAD
199211
if err = storage.UpdateStatsMeta(sctx, statsVersion, delta,
212+
=======
213+
isLocked = isTableLocked
214+
if err = storage.UpdateStatsMeta(utilstats.StatsCtx, sctx, statsVersion, delta,
215+
>>>>>>> 5e73267e719 (statistics: do not record historical stats meta if the table is locked (#57636))
200216
physicalTableID, isTableLocked); err != nil {
201217
return err
202218
}

0 commit comments

Comments
 (0)