Skip to content

Commit dc6258a

Browse files
hawkingreiti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#58048
Signed-off-by: ti-chi-bot <[email protected]>
1 parent c6e2ffa commit dc6258a

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-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
@@ -1462,3 +1462,31 @@ func TestSkipMissingPartitionStats(t *testing.T) {
14621462
require.True(t, idx.IsStatsInitialized())
14631463
}
14641464
}
1465+
1466+
func TestStatsCacheUpdateTimeout(t *testing.T) {
1467+
store, dom := testkit.CreateMockStoreAndDomain(t)
1468+
tk := testkit.NewTestKit(t, store)
1469+
tk.MustExec("use test")
1470+
tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'")
1471+
tk.MustExec("set @@tidb_skip_missing_partition_stats = 1")
1472+
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))")
1473+
tk.MustExec("insert into t values (1,1,1), (2,2,2), (101,101,101), (102,102,102), (201,201,201), (202,202,202)")
1474+
analyzehelper.TriggerPredicateColumnsCollection(t, tk, store, "t", "a", "b", "c")
1475+
h := dom.StatsHandle()
1476+
require.NoError(t, h.DumpStatsDeltaToKV(true))
1477+
tk.MustExec("analyze table t partition p0, p1")
1478+
tbl, err := dom.InfoSchema().TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr("t"))
1479+
require.NoError(t, err)
1480+
tblInfo := tbl.Meta()
1481+
globalStats := h.GetTableStats(tblInfo)
1482+
require.Equal(t, 6, int(globalStats.RealtimeCount))
1483+
require.Equal(t, 2, int(globalStats.ModifyCount))
1484+
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/statistics/handle/util/ExecRowsTimeout", "return()"))
1485+
defer func() {
1486+
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/util/ExecRowsTimeout"))
1487+
}()
1488+
require.Error(t, h.Update(context.Background(), dom.InfoSchema()))
1489+
globalStats2 := h.GetTableStats(tblInfo)
1490+
require.Equal(t, 6, int(globalStats2.RealtimeCount))
1491+
require.Equal(t, 2, int(globalStats2.ModifyCount))
1492+
}

pkg/statistics/handle/storage/read.go

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

531531
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)
532+
if err != nil {
533+
return nil, err
534+
}
532535
// Check deleted table.
533-
if err != nil || len(rows) == 0 {
536+
if len(rows) == 0 {
534537
return nil, nil
535538
}
536539
for _, row := range rows {

pkg/statistics/handle/util/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ go_library(
2929
"//pkg/util/sqlexec/mock",
3030
"@com_github_ngaut_pools//:pools",
3131
"@com_github_pingcap_errors//:errors",
32+
<<<<<<< HEAD
3233
"@com_github_pingcap_tipb//go-tipb",
34+
=======
35+
"@com_github_pingcap_failpoint//:failpoint",
36+
>>>>>>> 1521bf723dd (statistics: right deal with error for reading stats from storage (#58048))
3337
"@com_github_tiancaiamao_gp//:gp",
3438
"@com_github_tikv_client_go_v2//oracle",
3539
"@org_uber_go_atomic//:atomic",

pkg/statistics/handle/util/util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
"github.com/pingcap/errors"
23+
"github.com/pingcap/failpoint"
2324
"github.com/pingcap/tidb/pkg/kv"
2425
"github.com/pingcap/tidb/pkg/parser/ast"
2526
"github.com/pingcap/tidb/pkg/parser/terror"
@@ -219,7 +220,24 @@ func Exec(sctx sessionctx.Context, sql string, args ...any) (sqlexec.RecordSet,
219220
}
220221

221222
// ExecRows is a helper function to execute sql and return rows and fields.
223+
<<<<<<< HEAD
222224
func ExecRows(sctx sessionctx.Context, sql string, args ...any) (rows []chunk.Row, fields []*ast.ResultField, err error) {
225+
=======
226+
func ExecRows(sctx sessionctx.Context, sql string, args ...any) (rows []chunk.Row, fields []*resolve.ResultField, err error) {
227+
failpoint.Inject("ExecRowsTimeout", func() {
228+
failpoint.Return(nil, nil, errors.New("inject timeout error"))
229+
})
230+
return ExecRowsWithCtx(StatsCtx, sctx, sql, args...)
231+
}
232+
233+
// ExecRowsWithCtx is a helper function to execute sql and return rows and fields.
234+
func ExecRowsWithCtx(
235+
ctx context.Context,
236+
sctx sessionctx.Context,
237+
sql string,
238+
args ...any,
239+
) (rows []chunk.Row, fields []*resolve.ResultField, err error) {
240+
>>>>>>> 1521bf723dd (statistics: right deal with error for reading stats from storage (#58048))
223241
if intest.InTest {
224242
if v := sctx.Value(mock.RestrictedSQLExecutorKey{}); v != nil {
225243
return v.(*mock.MockRestrictedSQLExecutor).ExecRestrictedSQL(StatsCtx,

0 commit comments

Comments
 (0)