Skip to content

Commit 8b72a47

Browse files
authored
statistics, config: rightly deal with innodb_lock_wait_timeout when to load stats (#65632) (#65759)
close #65634
1 parent 0c2a357 commit 8b72a47

File tree

5 files changed

+100
-27
lines changed

5 files changed

+100
-27
lines changed

pkg/executor/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ go_library(
212212
"//pkg/util/chunk",
213213
"//pkg/util/codec",
214214
"//pkg/util/collate",
215+
"//pkg/util/config",
215216
"//pkg/util/context",
216217
"//pkg/util/cteutil",
217218
"//pkg/util/dbterror",
@@ -261,7 +262,6 @@ go_library(
261262
"//pkg/util/topsql",
262263
"//pkg/util/topsql/state",
263264
"//pkg/util/tracing",
264-
"@com_github_burntsushi_toml//:toml",
265265
"@com_github_docker_go_units//:go-units",
266266
"@com_github_gogo_protobuf//proto",
267267
"@com_github_google_uuid//:uuid",

pkg/executor/plan_replayer.go

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"os"
2424
"strings"
2525

26-
"github.com/BurntSushi/toml"
2726
"github.com/pingcap/errors"
2827
"github.com/pingcap/tidb/pkg/domain"
2928
"github.com/pingcap/tidb/pkg/executor/internal/exec"
@@ -35,6 +34,7 @@ import (
3534
"github.com/pingcap/tidb/pkg/sessiontxn"
3635
"github.com/pingcap/tidb/pkg/statistics/handle/util"
3736
"github.com/pingcap/tidb/pkg/util/chunk"
37+
"github.com/pingcap/tidb/pkg/util/config"
3838
"github.com/pingcap/tidb/pkg/util/logutil"
3939
"github.com/pingcap/tidb/pkg/util/replayer"
4040
"go.uber.org/zap"
@@ -365,41 +365,19 @@ func loadBindings(ctx sessionctx.Context, f *zip.File, isSession bool) error {
365365
}
366366

367367
func loadVariables(ctx sessionctx.Context, z *zip.Reader) error {
368-
unLoadVars := make([]string, 0)
368+
var unLoadVars []string
369369
for _, zipFile := range z.File {
370370
if strings.Compare(zipFile.Name, domain.PlanReplayerVariablesFile) == 0 {
371-
varMap := make(map[string]string)
372371
v, err := zipFile.Open()
373372
if err != nil {
374373
return errors.AddStack(err)
375374
}
376375
//nolint: errcheck,all_revive,revive
377376
defer v.Close()
378-
_, err = toml.NewDecoder(v).Decode(&varMap)
377+
unLoadVars, err = config.LoadConfigForPlanReplayerLoad(ctx, v)
379378
if err != nil {
380379
return errors.AddStack(err)
381380
}
382-
vars := ctx.GetSessionVars()
383-
for name, value := range varMap {
384-
sysVar := variable.GetSysVar(name)
385-
if sysVar == nil {
386-
unLoadVars = append(unLoadVars, name)
387-
logutil.BgLogger().Warn(fmt.Sprintf("skip set variable %s:%s", name, value), zap.Error(err))
388-
continue
389-
}
390-
sVal, err := sysVar.Validate(vars, value, variable.ScopeSession)
391-
if err != nil {
392-
unLoadVars = append(unLoadVars, name)
393-
logutil.BgLogger().Warn(fmt.Sprintf("skip variable %s:%s", name, value), zap.Error(err))
394-
continue
395-
}
396-
err = vars.SetSystemVar(name, sVal)
397-
if err != nil {
398-
unLoadVars = append(unLoadVars, name)
399-
logutil.BgLogger().Warn(fmt.Sprintf("skip set variable %s:%s", name, value), zap.Error(err))
400-
continue
401-
}
402-
}
403381
}
404382
}
405383
if len(unLoadVars) > 0 {
@@ -463,7 +441,7 @@ func loadStats(ctx sessionctx.Context, f *zip.File) error {
463441
do := domain.GetDomain(ctx)
464442
h := do.StatsHandle()
465443
if h == nil {
466-
return errors.New("plan replayer: hanlde is nil")
444+
return errors.New("plan replayer: handle is nil")
467445
}
468446
return h.LoadStatsFromJSON(context.Background(), ctx.GetInfoSchema().(infoschema.InfoSchema), jsonTbl, 0)
469447
}

pkg/statistics/handle/util/util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ func UpdateSCtxVarsForStats(sctx sessionctx.Context) error {
199199
return err
200200
}
201201
sctx.GetSessionVars().StmtCtx.SetTimeZone(sctx.GetSessionVars().Location())
202+
203+
// sync innodb_lock_wait_timeout
204+
val, err = sctx.GetSessionVars().GlobalVarsAccessor.GetGlobalSysVar(variable.InnodbLockWaitTimeout)
205+
if err != nil {
206+
return err
207+
}
208+
lockWaitSec, err := strconv.ParseInt(val, 10, 64)
209+
if err != nil {
210+
return err
211+
}
212+
sctx.GetSessionVars().LockWaitTimeout = lockWaitSec * 1000
202213
return nil
203214
}
204215

pkg/util/config/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "config",
5+
srcs = ["config.go"],
6+
importpath = "github.com/pingcap/tidb/pkg/util/config",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"//pkg/sessionctx",
10+
"//pkg/sessionctx/variable",
11+
"//pkg/util/logutil",
12+
"@com_github_burntsushi_toml//:toml",
13+
"@com_github_pingcap_errors//:errors",
14+
"@org_uber_go_zap//:zap",
15+
],
16+
)

pkg/util/config/config.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 config
16+
17+
import (
18+
"fmt"
19+
"io"
20+
21+
"github.com/BurntSushi/toml"
22+
"github.com/pingcap/errors"
23+
"github.com/pingcap/tidb/pkg/sessionctx"
24+
"github.com/pingcap/tidb/pkg/sessionctx/variable"
25+
"github.com/pingcap/tidb/pkg/util/logutil"
26+
"go.uber.org/zap"
27+
)
28+
29+
var ignoredSystemVariablesForPlanReplayerLoad = map[string]struct{}{
30+
variable.InnodbLockWaitTimeout: {}, // It is unnecessary to load this variable for plan replayer.
31+
}
32+
33+
// LoadConfigForPlanReplayerLoad loads system variables from a toml reader. it is only for plan replayer and test.
34+
func LoadConfigForPlanReplayerLoad(ctx sessionctx.Context, v io.ReadCloser) (unLoadVars []string, err error) {
35+
varMap := make(map[string]string)
36+
37+
_, err = toml.NewDecoder(v).Decode(&varMap)
38+
if err != nil {
39+
return nil, errors.AddStack(err)
40+
}
41+
unLoadVars = make([]string, 0)
42+
vars := ctx.GetSessionVars()
43+
for name, value := range varMap {
44+
if _, ok := ignoredSystemVariablesForPlanReplayerLoad[name]; ok {
45+
logutil.BgLogger().Warn(fmt.Sprintf("ignore set variable %s:%s", name, value), zap.Error(err))
46+
continue
47+
}
48+
sysVar := variable.GetSysVar(name)
49+
if sysVar == nil {
50+
unLoadVars = append(unLoadVars, name)
51+
logutil.BgLogger().Warn(fmt.Sprintf("skip set variable %s:%s", name, value), zap.Error(err))
52+
continue
53+
}
54+
sVal, err := sysVar.Validate(vars, value, variable.ScopeSession)
55+
if err != nil {
56+
unLoadVars = append(unLoadVars, name)
57+
logutil.BgLogger().Warn(fmt.Sprintf("skip variable %s:%s", name, value), zap.Error(err))
58+
continue
59+
}
60+
err = vars.SetSystemVar(name, sVal)
61+
if err != nil {
62+
unLoadVars = append(unLoadVars, name)
63+
logutil.BgLogger().Warn(fmt.Sprintf("skip set variable %s:%s", name, value), zap.Error(err))
64+
continue
65+
}
66+
}
67+
return unLoadVars, nil
68+
}

0 commit comments

Comments
 (0)