Skip to content

Commit f72e18a

Browse files
0xPoeti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#64097
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
1 parent 293331c commit f72e18a

17 files changed

Lines changed: 3269 additions & 40 deletions

File tree

pkg/ddl/notifier/testkit_test.go

Lines changed: 617 additions & 0 deletions
Large diffs are not rendered by default.

pkg/infoschema/infoschema_v2.go

Lines changed: 1788 additions & 0 deletions
Large diffs are not rendered by default.

pkg/meta/autoid/autoid.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ const (
5757
MetricSchemaDBID int64 = SystemSchemaIDFlag | 20000
5858
)
5959

60+
// IsMemSchemaID checks whether schemaID is memory schema ID.
61+
func IsMemSchemaID(schemaID int64) bool {
62+
return schemaID&SystemSchemaIDFlag != 0
63+
}
64+
6065
const (
6166
minStep = 30000
6267
maxStep = 2000000

pkg/planner/core/casetest/dag/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ go_test(
2222
"//pkg/sessiontxn",
2323
"//pkg/testkit",
2424
"//pkg/testkit/testdata",
25+
"//pkg/testkit/testfailpoint",
2526
"//pkg/testkit/testmain",
2627
"//pkg/testkit/testsetup",
2728
"//pkg/util/hint",

pkg/planner/core/casetest/dag/dag_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/pingcap/tidb/pkg/sessiontxn"
3131
"github.com/pingcap/tidb/pkg/testkit"
3232
"github.com/pingcap/tidb/pkg/testkit/testdata"
33+
"github.com/pingcap/tidb/pkg/testkit/testfailpoint"
3334
"github.com/pingcap/tidb/pkg/util/hint"
3435
"github.com/stretchr/testify/require"
3536
)
@@ -77,6 +78,25 @@ func TestDAGPlanBuilderSimpleCase(t *testing.T) {
7778
}
7879
}
7980

81+
<<<<<<< HEAD
82+
=======
83+
func TestDAGPlanBuilderSimpleCase(t *testing.T) {
84+
testfailpoint.Enable(t, "github.com/pingcap/tidb/pkg/statistics/handle/SkipSystemTableCheck", `return(true)`)
85+
if kerneltype.IsNextGen() {
86+
t.Skip("Please run the TestDAGPlanBuilderSimpleCaseForNextGen")
87+
}
88+
testkit.RunTestUnderCascades(t, testDAGPlanBuilderSimpleCase)
89+
}
90+
91+
func TestDAGPlanBuilderSimpleCaseForNextGen(t *testing.T) {
92+
testfailpoint.Enable(t, "github.com/pingcap/tidb/pkg/statistics/handle/SkipSystemTableCheck", `return(true)`)
93+
if kerneltype.IsClassic() {
94+
t.Skip("Please run the TestDAGPlanBuilderSimpleCase")
95+
}
96+
testkit.RunTestUnderCascades(t, testDAGPlanBuilderSimpleCase)
97+
}
98+
99+
>>>>>>> 9f3ae48f30b (statistics: ignore system tables in stats cache (#64097))
80100
func TestDAGPlanBuilderJoin(t *testing.T) {
81101
store := testkit.CreateMockStore(t)
82102

pkg/session/bootstrap_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,10 @@ func TestTiDBUpgradeToVer176(t *testing.T) {
20952095
ver, err = getBootstrapVersion(seV175)
20962096
require.NoError(t, err)
20972097
require.Less(t, int64(ver175), ver)
2098-
MustExec(t, seV175, "SELECT * from mysql.tidb_global_task_history")
2098+
// Avoid reusing the old session when checking the new table.
2099+
// Otherwise it may access the previous domain, which has already been closed.
2100+
newSession := CreateSessionAndSetID(t, store)
2101+
MustExec(t, newSession, "SELECT * from mysql.tidb_global_task_history")
20992102
dom.Close()
21002103
}
21012104

@@ -2125,7 +2128,10 @@ func TestTiDBUpgradeToVer177(t *testing.T) {
21252128
ver, err = getBootstrapVersion(seV176)
21262129
require.NoError(t, err)
21272130
require.Less(t, int64(ver176), ver)
2128-
MustExec(t, seV176, "SELECT * from mysql.dist_framework_meta")
2131+
// Avoid reusing the old session when checking the new table.
2132+
// Otherwise it may access the previous domain, which has already been closed.
2133+
newSession := CreateSessionAndSetID(t, store)
2134+
MustExec(t, newSession, "SELECT * from mysql.dist_framework_meta")
21292135
dom.Close()
21302136
}
21312137

pkg/session/syssession/BUILD.bazel

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "syssession",
5+
srcs = [
6+
"pool.go",
7+
"session.go",
8+
"session_test_util.go",
9+
],
10+
importpath = "github.com/pingcap/tidb/pkg/session/syssession",
11+
visibility = ["//visibility:public"],
12+
deps = [
13+
"//pkg/domain/infosync",
14+
"//pkg/kv",
15+
"//pkg/parser/ast",
16+
"//pkg/planner/core/resolve",
17+
"//pkg/sessionctx",
18+
"//pkg/util/chunk",
19+
"//pkg/util/intest",
20+
"//pkg/util/logutil",
21+
"//pkg/util/sqlexec",
22+
"@com_github_pingcap_errors//:errors",
23+
"@com_github_pingcap_failpoint//:failpoint",
24+
"@org_uber_go_zap//:zap",
25+
],
26+
)
27+
28+
go_test(
29+
name = "syssession_test",
30+
timeout = "short",
31+
srcs = [
32+
"main_test.go",
33+
"pool_test.go",
34+
"session_integration_test.go",
35+
"session_test.go",
36+
],
37+
embed = [":syssession"],
38+
flaky = True,
39+
shard_count = 21,
40+
deps = [
41+
"//pkg/kv",
42+
"//pkg/parser/ast",
43+
"//pkg/planner/core/resolve",
44+
"//pkg/session/sessmgr",
45+
"//pkg/sessionctx",
46+
"//pkg/sessiontxn",
47+
"//pkg/testkit",
48+
"//pkg/testkit/testfailpoint",
49+
"//pkg/testkit/testsetup",
50+
"//pkg/util/chunk",
51+
"//pkg/util/sqlexec",
52+
"@com_github_pingcap_errors//:errors",
53+
"@com_github_stretchr_testify//mock",
54+
"@com_github_stretchr_testify//require",
55+
"@org_uber_go_goleak//:goleak",
56+
],
57+
)
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// Copyright 2025 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 syssession_test
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/pingcap/errors"
22+
"github.com/pingcap/tidb/pkg/kv"
23+
"github.com/pingcap/tidb/pkg/session/syssession"
24+
"github.com/pingcap/tidb/pkg/sessionctx"
25+
"github.com/pingcap/tidb/pkg/sessiontxn"
26+
"github.com/pingcap/tidb/pkg/testkit"
27+
"github.com/pingcap/tidb/pkg/testkit/testfailpoint"
28+
"github.com/stretchr/testify/require"
29+
)
30+
31+
func TestDomainAdvancedSessionPoolInternalSessionRegistry(t *testing.T) {
32+
_, do := testkit.CreateMockStoreAndDomain(t)
33+
p := do.AdvancedSysSessionPool()
34+
require.NotNil(t, p)
35+
36+
sessManager := do.InfoSyncer().GetSessionManager()
37+
38+
// test session manager registry when put back
39+
// We test for more than one times to cover the case that the session is in the pool.
40+
var sctx sessionctx.Context
41+
var se *syssession.Session
42+
for range 2 {
43+
sctx = nil
44+
se = nil
45+
require.NoError(t, p.WithSession(func(session *syssession.Session) error {
46+
require.Nil(t, se)
47+
se = session
48+
require.True(t, session.IsOwner())
49+
return session.WithSessionContext(func(ctx sessionctx.Context) error {
50+
require.Nil(t, sctx)
51+
sctx = ctx
52+
require.True(t, sessManager.ContainsInternalSession(ctx))
53+
return nil
54+
})
55+
}))
56+
require.NotNil(t, se)
57+
require.False(t, se.IsInternalClosed())
58+
require.False(t, se.IsOwner())
59+
require.NotNil(t, sctx)
60+
require.False(t, sessManager.ContainsInternalSession(sctx))
61+
}
62+
63+
// test session manager registry when close session
64+
sctx = nil
65+
se, err := p.Get()
66+
require.NoError(t, err)
67+
require.NoError(t, se.WithSessionContext(func(ctx sessionctx.Context) error {
68+
sctx = ctx
69+
return nil
70+
}))
71+
require.NotNil(t, sctx)
72+
require.True(t, sessManager.ContainsInternalSession(sctx))
73+
se.Close()
74+
require.False(t, sessManager.ContainsInternalSession(sctx))
75+
}
76+
77+
func TestDomainAdvancedSessionPoolPutBackDirtySession(t *testing.T) {
78+
testfailpoint.Enable(t, "github.com/pingcap/tidb/pkg/statistics/handle/SkipSystemTableCheck", `return(true)`)
79+
store, do := testkit.CreateMockStoreAndDomain(t)
80+
p := do.AdvancedSysSessionPool()
81+
require.NotNil(t, p)
82+
83+
tk := testkit.NewTestKit(t, store)
84+
tk.MustExec("use test")
85+
tk.MustExec("create table t1(a int)")
86+
tk.MustExec("insert into t1 values(1), (2), (3), (4), (5)")
87+
88+
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnOthers)
89+
cases := []struct {
90+
name string
91+
withSession func(*syssession.Session) error
92+
withSessionContext func(sessionctx.Context) error
93+
}{
94+
{
95+
name: "put back closed one",
96+
withSession: func(session *syssession.Session) error {
97+
session.Close()
98+
return nil
99+
},
100+
},
101+
{
102+
name: "return error for withSession",
103+
withSession: func(session *syssession.Session) error {
104+
return errors.New("err1")
105+
},
106+
},
107+
{
108+
name: "return error for withSessionContext",
109+
withSessionContext: func(sctx sessionctx.Context) error {
110+
return errors.New("err2")
111+
},
112+
},
113+
{
114+
name: "resultSetNotClose",
115+
withSession: func(session *syssession.Session) error {
116+
_, err := session.ExecuteInternal(ctx, "select * from test.t1")
117+
require.NoError(t, err)
118+
return nil
119+
},
120+
},
121+
{
122+
name: "optimisticTxnNotClose",
123+
withSession: func(session *syssession.Session) error {
124+
_, err := session.ExecuteInternal(ctx, "begin optimistic")
125+
require.NoError(t, err)
126+
return nil
127+
},
128+
},
129+
{
130+
name: "pessimisticTxnNotClose",
131+
withSession: func(session *syssession.Session) error {
132+
_, err := session.ExecuteInternal(ctx, "begin pessimistic")
133+
require.NoError(t, err)
134+
return nil
135+
},
136+
},
137+
{
138+
name: "tsFuturePrepared",
139+
withSessionContext: func(sctx sessionctx.Context) error {
140+
require.NoError(t, sctx.PrepareTSFuture(ctx, sessiontxn.ConstantFuture(1), kv.GlobalTxnScope))
141+
return nil
142+
},
143+
},
144+
{
145+
name: "avoid reuse in withSession",
146+
withSession: func(session *syssession.Session) error {
147+
session.AvoidReuse()
148+
return nil
149+
},
150+
},
151+
{
152+
name: "avoid reuse in withSessionContext",
153+
withSession: func(session *syssession.Session) error {
154+
return session.WithSessionContext(func(sessionctx.Context) error {
155+
session.AvoidReuse()
156+
return nil
157+
})
158+
},
159+
},
160+
}
161+
162+
for _, c := range cases {
163+
t.Run(c.name, func(t *testing.T) {
164+
var se *syssession.Session
165+
var expectedErr error
166+
syssession.WithSuppressAssert(func() {
167+
err := p.WithSession(func(session *syssession.Session) error {
168+
require.Nil(t, se)
169+
se = session
170+
require.True(t, session.IsOwner())
171+
require.False(t, se.IsInternalClosed())
172+
if c.withSession != nil {
173+
expectedErr = c.withSession(session)
174+
return expectedErr
175+
}
176+
177+
if c.withSessionContext != nil {
178+
err := session.WithSessionContext(func(sessionctx sessionctx.Context) error {
179+
expectedErr = c.withSessionContext(sessionctx)
180+
return expectedErr
181+
})
182+
if expectedErr != nil {
183+
require.EqualError(t, err, expectedErr.Error())
184+
} else {
185+
require.NoError(t, err)
186+
}
187+
return err
188+
}
189+
190+
return nil
191+
})
192+
193+
if expectedErr != nil {
194+
require.EqualError(t, err, expectedErr.Error())
195+
} else {
196+
require.NoError(t, err)
197+
}
198+
})
199+
require.NotNil(t, se)
200+
require.True(t, se.IsInternalClosed())
201+
require.False(t, se.IsOwner())
202+
require.Zero(t, p.(*syssession.AdvancedSessionPool).Size())
203+
})
204+
}
205+
206+
t.Run("success case", func(t *testing.T) {
207+
var se *syssession.Session
208+
require.NoError(t, p.WithSession(func(s *syssession.Session) error {
209+
se = s
210+
return s.WithSessionContext(func(sessionctx.Context) error { return nil })
211+
}))
212+
require.NotNil(t, se)
213+
require.False(t, se.IsInternalClosed())
214+
require.False(t, se.IsOwner())
215+
require.Equal(t, 1, p.(*syssession.AdvancedSessionPool).Size())
216+
})
217+
218+
t.Run("put back a put back case", func(t *testing.T) {
219+
var se *syssession.Session
220+
require.NoError(t, p.WithSession(func(s *syssession.Session) error {
221+
se = s
222+
p.Put(s)
223+
return nil
224+
}))
225+
require.NotNil(t, se)
226+
require.False(t, se.IsInternalClosed())
227+
require.False(t, se.IsOwner())
228+
require.Equal(t, 1, p.(*syssession.AdvancedSessionPool).Size())
229+
})
230+
}

pkg/statistics/handle/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ go_library(
1515
"//pkg/ddl/util",
1616
"//pkg/infoschema",
1717
"//pkg/kv",
18+
<<<<<<< HEAD
1819
"//pkg/metrics",
1920
"//pkg/parser/model",
21+
=======
22+
"//pkg/meta/autoid",
23+
"//pkg/meta/model",
24+
>>>>>>> 9f3ae48f30b (statistics: ignore system tables in stats cache (#64097))
2025
"//pkg/parser/mysql",
2126
"//pkg/parser/terror",
2227
"//pkg/sessionctx",
@@ -36,6 +41,7 @@ go_library(
3641
"//pkg/types",
3742
"//pkg/util",
3843
"//pkg/util/chunk",
44+
"//pkg/util/filter",
3945
"//pkg/util/intest",
4046
"//pkg/util/logutil",
4147
"//pkg/util/memory",

0 commit comments

Comments
 (0)