use super::super::{LOWERED, clear_at_ten, ledger_test, lowered};
use super::ceiling::{check, host};
use super::{Daemon, G, HOUR, M, NOW, SizingChange, jvm_at, ledger, met};
use crate::api::{Advice, ProfileSizing, Tone};
use crate::config::{ByteSize, resize_profile};
fn jvm_bound(text: &str, line: &str) -> String {
text.replacen("memory = \"10G\"", &format!("memory = \"10G\"\n{line}"), 1)
}
impl Daemon {
fn jvm(&self) -> ProfileSizing {
self.sizing()
.profiles
.into_iter()
.find(|profile| profile.name == "jvm")
.unwrap()
}
fn applied_by_hand(&self) -> Option<u64> {
let change = self.jvm().advice.change()?;
let written = resize_profile(&self.text, "jvm", change).unwrap();
if let Err(error) = check(&written, self.memory_max) {
panic!("applying jvm's advice writes a config the check refuses: {error}");
}
Some(change.memory)
}
fn meeting_the_cap(&mut self, start: u64, count: u64) -> Vec<SizingChange> {
let mut changes = Vec::new();
for i in 0..count {
let at = start + i * 2 * HOUR;
let cap = self.cap();
self.history.jobs.push(met(ledger(at, cap, 0)));
self.now = at;
self.applied_by_hand();
changes.extend(self.checked_look(at + HOUR));
}
changes
}
}
fn steps(changes: &[SizingChange]) -> Vec<(u64, u64, u32)> {
changes
.iter()
.filter(|change| change.profile == "jvm" && change.to.memory != change.from.memory)
.map(|change| (change.from.memory, change.to.memory, change.to.slots))
.collect()
}
struct Held {
holds: &'static str,
word: &'static str,
then: &'static str,
fix: &'static str,
}
const BY_MAX_MEMORY: Held = Held {
holds: "its max_memory",
word: "at max_memory",
then: "raise max_memory to give them more",
fix: "raise max_memory in [profiles.jvm], then: runnerctl sizing apply jvm",
};
const BY_CEILING: Held = Held {
holds: "the most the host's 12.4G memory_ceiling leaves one runner beside the mirror's 512M",
word: "at ceiling",
then: "raise [host] memory_ceiling if the host can grow further",
fix: "raise [host] memory_ceiling if the host can grow further, then: runnerctl sizing apply jvm",
};
struct Bounds {
name: &'static str,
max_memory: &'static str,
ceiling: Option<&'static str>,
bound: u64,
held: Held,
}
const NEED: u64 = 11776 * M;
fn raised_to_a_bound(admission: &str, bounds: &Bounds) {
let case = format!("{admission}, {}", bounds.name);
let bound = bounds.bound;
let ceiling_line = bounds.ceiling.map_or(String::new(), |ceiling| {
format!("\nmemory_ceiling = \"{ceiling}\"")
});
let text = host(
&jvm_bound(
&jvm_at("10G", 2),
&format!("max_memory = \"{}\"", bounds.max_memory),
),
&format!("admission = \"{admission}\"{ceiling_line}"),
);
let mut daemon = Daemon::new(text, NOW);
if admission == "caps" {
daemon.memory_max = 37 * G;
}
for i in 0..12 {
let at = NOW - 30 * HOUR + i * 2 * HOUR;
daemon.history.jobs.push(ledger(at, 10 * G, 6 * G));
}
let changes = daemon.meeting_the_cap(NOW, 48);
let expected = match admission {
_ if bound < NEED => vec![(10 * G, bound, 2)],
"caps" => vec![(10 * G, NEED, 1), (NEED, bound, 1)],
_ => vec![(10 * G, NEED, 2), (NEED, bound, 2)],
};
assert_eq!(
steps(&changes),
expected,
"{case}: to the bound, then nothing more"
);
let last = changes
.iter()
.rfind(|change| change.to.memory == bound)
.unwrap();
let held = &bounds.held;
assert!(
last.reason.starts_with(&format!(
"raise memory to {}, {}, short of the ",
ByteSize(bound),
held.holds
)),
"{case}: {}",
last.reason
);
let jvm = daemon.jvm();
let sizing = daemon.sizing();
assert!(jvm.held().is_some(), "{case}: jvm is held");
assert_eq!(jvm.advice_word(), held.word, "{case}");
assert_eq!(jvm.advice.tone(), Tone::Bad, "{case}: its jobs need more");
assert_eq!(daemon.applied_by_hand(), None, "{case}: nothing to apply");
let line = sizing.advice_line(&jvm);
assert!(
line.starts_with(&format!(
"keep {}, {}: its jobs need ",
ByteSize(bound),
held.holds
)) && line.ends_with(held.then),
"{case}: {line}"
);
let fix = sizing.doctor_check().fix.unwrap_or_default();
assert!(fix.contains(held.fix), "{case}: {fix}");
daemon.text = daemon.text.replace(
&format!("max_memory = \"{}\"", bounds.max_memory),
"max_memory = \"20G\"",
);
daemon.announce();
let raised = daemon.meeting_the_cap(NOW + 100 * HOUR, 1);
let raised: Vec<u64> = steps(&raised).into_iter().map(|(_, to, _)| to).collect();
if bound < NEED {
assert!(
matches!(raised[..], [to] if to > bound),
"{case}: raised past the old max_memory: {raised:?}"
);
} else {
assert!(
raised.is_empty(),
"{case}: the ceiling still holds it: {raised:?}"
);
}
}
#[test]
fn a_raise_goes_no_further_than_max_memory_or_the_ceiling_whichever_is_lower_and_then_proposes_nothing()
{
let over = jvm_bound(&jvm_at("10G", 2), "max_memory = \"11G\"").replacen(
"memory = \"10G\"",
"memory = \"12G\"",
1,
);
assert!(
check(&over, 64 * G).is_err_and(|error| error.contains("memory is above its max_memory")),
"a cap over max_memory is refused"
);
let cases = [
Bounds {
name: "max_memory",
max_memory: "11G",
ceiling: None,
bound: 11 * G,
held: BY_MAX_MEMORY,
},
Bounds {
name: "max_memory under the ceiling",
max_memory: "11G",
ceiling: Some("12.4G"),
bound: 11 * G,
held: BY_MAX_MEMORY,
},
Bounds {
name: "the ceiling under max_memory",
max_memory: "12.5G",
ceiling: Some("12.4G"),
bound: 12032 * M,
held: BY_CEILING,
},
Bounds {
name: "max_memory at the ceiling's bound",
max_memory: "11.75G",
ceiling: Some("12.4G"),
bound: 12032 * M,
held: BY_CEILING,
},
];
for admission in ["caps", "measured"] {
for bounds in &cases {
raised_to_a_bound(admission, bounds);
}
}
}
#[test]
fn a_revert_past_max_memory_goes_back_as_far_as_it_allows_and_finishes_once_it_is_raised() {
let mut after = clear_at_ten(LOWERED + HOUR, 10);
after.push(met(ledger(NOW - 2 * HOUR, 10 * G, 0)));
let text = jvm_bound(&jvm_at("10G", 3), "max_memory = \"12G\"");
let mut daemon = Daemon::restarted(text, lowered(after), 64 * G, NOW - HOUR);
let mut changes = Vec::new();
for look in 0..6 {
daemon.now = NOW + look * HOUR;
daemon.applied_by_hand();
changes.extend(daemon.checked_look(NOW + look * HOUR));
}
assert_eq!(
steps(&changes),
[(10 * G, 12 * G, 2)],
"to max_memory, once"
);
assert_eq!(
changes[0].reason,
format!(
"raise memory back to 12G, its max_memory: {} met the 10G cap since auto sizing lowered it from 14G",
ledger_test()
)
);
let jvm = daemon.jvm();
assert_eq!(jvm.advice, Advice::SliceTooSmall { memory: 14 * G });
assert_eq!(jvm.advice_word(), "at max_memory");
assert_eq!(
daemon.sizing().advice_line(&jvm),
format!(
"keep 12G, its max_memory: its jobs need 14G ({} met the 10G cap since auto sizing lowered it from \
14G); raise max_memory to give them more",
ledger_test()
)
);
daemon.text = daemon
.text
.replace("max_memory = \"12G\"", "max_memory = \"16G\"");
daemon.announce();
let finished = daemon.checked_look(NOW + 7 * HOUR);
assert_eq!(steps(&finished), [(12 * G, 14 * G, 2)]);
assert_eq!(daemon.checked_look(NOW + 8 * HOUR), [], "then nothing more");
}
#[test]
fn a_lowering_stops_at_min_memory_and_then_keeps_it_saying_what_holds_it() {
for admission in ["caps", "measured"] {
let text = host(
&jvm_bound(&jvm_at("10G", 2), "min_memory = \"8G\""),
&format!("admission = \"{admission}\""),
);
let mut daemon = Daemon::new(text, NOW - 2 * 24 * HOUR);
for i in 0..12 {
let at = NOW - 40 * HOUR + i * HOUR;
daemon.history.jobs.push(ledger(at, 10 * G, 4 * G));
}
daemon.now = NOW;
let jvm = daemon.jvm();
assert_eq!(
jvm.advice,
Advice::Lower {
memory: 8 * G,
slots: 2
},
"{admission}"
);
let line = daemon.sizing().advice_line(&jvm);
assert!(
line.starts_with(
"lower memory to 8G from 10G, its min_memory: its largest peak + 1G for page cache is 5G"
),
"{admission}: {line}"
);
assert_eq!(daemon.applied_by_hand(), Some(8 * G), "{admission}");
let mut changes = Vec::new();
for day in 0..4 {
changes.extend(daemon.checked_look(NOW + day * 25 * HOUR));
}
assert_eq!(
steps(&changes),
[(10 * G, 8 * G, 2)],
"{admission}: to min_memory, once"
);
let jvm = daemon.jvm();
assert_eq!(jvm.advice, Advice::Keep { memory: 5 * G }, "{admission}");
assert_eq!(jvm.advice.tone(), Tone::Good, "{admission}");
assert_eq!(
daemon.applied_by_hand(),
None,
"{admission}: nothing to apply"
);
assert_eq!(
daemon.sizing().advice_line(&jvm),
"keep 8G, its min_memory: its largest peak + 1G for page cache is 5G",
"{admission}"
);
}
}
#[test]
fn a_lowering_whose_jobs_waited_adds_no_slots_past_max_slots() {
let text = host(
&jvm_bound(&jvm_at("10G", 2), "min_memory = \"6G\"\nmax_slots = 2"),
"admission = \"caps\"",
);
let mut daemon = Daemon::new(text, NOW - 2 * 24 * HOUR);
for i in 0..12 {
let at = NOW - 40 * HOUR + i * HOUR;
let run = ledger(at, 10 * G, 4 * G);
daemon.history.jobs.push(crate::sizing::Ran {
started_at: Some(at + 2 * 60 * 1000),
..run
});
}
daemon.now = NOW;
assert_eq!(
daemon.jvm().advice,
Advice::Lower {
memory: 6 * G,
slots: 2
}
);
assert_eq!(daemon.applied_by_hand(), Some(6 * G));
let mut changes = Vec::new();
for day in 0..4 {
changes.extend(daemon.checked_look(NOW + day * 25 * HOUR));
}
assert!(
changes.iter().all(|change| change.to.slots <= 2),
"auto sizing keeps to max_slots too: {changes:?}"
);
assert_eq!(steps(&changes).last().map(|step| step.1), Some(6 * G));
}
#[test]
fn a_revert_gives_back_no_slots_past_max_slots() {
let mut after = clear_at_ten(LOWERED + HOUR, 10);
after.push(met(ledger(NOW - 2 * HOUR, 10 * G, 0)));
let text = jvm_bound(&jvm_at("10G", 1), "max_memory = \"11G\"\nmax_slots = 1");
let mut daemon = Daemon::restarted(text, lowered(after), 64 * G, NOW - HOUR);
daemon.now = NOW;
assert_eq!(
daemon.jvm().advice,
Advice::Raise {
memory: 11 * G,
slots: 1
}
);
assert_eq!(daemon.applied_by_hand(), Some(11 * G));
let changes = daemon.checked_look(NOW);
assert_eq!(
steps(&changes),
[(10 * G, 11 * G, 1)],
"the revert, within its bounds"
);
assert!(
changes[0]
.reason
.starts_with("raise memory back to 11G, its max_memory: "),
"{}",
changes[0].reason
);
}