Skip to content

Commit acf34ca

Browse files
committed
perf: cache efm2 monitor key for better performance
1 parent 6d11406 commit acf34ca

File tree

3 files changed

+30
-45
lines changed

3 files changed

+30
-45
lines changed

wrapper/src/main/java/software/amazon/jdbc/plugin/efm2/HostMonitorService.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public interface HostMonitorService {
3030
HostMonitorConnectionContext startMonitoring(
3131
Connection connectionToAbort,
3232
HostSpec hostSpec,
33-
Properties properties,
34-
int failureDetectionTimeMillis,
35-
int failureDetectionIntervalMillis,
36-
int failureDetectionCount) throws SQLException;
33+
Properties properties) throws SQLException;
3734

3835
/**
3936
* Stop monitoring for a connection represented by the given {@link HostMonitorConnectionContext}.

wrapper/src/main/java/software/amazon/jdbc/plugin/efm2/HostMonitorServiceImpl.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@
1616

1717
package software.amazon.jdbc.plugin.efm2;
1818

19+
import static software.amazon.jdbc.plugin.efm2.HostMonitoringConnectionPlugin.FAILURE_DETECTION_COUNT;
20+
import static software.amazon.jdbc.plugin.efm2.HostMonitoringConnectionPlugin.FAILURE_DETECTION_INTERVAL;
21+
import static software.amazon.jdbc.plugin.efm2.HostMonitoringConnectionPlugin.FAILURE_DETECTION_TIME;
22+
1923
import java.sql.Connection;
2024
import java.sql.SQLException;
2125
import java.util.Properties;
26+
import java.util.concurrent.ConcurrentHashMap;
27+
import java.util.concurrent.ConcurrentMap;
2228
import java.util.concurrent.Executor;
2329
import java.util.concurrent.TimeUnit;
2430
import java.util.logging.Logger;
@@ -49,11 +55,15 @@ public class HostMonitorServiceImpl implements HostMonitorService {
4955
protected static final Executor ABORT_EXECUTOR =
5056
ExecutorFactory.newSingleThreadExecutor("abort");
5157

58+
protected final ConcurrentMap<String, String> monitorKeysByUrl = new ConcurrentHashMap<>();
5259
protected final FullServicesContainer serviceContainer;
5360
protected final PluginService pluginService;
5461
protected final MonitorService coreMonitorService;
5562
protected final TelemetryFactory telemetryFactory;
5663
protected final TelemetryCounter abortedConnectionsCounter;
64+
protected final int failureDetectionTimeMillis;
65+
protected final int failureDetectionIntervalMillis;
66+
protected final int failureDetectionCount;
5767

5868
public HostMonitorServiceImpl(final @NonNull FullServicesContainer serviceContainer, Properties props) {
5969
this.serviceContainer = serviceContainer;
@@ -62,6 +72,10 @@ public HostMonitorServiceImpl(final @NonNull FullServicesContainer serviceContai
6272
this.telemetryFactory = serviceContainer.getTelemetryFactory();
6373
this.abortedConnectionsCounter = telemetryFactory.createCounter("efm2.connections.aborted");
6474

75+
this.failureDetectionTimeMillis = FAILURE_DETECTION_TIME.getInteger(props);
76+
this.failureDetectionIntervalMillis = FAILURE_DETECTION_INTERVAL.getInteger(props);
77+
this.failureDetectionCount = FAILURE_DETECTION_COUNT.getInteger(props);
78+
6579
this.coreMonitorService.registerMonitorTypeIfAbsent(
6680
HostMonitorImpl.class,
6781
TimeUnit.MILLISECONDS.toNanos(MONITOR_DISPOSAL_TIME_MS.getLong(props)),
@@ -78,21 +92,10 @@ public static void closeAllMonitors() {
7892
public HostMonitorConnectionContext startMonitoring(
7993
final Connection connectionToAbort,
8094
final HostSpec hostSpec,
81-
final Properties properties,
82-
final int failureDetectionTimeMillis,
83-
final int failureDetectionIntervalMillis,
84-
final int failureDetectionCount) throws SQLException {
85-
86-
final HostMonitor monitor = this.getMonitor(
87-
hostSpec,
88-
properties,
89-
failureDetectionTimeMillis,
90-
failureDetectionIntervalMillis,
91-
failureDetectionCount);
92-
95+
final Properties properties) throws SQLException {
96+
final HostMonitor monitor = this.getMonitor(hostSpec, properties);
9397
final HostMonitorConnectionContext context = new HostMonitorConnectionContext(connectionToAbort);
9498
monitor.startMonitoring(context);
95-
9699
return context;
97100
}
98101

@@ -131,24 +134,18 @@ public void releaseResources() {
131134
*
132135
* @param hostSpec Information such as hostname of the server.
133136
* @param properties The user configuration for the current connection.
134-
* @param failureDetectionTimeMillis A failure detection time in millis.
135-
* @param failureDetectionIntervalMillis A failure detection interval in millis.
136-
* @param failureDetectionCount A failure detection count.
137137
* @return A {@link HostMonitorImpl} object associated with a specific server.
138138
* @throws SQLException if there's errors getting or creating a monitor
139139
*/
140-
protected HostMonitor getMonitor(
141-
final HostSpec hostSpec,
142-
final Properties properties,
143-
final int failureDetectionTimeMillis,
144-
final int failureDetectionIntervalMillis,
145-
final int failureDetectionCount) throws SQLException {
146-
147-
final String monitorKey = String.format("%d:%d:%d:%s",
148-
failureDetectionTimeMillis,
149-
failureDetectionIntervalMillis,
150-
failureDetectionCount,
151-
hostSpec.getUrl());
140+
protected HostMonitor getMonitor(final HostSpec hostSpec, final Properties properties) throws SQLException {
141+
final String monitorKey = monitorKeysByUrl.computeIfAbsent(
142+
hostSpec.getUrl(),
143+
(url) -> String.format(
144+
"%d:%d:%d:%s",
145+
failureDetectionTimeMillis,
146+
failureDetectionIntervalMillis,
147+
failureDetectionCount,
148+
url));
152149

153150
return this.coreMonitorService.runIfAbsent(
154151
HostMonitorImpl.class,

wrapper/src/main/java/software/amazon/jdbc/plugin/efm2/HostMonitoringConnectionPlugin.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,6 @@ public <T, E extends Exception> T execute(
148148
return jdbcMethodFunc.call();
149149
}
150150

151-
final int failureDetectionTimeMillis = FAILURE_DETECTION_TIME.getInteger(this.properties);
152-
final int failureDetectionIntervalMillis =
153-
FAILURE_DETECTION_INTERVAL.getInteger(this.properties);
154-
final int failureDetectionCount = FAILURE_DETECTION_COUNT.getInteger(this.properties);
155-
156151
initMonitorService();
157152

158153
T result;
@@ -167,14 +162,10 @@ public <T, E extends Exception> T execute(
167162
final HostSpec monitoringHostSpec = this.getMonitoringHostSpec();
168163

169164
try {
170-
monitorContext =
171-
this.monitorService.startMonitoring(
172-
this.pluginService.getCurrentConnection(), // abort this connection if needed
173-
monitoringHostSpec,
174-
this.properties,
175-
failureDetectionTimeMillis,
176-
failureDetectionIntervalMillis,
177-
failureDetectionCount);
165+
monitorContext = this.monitorService.startMonitoring(
166+
this.pluginService.getCurrentConnection(), // abort this connection if needed
167+
monitoringHostSpec,
168+
this.properties);
178169
} catch (SQLException e) {
179170
throw WrapperUtils.wrapExceptionIfNeeded(exceptionClass, e);
180171
}

0 commit comments

Comments
 (0)