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

tikv/txn: support local latch in transaction #6418

Merged
merged 22 commits into from
May 7, 2018

Conversation

AndreMouche
Copy link
Contributor

if err == nil {
commitTS = committer.commitTS
}
txn.store.txnLatches.UnLock(lock, commitTS)
Copy link
Member

Choose a reason for hiding this comment

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

The commitTS is not used in UnLock.

Copy link
Member

Choose a reason for hiding this comment

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

Can we put commitTS into lock?

@@ -85,6 +85,7 @@ func (scheduler *LatchesScheduler) Lock(startTS uint64, keys [][]byte) *Lock {

// UnLock unlocks a lock with commitTS.
func (scheduler *LatchesScheduler) UnLock(lock *Lock, commitTS uint64) {
lock.commitTS = commitTS
Copy link
Member

Choose a reason for hiding this comment

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

We can remove the argument, and set commitTS before UnLock.

store/tikv/kv.go Outdated
@@ -165,7 +168,7 @@ func (s *tikvStore) CheckVisibility(startTime uint64) error {
return nil
}

func newTikvStore(uuid string, pdClient pd.Client, spkv SafePointKV, client Client, enableGC bool) (*tikvStore, error) {
func newTikvStore(uuid string, pdClient pd.Client, spkv SafePointKV, client Client, enableGC, enableTxnLocalLatches bool) (*tikvStore, error) {
Copy link
Member

Choose a reason for hiding this comment

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

There are too many arguments to new TiKVStore.
Can we remove enableGC and enableTxnLocalLatches, and set them in a method when we want the non-default value?

@coocood
Copy link
Member

coocood commented Apr 28, 2018

@AndreMouche
We can enable the latch in unit tests.

@coocood
Copy link
Member

coocood commented Apr 28, 2018

@lamxTyler PTAL

return context.WithValue(ctx, Retryable, retryAble)
}

// GetRetryable returns the value of GetRetryable from the ctx.
Copy link
Contributor

Choose a reason for hiding this comment

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

value of retryable.


// GetRetryable returns the value of GetRetryable from the ctx.
func GetRetryable(ctx context.Context) bool {
var retryAble bool
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is no need to capitalize the a.

err := c.execute(ctx)
if err != nil {
c.writeFinishBinlog(binlog.BinlogType_Rollback, 0)

Copy link
Contributor

Choose a reason for hiding this comment

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

redundant blank line.

// for transactions not retryable, commit directly.
if !sessionctx.GetRetryable(ctx) {
err = committer.executeAndWriteFinishBinlog(ctx)
txn.store.txnLatches.RefreshCommitTS(committer.keys, committer.startTS)
Copy link
Contributor

@alivxxx alivxxx Apr 28, 2018

Choose a reason for hiding this comment

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

Why use the startTS to refresh? Can we refresh if an error occurred when commit?

@tiancaiamao tiancaiamao added the require-LGT3 Indicates that the PR requires three LGTM. label Apr 28, 2018
@@ -185,20 +184,41 @@ func (txn *tikvTxn) Commit(ctx context.Context) error {
connID = val.(uint64)
}
committer, err := newTwoPhaseCommitter(txn, connID)
if err != nil {
if err != nil || committer == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

When will commiter == nil ? and if it's really nil, the error trace would be oddly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When no row needs to update in this transaction.

const Retryable contextKey = "Retryable"

// SetCommitCtx sets the variables for context before commit a transaction.
func SetCommitCtx(ctx context.Context, sessCtx Context) context.Context {
Copy link
Contributor

Choose a reason for hiding this comment

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

Pass such important information through context.Context is not reliable.
If the call just pass a context.Background, those information will loss.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any suggestion?

@tiancaiamao
Copy link
Contributor

/run-all-tests

@disksing
Copy link
Contributor

disksing commented May 2, 2018

LGTM.

@AndreMouche
Copy link
Contributor Author

/run-all-tests

@AndreMouche
Copy link
Contributor Author

/run-all-tests

@jackysp
Copy link
Member

jackysp commented May 2, 2018

/run-unit-test

@AndreMouche
Copy link
Contributor Author

/run-all-tests

Copy link
Contributor

@tiancaiamao tiancaiamao left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -54,7 +60,7 @@ func RunInNewTxn(store Storage, retryable bool, f func(txn Transaction) error) e
return errors.Trace(err)
}

err = txn.Commit(context.Background())
err = txn.Commit(context.WithValue(context.Background(), Retryable, retryable))
Copy link
Contributor

Choose a reason for hiding this comment

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

I really dislike passing those value through context, even txn.SetOption(retryable) is better than this.
If you insist, I'll refactor the code here after it's merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried your suggestion and it's a little complex. I think to refactor the code in another PR is
ok for me.

@@ -84,7 +84,7 @@ func (scheduler *LatchesScheduler) Lock(startTS uint64, keys [][]byte) *Lock {
}

// UnLock unlocks a lock with commitTS.
func (scheduler *LatchesScheduler) UnLock(lock *Lock, commitTS uint64) {
func (scheduler *LatchesScheduler) UnLock(lock *Lock) {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO, this API design is worse than before, because the user may forget to call lock.SetCommitTS after UnLock, while previous API make a stronger contract.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both are ok for me. It's suggested by @coocood , maybe I could update it when you reach an agreement?

return nil

defer func() {
if err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this can be moved into executeAndWriteFinishBinlog, so defer is unnecessary

@tiancaiamao tiancaiamao added the status/LGT2 Indicates that a PR has LGTM 2. label May 3, 2018
@AndreMouche
Copy link
Contributor Author

/run-all-tests

@AndreMouche
Copy link
Contributor Author

friendly ping @coocood

@coocood
Copy link
Member

coocood commented May 7, 2018

LGTM

@coocood coocood added status/LGT3 The PR has already had 3 LGTM. and removed status/LGT2 Indicates that a PR has LGTM 2. labels May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
require-LGT3 Indicates that the PR requires three LGTM. status/LGT3 The PR has already had 3 LGTM.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants