Skip to content

Commit

Permalink
table/tables: fix bug for jepsen test on cached table (pingcap#37020)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Aug 16, 2022
1 parent f65948d commit 483183e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 21 deletions.
4 changes: 2 additions & 2 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3366,8 +3366,8 @@ def go_deps():
name = "com_github_tikv_client_go_v2",
build_file_proto_mode = "disable_global",
importpath = "github.com/tikv/client-go/v2",
sum = "h1:tugwRUqH0gYZ0noHr17AVSKxNVd+Jl2BmNqgANeDIok=",
version = "v2.0.1-0.20220809034808-2ed2113d1090",
sum = "h1:WFR3seA8YtBhDn47YJSW1P1/lwBIXsk0vALnRVuaL/M=",
version = "v2.0.1-0.20220815094724-025596b7a20a",
)
go_repository(
name = "com_github_tikv_pd_client",
Expand Down
3 changes: 3 additions & 0 deletions ddl/schematracker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ go_test(
"//parser",
"//parser/ast",
"//parser/model",
"//sessionctx",
"//util/chunk",
"//util/mock",
"//util/sqlexec",
"@com_github_stretchr_testify//require",
],
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2
github.com/tikv/client-go/v2 v2.0.1-0.20220809034808-2ed2113d1090
github.com/tikv/client-go/v2 v2.0.1-0.20220815094724-025596b7a20a
github.com/tikv/pd/client v0.0.0-20220725055910-7187a7ab72db
github.com/twmb/murmur3 v1.1.3
github.com/uber/jaeger-client-go v2.22.1+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -906,8 +906,8 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpR
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY=
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 h1:mbAskLJ0oJfDRtkanvQPiooDH8HvJ2FBh+iKT/OmiQQ=
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfKggNGDuadAa0LElHrByyrz4JPZ9fFx6Gs7nx7ZZU=
github.com/tikv/client-go/v2 v2.0.1-0.20220809034808-2ed2113d1090 h1:tugwRUqH0gYZ0noHr17AVSKxNVd+Jl2BmNqgANeDIok=
github.com/tikv/client-go/v2 v2.0.1-0.20220809034808-2ed2113d1090/go.mod h1:v3DEt8LS9olI6D6El17pYBWq7B28hw3NnDFTxQHDLpY=
github.com/tikv/client-go/v2 v2.0.1-0.20220815094724-025596b7a20a h1:WFR3seA8YtBhDn47YJSW1P1/lwBIXsk0vALnRVuaL/M=
github.com/tikv/client-go/v2 v2.0.1-0.20220815094724-025596b7a20a/go.mod h1:v3DEt8LS9olI6D6El17pYBWq7B28hw3NnDFTxQHDLpY=
github.com/tikv/pd/client v0.0.0-20220725055910-7187a7ab72db h1:r1eMh9Rny3hfWuBuxOnbsCRrR4FhthiNxLQ5rAUtaww=
github.com/tikv/pd/client v0.0.0-20220725055910-7187a7ab72db/go.mod h1:ew8kS0yIcEaSetuuywkTLIUBR+sz3J5XvAYRae11qwc=
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 h1:kl4KhGNsJIbDHS9/4U9yQo1UcPQM0kOMJHn29EoH/Ro=
Expand Down
4 changes: 2 additions & 2 deletions planner/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go_library(
name = "core",
srcs = [
"access_object.go",
"cache.go",
"cacheable_checker.go",
"collect_column_stats_usage.go",
"common_plans.go",
Expand All @@ -30,6 +29,7 @@ go_library(
"physical_plans.go",
"plan.go",
"plan_cache.go",
"plan_cache_utils.go",
"plan_cost.go",
"plan_cost_detail.go",
"plan_stats.go",
Expand Down Expand Up @@ -151,7 +151,6 @@ go_test(
timeout = "short",
srcs = [
"binary_plan_test.go",
"cache_test.go",
"cacheable_checker_test.go",
"cbo_test.go",
"collect_column_stats_usage_test.go",
Expand All @@ -176,6 +175,7 @@ go_test(
"partition_pruning_test.go",
"physical_plan_test.go",
"physical_plan_trace_test.go",
"plan_cache_utils_test.go",
"plan_cost_detail_test.go",
"plan_cost_test.go",
"plan_stats_test.go",
Expand Down
2 changes: 2 additions & 0 deletions session/txninfo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ go_library(
importpath = "github.com/pingcap/tidb/session/txninfo",
visibility = ["//visibility:public"],
deps = [
"//metrics",
"//parser/mysql",
"//types",
"//util/logutil",
"@com_github_prometheus_client_golang//prometheus",
"@com_github_tikv_client_go_v2//oracle",
"@org_uber_go_zap//:zap",
],
Expand Down
9 changes: 6 additions & 3 deletions table/tables/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,11 @@ func (c *cachedTable) renewLease(handle StateRemote, ts uint64, data *cacheData,
tid := c.Meta().ID
lease := leaseFromTS(ts, leaseDuration)
newLease, err := handle.RenewReadLease(context.Background(), tid, data.Lease, lease)
if err != nil && !kv.IsTxnRetryableError(err) {
log.Warn("Renew read lease error", zap.Error(err))
if err != nil {
if !kv.IsTxnRetryableError(err) {
log.Warn("Renew read lease error", zap.Error(err))
}
return
}
if newLease > 0 {
c.cacheData.Store(&cacheData{
Expand All @@ -317,7 +320,7 @@ func (c *cachedTable) WriteLockAndKeepAlive(ctx context.Context, exit chan struc
return
}

t := time.NewTicker(cacheTableWriteLease)
t := time.NewTicker(cacheTableWriteLease / 2)
defer t.Stop()
for {
select {
Expand Down
30 changes: 19 additions & 11 deletions table/tables/state_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ func (h *stateRemoteHandle) LockForRead(ctx context.Context, tid int64, newLease
}
// The old lock is outdated, clear orphan lock.
if now > lease {
succ = true
if err := h.updateRow(ctx, tid, "READ", newLease); err != nil {
return errors.Trace(err)
if newLease > now { // Note the check, don't decrease the lease value!
succ = true
if err := h.updateRow(ctx, tid, "READ", newLease); err != nil {
return errors.Trace(err)
}
}
return nil
}
Expand Down Expand Up @@ -209,11 +211,15 @@ func (h *stateRemoteHandle) lockForWriteOnce(ctx context.Context, tid int64, lea
_lease = ts
}
case CachedTableLockRead:
newLease := ts
if newLease < lease { // Never, never decrease lease
newLease = lease
}
// Change from READ to INTEND
if _, err = h.execSQL(ctx,
"update mysql.table_cache_meta set lock_type='INTEND', oldReadLease=%?, lease=%? where tid=%?",
lease,
ts,
newLease,
tid); err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -243,13 +249,15 @@ func (h *stateRemoteHandle) lockForWriteOnce(ctx context.Context, tid int64, lea
// And then retry changing the lock to WRITE
waitAndRetry = waitForLeaseExpire(oldReadLease, now)
case CachedTableLockWrite:
if err = h.updateRow(ctx, tid, "WRITE", ts); err != nil {
return errors.Trace(err)
}
{
_updateLocal = true
_lockType = CachedTableLockWrite
_lease = ts
if ts > lease { // Note the check, don't decrease lease value!
if err = h.updateRow(ctx, tid, "WRITE", ts); err != nil {
return errors.Trace(err)
}
{
_updateLocal = true
_lockType = CachedTableLockWrite
_lease = ts
}
}
}
return nil
Expand Down

0 comments on commit 483183e

Please sign in to comment.