From 6c8106b818825b46f7e9eb065f262ce3c4ff3422 Mon Sep 17 00:00:00 2001 From: disksing Date: Wed, 12 May 2021 18:45:43 +0800 Subject: [PATCH 1/3] store/tikv: remove use of GuaranteeLinearbility option in store/tikv Signed-off-by: disksing --- session/session.go | 2 +- store/driver/txn/txn_driver.go | 4 ++++ store/tikv/2pc.go | 4 +--- store/tikv/tests/async_commit_test.go | 2 +- store/tikv/tests/snapshot_fail_test.go | 2 +- store/tikv/txn.go | 14 ++++++++++++++ 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/session/session.go b/session/session.go index 2f842f92e183a..eb114e55ef780 100644 --- a/session/session.go +++ b/session/session.go @@ -500,7 +500,7 @@ func (s *session) doCommit(ctx context.Context) error { s.txn.SetOption(tikvstore.EnableAsyncCommit, s.GetSessionVars().EnableAsyncCommit) s.txn.SetOption(tikvstore.Enable1PC, s.GetSessionVars().Enable1PC) // priority of the sysvar is lower than `start transaction with causal consistency only` - if s.txn.GetOption(tikvstore.GuaranteeLinearizability) == nil { + if val := s.txn.GetOption(tikvstore.GuaranteeLinearizability); val == nil || val.(bool) { // We needn't ask the TiKV client to guarantee linearizability for auto-commit transactions // because the property is naturally holds: // We guarantee the commitTS of any transaction must not exceed the next timestamp from the TSO. diff --git a/store/driver/txn/txn_driver.go b/store/driver/txn/txn_driver.go index 50bba80d2b54e..4f6827aa4236a 100644 --- a/store/driver/txn/txn_driver.go +++ b/store/driver/txn/txn_driver.go @@ -152,6 +152,8 @@ func (txn *tikvTxn) SetOption(opt int, val interface{}) { txn.SetCommitCallback(val.(func(string, error))) case tikvstore.Enable1PC: txn.SetEnable1PC(val.(bool)) + case tikvstore.GuaranteeLinearizability: + txn.SetCasualConsistency(!val.(bool)) case tikvstore.TxnScope: txn.SetScope(val.(string)) default: @@ -161,6 +163,8 @@ func (txn *tikvTxn) SetOption(opt int, val interface{}) { func (txn *tikvTxn) GetOption(opt int) interface{} { switch opt { + case tikvstore.GuaranteeLinearizability: + return !txn.KVTxn.IsCasualConsistency() case tikvstore.TxnScope: return txn.KVTxn.GetScope() default: diff --git a/store/tikv/2pc.go b/store/tikv/2pc.go index 8703b1861c65d..c3dee8286204f 100644 --- a/store/tikv/2pc.go +++ b/store/tikv/2pc.go @@ -856,9 +856,7 @@ func (c *twoPhaseCommitter) checkOnePC() bool { } func (c *twoPhaseCommitter) needLinearizability() bool { - GuaranteeLinearizabilityOption := c.txn.us.GetOption(kv.GuaranteeLinearizability) - // by default, guarantee - return GuaranteeLinearizabilityOption == nil || GuaranteeLinearizabilityOption.(bool) + return !c.txn.causalConsistency } func (c *twoPhaseCommitter) isAsyncCommit() bool { diff --git a/store/tikv/tests/async_commit_test.go b/store/tikv/tests/async_commit_test.go index 0f4985fa7ab86..3422ab99cd68c 100644 --- a/store/tikv/tests/async_commit_test.go +++ b/store/tikv/tests/async_commit_test.go @@ -127,7 +127,7 @@ func (s *testAsyncCommitCommon) mustGetNoneFromSnapshot(c *C, version uint64, ke func (s *testAsyncCommitCommon) beginAsyncCommitWithLinearizability(c *C) tikv.TxnProbe { txn := s.beginAsyncCommit(c) - txn.SetOption(kv.GuaranteeLinearizability, true) + txn.SetCasualConsistency(false) return txn } diff --git a/store/tikv/tests/snapshot_fail_test.go b/store/tikv/tests/snapshot_fail_test.go index 1360841bd743a..cf58deb790b95 100644 --- a/store/tikv/tests/snapshot_fail_test.go +++ b/store/tikv/tests/snapshot_fail_test.go @@ -212,7 +212,7 @@ func (s *testSnapshotFailSuite) TestRetryPointGetResolveTS(c *C) { c.Assert(err, IsNil) txn.SetOption(kv.EnableAsyncCommit, false) txn.SetEnable1PC(false) - txn.SetOption(kv.GuaranteeLinearizability, false) + txn.SetCasualConsistency(true) // Prewrite the lock without committing it c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/beforeCommit", `pause`), IsNil) diff --git a/store/tikv/txn.go b/store/tikv/txn.go index 1d678d010957e..d2a09b53112a1 100644 --- a/store/tikv/txn.go +++ b/store/tikv/txn.go @@ -83,6 +83,7 @@ type KVTxn struct { priority Priority isPessimistic bool enable1PC bool + causalConsistency bool scope string kvFilter KVFilter } @@ -276,6 +277,13 @@ func (txn *KVTxn) SetEnable1PC(b bool) { txn.enable1PC = b } +// SetCasualConsistency indicates if the transaction does not need to +// guarantee linearizability. Default value is false which means +// linearizability is guaranteed. +func (txn *KVTxn) SetCasualConsistency(b bool) { + txn.causalConsistency = b +} + // SetScope sets the geographical scope of the transaction. func (txn *KVTxn) SetScope(scope string) { txn.scope = scope @@ -291,6 +299,12 @@ func (txn *KVTxn) IsPessimistic() bool { return txn.isPessimistic } +// IsCasualConsistency returns if the transaction allows linearizability +// inconsistency. +func (txn *KVTxn) IsCasualConsistency() bool { + return txn.causalConsistency +} + // GetScope returns the geographical scope of the transaction. func (txn *KVTxn) GetScope() string { return txn.scope From 3baf909e12bc6b6283ba287304211a34e71fccc1 Mon Sep 17 00:00:00 2001 From: disksing Date: Thu, 13 May 2021 00:48:24 +0800 Subject: [PATCH 2/3] minor fix Signed-off-by: disksing --- store/tikv/tests/async_commit_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/store/tikv/tests/async_commit_test.go b/store/tikv/tests/async_commit_test.go index 2fea22fc3c360..77701687e448b 100644 --- a/store/tikv/tests/async_commit_test.go +++ b/store/tikv/tests/async_commit_test.go @@ -28,7 +28,6 @@ import ( "github.com/pingcap/tidb/store/mockstore/unistore" "github.com/pingcap/tidb/store/tikv" tikverr "github.com/pingcap/tidb/store/tikv/error" - "github.com/pingcap/tidb/store/tikv/kv" "github.com/pingcap/tidb/store/tikv/mockstore/cluster" "github.com/pingcap/tidb/store/tikv/oracle" "github.com/pingcap/tidb/store/tikv/tikvrpc" From a22f2c6afbd9f9cf11171c26c60998e942438901 Mon Sep 17 00:00:00 2001 From: disksing Date: Thu, 13 May 2021 11:53:55 +0800 Subject: [PATCH 3/3] fix wrong spell Signed-off-by: disksing --- store/driver/txn/txn_driver.go | 2 +- store/tikv/tests/async_commit_test.go | 2 +- store/tikv/tests/snapshot_fail_test.go | 2 +- store/tikv/txn.go | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/store/driver/txn/txn_driver.go b/store/driver/txn/txn_driver.go index 79631d7e5a187..8e8d776eea982 100644 --- a/store/driver/txn/txn_driver.go +++ b/store/driver/txn/txn_driver.go @@ -163,7 +163,7 @@ func (txn *tikvTxn) SetOption(opt int, val interface{}) { case tikvstore.Enable1PC: txn.SetEnable1PC(val.(bool)) case tikvstore.GuaranteeLinearizability: - txn.SetCasualConsistency(!val.(bool)) + txn.SetCausalConsistency(!val.(bool)) case tikvstore.TxnScope: txn.SetScope(val.(string)) case tikvstore.IsStalenessReadOnly: diff --git a/store/tikv/tests/async_commit_test.go b/store/tikv/tests/async_commit_test.go index 77701687e448b..f67482e69a44e 100644 --- a/store/tikv/tests/async_commit_test.go +++ b/store/tikv/tests/async_commit_test.go @@ -126,7 +126,7 @@ func (s *testAsyncCommitCommon) mustGetNoneFromSnapshot(c *C, version uint64, ke func (s *testAsyncCommitCommon) beginAsyncCommitWithLinearizability(c *C) tikv.TxnProbe { txn := s.beginAsyncCommit(c) - txn.SetCasualConsistency(false) + txn.SetCausalConsistency(false) return txn } diff --git a/store/tikv/tests/snapshot_fail_test.go b/store/tikv/tests/snapshot_fail_test.go index 46690fbfcfc8a..9892061c44b8d 100644 --- a/store/tikv/tests/snapshot_fail_test.go +++ b/store/tikv/tests/snapshot_fail_test.go @@ -213,7 +213,7 @@ func (s *testSnapshotFailSuite) TestRetryPointGetResolveTS(c *C) { c.Assert(err, IsNil) txn.SetEnableAsyncCommit(false) txn.SetEnable1PC(false) - txn.SetCasualConsistency(true) + txn.SetCausalConsistency(true) // Prewrite the lock without committing it c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/beforeCommit", `pause`), IsNil) diff --git a/store/tikv/txn.go b/store/tikv/txn.go index e087b8e03af68..baeaa6b7fa508 100644 --- a/store/tikv/txn.go +++ b/store/tikv/txn.go @@ -284,10 +284,10 @@ func (txn *KVTxn) SetEnable1PC(b bool) { txn.enable1PC = b } -// SetCasualConsistency indicates if the transaction does not need to +// SetCausalConsistency indicates if the transaction does not need to // guarantee linearizability. Default value is false which means // linearizability is guaranteed. -func (txn *KVTxn) SetCasualConsistency(b bool) { +func (txn *KVTxn) SetCausalConsistency(b bool) { txn.causalConsistency = b }