Skip to content

domain: add idle GC for cross-keyspace runtimes - #69013

Merged
ti-chi-bot[bot] merged 7 commits into
pingcap:masterfrom
D3Hunter:crossks-gc
Jun 8, 2026
Merged

ti-chi-bot[bot] merged 7 commits into
pingcap:masterfrom
D3Hunter:crossks-gc

Conversation

@D3Hunter

@D3Hunter D3Hunter commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: ref #68883

Problem Summary:

NextGen cross-keyspace runtimes can be created for DXF work and then become idle after their runtime handles are released. Each runtime owns resources such as the target keyspace store, session pool, schema syncer, etcd client, and background loops, so the cross-keyspace manager needs an idle reclamation path that keeps active holders alive while eventually closing released runtimes.
it's now wired up in this pr, we will do it in later pr

What changed and how does it work?

This PR adds an idle GC loop entrypoint to crossks.Manager. The sweeper skips runtimes with active holders, skips entries that have never been released, and evicts entries whose last release is older than the idle timeout. Evicted runtimes are removed from the manager map while holding the manager lock, then their session managers are closed outside the lock.

The change keeps Release() idempotent, preserves active runtime ownership, allows a later acquire to create a fresh runtime after eviction, and documents the remaining direct GetOrCreate callers as temporary lifecycle-management exceptions until they can move to Acquire.

Check List

Tests

  • Unit test
    • make bazel_prepare
    • ./tools/check/failpoint-go-test.sh pkg/domain/crossks -run 'Test(AcquireRuntimeHandle|EvictRuntime|RuntimeHandleManagerCloseClosesAllEntriesRegardlessOfIdleTimeout|GCLoopExitsWhenContextCancelled)' -count=1 -tags=intest,deadlock,nextgen
    • make lint
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

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

Please refer to Release Notes Language Style Guide to write a quality release note.

None

Summary by CodeRabbit

  • New Features

    • Added automatic garbage collection for idle cross-keyspace runtimes to optimize resource cleanup.
  • Tests

    • Enhanced test coverage for runtime eviction behavior and manager lifecycle management.
  • Documentation

    • Updated code comments for improved clarity.
  • Chores

    • Updated build configuration.

@ti-chi-bot ti-chi-bot Bot added the release-note-none Denotes a PR that doesn't merit a release note. label Jun 8, 2026
@pantheon-ai

pantheon-ai Bot commented Jun 8, 2026 •

Copy link
Copy Markdown

@D3Hunter I've received your pull request and will start the review. I'll conduct a thorough review covering code quality, potential issues, and implementation details.

⏳ This process typically takes 10-30 minutes depending on the complexity of the changes.

ℹ️ Learn more details on Pantheon AI.

@ti-chi-bot ti-chi-bot Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 8, 2026
@coderabbitai

coderabbitai Bot commented Jun 8, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR implements periodic garbage collection for cross-keyspace runtimes by introducing RunSystemKSGCLoop, which creates a ticker and evicts runtimes idle longer than a configured timeout. Idle eviction scans runtime entries with no active holders, removes them from the manager under lock, and closes their session managers outside the lock. Test scaffolding is enhanced to track resource lifecycle, and comprehensive tests verify eviction logic, manager shutdown, and GC loop cancellation behavior.

Changes

Cross-keyspace Runtime GC Implementation

Layer / File(s) Summary
GC constants and eviction loop
pkg/domain/crossks/cross_ks.go
Introduces crossKSIdleTimeout and crossKSSweepInterval constants; RunSystemKSGCLoop creates a ticker and triggers sweepIdleRuntimes on each tick or context cancellation. sweepIdleRuntimes locks the manager, identifies runtimes with zero active holders and lastReleaseAt older than idle timeout, deletes them from m.runtimes, logs eviction, and closes their session managers outside the lock.
Test store and session pool instrumentation
pkg/domain/crossks/cross_ks_internal_test.go (lines 18–87)
Adds close-count tracking to runtimeHandleTestStore, an onClose callback to runtimeHandleTestSessPool for lock assertions, and a newTestSessionManager helper that fully initializes a SessionManager with context, cancel, exit channel, etcd client, and in-memory schema version syncer.
GC eviction and manager lifecycle tests
pkg/domain/crossks/cross_ks_internal_test.go (lines 265–387)
TestEvictRuntime validates that eviction skips runtimes with active holders, closes idle entries outside the manager lock via sessPool.onClose assertions, and recreates a new runtime on reacquire. TestRuntimeHandleManagerCloseClosesAllEntriesRegardlessOfIdleTimeout asserts Manager.Close() closes all entries regardless of idle timeout. TestGCLoopExitsWhenContextCancelled ensures RunSystemKSGCLoop exits when its context is cancelled.
Test configuration and documentation
pkg/domain/crossks/BUILD.bazel, pkg/domain/crossks/cross_ks.go, pkg/domain/domain.go
Increases crossks_test shard count to 7 and adds //pkg/ddl/schemaver dependency. Updates comment near release tracking in cross_ks.go; clarifies loadSysKSInfoSchema documentation and adds cross-reference to GetKSStore comments in domain.go.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Possibly related PRs

  • pingcap/tidb#68890: Introduces the Manager.runtimes entry tracking with active holders and last release time that this PR's GC eviction logic directly depends on.

Suggested labels

size/XXL

Suggested reviewers

  • joechenrh
  • wjhuang2016

Poem

🐇 A rabbit's ode to cleanup true,
Where idle runtimes bid adieu,
The sweeper ticks, the locks stay tight,
And garbage flows without a fight! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding idle garbage collection for cross-keyspace runtimes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed PR description is well-structured with clear problem statement, technical details, and comprehensive testing information following the repository template.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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.

@tiprow

tiprow Bot commented Jun 8, 2026

Copy link
Copy Markdown

Hi @D3Hunter. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

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.

@codecov

codecov Bot commented Jun 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 38 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.6654%. Comparing base (d568a85) to head (15b011f).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #69013        +/-   ##
================================================
+ Coverage   76.3213%   76.6654%   +0.3441%     
================================================
  Files          2041       2056        +15     
  Lines        562689     573121     +10432     
================================================
+ Hits         429452     439386      +9934     
+ Misses       132324     131942       -382     
- Partials        913       1793       +880     
Flag Coverage Δ
integration 46.3160% <0.0000%> (+6.5873%) ⬆️

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

Components Coverage Δ
dumpling 60.4610% <ø> (ø)
parser ∅ <ø> (∅)
br 65.7271% <ø> (+2.9367%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ti-chi-bot ti-chi-bot Bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jun 8, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jun 8, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: joechenrh, wjhuang2016

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

The pull request process is described 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

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jun 8, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jun 8, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-06-08 07:06:07.693290005 +0000 UTC m=+770868.763607395: ☑️ agreed by wjhuang2016.
  • 2026-06-08 07:22:15.521656323 +0000 UTC m=+771836.591973713: ☑️ agreed by joechenrh.

@D3Hunter

D3Hunter commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

/retest

@tiprow

tiprow Bot commented Jun 8, 2026

Copy link
Copy Markdown

@D3Hunter: PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test.

Details

In response to this:

/retest

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.

@D3Hunter

D3Hunter commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

/retest

@tiprow

tiprow Bot commented Jun 8, 2026

Copy link
Copy Markdown

@D3Hunter: PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test.

Details

In response to this:

/retest

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.

@D3Hunter

D3Hunter commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

/retest

@tiprow

tiprow Bot commented Jun 8, 2026

Copy link
Copy Markdown

@D3Hunter: PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test.

Details

In response to this:

/retest

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.

@ti-chi-bot
ti-chi-bot Bot merged commit 452e517 into pingcap:master Jun 8, 2026
38 of 40 checks passed
@D3Hunter
D3Hunter deleted the crossks-gc branch June 8, 2026 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm release-note-none Denotes a PR that doesn't merit a 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.

3 participants