|
| 1 | +// Copyright 2026 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 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/pingcap/failpoint" |
| 23 | + "github.com/pingcap/tidb/pkg/config" |
| 24 | + server2 "github.com/pingcap/tidb/pkg/server" |
| 25 | + "github.com/pingcap/tidb/pkg/session" |
| 26 | + "github.com/pingcap/tidb/pkg/store/mockstore" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + "github.com/tikv/client-go/v2/oracle" |
| 29 | +) |
| 30 | + |
| 31 | +func TestInitStatsSessionBlockGC(t *testing.T) { |
| 32 | + origConfig := config.GetGlobalConfig() |
| 33 | + defer func() { |
| 34 | + config.StoreGlobalConfig(origConfig) |
| 35 | + }() |
| 36 | + newConfig := *origConfig |
| 37 | + for _, lite := range []bool{false, true} { |
| 38 | + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStats", "pause")) |
| 39 | + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStatsLite", "pause")) |
| 40 | + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest", "return(true)")) |
| 41 | + newConfig.Performance.LiteInitStats = lite |
| 42 | + config.StoreGlobalConfig(&newConfig) |
| 43 | + |
| 44 | + store, err := mockstore.NewMockStore() |
| 45 | + require.NoError(t, err) |
| 46 | + dom, err := session.BootstrapSession(store) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + infoSyncer := dom.InfoSyncer() |
| 50 | + sv := server2.CreateMockServer(t, store) |
| 51 | + sv.SetDomain(dom) |
| 52 | + infoSyncer.SetSessionManager(sv) |
| 53 | + time.Sleep(time.Second) |
| 54 | + require.Eventually(t, func() bool { |
| 55 | + now := time.Now() |
| 56 | + startTSList := sv.GetInternalSessionStartTSList() |
| 57 | + for _, startTS := range startTSList { |
| 58 | + if startTS != 0 { |
| 59 | + startTime := oracle.GetTimeFromTS(startTS) |
| 60 | + if now.Sub(startTime) > time.Second { |
| 61 | + return true |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + return false |
| 66 | + }, 10*time.Second, 10*time.Millisecond, "min_start_ts is not blocked over 1s") |
| 67 | + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStats")) |
| 68 | + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStatsLite")) |
| 69 | + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest")) |
| 70 | + dom.Close() |
| 71 | + require.NoError(t, store.Close()) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestInitStatsSessionBlockGCCanBeCanceled(t *testing.T) { |
| 76 | + require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest", "return(true)")) |
| 77 | + |
| 78 | + store, err := mockstore.NewMockStore() |
| 79 | + require.NoError(t, err) |
| 80 | + dom, err := session.BootstrapSession(store) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + infoSyncer := dom.InfoSyncer() |
| 84 | + infoSyncer.SetSessionManager(nil) |
| 85 | + h := dom.StatsHandle() |
| 86 | + ctx, cancel := context.WithCancel(context.Background()) |
| 87 | + go func() { |
| 88 | + time.Sleep(time.Second) |
| 89 | + cancel() |
| 90 | + }() |
| 91 | + require.ErrorIs(t, h.InitStats(ctx, dom.InfoSchema()), context.Canceled) |
| 92 | + require.ErrorIs(t, h.InitStatsLite(ctx), context.Canceled) |
| 93 | + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest")) |
| 94 | + dom.Close() |
| 95 | + require.NoError(t, store.Close()) |
| 96 | +} |
0 commit comments