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 (release-6.5) #8593

Merged

Conversation

JaySon-Huang
Copy link
Contributor

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

cherry-pick of #8584

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 do-not-merge/cherry-pick-not-approved release-note size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels 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:57:40.473029124 +0000 UTC m=+1559751.510256050: ☑️ agreed by Lloyd-Pottiger.
  • 2023-12-26 10:07:00.985531652 +0000 UTC m=+1560312.022758579: ☑️ agreed by CalvinNeo.

@ti-chi-bot ti-chi-bot added the cherry-pick-approved Cherry pick PR approved by release team. label Dec 26, 2023
@ti-chi-bot ti-chi-bot bot merged commit 1226495 into pingcap:release-6.5 Dec 26, 2023
5 checks passed
@JaySon-Huang JaySon-Huang deleted the refine_region_persister_65 branch December 26, 2023 10:19
JaySon-Huang added a commit to ti-chi-bot/tiflash that referenced this pull request Dec 26, 2023
JaySon-Huang added a commit to ti-chi-bot/tiflash that referenced this pull request Dec 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved cherry-pick-approved Cherry pick PR approved by release team. lgtm 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.

None yet

4 participants