From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 032AA1FF0C0 for ; Wed, 18 Nov 2026 10:49:32 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 9218421B9A; Wed, 18 Nov 2026 10:45:05 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox v2 41/63] notify: implement notification queue submitter Date: Wed, 18 Nov 2026 10:43:25 +0100 Message-ID: <20261118094348.273646-42-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118094348.273646-1-l.wagner@proxmox.com> References: <20261118094348.273646-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794995040182 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.190 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: VXV5P4HELJHV7P2CO33OEJPKZY244XJ6 X-Message-ID-Hash: VXV5P4HELJHV7P2CO33OEJPKZY244XJ6 X-MailFrom: l.wagner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Each product will be switched to a queue/worker approach, where each sender simply drops the notification in serialized form into a queue directory. Each notification has a unique UUID which is used as the filename, making filename collisions not an issue in practice. The new QueueSubmitter has a single method `submit`, which serialized the notification and atomically places it in the pre-existing queue directory. Signed-off-by: Lukas Wagner --- Cargo.toml | 1 + proxmox-notify/Cargo.toml | 2 + proxmox-notify/src/error.rs | 16 +++ proxmox-notify/src/lib.rs | 3 + proxmox-notify/src/queue/mod.rs | 18 +++ proxmox-notify/src/queue/submitter.rs | 171 ++++++++++++++++++++++++++ 6 files changed, 211 insertions(+) create mode 100644 proxmox-notify/src/queue/mod.rs create mode 100644 proxmox-notify/src/queue/submitter.rs diff --git a/Cargo.toml b/Cargo.toml index 091f585d1..c1ce30ee0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -146,6 +146,7 @@ signal-hook-registry = "1" syn = { version = "2", features = [ "full", "visit-mut" ] } sync_wrapper = "1" tar = "0.4" +tempfile = "3.15" termcolor = "1.1.2" thiserror = "2" tokio = "1.6" diff --git a/proxmox-notify/Cargo.toml b/proxmox-notify/Cargo.toml index e47912d11..4cfb1f2eb 100644 --- a/proxmox-notify/Cargo.toml +++ b/proxmox-notify/Cargo.toml @@ -42,6 +42,7 @@ proxmox-uuid = { workspace = true, features = ["serde"] } [dev-dependencies] pretty_assertions.workspace = true +tempfile.workspace = true [features] default = [] @@ -78,3 +79,4 @@ send-impl = [ pve-context = ["dep:proxmox-sys", "legacy-matchers"] pbs-context = ["dep:proxmox-sys", "legacy-matchers"] legacy-matchers = [] +queue-submitter = ["dep:proxmox-sys"] diff --git a/proxmox-notify/src/error.rs b/proxmox-notify/src/error.rs index c8747f39f..bab240506 100644 --- a/proxmox-notify/src/error.rs +++ b/proxmox-notify/src/error.rs @@ -56,6 +56,14 @@ pub enum Error { source: Option, }, + #[error("notification queue error: {err}", err = print_optional_error(.message, .source))] + Queue { + /// Description of the error. + message: String, + /// Underlying error, if any. + source: Option, + }, + /// The notification configuration could not be serialized. #[error("could not serialize config: {0}")] ConfigSerialization(#[source] BoxedError), @@ -155,6 +163,14 @@ impl Error { } } + /// Create an [`Error::Queue`] with an underlying error. + pub(crate) fn queue_with_source(s: impl Into, source: impl Into) -> Self { + Self::Queue { + message: s.into(), + source: Some(source.into()), + } + } + /// Create an [`Error::ConfigDeserialization`] from the given error. pub(crate) fn config_deserialization(err: impl Into) -> Self { Self::ConfigDeserialization(err.into()) diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs index 4304e2977..ab5a929c1 100644 --- a/proxmox-notify/src/lib.rs +++ b/proxmox-notify/src/lib.rs @@ -28,6 +28,9 @@ mod send; #[cfg(feature = "send-impl")] pub use send::send; +#[cfg(feature = "queue-submitter")] +pub mod queue; + mod error; pub use error::Error; diff --git a/proxmox-notify/src/queue/mod.rs b/proxmox-notify/src/queue/mod.rs new file mode 100644 index 000000000..25137e123 --- /dev/null +++ b/proxmox-notify/src/queue/mod.rs @@ -0,0 +1,18 @@ +use std::path::Path; + +use crate::Error; + +mod submitter; +pub use submitter::QueueSubmitter; + +/// Maximum size of a queued notification file. +/// +/// Well above what legitimate producers submit, e.g. forwarded mails or backup logs limited to +/// 1 MiB, but bounds the memory a bogus file written directly to the queue can consume. +const MAX_NOTIFICATION_SIZE: u64 = 16 * 1024 * 1024; + +fn fsync_queue_dir(queue_dir: &Path) -> Result<(), Error> { + std::fs::File::open(queue_dir) + .and_then(|dir| dir.sync_all()) + .map_err(|err| Error::queue_with_source("fsync of queue directory failed", err)) +} diff --git a/proxmox-notify/src/queue/submitter.rs b/proxmox-notify/src/queue/submitter.rs new file mode 100644 index 000000000..3f96a2152 --- /dev/null +++ b/proxmox-notify/src/queue/submitter.rs @@ -0,0 +1,171 @@ +//! Submission of notifications to the queue. + +use std::path::PathBuf; + +use proxmox_sys::fs::CreateOptions; + +use crate::{Error, Notification}; + +use super::MAX_NOTIFICATION_SIZE; + +/// Writes notifications to the queue directory. +pub struct QueueSubmitter { + queue_dir: PathBuf, + create_options: CreateOptions, +} + +impl QueueSubmitter { + /// Create a new submitter for the queue directory `base_dir`. + /// + /// Queued notification files are created with `create_options`. + pub fn new>(base_dir: P, create_options: CreateOptions) -> Self { + Self { + queue_dir: base_dir.into(), + create_options, + } + } + + /// Add a notification to the queue. + /// + /// The file is written atomically and the queue directory is synced before returning. + pub fn submit(&self, notification: &Notification) -> Result<(), Error> { + let filename = format!("notification-{}", notification.id()); + let path = self.queue_dir.join(filename); + + let bytes = serde_json::to_vec(notification).map_err(|err| { + Error::other_with_source( + format!("failed to serialize notification {}", notification.id()), + err, + ) + })?; + + let serialized_size = bytes.len(); + if serialized_size as u64 > MAX_NOTIFICATION_SIZE { + return Err(Error::invalid_notification(format!( + "serialized notification too large ({serialized_size} > {MAX_NOTIFICATION_SIZE} bytes)" + ))); + } + + proxmox_sys::fs::replace_file(&path, &bytes, self.create_options, true).map_err(|err| { + Error::queue_with_source( + format!( + "failed to store serialized notification {}", + notification.id() + ), + err, + ) + })?; + + super::fsync_queue_dir(&self.queue_dir)?; + + tracing::info!( + "submitted notification {} to notification queue", + notification.id() + ); + + Ok(()) + } +} + +#[cfg(test)] +mod test { + use proxmox_uuid::Uuid; + use serde_json::json; + use tempfile::tempdir; + + use crate::notification::Content; + + use super::*; + + /// Build a notification that serializes to exactly `size` bytes. + fn notification_with_size(size: u64) -> Notification { + let mut notification = Notification::template_builder("foo", json!({ "pad": "" })) + .build() + .unwrap(); + + let base_size = serde_json::to_vec(¬ification).unwrap().len() as u64; + let Content::Template { data, .. } = &mut notification.content else { + unreachable!(); + }; + data["pad"] = "a".repeat((size - base_size) as usize).into(); + + assert_eq!( + serde_json::to_vec(¬ification).unwrap().len() as u64, + size + ); + + notification + } + + #[test] + fn test_submit_multiple() { + let base_dir = tempdir().unwrap(); + let submitter = QueueSubmitter::new(base_dir.path(), CreateOptions::new()); + + let notifications: Vec<_> = (0..3) + .map(|_| Notification::template_builder("foo", ()).build().unwrap()) + .collect(); + + for notification in ¬ifications { + submitter.submit(notification).unwrap(); + } + + let entries: Vec<_> = std::fs::read_dir(base_dir.path()) + .unwrap() + .map(|entry| entry.unwrap()) + .collect(); + assert_eq!(entries.len(), notifications.len()); + + for entry in entries { + let filename = entry.file_name().into_string().unwrap(); + let id: Uuid = filename + .strip_prefix("notification-") + .unwrap() + .parse() + .unwrap(); + + let submitted = notifications.iter().find(|n| n.id() == &id).unwrap(); + + let content = std::fs::read(entry.path()).unwrap(); + let queued: Notification = serde_json::from_slice(&content).unwrap(); + assert_eq!(&queued, submitted); + } + } + + #[test] + fn test_submit_missing_queue_dir() { + let base_dir = tempdir().unwrap(); + let submitter = QueueSubmitter::new(base_dir.path().join("missing"), CreateOptions::new()); + + let notification = Notification::template_builder("foo", ()).build().unwrap(); + assert!(submitter.submit(¬ification).is_err()); + } + + #[test] + fn test_submit_max_size() { + let base_dir = tempdir().unwrap(); + let submitter = QueueSubmitter::new(base_dir.path(), CreateOptions::new()); + + let notification = notification_with_size(MAX_NOTIFICATION_SIZE); + submitter.submit(¬ification).unwrap(); + + let path = base_dir + .path() + .join(format!("notification-{}", notification.id())); + assert_eq!( + std::fs::metadata(path).unwrap().len(), + MAX_NOTIFICATION_SIZE + ); + } + + #[test] + fn test_submit_oversized() { + let base_dir = tempdir().unwrap(); + let submitter = QueueSubmitter::new(base_dir.path(), CreateOptions::new()); + + let notification = notification_with_size(MAX_NOTIFICATION_SIZE + 1); + assert!(submitter.submit(¬ification).is_err()); + + assert_eq!(std::fs::read_dir(base_dir.path()).unwrap().count(), 0); + } +} -- 2.47.3