@@ -83,9 +83,9 @@ public class HostMonitoringConnectionPlugin extends AbstractConnectionPlugin
8383 protected @ NonNull Properties properties ;
8484 private final @ NonNull Supplier <MonitorService > monitorServiceSupplier ;
8585 private final @ NonNull PluginService pluginService ;
86- private final @ NonNull Set <String > nodeKeys = ConcurrentHashMap .newKeySet (); // Shared with monitor thread
8786 private MonitorService monitorService ;
88- private RdsUtils rdsHelper ;
87+ private final RdsUtils rdsHelper ;
88+ private HostSpec monitoringHostSpec ;
8989
9090 /**
9191 * Initialize the node monitoring plugin.
@@ -155,34 +155,17 @@ public <T, E extends Exception> T execute(
155155 T result ;
156156 MonitorConnectionContext monitorContext = null ;
157157
158- HostSpec monitoringHostSpec = this .pluginService .getCurrentHostSpec ();
159- final RdsUrlType rdsUrlType = this .rdsHelper .identifyRdsType (monitoringHostSpec .getUrl ());
160-
161- try {
162- if (rdsUrlType .isRdsCluster ()) {
163- monitoringHostSpec = this .pluginService .identifyConnection (this .pluginService .getCurrentConnection ());
164- monitoringHostSpec .resetAliases ();
165- this .pluginService .fillAliases (this .pluginService .getCurrentConnection (), monitoringHostSpec );
166- }
167- } catch (SQLException e ) {
168- // Log and ignore
169- LOGGER .finest (Messages .get ("HostMonitoringConnectionPlugin.errorIdentifyingConnection" , new Object [] {e }));
170- }
171-
172158 try {
173159 LOGGER .finest (
174160 () -> Messages .get (
175161 "HostMonitoringConnectionPlugin.activatedMonitoring" ,
176162 new Object [] {methodName }));
177163
178- this .nodeKeys .clear ();
179- this .nodeKeys .addAll (this .pluginService .getCurrentHostSpec ().asAliases ());
180-
181164 monitorContext =
182165 this .monitorService .startMonitoring (
183166 this .pluginService .getCurrentConnection (), // abort this connection if needed
184- this .nodeKeys ,
185- monitoringHostSpec ,
167+ this .getMonitoringHostSpec (). asAliases () ,
168+ this . getMonitoringHostSpec () ,
186169 this .properties ,
187170 failureDetectionTimeMillis ,
188171 failureDetectionIntervalMillis ,
@@ -196,7 +179,7 @@ public <T, E extends Exception> T execute(
196179 this .monitorService .stopMonitoring (monitorContext );
197180
198181 if (monitorContext .isNodeUnhealthy ()) {
199- this .pluginService .setAvailability (this .nodeKeys , HostAvailability .NOT_AVAILABLE );
182+ this .pluginService .setAvailability (this .getMonitoringHostSpec (). asAliases () , HostAvailability .NOT_AVAILABLE );
200183
201184 final boolean isConnectionClosed ;
202185 try {
@@ -262,16 +245,16 @@ public void releaseResources() {
262245
263246 @ Override
264247 public OldConnectionSuggestedAction notifyConnectionChanged (final EnumSet <NodeChangeOptions > changes ) {
265-
266248 if (changes .contains (NodeChangeOptions .WENT_DOWN )
267249 || changes .contains (NodeChangeOptions .NODE_DELETED )) {
268- if (!this .nodeKeys .isEmpty ()) {
269- this .monitorService .stopMonitoringForAllConnections (this .nodeKeys );
250+ if (!this .getMonitoringHostSpec (). asAliases () .isEmpty ()) {
251+ this .monitorService .stopMonitoringForAllConnections (this .getMonitoringHostSpec (). asAliases () );
270252 }
271- this .nodeKeys .clear ();
272- this .nodeKeys .addAll (this .pluginService .getCurrentHostSpec ().getAliases ());
273253 }
274254
255+ // Reset monitoring HostSpec since the associated connection has changed.
256+ this .monitoringHostSpec = null ;
257+
275258 return OldConnectionSuggestedAction .NO_OPINION ;
276259 }
277260
@@ -291,8 +274,11 @@ private Connection connectInternal(String driverProtocol, HostSpec hostSpec,
291274 final Connection conn = connectFunc .call ();
292275
293276 if (conn != null ) {
294- hostSpec .resetAliases ();
295- this .pluginService .fillAliases (conn , hostSpec );
277+ final RdsUrlType type = this .rdsHelper .identifyRdsType (hostSpec .getHost ());
278+ if (type .isRdsCluster ()) {
279+ hostSpec .resetAliases ();
280+ this .pluginService .fillAliases (conn , hostSpec );
281+ }
296282 }
297283
298284 return conn ;
@@ -308,4 +294,24 @@ public Connection forceConnect(
308294 throws SQLException {
309295 return connectInternal (driverProtocol , hostSpec , forceConnectFunc );
310296 }
297+
298+ public HostSpec getMonitoringHostSpec () {
299+ if (this .monitoringHostSpec == null ) {
300+ this .monitoringHostSpec = this .pluginService .getCurrentHostSpec ();
301+ final RdsUrlType rdsUrlType = this .rdsHelper .identifyRdsType (monitoringHostSpec .getUrl ());
302+
303+ try {
304+ if (rdsUrlType .isRdsCluster ()) {
305+ LOGGER .finest ("Monitoring HostSpec is associated with a cluster endpoint, "
306+ + "plugin needs to identify the cluster connection." );
307+ this .monitoringHostSpec = this .pluginService .identifyConnection (this .pluginService .getCurrentConnection ());
308+ this .pluginService .fillAliases (this .pluginService .getCurrentConnection (), monitoringHostSpec );
309+ }
310+ } catch (SQLException e ) {
311+ // Log and ignore
312+ LOGGER .finest (Messages .get ("HostMonitoringConnectionPlugin.errorIdentifyingConnection" , new Object [] {e }));
313+ }
314+ }
315+ return this .monitoringHostSpec ;
316+ }
311317}
0 commit comments