Skip to content

Commit ffde1ff

Browse files
committed
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.
1 parent 1044c5c commit ffde1ff

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

tools/federation/src/main/java/org/eclipse/rdf4j/federated/monitoring/MonitoringImpl.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.List;
1515
import java.util.Map;
1616
import java.util.concurrent.ConcurrentHashMap;
17+
import java.util.concurrent.atomic.AtomicInteger;
1718

1819
import org.eclipse.rdf4j.federated.FedXConfig;
1920
import org.eclipse.rdf4j.federated.endpoint.Endpoint;
@@ -50,11 +51,7 @@ public class MonitoringImpl implements MonitoringService {
5051

5152
@Override
5253
public void monitorRemoteRequest(Endpoint e) {
53-
MonitoringInformation m = requestMap.get(e);
54-
if (m == null) {
55-
m = new MonitoringInformation(e);
56-
requestMap.put(e, m);
57-
}
54+
MonitoringInformation m = requestMap.computeIfAbsent(e, (endpoint) -> new MonitoringInformation(endpoint));
5855
m.increaseRequests();
5956
}
6057

@@ -75,28 +72,27 @@ public void resetMonitoringInformation() {
7572

7673
public static class MonitoringInformation {
7774
private final Endpoint e;
78-
private int numberOfRequests = 0;
75+
private AtomicInteger numberOfRequests = new AtomicInteger(0);
7976

8077
public MonitoringInformation(Endpoint e) {
8178
this.e = e;
8279
}
8380

8481
private void increaseRequests() {
85-
// TODO make thread safe
86-
numberOfRequests++;
82+
numberOfRequests.incrementAndGet();
8783
}
8884

8985
@Override
9086
public String toString() {
91-
return e.getName() + " => " + numberOfRequests;
87+
return e.getName() + " => " + numberOfRequests.get();
9288
}
9389

9490
public Endpoint getE() {
9591
return e;
9692
}
9793

9894
public int getNumberOfRequests() {
99-
return numberOfRequests;
95+
return numberOfRequests.get();
10096
}
10197
}
10298

0 commit comments

Comments
 (0)