Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ttl: always enable all read engines for TTL sessions (#56604) #56807

Open
wants to merge 2 commits into
base: release-8.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/ttl/ttlworker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ go_library(
"//pkg/parser/terror",
"//pkg/sessionctx",
"//pkg/sessionctx/variable",
"//pkg/statistics/handle/util",
"//pkg/store/driver/error",
"//pkg/timer/api",
"//pkg/timer/runtime",
Expand Down
9 changes: 5 additions & 4 deletions pkg/ttl/ttlworker/job_manager_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestGetSession(t *testing.T) {
tk.MustExec("set @@tidb_retry_limit=1")
tk.MustExec("set @@tidb_enable_1pc=0")
tk.MustExec("set @@tidb_enable_async_commit=0")
tk.MustExec("set @@tidb_isolation_read_engines='tiflash,tidb'")
var getCnt atomic.Int32

pool := pools.NewResourcePool(func() (pools.Resource, error) {
Expand All @@ -94,13 +95,13 @@ func TestGetSession(t *testing.T) {
require.Equal(t, "Europe/Berlin", tz.String())

// session variables should be set
tk.MustQuery("select @@time_zone, @@tidb_retry_limit, @@tidb_enable_1pc, @@tidb_enable_async_commit").
Check(testkit.Rows("UTC 0 1 1"))
tk.MustQuery("select @@time_zone, @@tidb_retry_limit, @@tidb_enable_1pc, @@tidb_enable_async_commit, @@tidb_isolation_read_engines").
Check(testkit.Rows("UTC 0 1 1 tikv,tiflash,tidb"))

// all session variables should be restored after close
se.Close()
tk.MustQuery("select @@time_zone, @@tidb_retry_limit, @@tidb_enable_1pc, @@tidb_enable_async_commit").
Check(testkit.Rows("Asia/Shanghai 1 0 0"))
tk.MustQuery("select @@time_zone, @@tidb_retry_limit, @@tidb_enable_1pc, @@tidb_enable_async_commit, @@tidb_isolation_read_engines").
Check(testkit.Rows("Asia/Shanghai 1 0 0 tiflash,tidb"))
}

func TestParallelLockNewJob(t *testing.T) {
Expand Down
43 changes: 42 additions & 1 deletion pkg/ttl/ttlworker/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (

"github.com/ngaut/pools"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/parser/terror"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
"github.com/pingcap/tidb/pkg/statistics/handle/util"
"github.com/pingcap/tidb/pkg/ttl/cache"
"github.com/pingcap/tidb/pkg/ttl/metrics"
"github.com/pingcap/tidb/pkg/ttl/session"
Expand Down Expand Up @@ -55,12 +57,18 @@ var DetachStatsCollector = func(s sqlexec.SQLExecutor) sqlexec.SQLExecutor {
return s
}

var allIsolationReadEngines = map[kv.StoreType]struct{}{
kv.TiKV: {},
kv.TiFlash: {},
kv.TiDB: {},
}

type sessionPool interface {
Get() (pools.Resource, error)
Put(pools.Resource)
}

func getSession(pool sessionPool) (session.Session, error) {
func getSession(pool util.SessionPool) (session.Session, error) {
resource, err := pool.Get()
if err != nil {
return nil, err
Expand All @@ -82,6 +90,7 @@ func getSession(pool sessionPool) (session.Session, error) {
originalEnable1PC := sctx.GetSessionVars().Enable1PC
originalEnableAsyncCommit := sctx.GetSessionVars().EnableAsyncCommit
originalTimeZone, restoreTimeZone := "", false
originalIsolationReadEngines, restoreIsolationReadEngines := "", false

se := session.NewSession(sctx, exec, func(se session.Session) {
_, err = se.ExecuteSQL(context.Background(), fmt.Sprintf("set tidb_retry_limit=%d", originalRetryLimit))
Expand All @@ -108,6 +117,12 @@ func getSession(pool sessionPool) (session.Session, error) {
terror.Log(err)
}

if restoreIsolationReadEngines {
_, err = se.ExecuteSQL(context.Background(), "set tidb_isolation_read_engines=%?", originalIsolationReadEngines)
intest.AssertNoError(err)
terror.Log(err)
}

DetachStatsCollector(exec)

pool.Put(resource)
Expand Down Expand Up @@ -162,6 +177,32 @@ func getSession(pool sessionPool) (session.Session, error) {
}
restoreTimeZone = true

// allow the session in TTL to use all read engines.
_, hasTiDBEngine := se.GetSessionVars().IsolationReadEngines[kv.TiDB]
_, hasTiKVEngine := se.GetSessionVars().IsolationReadEngines[kv.TiKV]
_, hasTiFlashEngine := se.GetSessionVars().IsolationReadEngines[kv.TiFlash]
if !hasTiDBEngine || !hasTiKVEngine || !hasTiFlashEngine {
rows, err := se.ExecuteSQL(context.Background(), "select @@tidb_isolation_read_engines")
if err != nil {
se.Close()
return nil, err
}

if len(rows) == 0 || rows[0].Len() == 0 {
se.Close()
return nil, errors.New("failed to get tidb_isolation_read_engines variable")
}
originalIsolationReadEngines = rows[0].GetString(0)

_, err = se.ExecuteSQL(context.Background(), "set tidb_isolation_read_engines='tikv,tiflash,tidb'")
if err != nil {
se.Close()
return nil, err
}

restoreIsolationReadEngines = true
}

return se, nil
}

Expand Down