Skip to content

Commit b0d000d

Browse files
qw4990hawkingrei
authored andcommitted
This is an automated cherry-pick of pingcap#58328
Signed-off-by: ti-chi-bot <[email protected]>
1 parent c6468fb commit b0d000d

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

pkg/expression/constant.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,21 @@ func (d *ParamMarker) GetUserVar(ctx ParamValues) (types.Datum, error) {
167167

168168
// StringWithCtx implements Expression interface.
169169
func (c *Constant) StringWithCtx(ctx ParamValues, redact string) string {
170+
v := c.Value
170171
if c.ParamMarker != nil {
171172
dt, err := c.ParamMarker.GetUserVar(ctx)
172173
intest.AssertNoError(err, "fail to get param")
173174
if err != nil {
174175
return "?"
175176
}
176-
c.Value.SetValue(dt.GetValue(), c.RetType)
177+
v = dt
177178
} else if c.DeferredExpr != nil {
178179
return c.DeferredExpr.StringWithCtx(ctx, redact)
179180
}
180181
if redact == perrors.RedactLogDisable {
181-
return c.Value.TruncatedStringify()
182+
return v.TruncatedStringify()
182183
} else if redact == perrors.RedactLogMarker {
183-
return fmt.Sprintf("‹%s›", c.Value.TruncatedStringify())
184+
return fmt.Sprintf("‹%s›", v.TruncatedStringify())
184185
}
185186
return "?"
186187
}
@@ -312,8 +313,7 @@ func (c *Constant) Eval(ctx EvalContext, row chunk.Row) (types.Datum, error) {
312313
return c.Value, err
313314
}
314315
if dt.IsNull() {
315-
c.Value.SetNull()
316-
return c.Value, nil
316+
return dt, nil
317317
}
318318
if c.DeferredExpr != nil {
319319
if dt.Kind() != types.KindMysqlDecimal {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")
22

33
go_test(
44
name = "instanceplancache_test",
5-
timeout = "short",
5+
timeout = "moderate",
66
srcs = [
77
"concurrency_test.go",
88
"concurrency_tpcc_test.go",
@@ -11,7 +11,12 @@ go_test(
1111
"others_test.go",
1212
],
1313
flaky = True,
14+
<<<<<<< HEAD
1415
shard_count = 18,
16+
=======
17+
race = "on",
18+
shard_count = 40,
19+
>>>>>>> 80b34784bf5 (planner: fix possible read-write DATA RACE on Instance Plan Cache (#58328))
1520
deps = [
1621
"//pkg/parser/auth",
1722
"//pkg/testkit",

pkg/planner/core/plan_cache.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,15 @@ func generateNewPlan(ctx context.Context, sctx sessionctx.Context, isNonPrepared
321321
stmtCtx.SetPlan(p)
322322
stmtCtx.SetPlanDigest(stmt.NormalizedPlan, stmt.PlanDigest)
323323
if instancePlanCacheEnabled(ctx) {
324-
domain.GetDomain(sctx).GetInstancePlanCache().Put(cacheKey, cached, paramTypes)
324+
if cloned, ok := p.CloneForPlanCache(sctx.GetPlanCtx()); ok {
325+
// Clone this plan before putting it into the cache to avoid read-write DATA RACE. For example,
326+
// before this session finishes the execution, the next session has started cloning this plan.
327+
// Time: | ------------------------------------------------------------------------------- |
328+
// Sess1: | put plan into cache | ----------- execution (might modify the plan) ----------- |
329+
// Sess2: | start | ------- hit this plan and clone it (DATA RACE) ------- |
330+
cached.Plan = cloned
331+
domain.GetDomain(sctx).GetInstancePlanCache().Put(cacheKey, cached, paramTypes)
332+
}
325333
} else {
326334
sctx.GetSessionPlanCache().Put(cacheKey, cached, paramTypes)
327335
}

0 commit comments

Comments
 (0)