-
Notifications
You must be signed in to change notification settings - Fork 152
Replace readiness probe with startup for autopilot #3856
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
Merged
+208
−66
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1f7b832
Replace readiness probe with startup for autopilot
m-sz c288752
Added startup leader take over log
m-sz 17dd737
Increase non-leader polling rate
m-sz a73ce73
Revert "Increase non-leader polling rate"
m-sz fe242be
Introduce LeaderLockTracker
m-sz 2622eda
Satisfy clippy
m-sz 2cea524
fmt
m-sz 39a4310
Update crates/autopilot/src/run_loop.rs
m-sz c4041c4
Update crates/autopilot/src/run_loop.rs
m-sz b383d5e
Update crates/autopilot/src/run_loop.rs
m-sz 91a1779
Update crates/autopilot/src/run_loop.rs
m-sz b5d1ea4
Move Leader lock to separate file
m-sz d230b29
Update crates/autopilot/src/leader_lock_tracker.rs
m-sz b301ad5
fmt
m-sz 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
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,129 @@ | ||
| use {database::leader_pg_lock::LeaderLock, observe::metrics}; | ||
|
|
||
| /// Tracks the autopilot leader lock status. | ||
| /// Leader lock status is tracked based on calls to try_acquire() | ||
| #[expect(clippy::large_enum_variant)] | ||
| pub enum LeaderLockTracker { | ||
| /// Leader lock mechanism is disabled. | ||
| /// Only one autopilot instance should be running at all times. | ||
| Disabled, | ||
| /// Leader lock mechanism is enabled. | ||
| /// This allows for multiple autopilots to run simultaneously with only one | ||
| /// driving the auctions forward. | ||
| /// The autopilot followers (not holding the lock) will keep their caches | ||
| /// warm. It facilitates zero downtime deployments. | ||
| Enabled { | ||
| /// Are we currently the leader since last call to try_acquire() | ||
| is_leader: bool, | ||
| /// Were we previously the leader sinec the last call to try_acquire() | ||
m-sz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| was_leader: bool, | ||
| leader_lock: LeaderLock, | ||
| }, | ||
| } | ||
|
|
||
| impl LeaderLockTracker { | ||
| pub fn new(leader_lock: Option<LeaderLock>) -> Self { | ||
| match leader_lock { | ||
| Some(leader_lock) => Self::Enabled { | ||
| is_leader: false, | ||
| was_leader: false, | ||
| leader_lock, | ||
| }, | ||
| None => Self::Disabled, | ||
| } | ||
| } | ||
|
|
||
| /// Tries to acquire the leader lock if it's enabled | ||
| /// If not, does nothing | ||
| /// Should be called at the beginning of every run loop iteration | ||
| pub async fn try_acquire(&mut self) { | ||
| let Self::Enabled { | ||
| is_leader, | ||
| was_leader, | ||
| leader_lock, | ||
| } = self | ||
| else { | ||
| return; | ||
| }; | ||
|
|
||
| *was_leader = *is_leader; | ||
|
|
||
| *is_leader = leader_lock.try_acquire().await.unwrap_or_else(|err| { | ||
| tracing::error!(?err, "failed to become leader"); | ||
| Metrics::leader_lock_error(); | ||
| false | ||
| }); | ||
|
|
||
| if self.just_stepped_up() { | ||
| tracing::info!("Stepped up as a leader"); | ||
| Metrics::leader_step_up(); | ||
| } | ||
| } | ||
|
|
||
| /// Releases the leader lock if it was held | ||
| /// Should be called after breaking out of run loop (for example: due to | ||
| /// shutdown) | ||
| pub async fn release(self) { | ||
| if let Self::Enabled { | ||
| mut leader_lock, | ||
| is_leader: true, | ||
| .. | ||
| } = self | ||
| { | ||
| tracing::info!("Shutdown received, stepping down as the leader"); | ||
| leader_lock.release().await; | ||
| Metrics::leader_step_down(); | ||
| } | ||
| } | ||
|
|
||
| /// Returns true if the last try_acquire call resulted in acquiring the | ||
| /// leader lock If the feature is disabled, always returns false | ||
| pub fn just_stepped_up(&self) -> bool { | ||
| matches!( | ||
| self, | ||
| Self::Enabled { | ||
| is_leader: true, | ||
| was_leader: false, | ||
| .. | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| /// Returns true if the leader lock is being held | ||
| /// If the feature is disabled, always returns true | ||
| pub fn is_leader(&self) -> bool { | ||
| match self { | ||
| Self::Enabled { is_leader, .. } => *is_leader, | ||
| _ => true, | ||
| } | ||
| } | ||
| } | ||
| #[derive(prometheus_metric_storage::MetricStorage)] | ||
| #[metric(subsystem = "leader_lock_tracker")] | ||
| struct Metrics { | ||
| /// Tracks the current leader status | ||
| /// 1 - is currently autopilot leader | ||
| /// 0 - is currently autopilot follower | ||
| is_leader: prometheus::IntGauge, | ||
|
|
||
| /// Trackes the count of errors acquiring leader lock (should never happen) | ||
| leader_lock_error: prometheus::IntCounter, | ||
| } | ||
|
|
||
| impl Metrics { | ||
| fn get() -> &'static Self { | ||
| Metrics::instance(metrics::get_storage_registry()).unwrap() | ||
| } | ||
|
|
||
| fn leader_step_up() { | ||
| Self::get().is_leader.set(1) | ||
| } | ||
|
|
||
| fn leader_step_down() { | ||
| Self::get().is_leader.set(0) | ||
| } | ||
|
|
||
| fn leader_lock_error() { | ||
| Self::get().leader_lock_error.inc() | ||
| } | ||
| } | ||
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
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.
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.