Skip to content

Commit

Permalink
Add a unit test for calculate_log_volume_size_limit
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedweber committed Jul 13, 2023
1 parent 1dda71b commit 4f71f38
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ impl MemoryQuantity {
}
}

/// Ceils the value of this MemoryQuantity.
pub fn ceil(&self) -> Self {
Self {
value: self.value.ceil(),
unit: self.unit,
}
}

/// If the MemoryQuantity value is smaller than 1 (starts with a zero), convert it to a smaller
/// unit until the non fractional part of the value is not zero anymore.
/// This can fail if the quantity is smaller than 1kB.
Expand Down
31 changes: 29 additions & 2 deletions src/product_logging/framework.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Log aggregation framework

use std::cmp;
use std::{cmp, ops::Mul};

use crate::{
builder::ContainerBuilder,
Expand Down Expand Up @@ -86,7 +86,12 @@ pub fn calculate_log_volume_size_limit(max_log_files_size: &[MemoryQuantity]) ->
.cloned()
.sum::<MemoryQuantity>()
.scale_to(BinaryMultiple::Mebi)
* 3.0;
// According to the reasons mentioned in the function documentation, the multiplier must be
// greater than 2. Manual tests with ZooKeeper 3.8 in an OpenShift cluster showed that 3 is
// absolutely sufficient.
.mul(3.0)
// Avoid bulky numbers due to the floating-point arithmetic.
.ceil();
log_volume_size_limit.into()
}

Expand Down Expand Up @@ -1112,8 +1117,30 @@ touch {stackable_log_dir}/{VECTOR_LOG_DIR}/{SHUTDOWN_FILE}"
mod tests {
use super::*;
use crate::product_logging::spec::{AppenderConfig, LoggerConfig};
use rstest::rstest;
use std::collections::BTreeMap;

#[rstest]
#[case("0Mi", &[])]
#[case("3Mi", &["1Mi"])]
#[case("5Mi", &["1.5Mi"])]
#[case("1Mi", &["100Ki"])]
#[case("3076Mi", &["1Ki", "1Mi", "1Gi"])]
fn test_calculate_log_volume_size_limit(
#[case] expected_log_volume_size_limit: &str,
#[case] max_log_files_sizes: &[&str],
) {
let input = max_log_files_sizes
.iter()
.map(|v| MemoryQuantity::try_from(Quantity(v.to_string())).unwrap())
.collect::<Vec<_>>();
let calculated_log_volume_size_limit = calculate_log_volume_size_limit(&input);
assert_eq!(
Quantity(expected_log_volume_size_limit.to_string()),
calculated_log_volume_size_limit
);
}

#[test]
fn test_create_log4j2_config() {
let log_config = AutomaticContainerLogConfig {
Expand Down

0 comments on commit 4f71f38

Please sign in to comment.