Skip to content

Commit cdab4e5

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

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
@@ -26,7 +26,11 @@ go_library(
2626
"//pkg/util/logutil",
2727
"//pkg/util/sqlescape",
2828
"@com_github_pingcap_errors//:errors",
29+
<<<<<<< HEAD
2930
"@org_uber_go_zap//:zap",
31+
=======
32+
"@com_github_pingcap_failpoint//:failpoint",
33+
>>>>>>> 5e73267e719 (statistics: do not record historical stats meta if the table is locked (#57636))
3034
],
3135
)
3236

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"
@@ -127,8 +128,13 @@ func (s *statsUsageImpl) DumpStatsDeltaToKV(dumpAll bool) error {
127128
// For a partitioned table, we will update its global-stats as well.
128129
func (s *statsUsageImpl) dumpTableStatCountToKV(is infoschema.InfoSchema, physicalTableID int64, delta variable.TableDelta) (updated bool, err error) {
129130
statsVersion := uint64(0)
131+
isLocked := false
130132
defer func() {
131-
if err == nil && statsVersion != 0 {
133+
// Only record the historical stats meta when the table is not locked because all stats meta are stored in the locked table.
134+
if err == nil && statsVersion != 0 && !isLocked {
135+
failpoint.Inject("panic-when-record-historical-stats-meta", func() {
136+
panic("panic when record historical stats meta")
137+
})
132138
s.statsHandle.RecordHistoricalStatsMeta(physicalTableID, statsVersion, "flush stats", false)
133139
}
134140
}()
@@ -169,7 +175,12 @@ func (s *statsUsageImpl) dumpTableStatCountToKV(is infoschema.InfoSchema, physic
169175
isPartitionLocked = true
170176
}
171177
tableOrPartitionLocked := isTableLocked || isPartitionLocked
178+
<<<<<<< HEAD
172179
if err = storage.UpdateStatsMeta(sctx, statsVersion, delta,
180+
=======
181+
isLocked = tableOrPartitionLocked
182+
if err = storage.UpdateStatsMeta(utilstats.StatsCtx, sctx, statsVersion, delta,
183+
>>>>>>> 5e73267e719 (statistics: do not record historical stats meta if the table is locked (#57636))
173184
physicalTableID, tableOrPartitionLocked); err != nil {
174185
return err
175186
}
@@ -199,7 +210,12 @@ func (s *statsUsageImpl) dumpTableStatCountToKV(is infoschema.InfoSchema, physic
199210
if _, ok := lockedTables[physicalTableID]; ok {
200211
isTableLocked = true
201212
}
213+
<<<<<<< HEAD
202214
if err = storage.UpdateStatsMeta(sctx, statsVersion, delta,
215+
=======
216+
isLocked = isTableLocked
217+
if err = storage.UpdateStatsMeta(utilstats.StatsCtx, sctx, statsVersion, delta,
218+
>>>>>>> 5e73267e719 (statistics: do not record historical stats meta if the table is locked (#57636))
203219
physicalTableID, isTableLocked); err != nil {
204220
return err
205221
}

0 commit comments

Comments
 (0)