258 lines
11 KiB
Rust
258 lines
11 KiB
Rust
//! Health checks, restarts with back-off, log rotation and the failures that end the service.
|
|
|
|
use crate::harness::*;
|
|
use leleka_deploy::profile::{KarSource, Profile};
|
|
use std::{
|
|
fs,
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
/// A fixture with the fakes, the file-backed profile written as state and an archive stand-in; with the path of the
|
|
/// config.
|
|
fn file_service(name: &str) -> (Fixture, std::path::PathBuf) {
|
|
let fixture = Fixture::new(name);
|
|
fixture.install_fake_binaries(&fixture.dirs.prefix.join("bin"));
|
|
fs::create_dir_all(fixture.fake_dir()).unwrap();
|
|
let config = fixture.write_state(file_profile());
|
|
fs::create_dir_all(&fixture.dirs.assets).unwrap();
|
|
fs::write(fixture.dirs.paths().kar(), b"archive").unwrap();
|
|
(fixture, config)
|
|
}
|
|
|
|
/// SIGTERM, and the supervisor exits with success.
|
|
fn stop_cleanly(supervisor: &mut std::process::Child) {
|
|
signal(u64::from(supervisor.id()), "TERM");
|
|
assert!(
|
|
wait_exit(supervisor, Duration::from_secs(60))
|
|
.unwrap()
|
|
.success()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_hung_engine_is_killed_and_restarted_by_the_health_check() {
|
|
// `max_request_s` is 3 here; the fake API reports a request that has been active for 600 s.
|
|
let (fixture, config) = file_service("hang");
|
|
tune(&config, |c| {
|
|
c["health"] =
|
|
serde_json::json!({"enabled": true, "interval_s": 1, "fail_s": 30, "max_request_s": 3});
|
|
c["restart"]["backoff_initial_s"] = serde_json::json!(1);
|
|
});
|
|
let mut supervisor = start_supervisor(&fixture, &config, None);
|
|
let ready = fixture.wait_for("ready", Duration::from_secs(60), |s| s["phase"] == "ready");
|
|
assert_eq!(ready["restarts_total"], 0);
|
|
let hung_pid = ready["engine_pid"].as_u64().unwrap();
|
|
// The process lives and the API answers, but a request has been active for ten minutes.
|
|
fs::write(fixture.fake_dir().join("hang-decode"), "").unwrap();
|
|
check_the_stalled_engine_is_replaced_once(&fixture, hung_pid);
|
|
stop_cleanly(&mut supervisor);
|
|
}
|
|
|
|
fn check_the_stalled_engine_is_replaced_once(fixture: &Fixture, hung_pid: u64) {
|
|
let again = fixture.wait_for("restart after the stall", Duration::from_secs(60), |s| {
|
|
s["phase"] == "ready"
|
|
&& s["restarts_total"] == 1
|
|
&& s["engine_pid"].as_u64() != Some(hung_pid)
|
|
});
|
|
let reason = again["last_restart_reason"].as_str().unwrap();
|
|
assert!(
|
|
reason.contains("request has been active") && reason.contains("600"),
|
|
"{reason}"
|
|
);
|
|
assert!(!pid_alive(hung_pid), "the hung engine must be gone");
|
|
// The fake reports the hang once; afterwards the new engine is judged healthy.
|
|
let healthy = fixture.wait_for("healthy again", Duration::from_secs(30), |s| {
|
|
s["health"] == "healthy"
|
|
});
|
|
assert_eq!(
|
|
healthy["restarts_total"], 1,
|
|
"no second restart without a second stall"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn engine_restarts_back_off_survive_failed_attempts_and_give_up_in_the_end() {
|
|
let (fixture, config) = file_service("backoff");
|
|
tune(&config, |c| {
|
|
c["health"]["enabled"] = serde_json::json!(false);
|
|
c["restart"] = serde_json::json!({
|
|
"max_engine_restarts": 3, "stable_after_s": 600, "backoff_initial_s": 1, "backoff_max_s": 2
|
|
});
|
|
});
|
|
let mut supervisor = start_supervisor(&fixture, &config, None);
|
|
let ready = fixture.wait_for("ready", Duration::from_secs(60), |s| s["phase"] == "ready");
|
|
// Crash it and make every restart fail: three attempts in a row, then the service ends with a message.
|
|
fs::write(fixture.fake_dir().join("fail-engine"), "").unwrap();
|
|
signal(ready["engine_pid"].as_u64().unwrap(), "KILL");
|
|
let status =
|
|
wait_exit(&mut supervisor, Duration::from_secs(90)).expect("the supervisor must give up");
|
|
assert_eq!(status.code(), Some(1));
|
|
let state = fixture.status().unwrap();
|
|
assert_eq!(state["phase"], "failed");
|
|
let message = state["message"].as_str().unwrap();
|
|
assert!(message.contains("failed 3 times in a row"), "{message}");
|
|
assert_eq!(state["restarts_total"], 1);
|
|
let log = fs::read_to_string(fixture.dirs.paths().logs().join("leleka.log")).unwrap();
|
|
assert!(
|
|
log.matches("the engine did not come back").count() >= 3,
|
|
"{log}"
|
|
);
|
|
check_a_restart_that_fails_once_is_just_a_slower_restart();
|
|
}
|
|
|
|
fn check_a_restart_that_fails_once_is_just_a_slower_restart() {
|
|
// A restart that fails once and then works is just a slower restart.
|
|
let (fixture, config) = file_service("backoff2");
|
|
tune(&config, |c| {
|
|
c["health"]["enabled"] = serde_json::json!(false);
|
|
c["restart"] = serde_json::json!({
|
|
"max_engine_restarts": 5, "stable_after_s": 600, "backoff_initial_s": 1, "backoff_max_s": 2
|
|
});
|
|
});
|
|
let mut supervisor = start_supervisor(&fixture, &config, None);
|
|
let ready = fixture.wait_for("ready", Duration::from_secs(60), |s| s["phase"] == "ready");
|
|
fs::write(fixture.fake_dir().join("fail-engine"), "").unwrap();
|
|
signal(ready["engine_pid"].as_u64().unwrap(), "KILL");
|
|
fixture.wait_for("restart attempts", Duration::from_secs(30), |s| {
|
|
s["phase"] == "engine_restarting"
|
|
});
|
|
std::thread::sleep(Duration::from_millis(2500));
|
|
fs::remove_file(fixture.fake_dir().join("fail-engine")).unwrap();
|
|
let again = fixture.wait_for("recovered", Duration::from_secs(60), |s| {
|
|
s["phase"] == "ready" && s["restarts_total"] == 1
|
|
});
|
|
assert!(again["engine_restarts"].as_u64().unwrap() >= 2, "{again}");
|
|
stop_cleanly(&mut supervisor);
|
|
}
|
|
|
|
#[test]
|
|
fn logs_past_their_limit_are_rotated_while_the_service_runs() {
|
|
let (fixture, config) = file_service("logs");
|
|
tune(&config, |c| {
|
|
c["logs"] = serde_json::json!({"limit_mib": 1, "keep": 2})
|
|
});
|
|
let big = fixture.dirs.paths().logs().join("api.log");
|
|
fs::write(&big, "x".repeat(300) + "\n").unwrap();
|
|
{
|
|
use std::io::Write;
|
|
let mut file = fs::OpenOptions::new().append(true).open(&big).unwrap();
|
|
for _ in 0..(3 << 20) / 100 {
|
|
writeln!(file, "{}", "y".repeat(99)).unwrap();
|
|
}
|
|
}
|
|
let mut supervisor = start_supervisor(&fixture, &config, None);
|
|
fixture.wait_for("ready", Duration::from_secs(60), |s| s["phase"] == "ready");
|
|
let rotated = fixture.dirs.paths().logs().join("api.log.1");
|
|
let started = Instant::now();
|
|
while !rotated.exists() && started.elapsed() < Duration::from_secs(30) {
|
|
std::thread::sleep(Duration::from_millis(200));
|
|
}
|
|
assert!(rotated.exists(), "{}", fixture.logs());
|
|
assert!(fs::metadata(&rotated).unwrap().len() <= 1 << 20);
|
|
assert!(fs::metadata(&big).unwrap().len() < 1 << 20);
|
|
let doc = fixture.status().unwrap();
|
|
assert!(doc["log_bytes"].as_u64().unwrap() > 0);
|
|
stop_cleanly(&mut supervisor);
|
|
}
|
|
|
|
#[test]
|
|
fn a_killed_api_or_gateway_is_restarted_in_place_and_the_engine_is_left_alone() {
|
|
let (fixture, config) = file_service("sidecars");
|
|
tune(&config, |c| {
|
|
c["health"]["enabled"] = serde_json::json!(false)
|
|
});
|
|
let mut supervisor = start_supervisor(&fixture, &config, None);
|
|
let ready = fixture.wait_for("ready", Duration::from_secs(60), |s| s["phase"] == "ready");
|
|
let (engine, gateway, api) = (
|
|
ready["engine_pid"].as_u64().unwrap(),
|
|
ready["gateway_pid"].as_u64().unwrap(),
|
|
ready["api_pid"].as_u64().unwrap(),
|
|
);
|
|
signal(api, "KILL");
|
|
let after_api = fixture.wait_for("a new API", Duration::from_secs(30), |s| {
|
|
s["api_pid"].as_u64().is_some_and(|p| p != api) && s["phase"] == "ready"
|
|
});
|
|
assert_eq!(after_api["engine_pid"].as_u64(), Some(engine));
|
|
assert_eq!(
|
|
after_api["restarts_total"], 0,
|
|
"an API restart is not an engine restart"
|
|
);
|
|
check_a_killed_gateway_is_restarted(&fixture, gateway, engine);
|
|
stop_cleanly(&mut supervisor);
|
|
}
|
|
|
|
fn check_a_killed_gateway_is_restarted(fixture: &Fixture, gateway: u64, engine: u64) {
|
|
signal(gateway, "KILL");
|
|
let after_gateway = fixture.wait_for("a new gateway", Duration::from_secs(30), |s| {
|
|
s["gateway_pid"].as_u64().is_some_and(|p| p != gateway)
|
|
});
|
|
assert_eq!(after_gateway["engine_pid"].as_u64(), Some(engine));
|
|
let started = Instant::now();
|
|
while fixture.http_status("/ready") != 200 && started.elapsed() < Duration::from_secs(30) {
|
|
std::thread::sleep(Duration::from_millis(200));
|
|
}
|
|
assert_eq!(fixture.http_status("/ready"), 200);
|
|
}
|
|
|
|
/// The laptop profile with a file archive and the fake engine told to fail through the `marker` file.
|
|
fn failing_service(name: &str, marker: &str) -> (Fixture, std::path::PathBuf) {
|
|
let fixture = Fixture::new(name);
|
|
fixture.install_fake_binaries(&fixture.dirs.prefix.join("bin"));
|
|
fs::create_dir_all(fixture.fake_dir()).unwrap();
|
|
fs::write(fixture.fake_dir().join(marker), "").unwrap();
|
|
let mut profile = Profile::builtin("rtx3080-16g").unwrap();
|
|
profile.kar_source = KarSource::File;
|
|
let config = fixture.write_state(profile);
|
|
fs::create_dir_all(&fixture.dirs.assets).unwrap();
|
|
fs::write(fixture.dirs.paths().kar(), b"archive").unwrap();
|
|
(fixture, config)
|
|
}
|
|
|
|
#[test]
|
|
fn an_engine_that_cannot_start_fails_the_service_and_names_the_log() {
|
|
let (fixture, config) = failing_service("fail", "fail-engine");
|
|
let mut supervisor = start_supervisor(&fixture, &config, None);
|
|
let status =
|
|
wait_exit(&mut supervisor, Duration::from_secs(60)).expect("the supervisor must give up");
|
|
assert_eq!(status.code(), Some(1));
|
|
let state = fixture.status().unwrap();
|
|
assert_eq!(state["phase"], "failed");
|
|
let message = state["message"].as_str().unwrap();
|
|
assert!(
|
|
message.contains("engine") && message.contains("exited"),
|
|
"{message}"
|
|
);
|
|
let output = fs::read_to_string(fixture.root.join("supervisor.out")).unwrap();
|
|
assert!(output.contains("fatal:"), "{output}");
|
|
// The gateway started for the load is not left behind.
|
|
assert!(state["gateway_pid"].is_null() || !pid_alive(state["gateway_pid"].as_u64().unwrap()));
|
|
}
|
|
|
|
#[test]
|
|
fn a_refused_load_fails_fast_and_shows_the_engine_log() {
|
|
// The real engine stays alive when its load fails; only the operation record says so.
|
|
let (fixture, config) = failing_service("refused", "fail-load");
|
|
let started = Instant::now();
|
|
let mut supervisor = start_supervisor(&fixture, &config, None);
|
|
let status =
|
|
wait_exit(&mut supervisor, Duration::from_secs(60)).expect("the supervisor must give up");
|
|
assert_eq!(status.code(), Some(1));
|
|
assert!(
|
|
started.elapsed() < Duration::from_secs(40),
|
|
"a refused load must not wait for the load timeout"
|
|
);
|
|
let state = fixture.status().unwrap();
|
|
let message = state["message"].as_str().unwrap();
|
|
assert!(
|
|
message.contains("refused the load") && message.contains("BackendUnavailable"),
|
|
"{message}"
|
|
);
|
|
// The engine it started is gone.
|
|
let engine_pid = fs::read_to_string(fixture.fake_dir().join("engine.pid"))
|
|
.unwrap()
|
|
.trim()
|
|
.parse()
|
|
.unwrap();
|
|
assert!(!pid_alive(engine_pid));
|
|
}
|