-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enabling tracking the execution phase of a server
Adds a way for subcribers to track the execution phase of a Server. The new method `Server::watch_execution_phase` returns a broadcast channel that will receive ExecutionPhase messages on each phase transition. Example use cases: * A /health endpoint that reports the current execution phase (only interesting for shutdown) * Producing metrics for the various phase transitions
- Loading branch information
Showing
3 changed files
with
238 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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,53 @@ | ||
// NOTE: This test sends a shutdown signal to itself, | ||
// so it needs to be in an isolated test to prevent concurrency. | ||
|
||
use pingora_core::server::{ExecutionPhase, Server}; | ||
|
||
// Ensure that execution phases are reported correctly. | ||
#[test] | ||
fn test_server_execution_phase_monitor_fast_shutdown() { | ||
let mut server = Server::new(None).unwrap(); | ||
|
||
let mut phase = server.watch_execution_phase(); | ||
|
||
let join = std::thread::spawn(move || { | ||
server.bootstrap(); | ||
server.run(); | ||
}); | ||
|
||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::Bootstrap | ||
)); | ||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::BootstrapComplete, | ||
)); | ||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::Running, | ||
)); | ||
|
||
// Need to wait for startup, otherwise the signal handler is not | ||
// installed yet. | ||
unsafe { | ||
libc::raise(libc::SIGINT); | ||
} | ||
|
||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::ShutdownStarted, | ||
)); | ||
|
||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::ShutdownRuntimes, | ||
)); | ||
|
||
join.join().unwrap(); | ||
|
||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::Terminated, | ||
)); | ||
} |
This file contains 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,69 @@ | ||
// NOTE: This test sends a shutdown signal to itself, | ||
// so it needs to be in an isolated test to prevent concurrency. | ||
|
||
use pingora_core::server::{configuration::ServerConf, ExecutionPhase, Server}; | ||
|
||
// Ensure that execution phases are reported correctly. | ||
#[test] | ||
fn test_server_execution_phase_monitor_graceful_shutdown() { | ||
let conf = ServerConf { | ||
// Use small timeouts to speed up the test. | ||
grace_period_seconds: Some(1), | ||
graceful_shutdown_timeout_seconds: Some(1), | ||
..Default::default() | ||
}; | ||
let mut server = Server::new_with_opt_and_conf(None, conf); | ||
|
||
let mut phase = server.watch_execution_phase(); | ||
|
||
let join = std::thread::spawn(move || { | ||
server.bootstrap(); | ||
server.run(); | ||
}); | ||
|
||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::Bootstrap | ||
)); | ||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::BootstrapComplete, | ||
)); | ||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::Running, | ||
)); | ||
|
||
// Need to wait for startup, otherwise the signal handler is not | ||
// installed yet. | ||
unsafe { | ||
libc::raise(libc::SIGTERM); | ||
} | ||
|
||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::GracefulTerminate, | ||
)); | ||
|
||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::ShutdownStarted, | ||
)); | ||
|
||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::ShutdownGracePeriod, | ||
)); | ||
|
||
assert!(matches!( | ||
dbg!(phase.blocking_recv().unwrap()), | ||
ExecutionPhase::ShutdownRuntimes, | ||
)); | ||
|
||
join.join().unwrap(); | ||
|
||
assert!(matches!( | ||
phase.blocking_recv().unwrap(), | ||
ExecutionPhase::Terminated, | ||
)); | ||
} |