-
Notifications
You must be signed in to change notification settings - Fork 72
refactor: host alias logic in wrapper #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
f9d4cc0
68d9bdb
829ebcf
2997d86
10f4a56
9831f57
d4ceb49
c9a45a7
5dde1bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map.Entry; | ||
| import java.util.Objects; | ||
| import java.util.Properties; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
|
|
@@ -42,6 +43,7 @@ | |
| import software.amazon.jdbc.HostSpec; | ||
| import software.amazon.jdbc.dialect.Dialect; | ||
| import software.amazon.jdbc.dialect.TopologyAwareDatabaseCluster; | ||
| import software.amazon.jdbc.plugin.readwritesplitting.ReadWriteSplittingSQLException; | ||
| import software.amazon.jdbc.util.CacheMap; | ||
| import software.amazon.jdbc.util.ConnectionUrlParser; | ||
| import software.amazon.jdbc.util.Messages; | ||
|
|
@@ -273,7 +275,7 @@ private ClusterSuggestedResult getSuggestedClusterId(final String url) { | |
| for (final HostSpec host : hosts) { | ||
| if (host.getUrl().equals(url)) { | ||
| LOGGER.finest(() -> Messages.get("AuroraHostListProvider.suggestedClusterId", | ||
| new Object[]{key, url})); | ||
| new Object[] {key, url})); | ||
| return new ClusterSuggestedResult(key, isPrimaryCluster); | ||
| } | ||
| } | ||
|
|
@@ -398,8 +400,12 @@ private HostSpec createHost(final ResultSet resultSet) throws SQLException { | |
| // Calculate weight based on node lag in time and CPU utilization. | ||
| final long weight = Math.round(nodeLag) * 100L + Math.round(cpuUtilization); | ||
|
|
||
| hostName = hostName == null ? "?" : hostName; | ||
| final String endpoint = getHostEndpoint(hostName); | ||
| return createHost(hostName, isWriter, weight); | ||
| } | ||
|
|
||
| private HostSpec createHost(String host, final boolean isWriter, final long weight) { | ||
| host = host == null ? "?" : host; | ||
| final String endpoint = getHostEndpoint(host); | ||
| final int port = this.clusterInstanceTemplate.isPortSpecified() | ||
| ? this.clusterInstanceTemplate.getPort() | ||
| : this.initialHostSpec.getPort(); | ||
|
|
@@ -410,7 +416,8 @@ private HostSpec createHost(final ResultSet resultSet) throws SQLException { | |
| isWriter ? HostRole.WRITER : HostRole.READER, | ||
| HostAvailability.AVAILABLE, | ||
| weight); | ||
| hostSpec.addAlias(hostName); | ||
| hostSpec.addAlias(host); | ||
| hostSpec.setHostId(host); | ||
| return hostSpec; | ||
| } | ||
|
|
||
|
|
@@ -577,6 +584,7 @@ public FetchTopologyResult(final boolean isCachedData, final List<HostSpec> host | |
| } | ||
|
|
||
| static class ClusterSuggestedResult { | ||
|
|
||
| public String clusterId; | ||
| public boolean isPrimaryClusterId; | ||
|
|
||
|
|
@@ -593,13 +601,13 @@ public HostRole getHostRole(Connection conn) throws SQLException { | |
| if (!(dialect instanceof TopologyAwareDatabaseCluster)) { | ||
| throw new SQLException( | ||
| Messages.get("AuroraHostListProvider.invalidDialectForGetHostRole", | ||
| new Object[]{dialect})); | ||
| new Object[] {dialect})); | ||
| } | ||
| this.topologyAwareDialect = (TopologyAwareDatabaseCluster) this.hostListProviderService.getDialect(); | ||
| } | ||
|
|
||
| try (final Statement stmt = conn.createStatement(); | ||
| final ResultSet rs = stmt.executeQuery(this.topologyAwareDialect.getIsReaderQuery())) { | ||
| final ResultSet rs = stmt.executeQuery(this.topologyAwareDialect.getIsReaderQuery())) { | ||
| if (rs.next()) { | ||
| boolean isReader = rs.getBoolean(1); | ||
| return isReader ? HostRole.READER : HostRole.WRITER; | ||
|
|
@@ -610,4 +618,33 @@ public HostRole getHostRole(Connection conn) throws SQLException { | |
|
|
||
| throw new SQLException(Messages.get("AuroraHostListProvider.errorGettingHostRole")); | ||
| } | ||
|
|
||
| @Override | ||
| public HostSpec identifyConnection(Connection connection) throws SQLException { | ||
| try (final Statement stmt = connection.createStatement(); | ||
karenc-bq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final ResultSet resultSet = stmt.executeQuery(topologyAwareDialect.getNodeIdQuery())) { | ||
| if (resultSet.next()) { | ||
| final String instanceName = resultSet.getString(1); | ||
| if (this.getCachedTopology() == null) { | ||
| final HostSpec host = new HostSpec(getHostEndpoint(instanceName)); | ||
|
||
| host.setHostId(instanceName); | ||
| return host; | ||
| } | ||
|
|
||
| return this.getCachedTopology() | ||
| .stream() | ||
| .filter(host -> Objects.equals(instanceName, host.getHostId())) | ||
| .findAny() | ||
| .orElseGet(() -> { | ||
| final HostSpec host = new HostSpec(getHostEndpoint(instanceName)); | ||
karenc-bq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| host.setHostId(instanceName); | ||
| return host; | ||
| }); | ||
| } | ||
| } catch (final SQLException e) { | ||
| throw new SQLException(Messages.get("AuroraHostListProvider.errorIdentifyingConnection"), e); | ||
| } | ||
|
|
||
| throw new SQLException(Messages.get("AuroraHostListProvider.errorIdentifyingConnection")); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,4 +109,9 @@ public List<HostSpec> forceRefresh(final Connection connection) throws SQLExcept | |
| public HostRole getHostRole(Connection connection) { | ||
| throw new UnsupportedOperationException("ConnectionStringHostListProvider does not support getHostRole"); | ||
| } | ||
|
|
||
| @Override | ||
| public HostSpec identifyConnection(Connection connection) throws SQLException { | ||
| return this.hostList.get(0); | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -241,32 +241,6 @@ public void releaseResources() { | |
| this.monitorService = null; | ||
| } | ||
|
|
||
| /** | ||
| * Generate a set of node keys representing the node to monitor. | ||
| * | ||
| * @param driverProtocol Driver protocol for provided connection | ||
| * @param connection the connection to a specific node. | ||
| * @param hostSpec host details to add node keys to | ||
| */ | ||
| private void generateHostAliases( | ||
| final @NonNull String driverProtocol, | ||
| final @NonNull Connection connection, | ||
| final @NonNull HostSpec hostSpec) { | ||
|
|
||
| hostSpec.addAlias(hostSpec.asAlias()); | ||
|
|
||
| try (final Statement stmt = connection.createStatement()) { | ||
| try (final ResultSet rs = stmt.executeQuery(this.pluginService.getDialect().getHostAliasQuery())) { | ||
| while (rs.next()) { | ||
| hostSpec.addAlias(rs.getString(1)); | ||
| } | ||
| } | ||
| } catch (final SQLException sqlException) { | ||
| // log and ignore | ||
| LOGGER.finest(() -> Messages.get("HostMonitoringConnectionPlugin.failedToRetrieveHostPort")); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public OldConnectionSuggestedAction notifyConnectionChanged(final EnumSet<NodeChangeOptions> changes) { | ||
|
|
||
|
|
@@ -298,7 +272,8 @@ private Connection connectInternal(String driverProtocol, HostSpec hostSpec, | |
| final Connection conn = connectFunc.call(); | ||
|
|
||
| if (conn != null) { | ||
| generateHostAliases(driverProtocol, conn, hostSpec); | ||
| hostSpec.resetAliases(); | ||
|
||
| this.pluginService.fillAliases(conn, hostSpec); | ||
| } | ||
|
|
||
| return conn; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| package software.amazon.jdbc.plugin.efm; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.util.Collections; | ||
| import java.util.Properties; | ||
| import java.util.Set; | ||
|
|
@@ -42,6 +43,7 @@ public class MonitorServiceImpl implements MonitorService { | |
| "60000", | ||
| "Interval in milliseconds for a monitor to be considered inactive and to be disposed."); | ||
|
|
||
| private final PluginService pluginService; | ||
| private MonitorThreadContainer threadContainer; | ||
|
|
||
| final MonitorInitializer monitorInitializer; | ||
|
|
@@ -50,6 +52,7 @@ public class MonitorServiceImpl implements MonitorService { | |
|
|
||
| public MonitorServiceImpl(final @NonNull PluginService pluginService) { | ||
| this( | ||
| pluginService, | ||
| (hostSpec, properties, monitorService) -> | ||
| new MonitorImpl( | ||
| pluginService, | ||
|
|
@@ -67,9 +70,11 @@ public MonitorServiceImpl(final @NonNull PluginService pluginService) { | |
| } | ||
|
|
||
| MonitorServiceImpl( | ||
| final PluginService pluginService, | ||
| final MonitorInitializer monitorInitializer, | ||
| final ExecutorServiceInitializer executorServiceInitializer) { | ||
|
|
||
| this.pluginService = pluginService; | ||
| this.monitorInitializer = monitorInitializer; | ||
| this.threadContainer = MonitorThreadContainer.getInstance(executorServiceInitializer); | ||
| } | ||
|
|
@@ -89,7 +94,12 @@ public MonitorConnectionContext startMonitoring( | |
| () -> Messages.get( | ||
| "MonitorServiceImpl.emptyAliasSet", | ||
| new Object[] {hostSpec})); | ||
| hostSpec.addAlias(hostSpec.asAlias()); | ||
| try { | ||
|
||
| this.pluginService.fillAliases(connectionToAbort, hostSpec); | ||
sergiyvamz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (SQLException e) { | ||
| // Log and ignore the error. | ||
| LOGGER.finest(Messages.get("MonitorServiceImpl.errorPopulatingAliases", new Object[] {e})); | ||
| } | ||
| } | ||
|
|
||
| final Monitor monitor; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ AuroraHostListProvider.invalidQuery=Error obtaining host list. Provided database | |
| AuroraHostListProvider.invalidDialect=Expecting a dialect that supports a cluster topology. | ||
| AuroraHostListProvider.invalidDialectForGetHostRole=An Aurora dialect is required to analyze a host's role. The detected dialect was ''{0}'' | ||
| AuroraHostListProvider.errorGettingHostRole=An error occurred while obtaining the connected host's role. This could occur if the connection is broken or if you are not connected to an Aurora database. | ||
| AuroraHostListProvider.errorIdentifyingConnection=An error occurred while obtaining the connection's host ID. | ||
|
|
||
| # AWS Credentials Manager | ||
| AwsCredentialsManager.nullProvider=The configured AwsCredentialsProvider was null. If you have configured the AwsCredentialsManager to use a custom AwsCredentialsProviderHandler, please ensure the handler does not return null. | ||
|
|
@@ -139,7 +140,6 @@ Failover.noOperationsAfterConnectionClosed=No operations allowed after connectio | |
| HostMonitoringConnectionPlugin.activatedMonitoring=Executing method ''{0}'', monitoring is activated. | ||
| HostMonitoringConnectionPlugin.monitoringDeactivated=Monitoring deactivated for method ''{0}''. | ||
| HostMonitoringConnectionPlugin.unavailableNode=Node ''{0}'' is unavailable. | ||
| HostMonitoringConnectionPlugin.failedToRetrieveHostPort=Could not retrieve Host:Port for connection. | ||
| HostMonitoringConnectionPlugin.unsupportedDriverProtocol=Driver protocol ''{0}'' is not supported. | ||
|
|
||
| # IAM Auth Connection Plugin | ||
|
|
@@ -164,15 +164,18 @@ MonitorThreadContainer.emptyNodeKeys=Provided node keys are empty. | |
| MonitorImpl.contextNullWarning=Parameter 'context' should not be null. | ||
|
|
||
| # Monitor Service Impl | ||
| MonitorServiceImpl.nullMonitorParam=Parameter monitor' should not be null. | ||
| MonitorServiceImpl.emptyAliasSet=Empty alias set passed for {0}. Set should not be empty. | ||
| MonitorServiceImpl.nullMonitorParam=Parameter 'monitor' should not be null. | ||
| MonitorServiceImpl.emptyAliasSet=Empty alias set passed for ''{0}''. Set should not be empty. | ||
| MonitorServiceImpl.errorPopulatingAliases=Error occurred while populating aliases: ''{0}''. | ||
|
|
||
| # Plugin Service Impl | ||
| PluginServiceImpl.hostListEmpty=Current host list is empty. | ||
| PluginServiceImpl.releaseResources=Releasing resources. | ||
| PluginServiceImpl.hostListException=Exception while getting a host list. | ||
| PluginServiceImpl.hostAliasNotFound=Can''t find any host by the following aliases: ''{0}''. | ||
| PluginServiceImpl.hostsChangelistEmpty=There are no changes in the hosts' availability. | ||
| PluginServiceImpl.failedToRetrieveHostPort=Could not retrieve Host:Port for connection. | ||
| PluginServiceImpl.nonEmptyAliases=fillAliases called when HostSpec already contains the following aliases: ''{0}''. Please reset HostSpec aliases before calling this method. | ||
|
||
|
|
||
| # Property Utils | ||
| PropertyUtils.setMethodDoesNotExistOnTarget=Set method for property ''{0}'' does not exist on target ''{1}''. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.