Skip to content

Commit cf9f6d6

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

File tree

3 files changed

+53
-45
lines changed

3 files changed

+53
-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: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
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;
@@ -54,14 +58,23 @@ public class HostMonitorServiceImpl implements HostMonitorService {
5458
protected final MonitorService coreMonitorService;
5559
protected final TelemetryFactory telemetryFactory;
5660
protected final TelemetryCounter abortedConnectionsCounter;
61+
protected final int failureDetectionTimeMillis;
62+
protected final int failureDetectionIntervalMillis;
63+
protected final int failureDetectionCount;
64+
65+
protected HostMonitorKey monitorKey;
5766

58-
public HostMonitorServiceImpl(final @NonNull FullServicesContainer serviceContainer, Properties props) {
67+
public HostMonitorServiceImpl(final @NonNull FullServicesContainer serviceContainer, final Properties props) {
5968
this.serviceContainer = serviceContainer;
6069
this.coreMonitorService = serviceContainer.getMonitorService();
6170
this.pluginService = serviceContainer.getPluginService();
6271
this.telemetryFactory = serviceContainer.getTelemetryFactory();
6372
this.abortedConnectionsCounter = telemetryFactory.createCounter("efm2.connections.aborted");
6473

74+
this.failureDetectionTimeMillis = FAILURE_DETECTION_TIME.getInteger(props);
75+
this.failureDetectionIntervalMillis = FAILURE_DETECTION_INTERVAL.getInteger(props);
76+
this.failureDetectionCount = FAILURE_DETECTION_COUNT.getInteger(props);
77+
6578
this.coreMonitorService.registerMonitorTypeIfAbsent(
6679
HostMonitorImpl.class,
6780
TimeUnit.MILLISECONDS.toNanos(MONITOR_DISPOSAL_TIME_MS.getLong(props)),
@@ -78,21 +91,10 @@ public static void closeAllMonitors() {
7891
public HostMonitorConnectionContext startMonitoring(
7992
final Connection connectionToAbort,
8093
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-
94+
final Properties properties) throws SQLException {
95+
final HostMonitor monitor = this.getMonitor(hostSpec, properties);
9396
final HostMonitorConnectionContext context = new HostMonitorConnectionContext(connectionToAbort);
9497
monitor.startMonitoring(context);
95-
9698
return context;
9799
}
98100

@@ -131,28 +133,28 @@ public void releaseResources() {
131133
*
132134
* @param hostSpec Information such as hostname of the server.
133135
* @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.
137136
* @return A {@link HostMonitorImpl} object associated with a specific server.
138137
* @throws SQLException if there's errors getting or creating a monitor
139138
*/
140139
protected HostMonitor getMonitor(
141140
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());
141+
final Properties properties) throws SQLException {
142+
String hostUrl = hostSpec.getUrl();
143+
if (this.monitorKey == null || !hostUrl.equals(this.monitorKey.getUrl())) {
144+
// The URL being monitored has changed, so we need to recalculate the monitor key.
145+
this.monitorKey = new HostMonitorKey(
146+
hostUrl,
147+
String.format("%d:%d:%d:%s",
148+
this.failureDetectionTimeMillis,
149+
this.failureDetectionIntervalMillis,
150+
this.failureDetectionCount,
151+
hostUrl)
152+
);
153+
}
152154

153155
return this.coreMonitorService.runIfAbsent(
154156
HostMonitorImpl.class,
155-
monitorKey,
157+
this.monitorKey,
156158
this.serviceContainer,
157159
this.pluginService.getProperties(),
158160
(servicesContainer) -> new HostMonitorImpl(
@@ -164,4 +166,22 @@ protected HostMonitor getMonitor(
164166
failureDetectionCount,
165167
this.abortedConnectionsCounter));
166168
}
169+
170+
protected static class HostMonitorKey {
171+
private final String url;
172+
private final String keyValue;
173+
174+
public HostMonitorKey(String url, String keyValue) {
175+
this.url = url;
176+
this.keyValue = keyValue;
177+
}
178+
179+
public String getUrl() {
180+
return url;
181+
}
182+
183+
public String getKeyValue() {
184+
return keyValue;
185+
}
186+
}
167187
}

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)