Skip to content

[Merged by Bors] - Add a function for calculating the size limit of log volumes #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Added

- Add a function for calculating the size limit of log volumes ([#621]).

[#621]: https://github.com/stackabletech/operator-rs/pull/621

## [0.43.0] - 2023-07-06

### Added
Expand Down
13 changes: 13 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use crate::error::{Error, OperatorResult};
use std::{
fmt::Display,
iter::Sum,
ops::{Add, AddAssign, Div, Mul, Sub, SubAssign},
str::FromStr,
};
Expand Down Expand Up @@ -347,6 +348,18 @@ impl Add<MemoryQuantity> for MemoryQuantity {
}
}

impl Sum<MemoryQuantity> for MemoryQuantity {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(
MemoryQuantity {
value: 0.0,
unit: BinaryMultiple::Kibi,
},
MemoryQuantity::add,
)
}
}

impl AddAssign<MemoryQuantity> for MemoryQuantity {
fn add_assign(&mut self, rhs: MemoryQuantity) {
self.value += rhs.value;
Expand Down
72 changes: 68 additions & 4 deletions src/product_logging/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

use std::cmp;

use k8s_openapi::api::core::v1::ResourceRequirements;

use crate::{
builder::ContainerBuilder, commons::product_image_selection::ResolvedProductImage,
k8s_openapi::api::core::v1::Container, kube::Resource, role_utils::RoleGroupRef,
builder::ContainerBuilder,
commons::product_image_selection::ResolvedProductImage,
k8s_openapi::{
api::core::v1::{Container, ResourceRequirements},
apimachinery::pkg::api::resource::Quantity,
},
kube::Resource,
memory::{BinaryMultiple, MemoryQuantity},
role_utils::RoleGroupRef,
};

use super::spec::{
Expand All @@ -26,6 +31,65 @@ const SHUTDOWN_FILE: &str = "shutdown";
/// File name of the Vector config file
pub const VECTOR_CONFIG_FILE: &str = "vector.toml";

/// Calculate the size limit for the log volume.
///
/// The size limit must be much larger than the sum of the given maximum log file sizes for the
/// following reasons:
/// - The log file rollover occurs when the log file exceeds the maximum log file size. Depending
/// on the size of the last log entries, the file can be several kilobytes larger than defined.
/// - The actual disk usage depends on the block size of the file system.
/// - OpenShift sometimes reserves more than twice the amount of blocks than needed. For instance,
/// a ZooKeeper log file with 4,127,151 bytes occupied 4,032 blocks. Then log entries were written
/// and the actual file size increased to 4,132,477 bytes which occupied 8,128 blocks.
///
/// # Example
///
/// ```
/// use stackable_operator::{
/// builder::{
/// PodBuilder,
/// meta::ObjectMetaBuilder,
/// },
/// memory::{
/// BinaryMultiple,
/// MemoryQuantity,
/// },
/// };
/// # use stackable_operator::product_logging;
///
/// const MAX_INIT_CONTAINER_LOG_FILES_SIZE: MemoryQuantity = MemoryQuantity {
/// value: 1.0,
/// unit: BinaryMultiple::Mebi,
/// };
/// const MAX_MAIN_CONTAINER_LOG_FILES_SIZE: MemoryQuantity = MemoryQuantity {
/// value: 10.0,
/// unit: BinaryMultiple::Mebi,
/// };
///
/// PodBuilder::new()
/// .metadata(ObjectMetaBuilder::default().build())
/// .add_empty_dir_volume(
/// "log",
/// Some(product_logging::framework::calculate_log_volume_size_limit(
/// &[
/// MAX_INIT_CONTAINER_LOG_FILES_SIZE,
/// MAX_MAIN_CONTAINER_LOG_FILES_SIZE,
/// ],
/// )),
/// )
/// .build()
/// .unwrap();
/// ```
pub fn calculate_log_volume_size_limit(max_log_files_size: &[MemoryQuantity]) -> Quantity {
let log_volume_size_limit = max_log_files_size
.iter()
.cloned()
.sum::<MemoryQuantity>()
.scale_to(BinaryMultiple::Mebi)
* 3.0;
log_volume_size_limit.into()
}

/// Create a Bash command which filters stdout and stderr according to the given log configuration
/// and additionally stores the output in log files
///
Expand Down