Skip to content

Commit 5a138ef

Browse files
authored
statistics: right deal with error for reading stats from storage (#58048) (#58061)
ref #57901
1 parent 71d1252 commit 5a138ef

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

pkg/statistics/handle/handletest/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ go_test(
99
],
1010
flaky = True,
1111
race = "on",
12-
shard_count = 33,
12+
shard_count = 34,
1313
deps = [
1414
"//pkg/config",
1515
"//pkg/domain",

pkg/statistics/handle/handletest/handle_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,3 +1490,31 @@ func TestSkipMissingPartitionStats(t *testing.T) {
14901490
return false
14911491
})
14921492
}
1493+
1494+
func TestStatsCacheUpdateTimeout(t *testing.T) {
1495+
store, dom := testkit.CreateMockStoreAndDomain(t)
1496+
tk := testkit.NewTestKit(t, store)
1497+
tk.MustExec("use test")
1498+
tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'")
1499+
tk.MustExec("set @@tidb_skip_missing_partition_stats = 1")
1500+
tk.MustExec("create table t (a int, b int, c int, index idx_b(b)) partition by range (a) (partition p0 values less than (100), partition p1 values less than (200), partition p2 values less than (300))")
1501+
tk.MustExec("insert into t values (1,1,1), (2,2,2), (101,101,101), (102,102,102), (201,201,201), (202,202,202)")
1502+
analyzehelper.TriggerPredicateColumnsCollection(t, tk, store, "t", "a", "b", "c")
1503+
h := dom.StatsHandle()
1504+
require.NoError(t, h.DumpStatsDeltaToKV(true))
1505+
tk.MustExec("analyze table t partition p0, p1")
1506+
tbl, err := dom.InfoSchema().TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr("t"))
1507+
require.NoError(t, err)
1508+
tblInfo := tbl.Meta()
1509+
globalStats := h.GetTableStats(tblInfo)
1510+
require.Equal(t, 6, int(globalStats.RealtimeCount))
1511+
require.Equal(t, 2, int(globalStats.ModifyCount))
1512+
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/statistics/handle/util/ExecRowsTimeout", "return()"))
1513+
defer func() {
1514+
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/util/ExecRowsTimeout"))
1515+
}()
1516+
require.Error(t, h.Update(context.Background(), dom.InfoSchema()))
1517+
globalStats2 := h.GetTableStats(tblInfo)
1518+
require.Equal(t, 6, int(globalStats2.RealtimeCount))
1519+
require.Equal(t, 2, int(globalStats2.ModifyCount))
1520+
}

pkg/statistics/handle/storage/read.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,11 @@ func TableStatsFromStorage(sctx sessionctx.Context, snapshot uint64, tableInfo *
539539
table.RealtimeCount = realtimeCount
540540

541541
rows, _, err := util.ExecRows(sctx, "select table_id, is_index, hist_id, distinct_count, version, null_count, tot_col_size, stats_ver, flag, correlation, last_analyze_pos from mysql.stats_histograms where table_id = %?", tableID)
542+
if err != nil {
543+
return nil, err
544+
}
542545
// Check deleted table.
543-
if err != nil || len(rows) == 0 {
546+
if len(rows) == 0 {
544547
return nil, nil
545548
}
546549
for _, row := range rows {

pkg/statistics/handle/util/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ go_library(
2828
"//pkg/util/sqlexec",
2929
"//pkg/util/sqlexec/mock",
3030
"@com_github_pingcap_errors//:errors",
31+
"@com_github_pingcap_failpoint//:failpoint",
3132
"@com_github_pingcap_tipb//go-tipb",
3233
"@com_github_tiancaiamao_gp//:gp",
3334
"@com_github_tikv_client_go_v2//oracle",

pkg/statistics/handle/util/util.go

Lines changed: 4 additions & 0 deletions
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/kv"
2627
"github.com/pingcap/tidb/pkg/meta/model"
2728
"github.com/pingcap/tidb/pkg/parser/terror"
@@ -235,6 +236,9 @@ func ExecWithCtx(
235236

236237
// ExecRows is a helper function to execute sql and return rows and fields.
237238
func ExecRows(sctx sessionctx.Context, sql string, args ...any) (rows []chunk.Row, fields []*resolve.ResultField, err error) {
239+
failpoint.Inject("ExecRowsTimeout", func() {
240+
failpoint.Return(nil, nil, errors.New("inject timeout error"))
241+
})
238242
return ExecRowsWithCtx(StatsCtx, sctx, sql, args...)
239243
}
240244

0 commit comments

Comments
 (0)