Skip to content

Commit

Permalink
remove the variable txn_scope
Browse files Browse the repository at this point in the history
Signed-off-by: ekexium <eke@fastmail.com>
  • Loading branch information
ekexium committed Jan 9, 2025
1 parent b22555b commit ab66aec
Show file tree
Hide file tree
Showing 9 changed files with 2 additions and 564 deletions.
122 changes: 0 additions & 122 deletions pkg/ddl/placement_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package ddl_test

import (
"context"
"fmt"
"testing"

"github.com/pingcap/failpoint"
Expand All @@ -31,127 +30,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestTxnScopeConstraint(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop placement policy if exists p2")
defer func() {
tk.MustExec("drop table if exists t1")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop placement policy if exists p2")
}()

tk.MustExec("create placement policy p1 leader_constraints='[+zone=sh]'")
tk.MustExec("create placement policy p2 follower_constraints='[+zone=sh]'")
tk.MustExec(`create table t1 (c int)
PARTITION BY RANGE (c) (
PARTITION p0 VALUES LESS THAN (6),
PARTITION p1 VALUES LESS THAN (11),
PARTITION p2 VALUES LESS THAN (16),
PARTITION p3 VALUES LESS THAN (21)
);`)

is := dom.InfoSchema()

tb, err := is.TableByName(context.Background(), ast.NewCIStr("test"), ast.NewCIStr("t1"))
require.NoError(t, err)
partDefs := tb.Meta().GetPartitionInfo().Definitions

for _, def := range partDefs {
if def.Name.String() == "p0" {
tk.MustExec("alter table t1 partition p0 placement policy p1")
} else if def.Name.String() == "p2" {
tk.MustExec("alter table t1 partition p2 placement policy p2")
}
}

testCases := []struct {
name string
sql string
txnScope string
zone string
disableAutoCommit bool
err error
}{
{
name: "Insert into PARTITION p0 with global txnScope",
sql: "insert into t1 (c) values (1)",
txnScope: "global",
zone: "",
err: nil,
},
{
name: "insert into PARTITION p0 with wrong txnScope",
sql: "insert into t1 (c) values (1)",
txnScope: "local",
zone: "bj",
err: fmt.Errorf(".*out of txn_scope.*"),
},
{
name: "insert into PARTITION p1 with local txnScope",
sql: "insert into t1 (c) values (10)",
txnScope: "local",
zone: "bj",
err: fmt.Errorf(".*doesn't have placement policies with txn_scope.*"),
},
{
name: "insert into PARTITION p1 with global txnScope",
sql: "insert into t1 (c) values (10)",
txnScope: "global",
err: nil,
},
{
name: "insert into PARTITION p2 with local txnScope",
sql: "insert into t1 (c) values (15)",
txnScope: "local",
zone: "bj",
err: fmt.Errorf(".*leader placement policy is not defined.*"),
},
{
name: "insert into PARTITION p2 with global txnScope",
sql: "insert into t1 (c) values (15)",
txnScope: "global",
zone: "",
err: nil,
},
{
name: "insert into PARTITION p0 with wrong txnScope and autocommit off",
sql: "insert into t1 (c) values (1)",
txnScope: "local",
zone: "bj",
disableAutoCommit: true,
err: fmt.Errorf(".*out of txn_scope.*"),
},
}

for _, testcase := range testCases {
failpoint.Enable("tikvclient/injectTxnScope",
fmt.Sprintf(`return("%v")`, testcase.zone))
tk.MustExec("use test")
tk.MustExec("set global tidb_enable_local_txn = on;")
tk.MustExec(fmt.Sprintf("set @@txn_scope = %v", testcase.txnScope))
if testcase.disableAutoCommit {
tk.MustExec("set @@autocommit = 0")
tk.MustExec("begin")
tk.MustExec(testcase.sql)
err = tk.ExecToErr("commit")
} else {
err = tk.ExecToErr(testcase.sql)
}
if testcase.err == nil {
require.NoError(t, err)
} else {
require.Error(t, err)
require.Regexp(t, testcase.err.Error(), err.Error())
}
tk.MustExec("set global tidb_enable_local_txn = off;")
failpoint.Disable("tikvclient/injectTxnScope")
}
}

func TestCreateSchemaWithPlacement(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
Expand Down
126 changes: 0 additions & 126 deletions pkg/expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3887,132 +3887,6 @@ func TestIssue16205(t *testing.T) {
require.NotEqual(t, rows1[0][0].(string), rows2[0][0].(string))
}

func TestCrossDCQuery(t *testing.T) {
store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop placement policy if exists p2")
tk.MustExec("create placement policy p1 leader_constraints='[+zone=sh]'")
tk.MustExec("create placement policy p2 leader_constraints='[+zone=bj]'")
tk.MustExec(`create table t1 (c int primary key, d int,e int,index idx_d(d),index idx_e(e))
PARTITION BY RANGE (c) (
PARTITION p0 VALUES LESS THAN (6) placement policy p1,
PARTITION p1 VALUES LESS THAN (11) placement policy p2
);`)
defer func() {
tk.MustExec("drop table if exists t1")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop placement policy if exists p2")
}()

tk.MustExec(`insert into t1 (c,d,e) values (1,1,1);`)
tk.MustExec(`insert into t1 (c,d,e) values (2,3,5);`)
tk.MustExec(`insert into t1 (c,d,e) values (3,5,7);`)

testcases := []struct {
name string
txnScope string
zone string
sql string
expectErr error
}{
// FIXME: block by https://github.com/pingcap/tidb/issues/21872
//{
// name: "cross dc read to sh by holding bj, IndexReader",
// txnScope: "bj",
// sql: "select /*+ USE_INDEX(t1, idx_d) */ d from t1 where c < 5 and d < 1;",
// expectErr: fmt.Errorf(".*can not be read by.*"),
//},
// FIXME: block by https://github.com/pingcap/tidb/issues/21847
//{
// name: "cross dc read to sh by holding bj, BatchPointGet",
// txnScope: "bj",
// sql: "select * from t1 where c in (1,2,3,4);",
// expectErr: fmt.Errorf(".*can not be read by.*"),
//},
{
name: "cross dc read to sh by holding bj, PointGet",
txnScope: "local",
zone: "bj",
sql: "select * from t1 where c = 1",
expectErr: fmt.Errorf(".*can not be read by.*"),
},
{
name: "cross dc read to sh by holding bj, IndexLookUp",
txnScope: "local",
zone: "bj",
sql: "select * from t1 use index (idx_d) where c < 5 and d < 5;",
expectErr: fmt.Errorf(".*can not be read by.*"),
},
{
name: "cross dc read to sh by holding bj, IndexMerge",
txnScope: "local",
zone: "bj",
sql: "select /*+ USE_INDEX_MERGE(t1, idx_d, idx_e) */ * from t1 where c <5 and (d =5 or e=5);",
expectErr: fmt.Errorf(".*can not be read by.*"),
},
{
name: "cross dc read to sh by holding bj, TableReader",
txnScope: "local",
zone: "bj",
sql: "select * from t1 where c < 6",
expectErr: fmt.Errorf(".*can not be read by.*"),
},
{
name: "cross dc read to global by holding bj",
txnScope: "local",
zone: "bj",
sql: "select * from t1",
expectErr: fmt.Errorf(".*can not be read by.*"),
},
{
name: "read sh dc by holding sh",
txnScope: "local",
zone: "sh",
sql: "select * from t1 where c < 6",
expectErr: nil,
},
{
name: "read sh dc by holding global",
txnScope: "global",
zone: "",
sql: "select * from t1 where c < 6",
expectErr: nil,
},
}
tk.MustExec("set global tidb_enable_local_txn = on;")
for _, testcase := range testcases {
t.Log(testcase.name)
require.NoError(t, failpoint.Enable("tikvclient/injectTxnScope",
fmt.Sprintf(`return("%v")`, testcase.zone)))
tk.MustExec(fmt.Sprintf("set @@txn_scope='%v'", testcase.txnScope))
tk.Exec("begin")
res, err := tk.Exec(testcase.sql)
_, resErr := session.GetRows4Test(context.Background(), tk.Session(), res)
var checkErr error
if err != nil {
checkErr = err
} else {
checkErr = resErr
}
if testcase.expectErr != nil {
require.Error(t, checkErr)
require.Regexp(t, ".*can not be read by.*", checkErr.Error())
} else {
require.NoError(t, checkErr)
}
if res != nil {
res.Close()
}
tk.Exec("commit")
}
require.NoError(t, failpoint.Disable("tikvclient/injectTxnScope"))
tk.MustExec("set global tidb_enable_local_txn = off;")
}

func calculateChecksum(cols ...any) string {
buf := make([]byte, 0, 64)
for _, col := range cols {
Expand Down
14 changes: 0 additions & 14 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import (
"github.com/pingcap/tidb/pkg/domain"
"github.com/pingcap/tidb/pkg/executor/mppcoordmanager"
"github.com/pingcap/tidb/pkg/extension"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/parser/auth"
Expand Down Expand Up @@ -253,7 +252,6 @@ func NewServer(cfg *config.Config, driver IDriver) (*Server, error) {
printMDLLogTime: time.Now(),
}
s.capability = defaultCapability
setTxnScope()
setSystemTimeZoneVariable()

tlsConfig, autoReload, err := util.LoadTLSCertificates(
Expand Down Expand Up @@ -410,18 +408,6 @@ func setSSLVariable(ca, key, cert string) {
variable.SetSysVar("ssl_ca", ca)
}

func setTxnScope() {
variable.SetSysVar(variable.TiDBTxnScope, func() string {
if !variable.EnableLocalTxn.Load() {
return kv.GlobalTxnScope
}
if txnScope := config.GetTxnScopeFromConfig(); txnScope == kv.GlobalTxnScope {
return kv.GlobalTxnScope
}
return kv.LocalTxnScope
}())
}

// Export config-related metrics
func (s *Server) reportConfig() {
metrics.ConfigStatus.WithLabelValues("token-limit").Set(float64(s.cfg.TokenLimit))
Expand Down
Loading

0 comments on commit ab66aec

Please sign in to comment.