-
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 4 commits
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; | ||
|
|
@@ -273,7 +274,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 +399,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 +415,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 +583,7 @@ public FetchTopologyResult(final boolean isCachedData, final List<HostSpec> host | |
| } | ||
|
|
||
| static class ClusterSuggestedResult { | ||
|
|
||
| public String clusterId; | ||
| public boolean isPrimaryClusterId; | ||
|
|
||
|
|
@@ -588,18 +595,10 @@ public ClusterSuggestedResult(final String clusterId, final boolean isPrimaryClu | |
|
|
||
| @Override | ||
| public HostRole getHostRole(Connection conn) throws SQLException { | ||
| if (this.topologyAwareDialect == null) { | ||
| Dialect dialect = this.hostListProviderService.getDialect(); | ||
| if (!(dialect instanceof TopologyAwareDatabaseCluster)) { | ||
| throw new SQLException( | ||
| Messages.get("AuroraHostListProvider.invalidDialectForGetHostRole", | ||
| 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( | ||
| getTopologyAwareDialect("AuroraHostListProvider.invalidDialectForGetHostRole") | ||
| .getIsReaderQuery())) { | ||
| if (rs.next()) { | ||
| boolean isReader = rs.getBoolean(1); | ||
| return isReader ? HostRole.READER : HostRole.WRITER; | ||
|
|
@@ -610,4 +609,46 @@ 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( | ||
| getTopologyAwareDialect("AuroraHostListProvider.invalidDialectForIdentifyConnection") | ||
| .getNodeIdQuery())) { | ||
| if (resultSet.next()) { | ||
| final String instanceName = resultSet.getString(1); | ||
|
|
||
| final List<HostSpec> topology = this.refresh(connection); | ||
|
|
||
| if (topology == null) { | ||
| final HostSpec host = new HostSpec(getHostEndpoint(instanceName)); | ||
|
||
| host.setHostId(instanceName); | ||
| return host; | ||
| } | ||
| return topology | ||
| .stream() | ||
| .filter(host -> Objects.equals(instanceName, host.getHostId())) | ||
| .findAny() | ||
| .orElse(null); | ||
| } | ||
| } catch (final SQLException e) { | ||
| throw new SQLException(Messages.get("AuroraHostListProvider.errorIdentifyConnection"), e); | ||
| } | ||
|
|
||
| throw new SQLException(Messages.get("AuroraHostListProvider.errorIdentifyConnection")); | ||
| } | ||
|
|
||
| private TopologyAwareDatabaseCluster getTopologyAwareDialect(String exceptionMessageIdentifier) throws SQLException { | ||
| if (this.topologyAwareDialect == null) { | ||
| Dialect dialect = this.hostListProviderService.getDialect(); | ||
| if (!(dialect instanceof TopologyAwareDatabaseCluster)) { | ||
| throw new SQLException( | ||
| Messages.get(exceptionMessageIdentifier, | ||
| new Object[] {dialect})); | ||
| } | ||
| this.topologyAwareDialect = (TopologyAwareDatabaseCluster) this.hostListProviderService.getDialect(); | ||
| } | ||
| return this.topologyAwareDialect; | ||
| } | ||
| } | ||
| 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); | ||
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.