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

KVStore: Reduce lock contention in RegionPersister::doPersist #8584

Merged
merged 5 commits into from
Dec 26, 2023

Conversation

JaySon-Huang
Copy link
Contributor

@JaySon-Huang JaySon-Huang commented Dec 25, 2023

What problem does this PR solve?

Issue Number: close #8583

Problem Summary:

void RegionPersister::doPersist(
RegionCacheWriteElement & region_write_buffer,
const RegionTaskLock & region_task_lock,
const Region & region)
{
auto & [region_id, buffer, region_size, applied_index] = region_write_buffer;
std::lock_guard lock(mutex);
auto entry = page_reader->getPageEntry(region_id);
if (entry.isValid() && entry.tag > applied_index)
return;
if (region.isPendingRemove())
{
LOG_DEBUG(log, "no need to persist {} because of pending remove", region.toString(false));
return;
}
auto read_buf = buffer.tryGetReadBuffer();
RUNTIME_CHECK_MSG(read_buf != nullptr, "failed to gen buffer for {}", region.toString(true));
DB::WriteBatchWrapper wb{run_mode, getWriteBatchPrefix()};
wb.putPage(region_id, applied_index, read_buf, region_size);
page_writer->write(std::move(wb), global_context.getWriteLimiter());
region.updateLastCompactLogApplied(region_task_lock);
}

RegionPersister::doPersist acquires a lock and calls PageStorage::write with the lock. This means when we're persisting Region meta, all other threads can not do the persistence for other Region meta. Which is not reasonable.

What is changed and how it works?

There is a region_task_lock to ensure that only one thread could call RegionPersister::doPersist for the same region_id at the same time. And PageStorage support multi-thread reading and writing for different page_id. There is no need to acquire another lock in RegionPersister::doPersist that could slowdown the raft apply process

And also remove calling KVStore::persistRegion with a null region_task_lock. So we can simplify codes and remove the member RegionPersister:: RegionManager

Check List

Tests

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

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Reduce the impact of disk performance jitter on read latency

@ti-chi-bot ti-chi-bot bot added needs-cherry-pick-release-5.4 Type: Need cherry pick to release-5.4 needs-cherry-pick-release-6.1 Type: Need cherry pick to release-6.1 release-note needs-cherry-pick-release-6.5 do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-cherry-pick-release-7.1 needs-cherry-pick-release-7.5 size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 25, 2023
@JaySon-Huang
Copy link
Contributor Author

/run-all-tests

@JaySon-Huang JaySon-Huang changed the title [WIP] KVStore: Reduce lock contention in RegionPersister::doPersist KVStore: Reduce lock contention in RegionPersister::doPersist Dec 26, 2023
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 26, 2023
@JaySon-Huang
Copy link
Contributor Author

/run-all-tests

Copy link
Member

@CalvinNeo CalvinNeo left a comment

Choose a reason for hiding this comment

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

lgtm

@ti-chi-bot ti-chi-bot bot added the lgtm label Dec 26, 2023
Copy link
Contributor

ti-chi-bot bot commented Dec 26, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: CalvinNeo, Lloyd-Pottiger

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [CalvinNeo,Lloyd-Pottiger]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Contributor

ti-chi-bot bot commented Dec 26, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-12-26 09:50:26.515807008 +0000 UTC m=+1559317.553033929: ☑️ agreed by CalvinNeo.
  • 2023-12-26 09:56:39.045213705 +0000 UTC m=+1559690.082440617: ☑️ agreed by Lloyd-Pottiger.

@ti-chi-bot ti-chi-bot bot merged commit 0329ed4 into pingcap:master Dec 26, 2023
6 checks passed
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-5.4: #8595.

ti-chi-bot pushed a commit to ti-chi-bot/tiflash that referenced this pull request Dec 26, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.1: #8596.

ti-chi-bot pushed a commit to ti-chi-bot/tiflash that referenced this pull request Dec 26, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.5: #8597.

ti-chi-bot pushed a commit to ti-chi-bot/tiflash that referenced this pull request Dec 26, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.1: #8598.

ti-chi-bot pushed a commit to ti-chi-bot/tiflash that referenced this pull request Dec 26, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
ti-chi-bot pushed a commit to ti-chi-bot/tiflash that referenced this pull request Dec 26, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.5: #8599.

@JaySon-Huang JaySon-Huang deleted the refine_region_persister branch December 26, 2023 10:12
windtalker pushed a commit to windtalker/tiflash that referenced this pull request Dec 27, 2023
JaySon-Huang added a commit to JaySon-Huang/tiflash that referenced this pull request Dec 27, 2023
ti-chi-bot bot pushed a commit that referenced this pull request Jan 1, 2024
@JaySon-Huang
Copy link
Contributor Author

Change the release note from "write throughput" to "read latency"

Tests with some failpoint injects (99%-10ms and %0.7-100ms and %0.2-1000ms) latency on RegionPersister::doPersist show that the changes mainly affect the read latency caused by wait index but not the write throughput

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm needs-cherry-pick-release-5.4 Type: Need cherry pick to release-5.4 needs-cherry-pick-release-6.1 Type: Need cherry pick to release-6.1 needs-cherry-pick-release-6.5 needs-cherry-pick-release-7.1 needs-cherry-pick-release-7.5 release-note size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The lock in RegionPersister::doPersist slow down raft apply when meets jitter
4 participants