Skip to main content

feldera_types/
preprocess.rs

1//! Configuration describing a preprocessor
2
3use std::time::Duration;
4
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7use utoipa::ToSchema;
8
9use crate::{
10    duration::{ConfigDuration, LegacyUnit},
11    nullable_duration_setting,
12};
13
14/// Default for [PreprocessorConfig::max_allowed_call_duration].
15pub const DEFAULT_PREPROCESSOR_MAX_ALLOWED_CALL_DURATION: ConfigDuration =
16    ConfigDuration::from_secs(1);
17
18fn default_max_allowed_call_duration() -> String {
19    DEFAULT_PREPROCESSOR_MAX_ALLOWED_CALL_DURATION.to_string()
20}
21
22nullable_duration_setting!(
23    duration_max_allowed_call_duration,
24    "max_allowed_call_duration",
25    LegacyUnit::Secs
26);
27
28/// Configuration for describing a preprocessor
29#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Serialize, ToSchema)]
30pub struct PreprocessorConfig {
31    /// Name of the preprocessor.
32    /// All preprocessors with the same name will perform the same task.
33    pub name: String,
34    /// True if the preprocessor is message-oriented: true if each preprocessor
35    /// output record corresponds to a whole number of of parser records.
36    pub message_oriented: bool,
37    /// Arbitrary additional configuration.
38    pub config: Value,
39    /// Maximum allowed duration of one call to the preprocessor, for example
40    /// `10s`.  While a call runs longer, the connector is unhealthy and the
41    /// pipeline logs warnings.
42    ///
43    /// When not set it defaults to `1s`; `null` disables the check.  Values
44    /// less than 1 second are raised to 1 second.
45    #[serde(
46        default,
47        deserialize_with = "duration_max_allowed_call_duration",
48        skip_serializing_if = "Option::is_none"
49    )]
50    #[schema(value_type = Option<String>, default = default_max_allowed_call_duration)]
51    pub max_allowed_call_duration: Option<Option<ConfigDuration>>,
52}
53
54impl PreprocessorConfig {
55    /// The maximum allowed call duration in effect, or `None` if the check is
56    /// disabled.
57    pub fn max_allowed_call_duration(&self) -> Option<Duration> {
58        let limit = self
59            .max_allowed_call_duration
60            .unwrap_or(Some(DEFAULT_PREPROCESSOR_MAX_ALLOWED_CALL_DURATION))?;
61        Some(Duration::from(limit).max(Duration::from_secs(1)))
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use std::time::Duration;
68
69    use serde_json::json;
70
71    use super::PreprocessorConfig;
72
73    /// The limit of a configuration whose `max_allowed_call_duration` key is
74    /// `field`, or that has no such key if `field` is `None`.
75    fn limit(field: Option<serde_json::Value>) -> Option<Duration> {
76        let mut config = json!({"name": "p", "message_oriented": true, "config": {}});
77        if let Some(field) = field {
78            config["max_allowed_call_duration"] = field;
79        }
80        serde_json::from_value::<PreprocessorConfig>(config)
81            .unwrap()
82            .max_allowed_call_duration()
83    }
84
85    #[test]
86    fn max_allowed_call_duration() {
87        assert_eq!(limit(None), Some(Duration::from_secs(1)));
88        assert_eq!(limit(Some(json!("10s"))), Some(Duration::from_secs(10)));
89        assert_eq!(limit(Some(json!("0s"))), Some(Duration::from_secs(1)));
90        assert_eq!(limit(Some(json!("100ms"))), Some(Duration::from_secs(1)));
91        assert_eq!(limit(Some(json!(null))), None);
92    }
93}