From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 90F551FF170 for ; Thu, 21 Aug 2025 11:53:57 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id A620D1C82B; Thu, 21 Aug 2025 11:53:52 +0200 (CEST) From: Lukas Wagner To: pdm-devel@lists.proxmox.com Date: Thu, 21 Aug 2025 11:53:10 +0200 Message-ID: <20250821095319.134215-15-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250821095319.134215-1-l.wagner@proxmox.com> References: <20250821095319.134215-1-l.wagner@proxmox.com> MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1755769977478 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.024 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pdm-devel] [PATCH proxmox-datacenter-manager v6 14/23] metric collection: periodically clean removed remotes from statefile X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Datacenter Manager development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" Adding and removing remotes can leave leftover data in the statefile, hence it makes sense to clean it up periodically. Signed-off-by: Lukas Wagner Reviewed-by: Maximiliano Sandoval --- .../src/metric_collection/collection_task.rs | 6 ++++ server/src/metric_collection/state.rs | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/server/src/metric_collection/collection_task.rs b/server/src/metric_collection/collection_task.rs index 4013c59f..b960efff 100644 --- a/server/src/metric_collection/collection_task.rs +++ b/server/src/metric_collection/collection_task.rs @@ -91,6 +91,8 @@ impl MetricCollectionTask { log::debug!("starting metric collection from all remotes - triggered by timer"); if let Some(remotes) = Self::load_remote_config() { + self.cleanup_removed_remotes_from_state(&remotes); + let now = Instant::now(); let to_fetch = remotes.iter().map(|(name, _)| name.into()).collect::>(); self.fetch_remotes(&remotes, &to_fetch).await; @@ -136,6 +138,10 @@ impl MetricCollectionTask { } } + fn cleanup_removed_remotes_from_state(&mut self, remotes: &SectionConfigData) { + self.state.retain(|remote| remotes.get(remote).is_some()); + } + /// Set up a [`tokio::time::Interval`] instance with the provided interval. /// The timer will be aligned, e.g. an interval of `60` will let the timer /// fire at minute boundaries. diff --git a/server/src/metric_collection/state.rs b/server/src/metric_collection/state.rs index e76444a9..7f68843e 100644 --- a/server/src/metric_collection/state.rs +++ b/server/src/metric_collection/state.rs @@ -74,6 +74,11 @@ impl MetricCollectionState { Ok(()) } + /// Retain all remotes for which the predicate holds. + pub fn retain(&mut self, check: impl Fn(&str) -> bool) { + self.state.remote_status.retain(|remote, _| check(remote)); + } + fn load_or_default(path: &Path) -> Result { let content = proxmox_sys::fs::file_read_optional_string(path)?; @@ -115,4 +120,31 @@ mod tests { Ok(()) } + + #[test] + fn test_retain() -> Result<(), Error> { + let file = NamedTempFile::new(get_create_options())?; + let options = get_create_options(); + let mut state = MetricCollectionState::new(file.path().into(), options); + + state.set_status( + "remote-1".into(), + RemoteStatus { + ..Default::default() + }, + ); + state.set_status( + "remote-2".into(), + RemoteStatus { + ..Default::default() + }, + ); + + state.retain(|remote| remote == "remote-1"); + + assert!(state.get_status("remote-1").is_some()); + assert!(state.get_status("remote-2").is_none()); + + Ok(()) + } } -- 2.47.2 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel