From bc104c2cff1c258dccd188044d8839cc6acce1e2 Mon Sep 17 00:00:00 2001 From: sergiyvamz <75754709+sergiyvamz@users.noreply.github.com> Date: Tue, 1 Oct 2024 15:02:08 -0700 Subject: [PATCH] Verify plugin presence based on actual plugin list (#1141) --- .../main/java/software/amazon/jdbc/PluginService.java | 2 ++ .../java/software/amazon/jdbc/PluginServiceImpl.java | 9 +++++++++ .../software/amazon/jdbc/dialect/AuroraMysqlDialect.java | 6 +++--- .../software/amazon/jdbc/dialect/AuroraPgDialect.java | 7 +++---- .../jdbc/dialect/RdsMultiAzDbClusterMysqlDialect.java | 6 +++--- .../jdbc/dialect/RdsMultiAzDbClusterPgDialect.java | 7 ++++--- .../java/software/amazon/jdbc/DialectDetectionTests.java | 1 + .../amazon/jdbc/plugin/efm/ConcurrencyTests.java | 5 +++++ 8 files changed, 30 insertions(+), 13 deletions(-) diff --git a/wrapper/src/main/java/software/amazon/jdbc/PluginService.java b/wrapper/src/main/java/software/amazon/jdbc/PluginService.java index 8191fb5c1..05164ca12 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/PluginService.java +++ b/wrapper/src/main/java/software/amazon/jdbc/PluginService.java @@ -217,4 +217,6 @@ HostSpec getHostSpecByStrategy(List hosts, HostRole role, String strat String getTargetName(); @NonNull SessionStateService getSessionStateService(); + + T getPlugin(final Class pluginClazz); } diff --git a/wrapper/src/main/java/software/amazon/jdbc/PluginServiceImpl.java b/wrapper/src/main/java/software/amazon/jdbc/PluginServiceImpl.java index 8186cb3f4..10b4d0486 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/PluginServiceImpl.java +++ b/wrapper/src/main/java/software/amazon/jdbc/PluginServiceImpl.java @@ -689,4 +689,13 @@ public String getTargetName() { public @NonNull SessionStateService getSessionStateService() { return this.sessionStateService; } + + public T getPlugin(final Class pluginClazz) { + for (ConnectionPlugin p : this.pluginManager.plugins) { + if (pluginClazz.isAssignableFrom(p.getClass())) { + return pluginClazz.cast(p); + } + } + return null; + } } diff --git a/wrapper/src/main/java/software/amazon/jdbc/dialect/AuroraMysqlDialect.java b/wrapper/src/main/java/software/amazon/jdbc/dialect/AuroraMysqlDialect.java index 15775fac6..45a8ab8eb 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/dialect/AuroraMysqlDialect.java +++ b/wrapper/src/main/java/software/amazon/jdbc/dialect/AuroraMysqlDialect.java @@ -22,9 +22,9 @@ import java.sql.Statement; import java.util.Collections; import java.util.List; -import software.amazon.jdbc.ConnectionPluginChainBuilder; import software.amazon.jdbc.hostlistprovider.AuroraHostListProvider; import software.amazon.jdbc.hostlistprovider.monitoring.MonitoringRdsHostListProvider; +import software.amazon.jdbc.plugin.failover2.FailoverConnectionPlugin; public class AuroraMysqlDialect extends MysqlDialect { @@ -83,9 +83,9 @@ public boolean isDialect(final Connection connection) { public HostListProviderSupplier getHostListProvider() { return (properties, initialUrl, hostListProviderService, pluginService) -> { - final List plugins = ConnectionPluginChainBuilder.getPluginCodes(properties); + final FailoverConnectionPlugin failover2Plugin = pluginService.getPlugin(FailoverConnectionPlugin.class); - if (plugins.contains("failover2")) { + if (failover2Plugin != null) { return new MonitoringRdsHostListProvider( properties, initialUrl, diff --git a/wrapper/src/main/java/software/amazon/jdbc/dialect/AuroraPgDialect.java b/wrapper/src/main/java/software/amazon/jdbc/dialect/AuroraPgDialect.java index 8ae87ea88..b34d4dc61 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/dialect/AuroraPgDialect.java +++ b/wrapper/src/main/java/software/amazon/jdbc/dialect/AuroraPgDialect.java @@ -20,11 +20,10 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.List; import java.util.logging.Logger; -import software.amazon.jdbc.ConnectionPluginChainBuilder; import software.amazon.jdbc.hostlistprovider.AuroraHostListProvider; import software.amazon.jdbc.hostlistprovider.monitoring.MonitoringRdsHostListProvider; +import software.amazon.jdbc.plugin.failover2.FailoverConnectionPlugin; /** * Suitable for the following AWS PG configurations. @@ -128,9 +127,9 @@ public boolean isDialect(final Connection connection) { public HostListProviderSupplier getHostListProvider() { return (properties, initialUrl, hostListProviderService, pluginService) -> { - final List plugins = ConnectionPluginChainBuilder.getPluginCodes(properties); + final FailoverConnectionPlugin failover2Plugin = pluginService.getPlugin(FailoverConnectionPlugin.class); - if (plugins.contains("failover2")) { + if (failover2Plugin != null) { return new MonitoringRdsHostListProvider( properties, initialUrl, diff --git a/wrapper/src/main/java/software/amazon/jdbc/dialect/RdsMultiAzDbClusterMysqlDialect.java b/wrapper/src/main/java/software/amazon/jdbc/dialect/RdsMultiAzDbClusterMysqlDialect.java index c0c4806f1..91109c758 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/dialect/RdsMultiAzDbClusterMysqlDialect.java +++ b/wrapper/src/main/java/software/amazon/jdbc/dialect/RdsMultiAzDbClusterMysqlDialect.java @@ -24,11 +24,11 @@ import java.util.List; import java.util.Properties; import org.checkerframework.checker.nullness.qual.NonNull; -import software.amazon.jdbc.ConnectionPluginChainBuilder; import software.amazon.jdbc.HostSpec; import software.amazon.jdbc.hostlistprovider.RdsMultiAzDbClusterListProvider; import software.amazon.jdbc.hostlistprovider.monitoring.MonitoringRdsMultiAzHostListProvider; import software.amazon.jdbc.plugin.failover.FailoverRestriction; +import software.amazon.jdbc.plugin.failover2.FailoverConnectionPlugin; import software.amazon.jdbc.util.DriverInfo; public class RdsMultiAzDbClusterMysqlDialect extends MysqlDialect { @@ -98,9 +98,9 @@ public boolean isDialect(final Connection connection) { public HostListProviderSupplier getHostListProvider() { return (properties, initialUrl, hostListProviderService, pluginService) -> { - final List plugins = ConnectionPluginChainBuilder.getPluginCodes(properties); + final FailoverConnectionPlugin failover2Plugin = pluginService.getPlugin(FailoverConnectionPlugin.class); - if (plugins.contains("failover2")) { + if (failover2Plugin != null) { return new MonitoringRdsMultiAzHostListProvider( properties, initialUrl, diff --git a/wrapper/src/main/java/software/amazon/jdbc/dialect/RdsMultiAzDbClusterPgDialect.java b/wrapper/src/main/java/software/amazon/jdbc/dialect/RdsMultiAzDbClusterPgDialect.java index 7958a7c4e..df4af9c10 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/dialect/RdsMultiAzDbClusterPgDialect.java +++ b/wrapper/src/main/java/software/amazon/jdbc/dialect/RdsMultiAzDbClusterPgDialect.java @@ -22,11 +22,11 @@ import java.sql.Statement; import java.util.List; import java.util.logging.Logger; -import software.amazon.jdbc.ConnectionPluginChainBuilder; import software.amazon.jdbc.exceptions.ExceptionHandler; import software.amazon.jdbc.exceptions.MultiAzDbClusterPgExceptionHandler; import software.amazon.jdbc.hostlistprovider.RdsMultiAzDbClusterListProvider; import software.amazon.jdbc.hostlistprovider.monitoring.MonitoringRdsMultiAzHostListProvider; +import software.amazon.jdbc.plugin.failover2.FailoverConnectionPlugin; import software.amazon.jdbc.util.DriverInfo; public class RdsMultiAzDbClusterPgDialect extends PgDialect { @@ -113,9 +113,10 @@ public boolean isDialect(final Connection connection) { @Override public HostListProviderSupplier getHostListProvider() { return (properties, initialUrl, hostListProviderService, pluginService) -> { - final List plugins = ConnectionPluginChainBuilder.getPluginCodes(properties); - if (plugins.contains("failover2")) { + final FailoverConnectionPlugin failover2Plugin = pluginService.getPlugin(FailoverConnectionPlugin.class); + + if (failover2Plugin != null) { return new MonitoringRdsMultiAzHostListProvider( properties, initialUrl, diff --git a/wrapper/src/test/java/software/amazon/jdbc/DialectDetectionTests.java b/wrapper/src/test/java/software/amazon/jdbc/DialectDetectionTests.java index b04f85e27..3dd8b5a9b 100644 --- a/wrapper/src/test/java/software/amazon/jdbc/DialectDetectionTests.java +++ b/wrapper/src/test/java/software/amazon/jdbc/DialectDetectionTests.java @@ -78,6 +78,7 @@ void setUp() throws SQLException { when(this.mockConnection.createStatement()).thenReturn(this.mockStatement); when(this.mockHost.getUrl()).thenReturn("url"); when(this.failResultSet.next()).thenReturn(false); + pluginManager.plugins = new ArrayList<>(); } @AfterEach diff --git a/wrapper/src/test/java/software/amazon/jdbc/plugin/efm/ConcurrencyTests.java b/wrapper/src/test/java/software/amazon/jdbc/plugin/efm/ConcurrencyTests.java index 9ab2c46e3..ee8bc0806 100644 --- a/wrapper/src/test/java/software/amazon/jdbc/plugin/efm/ConcurrencyTests.java +++ b/wrapper/src/test/java/software/amazon/jdbc/plugin/efm/ConcurrencyTests.java @@ -571,6 +571,11 @@ public String getTargetName() { return new TestSessionStateService(); } + @Override + public T getPlugin(Class pluginClazz) { + return null; + } + @Override public boolean isNetworkException(Throwable throwable) { return false;