Skip to content

server: graceful shutdown tikv-impl#18930

Merged
ti-chi-bot[bot] merged 5 commits intotikv:masterfrom
hujiatao0:graceful-shutdown-impl
Sep 25, 2025
Merged

server: graceful shutdown tikv-impl#18930
ti-chi-bot[bot] merged 5 commits intotikv:masterfrom
hujiatao0:graceful-shutdown-impl

Conversation

@hujiatao0
Copy link
Contributor

@hujiatao0 hujiatao0 commented Sep 4, 2025

What is changed and how it works?

Issue Number: Close #17221

What's Changed:

When a SIGTERM signal is received, TiKV tells PD it's stopping by StoreHeartbeat. PD then try to move all the leaders from that TiKV instance before it fully shuts down.

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch

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

Release note

Support graceful shutdown for TiKV upon receiving a SIGTERM signal.

@ti-chi-bot ti-chi-bot bot added dco-signoff: yes Indicates the PR's author has signed the dco. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. contribution This PR is from a community contributor. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels Sep 4, 2025
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Sep 4, 2025

Hi @hujiatao0. Thanks for your PR.

I'm waiting for a tikv member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@ti-chi-bot ti-chi-bot bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Sep 4, 2025
@hujiatao0 hujiatao0 changed the title graceful shutdown tikv-impl server: graceful shutdown tikv-impl Sep 4, 2025
Comment on lines +164 to +186
.arg(
Arg::with_name("enable-graceful-shutdown")
.long("enable-graceful-shutdown")
.takes_value(true)
.value_name("BOOL")
.help("Enable graceful shutdown for TiKV server")
.long_help(
"Enable graceful shutdown operations like leader eviction before terminating. \
Defaults to true unless explicitly set to false.",
),
)
.arg(
Arg::with_name("evict-leader-timeout")
.long("evict-leader-timeout")
.takes_value(true)
.value_name("DURATION")
.help("Timeout for leader eviction during graceful shutdown")
.long_help(
"Timeout for leader eviction during graceful shutdown. \
After this timeout, TiKV will proceed with shutdown even if some regions \
haven't completed leader transfer. Defaults to 20s.",
),
)
Copy link
Member

Choose a reason for hiding this comment

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

Can we remove them from args? It seems unnecessary in args after all they can be configured via config file.

/// Timeout for leader eviction during graceful shutdown.
/// After this timeout, TiKV will proceed with shutdown even if
/// some regions haven't completed leader transfer.
#[online_config(skip)]
Copy link
Member

Choose a reason for hiding this comment

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

Please support online config change.

/// When enabled, TiKV will perform graceful shutdown operations like
/// leader eviction before terminating.
#[online_config(skip)]
pub enable_graceful_shutdown: bool,
Copy link
Member

Choose a reason for hiding this comment

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

It seems unnecessary, we can disable graceful shutdown if evict_leader_timeout is zero.

/// After this timeout, TiKV will proceed with shutdown even if
/// some regions haven't completed leader transfer.
#[online_config(skip)]
pub evict_leader_timeout: ReadableDuration,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
pub evict_leader_timeout: ReadableDuration,
pub graceful_shutdown_timeout: ReadableDuration,

How about graceful_shutdown_timeout? It's more intuitive.

for signal in &mut signals {
match signal {
SIGTERM | SIGINT | SIGHUP => {
SIGTERM => {
Copy link
Member

Choose a reason for hiding this comment

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

What happens if tikv receives SIGTERM twice within graceful shutdown timeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The receipt of the first SIGTERM signal triggers a graceful shutdown and forces an exit from the wait_for_signal loop using a break statement. The process will be unable to handle another SIGTERM signal.

Copy link
Member

Choose a reason for hiding this comment

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

Does tikv ignores the second SIGTERM signal or tikv exits immediately?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does tikv ignores the second SIGTERM signal or tikv exits immediately?

tikv will ignore the second SIGTERM

let now = Instant::now();
self.set_state(true);
self.wait_for_leader_eviction(now);
self.set_state(false);
Copy link
Member

Choose a reason for hiding this comment

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

Why set it to false? Please add some comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When the set_state function sets the is_stopping status, it also triggers an immediate storeheartbeat. The PD heartbeat handler has logic to clean up the graceful shutdown evict-leader scheduler when a store's state is not is_stopping.

I explicitly set the state and trigger a final heartbeat after the leader eviction is complete, ensuring PD removes the scheduler promptly. While this isn't strictly required—as a node restart would eventually trigger the same cleanup via a regular heartbeat—this approach prevents a delay in the scheduler's removal.

Copy link
Member

Choose a reason for hiding this comment

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

I see, please leave the above comment in code, thanks!

Copy link
Member

@overvenus overvenus left a comment

Choose a reason for hiding this comment

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

Rest LGTM

for signal in &mut signals {
match signal {
SIGTERM | SIGINT | SIGHUP => {
SIGTERM => {
Copy link
Member

Choose a reason for hiding this comment

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

Does tikv ignores the second SIGTERM signal or tikv exits immediately?

let now = Instant::now();
self.set_state(true);
self.wait_for_leader_eviction(now);
self.set_state(false);
Copy link
Member

Choose a reason for hiding this comment

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

I see, please leave the above comment in code, thanks!

@overvenus
Copy link
Member

To avoid blocking this PR from merging, please submit a separate PR that adds a test to verify that graceful shutdown evicts leaders.

@overvenus overvenus requested a review from hbisheng September 16, 2025 03:55
@hujiatao0 hujiatao0 force-pushed the graceful-shutdown-impl branch from b7bc98f to 4a8804f Compare September 16, 2025 09:25
Copy link
Member

@hbisheng hbisheng left a comment

Choose a reason for hiding this comment

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

Overall LGTM

Please add a release note, something like “Add graceful shutdown mechanism to evict leaders when receiving a SIGTERM signal.”

self.set_state(true);
self.wait_for_leader_eviction(now);
self.set_state(false);
std::thread::sleep(Duration::from_millis(200));
Copy link
Member

Choose a reason for hiding this comment

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

nit: add a comment for why sleep is needed here (waiting for the final store heartbeat to be uploadedd)

@hujiatao0 hujiatao0 force-pushed the graceful-shutdown-impl branch 3 times, most recently from 2099e15 to 496fa96 Compare September 17, 2025 16:21
@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Sep 17, 2025
Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>

change the signal handler

Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>

remove enable config

Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>
Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>
@hujiatao0 hujiatao0 force-pushed the graceful-shutdown-impl branch from 496fa96 to d136b22 Compare September 23, 2025 09:40
Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>
Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>
warn!("metrics push is not supported any more.");
}

if let Some(graceful_shutdown_timeout) = matches.value_of("graceful-shutdown-timeout") {
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary? Do we need to overwrite the config with command line args?

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Sep 24, 2025
@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 24, 2025
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Sep 24, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-09-24 03:06:09.004149183 +0000 UTC m=+412379.074642866: ☑️ agreed by hbisheng.
  • 2025-09-24 10:36:37.149399996 +0000 UTC m=+439407.219893656: ☑️ agreed by overvenus.

Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>
fn graceful_shutdown(&mut self) {
let now = Instant::now();
self.set_state(true);
self.wait_for_leader_eviction(now);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we wait for other possible ongoing tasks finishing like:

  • The cdc observer related handling
  • The backup restore task handling
  • The read write requests from kv-client, like the follower read requests

Besides, do we need a configuration for the graceful shutdow maxium wait time? From the product persipective.

Copy link
Contributor Author

@hujiatao0 hujiatao0 Sep 25, 2025

Choose a reason for hiding this comment

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

Should we wait for other possible ongoing tasks finishing like:

  • The cdc observer related handling
  • The backup restore task handling
  • The read write requests from kv-client, like the follower read requests

Besides, do we need a configuration for the graceful shutdow maxium wait time? From the product persipective.

Actually, each TiKV node has an online-configurable graceful shutdown timeout. If this timeout is triggered while the node still has leaders that haven't been transferred, it will fall back to the standard shutdown process. The wait_for_leader_eviction step is just an additional waiting period for the leader transfer to complete. After this process finishes, it proceeds to the existing shutdown logic to handle other pre-shutdown operations. I think the operations you mentioned are already covered by this existing shutdown logic? Or these operations didn't exist previously. We should consider adding them as part of a new requirement in the future.

Copy link
Collaborator

@cfzjywxk cfzjywxk Sep 25, 2025

Choose a reason for hiding this comment

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

I think the operations you mentioned are already covered by this existing shutdown logic?

I am not quire sure, there are requests that may not require leadership like follower/stale read requests for example. Perhaps we could add a TODO about it since this needs to be merged quickly.

@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Sep 25, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfzjywxk, hbisheng, overvenus

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 the approved label Sep 25, 2025
@ti-chi-bot ti-chi-bot bot merged commit 2870bde into tikv:master Sep 25, 2025
9 checks passed
@ti-chi-bot ti-chi-bot bot added this to the Pool milestone Sep 25, 2025
hujiatao0 added a commit to hujiatao0/tikv that referenced this pull request Sep 28, 2025
close tikv#17221

When a SIGTERM signal is received, TiKV tells PD it's stopping by StoreHeartbeat. PD then try to move all the leaders from that TiKV instance before it fully shuts down.

Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>
ti-chi-bot bot pushed a commit that referenced this pull request Sep 30, 2025
close #17221

When a SIGTERM signal is received, TiKV tells PD it's stopping by StoreHeartbeat. PD then try to move all the leaders from that TiKV instance before it fully shuts down.

Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>
3AceShowHand pushed a commit to 3AceShowHand/tikv that referenced this pull request Oct 13, 2025
close tikv#17221

When a SIGTERM signal is received, TiKV tells PD it's stopping by StoreHeartbeat. PD then try to move all the leaders from that TiKV instance before it fully shuts down.

Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>
Signed-off-by: 3AceShowHand <jinl1037@hotmail.com>
@overvenus overvenus added the needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. label Dec 1, 2025
ti-chi-bot pushed a commit to ti-chi-bot/tikv that referenced this pull request Dec 1, 2025
close tikv#17221

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-8.5: #19157.
But this PR has conflicts, please resolve them!

ti-chi-bot bot pushed a commit that referenced this pull request Dec 4, 2025
close #17221

When a SIGTERM signal is received, TiKV tells PD it's stopping by StoreHeartbeat. PD then try to move all the leaders from that TiKV instance before it fully shuts down.

Signed-off-by: hujiatao0 <hhjjtt110@gmail.com>

Co-authored-by: hujiatao0 <hhjjtt110@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved contribution This PR is from a community contributor. dco-signoff: yes Indicates the PR's author has signed the dco. lgtm needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Graceful Shutdown of TiKV pods when SIGTERM is sent to the pod

5 participants