Skip to content

gc_state_manager: Make methods of GCStateManager able to timeout or cancel#10606

Draft
MyonKeminta wants to merge 2 commits intotikv:masterfrom
MyonKeminta:m/gc-state-manager-avoid-long-mutex-wait
Draft

gc_state_manager: Make methods of GCStateManager able to timeout or cancel#10606
MyonKeminta wants to merge 2 commits intotikv:masterfrom
MyonKeminta:m/gc-state-manager-avoid-long-mutex-wait

Conversation

@MyonKeminta
Copy link
Copy Markdown
Contributor

@MyonKeminta MyonKeminta commented Apr 20, 2026

What problem does this PR solve?

Issue Number: Close #10607

What is changed and how does it work?

In this PR:

  • Implemented a simple cancellable RWMutex by using semaphore internally
  • Added OrderedSingleFlightGroup
  • In GCStateManager, Replace the original RWLock with the CancellableRWMutex, and set timeout for its APIs
  • Use read lock instead of write lock in GetGCState

We've encountered a case that some GC APIs are accumulating waiting for the mutex. By making these changes, the case should be able to be alleviated.

gc_state_manager: Make methods of GCStateManager able to timeout or cancel

* Implemented a simple cancellable `RWMutex` by using semaphore internally
* Added `OrderedSingleFlightGroup`
* In `GCStateManager`, Replace the original `RWLock` with the `CancellableRWMutex`, and set timeout for its APIs
* Use read lock instead of write lock in GetGCState

Check List

Tests

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

Code changes

Side effects

  • Possible performance regression
  • Increased code complexity
  • Breaking backward compatibility

Related changes

Release note

None.

Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
@ti-chi-bot ti-chi-bot Bot added do-not-merge/needs-linked-issue do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. labels Apr 20, 2026
@ti-chi-bot
Copy link
Copy Markdown
Contributor

ti-chi-bot Bot commented Apr 20, 2026

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot Bot added the dco-signoff: yes Indicates the PR's author has signed the dco. label Apr 20, 2026
@ti-chi-bot
Copy link
Copy Markdown
Contributor

ti-chi-bot Bot commented Apr 20, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign hundundm for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

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

Details Needs approval from an approver in each of these files:

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

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 20, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a94a52c3-2cc5-4cf9-ac46-8210016ca104

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ti-chi-bot ti-chi-bot Bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed do-not-merge/needs-linked-issue labels Apr 20, 2026
@MyonKeminta
Copy link
Copy Markdown
Contributor Author

/retest

@ti-chi-bot
Copy link
Copy Markdown
Contributor

ti-chi-bot Bot commented Apr 20, 2026

@MyonKeminta: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-unit-test-next-gen-1 aff7ba3 link true /test pull-unit-test-next-gen-1
pull-unit-test-next-gen-3 aff7ba3 link true /test pull-unit-test-next-gen-3
pull-unit-test-next-gen-2 aff7ba3 link true /test pull-unit-test-next-gen-2

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 20, 2026

Codecov Report

❌ Patch coverage is 81.39535% with 40 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.98%. Comparing base (b21a183) to head (aff7ba3).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10606      +/-   ##
==========================================
+ Coverage   78.96%   78.98%   +0.01%     
==========================================
  Files         532      533       +1     
  Lines       71883    72123     +240     
==========================================
+ Hits        56766    56965     +199     
- Misses      11093    11114      +21     
- Partials     4024     4044      +20     
Flag Coverage Δ
unittests 78.98% <81.39%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

// layer. It can be more efficient and avoid failures due to transaction conflict in most cases.
// The etcd transactions is still necessary considering the possibility of rare cases like PD leader changes.
mu syncutil.RWMutex
mu syncutil.CancellableRWMutex
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need a cancellable mutex?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As it may involve etcd IO in its critical section, and it may need a long time to successfully acquire the lock. Without the ability to cancel or timeout, the goroutines waiting for it would be unable to exit, and the memory used by them can't be released either.

})
}

func (m *GCStateManager) getGCStateImpl(ctx context.Context, keyspaceID uint32) (GCState, error) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When the context is cancelled, it cannot cancel the transaction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a problem but it's unable to pass ctx to the StorageEndpoint API now. Seems it requires a large refactor.


// RLock acquires a read lock or returns an error if ctx is canceled before the
// lock is acquired.
func (m *CancellableRWMutex) RLock(ctx context.Context) error {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The deadlock testing does not work anymore.


func (m *CancellableRWMutex) init() {
m.initOnce.Do(func() {
m.inner = semaphore.NewWeighted(maxReaderCount)
Copy link
Copy Markdown
Member

@rleungx rleungx Apr 20, 2026

Choose a reason for hiding this comment

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

Will the read be blocked more easily?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The overhead of the lock itself is larger than the builtin RWMutex, but in the current use case I think it won't

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dco-signoff: yes Indicates the PR's author has signed the dco. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GCStateManager calls may be blocked and unable to cancel or timeout when there's IO issue

2 participants