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

staleread, session: internal write request should be accepted with external ts (#39967) #40005

Closed
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
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,7 @@ func (s *session) onTxnManagerStmtStartOrRetry(ctx context.Context, node ast.Stm

func (s *session) validateStatementReadOnlyInStaleness(stmtNode ast.StmtNode) error {
vars := s.GetSessionVars()
if !vars.TxnCtx.IsStaleness && vars.TxnReadTS.PeakTxnReadTS() == 0 && !vars.EnableExternalTSRead {
if !vars.TxnCtx.IsStaleness && vars.TxnReadTS.PeakTxnReadTS() == 0 && !vars.EnableExternalTSRead || vars.InRestrictedSQL {
return nil
}
errMsg := "only support read-only statement during read-only staleness transactions"
Expand Down
10 changes: 10 additions & 0 deletions sessiontxn/staleread/externalts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package staleread_test

import (
"context"
"testing"

"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -67,12 +69,20 @@ func TestExternalTimestampReadonly(t *testing.T) {
tk.MustQuery("select @@tidb_external_ts").Check(testkit.Rows("0"))
tk.MustExec("start transaction;set global tidb_external_ts=@@tidb_current_ts;commit;")

// with tidb_enable_external_ts_read enabled, this session will be readonly
tk.MustExec("set tidb_enable_external_ts_read=ON")
_, err := tk.Exec("insert into t values (0)")
require.Error(t, err)

tk.MustExec("set tidb_enable_external_ts_read=OFF")
tk.MustExec("insert into t values (0)")

// even when tidb_enable_external_ts_read is enabled, internal SQL will not be affected
tk.MustExec("set tidb_enable_external_ts_read=ON")
tk.Session().GetSessionVars().InRestrictedSQL = true
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnOthers)
tk.MustExecWithContext(ctx, "insert into t values (1)")
tk.Session().GetSessionVars().InRestrictedSQL = false
}

func TestExternalTimestampReadWithTransaction(t *testing.T) {
Expand Down