From ffde1ffb77d6f8e2f9c913eebe01c1ea10a98573 Mon Sep 17 00:00:00 2001 From: Andreas Schwarte Date: Mon, 12 Aug 2024 10:58:48 +0200 Subject: [PATCH] GH-5105: make MonitoringImpl in FedX thread safe The monitoring service in FedX is a useful tool for evaluations / benchmarks to inspect the number of requests sent to the endpoints. This change makes the implementation thread safe. --- .../federated/monitoring/MonitoringImpl.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tools/federation/src/main/java/org/eclipse/rdf4j/federated/monitoring/MonitoringImpl.java b/tools/federation/src/main/java/org/eclipse/rdf4j/federated/monitoring/MonitoringImpl.java index c53fae51671..e0e38b97823 100644 --- a/tools/federation/src/main/java/org/eclipse/rdf4j/federated/monitoring/MonitoringImpl.java +++ b/tools/federation/src/main/java/org/eclipse/rdf4j/federated/monitoring/MonitoringImpl.java @@ -14,6 +14,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.rdf4j.federated.FedXConfig; import org.eclipse.rdf4j.federated.endpoint.Endpoint; @@ -50,11 +51,7 @@ public class MonitoringImpl implements MonitoringService { @Override public void monitorRemoteRequest(Endpoint e) { - MonitoringInformation m = requestMap.get(e); - if (m == null) { - m = new MonitoringInformation(e); - requestMap.put(e, m); - } + MonitoringInformation m = requestMap.computeIfAbsent(e, (endpoint) -> new MonitoringInformation(endpoint)); m.increaseRequests(); } @@ -75,20 +72,19 @@ public void resetMonitoringInformation() { public static class MonitoringInformation { private final Endpoint e; - private int numberOfRequests = 0; + private AtomicInteger numberOfRequests = new AtomicInteger(0); public MonitoringInformation(Endpoint e) { this.e = e; } private void increaseRequests() { - // TODO make thread safe - numberOfRequests++; + numberOfRequests.incrementAndGet(); } @Override public String toString() { - return e.getName() + " => " + numberOfRequests; + return e.getName() + " => " + numberOfRequests.get(); } public Endpoint getE() { @@ -96,7 +92,7 @@ public Endpoint getE() { } public int getNumberOfRequests() { - return numberOfRequests; + return numberOfRequests.get(); } }