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

txn: only wake up waiters when locks are indeed released #7379

Merged
merged 6 commits into from
Apr 20, 2020

Conversation

youjiali1995
Copy link
Contributor

@youjiali1995 youjiali1995 commented Apr 7, 2020

Signed-off-by: youjiali1995 zlwgx1023@gmail.com

What problem does this PR solve?

Problem Summary:
TiKV will wake up waiters as long as it receives requests that may release locks, e.g., pessimistic_rollback, rollback, commit. If a request doesn't release locks, typically the lock doesn't exist, it needn't wake up waiters.

In TiDB, if a pessimistic DML meets write conflict, it will use pessimistic_rollback to clean up all locks it needs to lock in this DML and then retry the DML. If a transaction is waked up and there are other transactions waiting for the lock, these transactions will be waked up by pessimistic_rollback one by one. It dramatically affects performance and results in useless retry.

What is changed and how it works?

What's Changed:
Only wake up waiters when locks are indeed released and small refactor.

Related changes

  • Need to cherry-pick to the release branch

Check List

Tests

  • Manual test (add detailed scripts or steps below)
  • No code

I think existing tests are enough and I benched it using sysbench with the workload below:

con:query(CREATE TABLE wmtest(k INT PRIMARY KEY, v INT))
con:query(string.format("UPDATE wmtest SET v = v + 1 WHERE k IN (%d, 1)", sysbench.rand.uniform(2, sysbench.opt.table_size)))

master:

threads tps avg lat(ms) .95 lat(ms) .99 retry count
1 783 1.28 1.79 0
100 71 1393 2493 800
500 16 30111 72316 1457
1000 12 71181 100000 654

This PR:

threads tps avg lat(ms) .95 lat(ms) .99 retry count
100 772 129 155 1
500 724 688 787 1
1000 647 1534 1903 5

image

Release note

Fix the issue that needless wake-up results in useless retry and performance reduction in heavy contention workloads.

Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
@youjiali1995
Copy link
Contributor Author

/release

@youjiali1995 youjiali1995 added sig/transaction SIG: Transaction type/bugfix Type: PR - Fix a bug needs-cherry-pick-release-3.0 Type: Need cherry pick to release 3.0 needs-cherry-pick-release-3.1 Type: Need cherry pick to release 3.1 needs-cherry-pick-release-4.0 Type: Need cherry pick to release 4.0 labels Apr 8, 2020
@youjiali1995 youjiali1995 added this to the v3.0.13 milestone Apr 8, 2020
@youjiali1995 youjiali1995 marked this pull request as ready for review April 8, 2020 11:27
@youjiali1995 youjiali1995 requested review from nrc and sticnarf and removed request for nrc April 8, 2020 11:28
Copy link
Contributor

@sticnarf sticnarf left a comment

Choose a reason for hiding this comment

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

LGTM except a small question.

src/storage/txn/process.rs Outdated Show resolved Hide resolved
@youjiali1995
Copy link
Contributor Author

If the DML only need to lock one key, it won't send pessimistic_rollback before retry. That's why I didn't find the bug earlier:
https://github.com/pingcap/tidb/blob/b54ac5b2ec3bd1f5de37eb813d17de43cc500bf3/store/tikv/txn.go#L397

Copy link
Contributor

@nrc nrc left a comment

Choose a reason for hiding this comment

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

Looks good, a few comments inline, none are major

src/storage/mvcc/txn.rs Show resolved Hide resolved
src/storage/mvcc/txn.rs Outdated Show resolved Hide resolved
src/storage/mvcc/txn.rs Outdated Show resolved Hide resolved
}
}

fn push(&mut self, lock: ReleasedLock) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could implement a from_iter method to build the hashes Vec in one go, rather than using for loops and push

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I don't get it. I still need to push hash to a vec to get the iter..

Copy link
Contributor

Choose a reason for hiding this comment

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

So for example, rather than use

for k in keys {
    released_locks.push(txn.rollback(k)?);
}

you can use

released_locks.hashes(keys.iter().map(|k| txn.rollback(k)))?;

where hashes is something like:

fn hashes<I, T, E>(&mut self, iter: I) -> Result<(), E>
    where
        I: Iterator<Item = Result<T, E>>
{
    self.hashes = iter.collect()?;
    Ok(())
}

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 see. Thanks!

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 tried to implement it but I found it's more complex than for loop:

impl ReleasedLocks {
    fn hashes<I, E>(&mut self, iter: I) -> std::result::Result<(), E>
    where
        I: Iterator<Item = std::result::Result<Option<ReleasedLock>, E>>,
    {
        self.hashes = iter
            .filter_map(|v| match v {
                Ok(Some(lock)) => {
                    if !self.pessimistic {
                        self.pessimistic = lock.pessimistic;
                    }
                    Some(Ok(lock.hash))
                }
                Ok(None) => None,
                Err(e) => Some(Err(e)),
            })
            .collect::<std::result::Result<Vec<u64>, E>>()?;
        Ok(())
    }
}

fn process_write_impl(...) {
...
            let mut released_locks = ReleasedLocks::new(lock_ts, commit_ts);
            // for k in keys {
            // released_locks.push(txn.commit(k, commit_ts)?);
            // }
            released_locks.hashes(keys.into_iter().map(|k| txn.commit(k, commit_ts)))?;
            released_locks.wake_up(lock_mgr.as_ref());
...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And I still need to call push when processing ResolveLock..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nrc PTAL again. Thanks!

src/storage/txn/process.rs Outdated Show resolved Hide resolved
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
@youjiali1995 youjiali1995 force-pushed the not-wake-up-when-lock-not-exist branch from 2095388 to 1641dbb Compare April 14, 2020 07:54
@youjiali1995
Copy link
Contributor Author

/run-all-tests

@youjiali1995
Copy link
Contributor Author

/bench +tpcc

@sre-bot
Copy link
Contributor

sre-bot commented Apr 14, 2020

Benchmark Report

Run TPC-C Performance Test on VMs

@@                               Benchmark Diff                               @@
================================================================================
tidb: c1a31708b0a3eb8e75adbc5cf75c86926fcf4d1b
--- tikv: 2b1b9b2cd537e47591795dc034b59a0585cfe7a8
+++ tikv: 1641dbb41273c50b7a0b630295d65fc9e722076e
pd: 8438f3fc004da1bff7442229e53fe4272f74ce2d
================================================================================
Measured tpmC (NewOrders): 8296.83 ± 1.98% (std=106.77), delta: -2.84% (p=0.036)

@youjiali1995
Copy link
Contributor Author

/bench +tpcc

…p-when-lock-not-exist

Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
@youjiali1995 youjiali1995 force-pushed the not-wake-up-when-lock-not-exist branch from 1641dbb to 58feb08 Compare April 14, 2020 12:18
@sre-bot
Copy link
Contributor

sre-bot commented Apr 14, 2020

Benchmark Report

Run TPC-C Performance Test on VMs

@@                               Benchmark Diff                               @@
================================================================================
tidb: c1a31708b0a3eb8e75adbc5cf75c86926fcf4d1b
--- tikv: 7935019849d46b7d32b1a6b0d14e795cd7da1591
+++ tikv: 1641dbb41273c50b7a0b630295d65fc9e722076e
pd: 8438f3fc004da1bff7442229e53fe4272f74ce2d
================================================================================
Measured tpmC (NewOrders): 7007.72 ± 1.01% (std=66.19), delta: -1.77% (p=0.072)

Copy link
Contributor

@nrc nrc left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the changes!

@youjiali1995
Copy link
Contributor Author

@MyonKeminta PTAL

@sre-bot
Copy link
Contributor

sre-bot commented Apr 20, 2020

/run-all-tests

@sre-bot sre-bot merged commit b4dd42f into tikv:master Apr 20, 2020
sre-bot pushed a commit to sre-bot/tikv that referenced this pull request Apr 20, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
@sre-bot
Copy link
Contributor

sre-bot commented Apr 20, 2020

cherry pick to release-3.0 in PR #7549

sre-bot pushed a commit to sre-bot/tikv that referenced this pull request Apr 20, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
@sre-bot
Copy link
Contributor

sre-bot commented Apr 20, 2020

cherry pick to release-3.1 in PR #7550

sre-bot pushed a commit to sre-bot/tikv that referenced this pull request Apr 20, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
@sre-bot
Copy link
Contributor

sre-bot commented Apr 20, 2020

cherry pick to release-4.0 in PR #7551

@youjiali1995 youjiali1995 deleted the not-wake-up-when-lock-not-exist branch April 20, 2020 06:42
youjiali1995 added a commit that referenced this pull request Apr 21, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
youjiali1995 added a commit to sre-bot/tikv that referenced this pull request Apr 21, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
youjiali1995 added a commit to youjiali1995/tikv that referenced this pull request Apr 21, 2020
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
youjiali1995 added a commit to youjiali1995/tikv that referenced this pull request Apr 21, 2020
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
youjiali1995 added a commit to youjiali1995/tikv that referenced this pull request Apr 21, 2020
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
youjiali1995 added a commit to youjiali1995/tikv that referenced this pull request Apr 21, 2020
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
youjiali1995 added a commit to youjiali1995/tikv that referenced this pull request Apr 21, 2020
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
youjiali1995 added a commit to youjiali1995/tikv that referenced this pull request Apr 21, 2020
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
youjiali1995 added a commit to sre-bot/tikv that referenced this pull request Apr 21, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
sre-bot pushed a commit that referenced this pull request Apr 21, 2020
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
sre-bot added a commit that referenced this pull request Apr 22, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
youjiali1995 added a commit that referenced this pull request Apr 27, 2020
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
c1ay pushed a commit to c1ay/tikv that referenced this pull request May 9, 2020
Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>
solotzg added a commit to pingcap/tidb-engine-ext that referenced this pull request May 14, 2020
txn: only wake up waiters when locks are indeed released (tikv#7379) (tikv#7585)

Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>

txn: don't protect rollback for BatchRollback (tikv#7605) (tikv#7608)

Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>

tidb_query: add is true/false keep null ScalarFuncSig (tikv#7532) (tikv#7566)

Signed-off-by: zhongzc <zhongzc_arch@outlook.com>

tidb_query: fix the logical behavior of floats (tikv#7342) (tikv#7582)

Signed-off-by: zhongzc <zhongzc_arch@outlook.com>

tidb_query: fix converting bytes to bool (tikv#7486) (tikv#7547)

Signed-off-by: zhongzc <zhongzc_arch@outlook.com>

raftstore: change the condition of proposing rollback merge (tikv#6584) (tikv#7762)

Signed-off-by: Liqi Geng <gengliqiii@gmail.com>
Signed-off-by: Tong Zhigao <tongzhigao@pingcap.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-cherry-pick-release-3.0 Type: Need cherry pick to release 3.0 needs-cherry-pick-release-3.1 Type: Need cherry pick to release 3.1 needs-cherry-pick-release-4.0 Type: Need cherry pick to release 4.0 sig/transaction SIG: Transaction status/can-merge Status: Can merge to base branch type/bugfix Type: PR - Fix a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants