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

planner, distsql: fix the Stale Read and Local Txn compatibility #31606

Merged
merged 3 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions distsql/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ func (builder *RequestBuilder) SetResourceGroupTagger(sc *stmtctx.StatementConte
}

func (builder *RequestBuilder) verifyTxnScope() error {
// Stale Read uses the calculated TSO for the read,
// so there is no need to check the TxnScope here.
if builder.IsStaleness {
return nil
}
if builder.ReadReplicaScope == "" {
builder.ReadReplicaScope = kv.GlobalReplicaScope
}
Expand Down
2 changes: 2 additions & 0 deletions executor/point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ func (e *PointGetExecutor) get(ctx context.Context, key kv.Key) ([]byte, error)
}

func (e *PointGetExecutor) verifyTxnScope() error {
// Stale Read uses the calculated TSO for the read,
// so there is no need to check the TxnScope here.
if e.isStaleness {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,10 @@ func (p *preprocessor) handleAsOfAndReadTS(node *ast.AsOfClause) {
p.LastSnapshotTS = ts
p.IsStaleness = true
}
case readTS == 0 && node == nil && readStaleness == 0:
// If both readTS and node is empty while the readStaleness is empty, it means we may be in a local txn,
// so setting p.ReadReplicaScope is necessary to verify the txn scope later.
p.ReadReplicaScope = scope
}

// If the select statement is related to multi tables, we should grantee that all tables use the same timestamp
Expand Down
25 changes: 25 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3726,6 +3726,7 @@ PARTITION BY RANGE (c) (
result = tk.MustQuery("select * from t1")
c.Assert(len(result.Rows()), Equals, 2)

timeBeforeWriting := time.Now()
tk.MustExec("insert into t1 (c) values (101)") // write dc-2 with global scope
result = tk.MustQuery("select * from t1") // read dc-1 and dc-2 with global scope
c.Assert(len(result.Rows()), Equals, 3)
Expand Down Expand Up @@ -3774,6 +3775,9 @@ PARTITION BY RANGE (c) (
_, err = tk.Exec("insert into t1 (c) values (101)") // write dc-2 with dc-1 scope
c.Assert(err, NotNil)
c.Assert(err.Error(), Matches, ".*out of txn_scope.*")
err = tk.ExecToErr("select * from t1 where c > 100") // read dc-2 with dc-1 scope
c.Assert(err, NotNil)
c.Assert(err.Error(), Matches, ".*can not be read by.*")
tk.MustExec("begin")
err = tk.ExecToErr("select * from t1 where c > 100") // read dc-2 with dc-1 scope
c.Assert(err, NotNil)
Expand All @@ -3800,6 +3804,27 @@ PARTITION BY RANGE (c) (
// Won't read the value 99 because the previous commit failed
result = tk.MustQuery("select * from t1 where c < 100") // read dc-1 with dc-1 scope
c.Assert(len(result.Rows()), Equals, 4)

// Stale Read will ignore the cross-dc txn scope.
c.Assert(tk.Se.GetSessionVars().CheckAndGetTxnScope(), Equals, "dc-1")
result = tk.MustQuery("select @@txn_scope;")
result.Check(testkit.Rows("local"))
err = tk.ExecToErr("select * from t1 where c > 100") // read dc-2 with dc-1 scope
c.Assert(err, NotNil)
c.Assert(err.Error(), Matches, ".*can not be read by.*")
// Read dc-2 with Stale Read (in dc-1 scope)
timestamp := timeBeforeWriting.Format(time.RFC3339Nano)
// TODO: check the result of Stale Read when we figure out how to make the time precision more accurate.
tk.MustExec(fmt.Sprintf("select * from t1 AS OF TIMESTAMP '%s' where c > 100", timestamp))
tk.MustExec(fmt.Sprintf("START TRANSACTION READ ONLY AS OF TIMESTAMP '%s'", timestamp))
tk.MustExec("select * from t1 where c > 100")
tk.MustExec("commit")
tk.MustExec("set @@tidb_replica_read='closest-replicas'")
tk.MustExec(fmt.Sprintf("select * from t1 AS OF TIMESTAMP '%s' where c > 100", timestamp))
tk.MustExec(fmt.Sprintf("START TRANSACTION READ ONLY AS OF TIMESTAMP '%s'", timestamp))
tk.MustExec("select * from t1 where c > 100")
tk.MustExec("commit")

tk.MustExec("set global tidb_enable_local_txn = off;")
}

Expand Down