Skip to main content

feldera_types/
memory_pressure.rs

1use std::fmt::Display;
2
3use bytemuck::NoUninit;
4use serde::{Deserialize, Serialize};
5use utoipa::ToSchema;
6
7pub const MODERATE_MEMORY_PRESSURE_THRESHOLD: f64 = 0.85;
8pub const HIGH_MEMORY_PRESSURE_THRESHOLD: f64 = 0.90;
9pub const CRITICAL_MEMORY_PRESSURE_THRESHOLD: f64 = 0.95;
10
11/// Memory pressure level.
12///
13/// The current memory pressure level is computed as a function of the current process
14/// resident set size (RSS) and the user-configured memory limit (`max_rss`).
15///
16/// As the memory pressure level increases, the system will apply increasing backpressure to
17/// push state cached in memory to storage.
18///
19/// - `Low`: less than 85% of the user-configured memory limit has been allocated.
20/// - `Moderate`: between 85% and 90% of the user-configured memory limit has been allocated.
21/// - `High`: between 90% and 95% of the user-configured memory limit has been allocated.
22/// - `Critical`: more than 95% of the user-configured memory limit has been allocated.
23#[derive(
24    Debug,
25    Default,
26    Clone,
27    Copy,
28    Eq,
29    PartialEq,
30    PartialOrd,
31    Ord,
32    Serialize,
33    Deserialize,
34    ToSchema,
35    NoUninit,
36)]
37#[repr(u8)]
38#[serde(rename_all = "snake_case")]
39pub enum MemoryPressure {
40    /// Less than 85% of the user-configured memory limit has been allocated.
41    #[default]
42    Low = 0,
43    /// Between 85% and 90% of the user-configured memory limit has been allocated.
44    Moderate = 1,
45    /// Between 90% and 95% of the user-configured memory limit has been allocated.
46    High = 2,
47    /// More than 95% of the user-configured memory limit has been allocated.
48    Critical = 3,
49}
50
51impl TryFrom<u8> for MemoryPressure {
52    type Error = ();
53
54    fn try_from(value: u8) -> Result<Self, Self::Error> {
55        match value {
56            0 => Ok(MemoryPressure::Low),
57            1 => Ok(MemoryPressure::Moderate),
58            2 => Ok(MemoryPressure::High),
59            3 => Ok(MemoryPressure::Critical),
60            _ => Err(()),
61        }
62    }
63}
64
65impl MemoryPressure {
66    pub fn as_str(&self) -> &'static str {
67        match self {
68            MemoryPressure::Low => "low",
69            MemoryPressure::Moderate => "moderate",
70            MemoryPressure::High => "high",
71            MemoryPressure::Critical => "critical",
72        }
73    }
74}
75
76impl Display for MemoryPressure {
77    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78        f.write_str(self.as_str())
79    }
80}