Skip to content

Commit

Permalink
staleread, session: internal write request should be accepted with ex…
Browse files Browse the repository at this point in the history
…ternal ts (#39967)

close #39966
  • Loading branch information
YangKeao authored Dec 17, 2022
1 parent 3970cfc commit 223a912
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2241,7 +2241,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

0 comments on commit 223a912

Please sign in to comment.