Skip to main content

feldera_types/
memory_pressure.rs

1use serde::{Deserialize, Serialize};
2use utoipa::ToSchema;
3
4pub const MODERATE_MEMORY_PRESSURE_THRESHOLD: f64 = 0.85;
5pub const HIGH_MEMORY_PRESSURE_THRESHOLD: f64 = 0.90;
6pub const CRITICAL_MEMORY_PRESSURE_THRESHOLD: f64 = 0.95;
7
8/// Memory pressure level.
9///
10/// The current memory pressure level is computed as a function of the current process
11/// resident set size (RSS) and the user-configured memory limit (`max_rss`).
12///
13/// As the memory pressure level increases, the system will apply increasing backpressure to
14/// push state cached in memory to storage.
15///
16/// - `Low`: less than 85% of the user-configured memory limit has been allocated.
17/// - `Moderate`: between 85% and 90% of the user-configured memory limit has been allocated.
18/// - `High`: between 90% and 95% of the user-configured memory limit has been allocated.
19/// - `Critical`: more than 95% of the user-configured memory limit has been allocated.
20#[derive(
21    Debug, Default, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize, ToSchema,
22)]
23#[repr(u8)]
24#[serde(rename_all = "snake_case")]
25pub enum MemoryPressure {
26    /// Less than 85% of the user-configured memory limit has been allocated.
27    #[default]
28    Low = 0,
29    /// Between 85% and 90% of the user-configured memory limit has been allocated.
30    Moderate = 1,
31    /// Between 90% and 95% of the user-configured memory limit has been allocated.
32    High = 2,
33    /// More than 95% of the user-configured memory limit has been allocated.
34    Critical = 3,
35}
36
37impl TryFrom<u8> for MemoryPressure {
38    type Error = ();
39
40    fn try_from(value: u8) -> Result<Self, Self::Error> {
41        match value {
42            0 => Ok(MemoryPressure::Low),
43            1 => Ok(MemoryPressure::Moderate),
44            2 => Ok(MemoryPressure::High),
45            3 => Ok(MemoryPressure::Critical),
46            _ => Err(()),
47        }
48    }
49}