From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pdm-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id A868B1FF15E
	for <inbox@lore.proxmox.com>; Tue, 11 Feb 2025 13:06:06 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 5BDC42AA5F;
	Tue, 11 Feb 2025 13:05:59 +0100 (CET)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Date: Tue, 11 Feb 2025 13:05:34 +0100
Message-Id: <20250211120541.163621-19-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250211120541.163621-1-l.wagner@proxmox.com>
References: <20250211120541.163621-1-l.wagner@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.010 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 18/25] 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
 <pdm-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pdm-devel>, 
 <mailto:pdm-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pdm-devel/>
List-Post: <mailto:pdm-devel@lists.proxmox.com>
List-Help: <mailto:pdm-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel>, 
 <mailto:pdm-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox Datacenter Manager development discussion
 <pdm-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pdm-devel-bounces@lists.proxmox.com
Sender: "pdm-devel" <pdm-devel-bounces@lists.proxmox.com>

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 <l.wagner@proxmox.com>
---
 .../src/metric_collection/collection_task.rs  | 10 ++++--
 server/src/metric_collection/state.rs         | 32 +++++++++++++++++++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/server/src/metric_collection/collection_task.rs b/server/src/metric_collection/collection_task.rs
index 501df9b..7475368 100644
--- a/server/src/metric_collection/collection_task.rs
+++ b/server/src/metric_collection/collection_task.rs
@@ -96,11 +96,11 @@ impl MetricCollectionTask {
                     self.sleep_for_random_interval_offset().await;
 
                     if let Some(remotes) = Self::load_remote_config() {
-                        let to_fetch = remotes.order.as_slice();
-                        self.fetch_remotes(&remotes, to_fetch).await;
+                        self.cleanup_removed_remotes_from_state(&remotes);
 
                         let now = Instant::now();
-                        self.fetch_remotes(&remotes, &remotes.order).await;
+                        let to_fetch = remotes.order.as_slice();
+                        self.fetch_remotes(&remotes, to_fetch).await;
                         let elapsed = now.elapsed();
 
                         if let Err(err) = self.metric_data_tx.send(
@@ -196,6 +196,10 @@ impl MetricCollectionTask {
         tokio::time::sleep(Duration::from_millis(jitter)).await;
     }
 
+    fn cleanup_removed_remotes_from_state(&mut self, remotes: &SectionConfigData<Remote>) {
+        self.state.retain(|remote| remotes.get(remote).is_some());
+    }
+
     fn get_settings_or_default() -> CollectionSettings {
         // This function is a bit odd, but not sure if there is a much nicer
         // way to do it. We want to fall back to defaults if
diff --git a/server/src/metric_collection/state.rs b/server/src/metric_collection/state.rs
index a9f712d..2e90a4a 100644
--- a/server/src/metric_collection/state.rs
+++ b/server/src/metric_collection/state.rs
@@ -76,6 +76,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<State, Error> {
         let content = proxmox_sys::fs::file_read_optional_string(path)?;
 
@@ -117,4 +122,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.clone());
+
+        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.39.5



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel