This is an automated email from the ASF dual-hosted git repository.

shuber pushed a commit to branch UNOMI-877
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/UNOMI-877 by this push:
     new 8fd8769d0 Cache cluster nodes to optimize retrieval and reduce 
reliance on PersistenceService.
8fd8769d0 is described below

commit 8fd8769d0419a9a03b22f7b45f2b2c5d98ce7341
Author: Serge Huber <[email protected]>
AuthorDate: Tue Aug 26 19:55:43 2025 +0200

    Cache cluster nodes to optimize retrieval and reduce reliance on 
PersistenceService.
---
 .../services/impl/cluster/ClusterServiceImpl.java      | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git 
a/services/src/main/java/org/apache/unomi/services/impl/cluster/ClusterServiceImpl.java
 
b/services/src/main/java/org/apache/unomi/services/impl/cluster/ClusterServiceImpl.java
index c61096f4e..e71973ce3 100644
--- 
a/services/src/main/java/org/apache/unomi/services/impl/cluster/ClusterServiceImpl.java
+++ 
b/services/src/main/java/org/apache/unomi/services/impl/cluster/ClusterServiceImpl.java
@@ -55,6 +55,7 @@ public class ClusterServiceImpl implements ClusterService {
     private long nodeStatisticsUpdateFrequency = 10000;
     private Map<String, Map<String, Serializable>> nodeSystemStatistics = new 
ConcurrentHashMap<>();
     private volatile boolean shutdownNow = false;
+    private volatile List<ClusterNode> cachedClusterNodes = 
Collections.emptyList();
 
     private BundleWatcher bundleWatcher;
     private ScheduledFuture<?> updateSystemStatsFuture;
@@ -431,6 +432,14 @@ public class ClusterServiceImpl implements ClusterService {
             if (!success) {
                 LOGGER.error("Failed to update node {} statistics", nodeId);
             }
+
+            // Always refresh cluster nodes cache after attempting stats update
+            try {
+                List<ClusterNode> nodes = 
persistenceService.getAllItems(ClusterNode.class, 0, -1, null).getList();
+                cachedClusterNodes = nodes;
+            } catch (Exception e) {
+                LOGGER.warn("Failed to refresh cluster nodes cache during 
stats update", e);
+            }
         } catch (Exception e) {
             LOGGER.error("Error updating system statistics for node {}: {}", 
nodeId, e.getMessage(), e);
         }
@@ -474,13 +483,8 @@ public class ClusterServiceImpl implements ClusterService {
 
     @Override
     public List<ClusterNode> getClusterNodes() {
-        if (persistenceService == null) {
-            LOGGER.warn("Cannot get cluster nodes: PersistenceService not 
available");
-            return Collections.emptyList();
-        }
-
-        // Query all nodes from the persistence service
-        return persistenceService.getAllItems(ClusterNode.class, 0, -1, 
null).getList();
+        // Return cached cluster nodes, creating a defensive copy
+        return cachedClusterNodes.isEmpty() ? Collections.emptyList() : new 
ArrayList<>(cachedClusterNodes);
     }
 
     @Override

Reply via email to