1616
1717package 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+
1923import java .sql .Connection ;
2024import java .sql .SQLException ;
2125import 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}
0 commit comments