pub const STREAMING_WORKING_SET_BYTES: u64 = 33 * 1024 * 1024;
pub const LOG_EXPANSION: u64 = 4;
pub fn estimated_slice_bytes(log_bytes: u64) -> u64 {
STREAMING_WORKING_SET_BYTES.saturating_add(log_bytes.saturating_mul(LOG_EXPANSION))
}
pub fn slices_in_flight(
budget_bytes: Option<u64>,
partitions: usize,
slice_log_bytes: &[Option<u64>],
configured_max: usize,
) -> usize {
let configured_max = configured_max.max(1);
let Some(budget) = budget_bytes else {
return configured_max;
};
if slice_log_bytes.iter().any(Option::is_none) {
return 1;
}
let share = budget / (partitions.max(1) as u64);
let per_slice = slice_log_bytes
.iter()
.filter_map(|b| *b)
.map(estimated_slice_bytes)
.max()
.unwrap_or_else(|| estimated_slice_bytes(0));
let admitted = share / per_slice.max(1);
(admitted as usize).clamp(1, configured_max)
}
#[cfg(test)]
mod tests {
use super::*;
const MIB: u64 = 1024 * 1024;
#[test]
fn without_a_budget_the_configured_ceiling_is_returned() {
assert_eq!(slices_in_flight(None, 4, &[Some(100 * MIB)], 4), 4);
assert_eq!(slices_in_flight(None, 1, &[], 7), 7);
}
#[test]
fn the_budget_is_shared_across_partitions_not_granted_to_each() {
let one_partition = slices_in_flight(Some(1024 * MIB), 1, &[Some(0)], 64);
let four_partitions = slices_in_flight(Some(1024 * MIB), 4, &[Some(0)], 64);
assert_eq!(one_partition, 31, "1024 / 33");
assert_eq!(four_partitions, 7, "256 / 33");
assert!(
four_partitions * 4 <= one_partition + 4,
"splitting the budget must not multiply it: {four_partitions} x 4 vs {one_partition}"
);
}
#[test]
fn a_log_heavy_slice_admits_fewer_than_a_log_light_one() {
let budget = Some(512 * MIB);
let light = slices_in_flight(budget, 1, &[Some(MIB)], 64);
let heavy = slices_in_flight(budget, 1, &[Some(64 * MIB)], 64);
assert!(
heavy < light,
"64 MiB of log must admit fewer than 1 MiB: {heavy} vs {light}"
);
assert_eq!(heavy, 1, "512 / (33 + 256) = 1");
}
#[test]
fn the_most_expensive_slice_sets_the_admission_count() {
let budget = Some(512 * MIB);
let even = slices_in_flight(budget, 1, &[Some(MIB); 4], 64);
let skewed = slices_in_flight(
budget,
1,
&[Some(MIB), Some(MIB), Some(MIB), Some(64 * MIB)],
64,
);
assert!(
skewed < even,
"one expensive slice must lower the count: {skewed} vs {even}"
);
}
#[test]
fn a_budget_smaller_than_one_slice_still_admits_one() {
assert_eq!(slices_in_flight(Some(MIB), 1, &[Some(512 * MIB)], 8), 1);
assert_eq!(slices_in_flight(Some(0), 1, &[Some(0)], 8), 1);
}
#[test]
fn an_unestimatable_slice_admits_one() {
let budget = Some(4096 * MIB);
assert_eq!(
slices_in_flight(budget, 1, &[Some(MIB), None, Some(MIB)], 8),
1,
"one slice with no recorded size makes the whole estimate unsafe"
);
assert!(
slices_in_flight(budget, 1, &[Some(MIB), Some(MIB), Some(MIB)], 8) > 1,
"the same slices with sizes known must admit more, or the test above \\
passes for the wrong reason"
);
assert_eq!(
slices_in_flight(None, 1, &[None], 8),
8,
"with no budget there is nothing to be conservative about"
);
}
#[test]
fn the_estimate_has_a_floor_and_grows_with_log_bytes() {
assert_eq!(estimated_slice_bytes(0), STREAMING_WORKING_SET_BYTES);
assert!(estimated_slice_bytes(MIB) > estimated_slice_bytes(0));
let predicted = estimated_slice_bytes(26 * MIB) / MIB;
assert!(
(100..=160).contains(&predicted),
"estimate for a 26 MiB log slice should be near the measured 117 MiB, got {predicted}"
);
}
}