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 [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id C2E491FF172
	for <inbox@lore.proxmox.com>; Wed, 16 Apr 2025 14:57:31 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 4FB6A37224;
	Wed, 16 Apr 2025 14:57:30 +0200 (CEST)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Date: Wed, 16 Apr 2025 14:56:42 +0200
Message-Id: <20250416125642.291552-27-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250416125642.291552-1-l.wagner@proxmox.com>
References: <20250416125642.291552-1-l.wagner@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.016 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 v3 26/26] metric
 collection: use JoinSet instead of joining from handles in a Vec
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>

This lets us process finished tasks in the order they finish, not in the
order they were spawned.

Suggested-by: Wolfang Bumiller <w.bumiller@proxmox.com>
Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
Reviewed-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---

Notes:
    New in v2.

 .../src/metric_collection/collection_task.rs  | 25 ++++++++-----------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/server/src/metric_collection/collection_task.rs b/server/src/metric_collection/collection_task.rs
index f36bad48..469b518c 100644
--- a/server/src/metric_collection/collection_task.rs
+++ b/server/src/metric_collection/collection_task.rs
@@ -9,6 +9,7 @@ use tokio::{
         mpsc::{Receiver, Sender},
         oneshot, OwnedSemaphorePermit, Semaphore,
     },
+    task::JoinSet,
     time::{Interval, MissedTickBehavior},
 };
 
@@ -239,7 +240,8 @@ impl MetricCollectionTask {
         remotes_to_fetch: &[String],
     ) {
         let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_CONNECTIONS));
-        let mut handles = Vec::new();
+        let mut handles = JoinSet::new();
+
         let now = proxmox_time::epoch_i64();
 
         for remote_name in remotes_to_fetch {
@@ -262,29 +264,22 @@ impl MetricCollectionTask {
 
             if let Some(remote) = remote_config.get(remote_name).cloned() {
                 log::debug!("fetching remote '{}'", remote.id);
-                let handle = tokio::spawn(Self::fetch_single_remote(
+                handles.spawn(Self::fetch_single_remote(
                     remote,
                     status,
                     self.metric_data_tx.clone(),
                     permit,
                 ));
-
-                handles.push((remote_name.clone(), handle));
             }
         }
 
-        for (remote_name, handle) in handles {
-            let res = handle.await;
-
+        while let Some(res) = handles.join_next().await {
             match res {
-                Ok(Ok(ts)) => {
-                    self.state.set_status(remote_name, ts);
+                Ok((name, status)) => {
+                    self.state.set_status(name, status);
                 }
-                Ok(Err(err)) => log::error!("failed to collect metrics for {remote_name}: {err}"),
                 Err(err) => {
-                    log::error!(
-                        "join error for metric collection task for remote {remote_name}: {err}"
-                    )
+                    log::error!("join error for metric collection task for remote: {err}")
                 }
             }
         }
@@ -332,7 +327,7 @@ impl MetricCollectionTask {
         mut status: RemoteStatus,
         sender: Sender<RrdStoreRequest>,
         _permit: OwnedSemaphorePermit,
-    ) -> Result<RemoteStatus, Error> {
+    ) -> (String, RemoteStatus) {
         let (result_tx, result_rx) = oneshot::channel();
 
         let now = proxmox_time::epoch_i64();
@@ -400,7 +395,7 @@ impl MetricCollectionTask {
             }
         }
 
-        Ok(status)
+        (remote.id, status)
     }
 }
 
-- 
2.39.5



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