|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package initstats |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/pingcap/tidb/pkg/config" |
| 23 | + "github.com/pingcap/tidb/pkg/parser/model" |
| 24 | + "github.com/pingcap/tidb/pkg/statistics/handle" |
| 25 | + "github.com/pingcap/tidb/pkg/statistics/handle/types" |
| 26 | + "github.com/pingcap/tidb/pkg/testkit" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +func TestConcurrentlyInitStatsWithMemoryLimit(t *testing.T) { |
| 31 | + restore := config.RestoreFunc() |
| 32 | + defer restore() |
| 33 | + config.UpdateGlobal(func(conf *config.Config) { |
| 34 | + conf.Performance.LiteInitStats = false |
| 35 | + conf.Performance.ConcurrentlyInitStats = true |
| 36 | + }) |
| 37 | + handle.IsFullCacheFunc = func(cache types.StatsCache, total uint64) bool { |
| 38 | + return true |
| 39 | + } |
| 40 | + testConcurrentlyInitStats(t) |
| 41 | +} |
| 42 | + |
| 43 | +func TestConcurrentlyInitStatsWithoutMemoryLimit(t *testing.T) { |
| 44 | + restore := config.RestoreFunc() |
| 45 | + defer restore() |
| 46 | + config.UpdateGlobal(func(conf *config.Config) { |
| 47 | + conf.Performance.LiteInitStats = false |
| 48 | + conf.Performance.ConcurrentlyInitStats = true |
| 49 | + }) |
| 50 | + handle.IsFullCacheFunc = func(cache types.StatsCache, total uint64) bool { |
| 51 | + return false |
| 52 | + } |
| 53 | + testConcurrentlyInitStats(t) |
| 54 | +} |
| 55 | + |
| 56 | +func testConcurrentlyInitStats(t *testing.T) { |
| 57 | + store, dom := testkit.CreateMockStoreAndDomain(t) |
| 58 | + tk := testkit.NewTestKit(t, store) |
| 59 | + tk.MustExec("use test") |
| 60 | + tk.MustExec("set global tidb_analyze_column_options='ALL'") |
| 61 | + tk.MustExec("create table t1 (a int, b int, c int, primary key(c))") |
| 62 | + tk.MustExec("insert into t1 values (1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,7,8)") |
| 63 | + tk.MustExec("analyze table t1") |
| 64 | + for i := 2; i < 10; i++ { |
| 65 | + tk.MustExec(fmt.Sprintf("create table t%v (a int, b int, c int, primary key(c))", i)) |
| 66 | + tk.MustExec(fmt.Sprintf("insert into t%v select * from t1", i)) |
| 67 | + tk.MustExec(fmt.Sprintf("analyze table t%v all columns", i)) |
| 68 | + } |
| 69 | + h := dom.StatsHandle() |
| 70 | + is := dom.InfoSchema() |
| 71 | + h.Clear() |
| 72 | + require.Equal(t, h.MemConsumed(), int64(0)) |
| 73 | + require.NoError(t, h.InitStats(context.Background(), is)) |
| 74 | + for i := 1; i < 10; i++ { |
| 75 | + tbl, err := is.TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr(fmt.Sprintf("t%v", i))) |
| 76 | + require.NoError(t, err) |
| 77 | + stats, ok := h.StatsCache.Get(tbl.Meta().ID) |
| 78 | + require.True(t, ok) |
| 79 | + for _, col := range stats.GetColSlice() { |
| 80 | + require.True(t, col.IsAllEvicted()) |
| 81 | + require.False(t, col.IsFullLoad()) |
| 82 | + } |
| 83 | + } |
| 84 | + for i := 1; i < 10; i++ { |
| 85 | + tk.MustQuery(fmt.Sprintf("explain select * from t%v where a = 1", i)).CheckNotContain("pseudo") |
| 86 | + } |
| 87 | + for i := 1; i < 10; i++ { |
| 88 | + tk.MustQuery(fmt.Sprintf("explain select * from t%v where b = 1", i)).CheckNotContain("pseudo") |
| 89 | + } |
| 90 | + for i := 1; i < 10; i++ { |
| 91 | + tk.MustQuery(fmt.Sprintf("explain select * from t%v where c >= 1", i)).CheckNotContain("pseudo") |
| 92 | + } |
| 93 | + for i := 1; i < 10; i++ { |
| 94 | + tbl, err := is.TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr(fmt.Sprintf("t%v", i))) |
| 95 | + require.NoError(t, err) |
| 96 | + stats, ok := h.StatsCache.Get(tbl.Meta().ID) |
| 97 | + require.True(t, ok) |
| 98 | + for _, col := range stats.GetColSlice() { |
| 99 | + require.True(t, col.IsFullLoad()) |
| 100 | + require.False(t, col.IsAllEvicted()) |
| 101 | + } |
| 102 | + } |
| 103 | + require.Equal(t, int64(126), handle.GetMaxTidRecordForTest()) |
| 104 | +} |
0 commit comments