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

store/tikv: remove use of GuaranteeLinearizability option in store/tikv #24605

Merged
merged 7 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -517,7 +517,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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val == nil seems impossible because IsCasualConsistency always returns a bool?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some other mock store implementations. I guess this way is better as it requires less detail of underlying engine.

// 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.
Expand Down
4 changes: 4 additions & 0 deletions store/driver/txn/txn_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ func (txn *tikvTxn) SetOption(opt int, val interface{}) {
txn.SetEnableAsyncCommit(val.(bool))
case tikvstore.Enable1PC:
txn.SetEnable1PC(val.(bool))
case tikvstore.GuaranteeLinearizability:
txn.SetCausalConsistency(!val.(bool))
case tikvstore.TxnScope:
txn.SetScope(val.(string))
case tikvstore.IsStalenessReadOnly:
Expand All @@ -175,6 +177,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:
Expand Down
4 changes: 1 addition & 3 deletions store/tikv/2pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,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 {
Expand Down
3 changes: 1 addition & 2 deletions store/tikv/tests/async_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -127,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.SetOption(kv.GuaranteeLinearizability, true)
txn.SetCausalConsistency(false)
return txn
}

Expand Down
2 changes: 1 addition & 1 deletion store/tikv/tests/snapshot_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (s *testSnapshotFailSuite) TestRetryPointGetResolveTS(c *C) {
c.Assert(err, IsNil)
txn.SetEnableAsyncCommit(false)
txn.SetEnable1PC(false)
txn.SetOption(kv.GuaranteeLinearizability, false)
txn.SetCausalConsistency(true)

// Prewrite the lock without committing it
c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/beforeCommit", `pause`), IsNil)
Expand Down
14 changes: 14 additions & 0 deletions store/tikv/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type KVTxn struct {
isPessimistic bool
enableAsyncCommit bool
enable1PC bool
causalConsistency bool
scope string
kvFilter KVFilter
}
Expand Down Expand Up @@ -283,6 +284,13 @@ func (txn *KVTxn) SetEnable1PC(b bool) {
txn.enable1PC = b
}

// SetCausalConsistency indicates if the transaction does not need to
// guarantee linearizability. Default value is false which means
// linearizability is guaranteed.
func (txn *KVTxn) SetCausalConsistency(b bool) {
txn.causalConsistency = b
}

// SetScope sets the geographical scope of the transaction.
func (txn *KVTxn) SetScope(scope string) {
txn.scope = scope
Expand All @@ -298,6 +306,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
Expand Down