-
-
Notifications
You must be signed in to change notification settings - Fork 64
feat(consumer): periodically restart the Rust consumer to reconnect to Kafka #8085
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
Open
phacops
wants to merge
9
commits into
master
Choose a base branch
from
claude/epic-bohr-dbnj39
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+519
−174
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d7ffc34
feat(consumer): periodically restart the Rust consumer to reconnect t…
claude 5339599
fix(consumer): quantize rebalance on periodic restart
claude 4488625
ref(consumer): restart the consumer loop in-process instead of exiting
claude 88c3af1
fix(consumer): tidy restart-loop rebalance delay and timer lifecycle
claude 69c2a00
fix(consumer): refresh rebalance delay per restart and make timer int…
claude d9f0813
fix(consumer): resolve restart/stop race and Ctrl-C lock-hold
claude 330ef8c
fix(rebalancing): guard quantized_rebalance_delay against a zero delay
claude 332962a
fix(consumer): clear shared handle after a run ends
claude 1ff9b86
Merge branch 'master' into claude/epic-bohr-dbnj39
mchen-sentry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| use std::time::Duration; | ||
|
|
||
| use crate::runtime_config; | ||
|
|
||
| /// Default interval between consumer restarts when periodic restart is enabled | ||
| /// but no explicit interval is configured: 15 minutes. | ||
| pub const DEFAULT_RESTART_INTERVAL_SECS: u64 = 900; | ||
|
|
||
| fn is_truthy(value: &str) -> bool { | ||
| matches!(value.trim().to_ascii_lowercase().as_str(), "1" | "true") | ||
| } | ||
|
|
||
| /// Returns the interval after which the consumer should gracefully shut down so | ||
| /// that it can be restarted and reconnect to Kafka, or `None` when periodic | ||
| /// restart is not enabled for `consumer_group`. | ||
| /// | ||
| /// This is gated behind a feature flag and controlled by two runtime config | ||
| /// keys (read from `snuba.state`, so they can be toggled live without a | ||
| /// redeploy): | ||
| /// | ||
| /// * `kafka_consumer_periodic_restart__<consumer_group>` — the feature flag. | ||
| /// Periodic restart is only enabled when this is set to a truthy value | ||
| /// (`"1"` or `"true"`). Defaults to disabled. | ||
| /// * `kafka_consumer_periodic_restart_interval_secs__<consumer_group>` — the | ||
| /// number of seconds between restarts. Must be a positive integer; defaults | ||
| /// to [`DEFAULT_RESTART_INTERVAL_SECS`] (15 minutes) when unset or invalid. | ||
| pub fn get_restart_interval(consumer_group: &str) -> Option<Duration> { | ||
| let enabled = runtime_config::get_str_config( | ||
| format!("kafka_consumer_periodic_restart__{consumer_group}").as_str(), | ||
| ) | ||
| .ok() | ||
| .flatten() | ||
| .map(|value| is_truthy(&value)) | ||
| .unwrap_or(false); | ||
|
|
||
| if !enabled { | ||
| return None; | ||
| } | ||
|
|
||
| let interval_secs = runtime_config::get_str_config( | ||
| format!("kafka_consumer_periodic_restart_interval_secs__{consumer_group}").as_str(), | ||
| ) | ||
| .ok() | ||
| .flatten() | ||
| .and_then(|value| value.parse::<u64>().ok()) | ||
| .filter(|&secs| secs > 0) | ||
| .unwrap_or(DEFAULT_RESTART_INTERVAL_SECS); | ||
|
|
||
| Some(Duration::from_secs(interval_secs)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn clear(consumer_group: &str) { | ||
| runtime_config::patch_str_config_for_test( | ||
| &format!("kafka_consumer_periodic_restart__{consumer_group}"), | ||
| None, | ||
| ); | ||
| runtime_config::patch_str_config_for_test( | ||
| &format!("kafka_consumer_periodic_restart_interval_secs__{consumer_group}"), | ||
| None, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_disabled_by_default() { | ||
| crate::testutils::initialize_python(); | ||
| assert_eq!(get_restart_interval("periodic_restart_default_test"), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_enabled_uses_default_interval() { | ||
| crate::testutils::initialize_python(); | ||
| let group = "periodic_restart_enabled_test"; | ||
| let _guard = scopeguard::guard((), |_| clear(group)); | ||
|
|
||
| runtime_config::patch_str_config_for_test( | ||
| &format!("kafka_consumer_periodic_restart__{group}"), | ||
| Some("1"), | ||
| ); | ||
| assert_eq!( | ||
| get_restart_interval(group), | ||
| Some(Duration::from_secs(DEFAULT_RESTART_INTERVAL_SECS)) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_enabled_with_custom_interval() { | ||
| crate::testutils::initialize_python(); | ||
| let group = "periodic_restart_custom_test"; | ||
| let _guard = scopeguard::guard((), |_| clear(group)); | ||
|
|
||
| runtime_config::patch_str_config_for_test( | ||
| &format!("kafka_consumer_periodic_restart__{group}"), | ||
| Some("true"), | ||
| ); | ||
| runtime_config::patch_str_config_for_test( | ||
| &format!("kafka_consumer_periodic_restart_interval_secs__{group}"), | ||
| Some("60"), | ||
| ); | ||
| assert_eq!(get_restart_interval(group), Some(Duration::from_secs(60))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_invalid_interval_falls_back_to_default() { | ||
| crate::testutils::initialize_python(); | ||
| let group = "periodic_restart_invalid_test"; | ||
| let _guard = scopeguard::guard((), |_| clear(group)); | ||
|
|
||
| runtime_config::patch_str_config_for_test( | ||
| &format!("kafka_consumer_periodic_restart__{group}"), | ||
| Some("1"), | ||
| ); | ||
| runtime_config::patch_str_config_for_test( | ||
| &format!("kafka_consumer_periodic_restart_interval_secs__{group}"), | ||
| Some("garbage"), | ||
| ); | ||
| assert_eq!( | ||
| get_restart_interval(group), | ||
| Some(Duration::from_secs(DEFAULT_RESTART_INTERVAL_SECS)) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_interval_without_flag_is_ignored() { | ||
| crate::testutils::initialize_python(); | ||
| let group = "periodic_restart_no_flag_test"; | ||
| let _guard = scopeguard::guard((), |_| clear(group)); | ||
|
|
||
| runtime_config::patch_str_config_for_test( | ||
| &format!("kafka_consumer_periodic_restart_interval_secs__{group}"), | ||
| Some("60"), | ||
| ); | ||
| assert_eq!(get_restart_interval(group), None); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.