Skip to content

Commit 2b93e94

Browse files
authored
refactor: failover handler tasks use separate PluginService instances (#1514)
1 parent ebcc08b commit 2b93e94

31 files changed

+769
-583
lines changed

wrapper/src/main/java/software/amazon/jdbc/PartialPluginService.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.checkerframework.checker.nullness.qual.Nullable;
3737
import software.amazon.jdbc.cleanup.CanReleaseResources;
3838
import software.amazon.jdbc.dialect.Dialect;
39+
import software.amazon.jdbc.dialect.HostListProviderSupplier;
3940
import software.amazon.jdbc.exceptions.ExceptionHandler;
4041
import software.amazon.jdbc.exceptions.ExceptionManager;
4142
import software.amazon.jdbc.hostavailability.HostAvailability;
@@ -108,6 +109,10 @@ public PartialPluginService(
108109
@NonNull final Dialect dbDialect,
109110
@Nullable final ConfigurationProfile configurationProfile) {
110111
this.servicesContainer = servicesContainer;
112+
this.servicesContainer.setHostListProviderService(this);
113+
this.servicesContainer.setPluginService(this);
114+
this.servicesContainer.setPluginManagerService(this);
115+
111116
this.pluginManager = servicesContainer.getConnectionPluginManager();
112117
this.props = props;
113118
this.originalUrl = originalUrl;
@@ -116,13 +121,17 @@ public PartialPluginService(
116121
this.dbDialect = dbDialect;
117122
this.configurationProfile = configurationProfile;
118123
this.exceptionManager = exceptionManager;
124+
119125
this.connectionProviderManager = new ConnectionProviderManager(
120126
this.pluginManager.getDefaultConnProvider(),
121127
this.pluginManager.getEffectiveConnProvider());
122128

123129
this.exceptionHandler = this.configurationProfile != null && this.configurationProfile.getExceptionHandler() != null
124130
? this.configurationProfile.getExceptionHandler()
125131
: null;
132+
133+
HostListProviderSupplier supplier = this.dbDialect.getHostListProvider();
134+
this.hostListProvider = supplier.getProvider(this.props, this.originalUrl, this.servicesContainer);
126135
}
127136

128137
@Override
@@ -141,7 +150,7 @@ public HostSpec getCurrentHostSpec() {
141150
throw new RuntimeException(Messages.get("PluginServiceImpl.hostListEmpty"));
142151
}
143152

144-
this.currentHostSpec = this.getWriter(this.getAllHosts());
153+
this.currentHostSpec = Utils.getWriter(this.getAllHosts());
145154
final List<HostSpec> allowedHosts = this.getHosts();
146155
if (!Utils.containsHostAndPort(allowedHosts, this.currentHostSpec.getHostAndPort())) {
147156
throw new RuntimeException(
@@ -207,21 +216,17 @@ public HostRole getHostRole(Connection conn) throws SQLException {
207216
return this.hostListProvider.getHostRole(conn);
208217
}
209218

210-
private HostSpec getWriter(final @NonNull List<HostSpec> hosts) {
211-
for (final HostSpec hostSpec : hosts) {
212-
if (hostSpec.getRole() == HostRole.WRITER) {
213-
return hostSpec;
214-
}
215-
}
216-
return null;
217-
}
218-
219219
@Override
220220
@Deprecated
221221
public ConnectionProvider getConnectionProvider() {
222222
return this.pluginManager.defaultConnProvider;
223223
}
224224

225+
@Override
226+
public ConnectionProvider getDefaultConnectionProvider() {
227+
return this.connectionProviderManager.getDefaultProvider();
228+
}
229+
225230
public boolean isPooledConnectionProvider(HostSpec host, Properties props) {
226231
final ConnectionProvider connectionProvider =
227232
this.connectionProviderManager.getConnectionProvider(this.driverProtocol, host, props);

wrapper/src/main/java/software/amazon/jdbc/PluginService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ Connection forceConnect(
242242
@Deprecated
243243
ConnectionProvider getConnectionProvider();
244244

245+
ConnectionProvider getDefaultConnectionProvider();
246+
245247
boolean isPooledConnectionProvider(HostSpec host, Properties props);
246248

247249
String getDriverProtocol();

wrapper/src/main/java/software/amazon/jdbc/PluginServiceImpl.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public HostSpec getCurrentHostSpec() {
180180
throw new RuntimeException(Messages.get("PluginServiceImpl.hostListEmpty"));
181181
}
182182

183-
this.currentHostSpec = this.getWriter(this.getAllHosts());
183+
this.currentHostSpec = Utils.getWriter(this.getAllHosts());
184184
final List<HostSpec> allowedHosts = this.getHosts();
185185
if (!Utils.containsHostAndPort(allowedHosts, this.currentHostSpec.getHostAndPort())) {
186186
throw new RuntimeException(
@@ -243,21 +243,17 @@ public HostRole getHostRole(Connection conn) throws SQLException {
243243
return this.hostListProvider.getHostRole(conn);
244244
}
245245

246-
private HostSpec getWriter(final @NonNull List<HostSpec> hosts) {
247-
for (final HostSpec hostSpec : hosts) {
248-
if (hostSpec.getRole() == HostRole.WRITER) {
249-
return hostSpec;
250-
}
251-
}
252-
return null;
253-
}
254-
255246
@Override
256247
@Deprecated
257248
public ConnectionProvider getConnectionProvider() {
258249
return this.pluginManager.defaultConnProvider;
259250
}
260251

252+
@Override
253+
public ConnectionProvider getDefaultConnectionProvider() {
254+
return this.connectionProviderManager.getDefaultProvider();
255+
}
256+
261257
public boolean isPooledConnectionProvider(HostSpec host, Properties props) {
262258
final ConnectionProvider connectionProvider =
263259
this.connectionProviderManager.getConnectionProvider(this.driverProtocol, host, props);

wrapper/src/main/java/software/amazon/jdbc/hostlistprovider/monitoring/MonitoringRdsHostListProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ protected ClusterTopologyMonitor initMonitor() throws SQLException {
8787
this.clusterId,
8888
this.servicesContainer.getStorageService(),
8989
this.pluginService.getTelemetryFactory(),
90+
this.pluginService.getDefaultConnectionProvider(),
9091
this.originalUrl,
9192
this.pluginService.getDriverProtocol(),
9293
this.pluginService.getTargetDriverDialect(),
@@ -133,6 +134,7 @@ protected void clusterIdChanged(final String oldClusterId) throws SQLException {
133134
this.clusterId,
134135
this.servicesContainer.getStorageService(),
135136
this.pluginService.getTelemetryFactory(),
137+
this.pluginService.getDefaultConnectionProvider(),
136138
this.originalUrl,
137139
this.pluginService.getDriverProtocol(),
138140
this.pluginService.getTargetDriverDialect(),

wrapper/src/main/java/software/amazon/jdbc/hostlistprovider/monitoring/MonitoringRdsMultiAzHostListProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ protected ClusterTopologyMonitor initMonitor() throws SQLException {
5555
this.clusterId,
5656
this.servicesContainer.getStorageService(),
5757
this.pluginService.getTelemetryFactory(),
58+
this.pluginService.getDefaultConnectionProvider(),
5859
this.originalUrl,
5960
this.pluginService.getDriverProtocol(),
6061
this.pluginService.getTargetDriverDialect(),

wrapper/src/main/java/software/amazon/jdbc/plugin/AuroraConnectionTrackerPlugin.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
import java.util.Collections;
2222
import java.util.EnumSet;
2323
import java.util.HashSet;
24-
import java.util.List;
2524
import java.util.Map;
2625
import java.util.Properties;
2726
import java.util.Set;
2827
import java.util.concurrent.TimeUnit;
2928
import java.util.concurrent.atomic.AtomicLong;
3029
import java.util.logging.Logger;
31-
import org.checkerframework.checker.nullness.qual.NonNull;
32-
import software.amazon.jdbc.HostRole;
3330
import software.amazon.jdbc.HostSpec;
3431
import software.amazon.jdbc.JdbcCallable;
3532
import software.amazon.jdbc.JdbcMethod;
@@ -38,6 +35,7 @@
3835
import software.amazon.jdbc.plugin.failover.FailoverSQLException;
3936
import software.amazon.jdbc.util.RdsUrlType;
4037
import software.amazon.jdbc.util.RdsUtils;
38+
import software.amazon.jdbc.util.Utils;
4139

4240
public class AuroraConnectionTrackerPlugin extends AbstractConnectionPlugin {
4341

@@ -157,7 +155,7 @@ private void checkWriterChanged(boolean needRefreshHostLists) {
157155
}
158156
}
159157

160-
final HostSpec hostSpecAfterFailover = this.getWriter(this.pluginService.getAllHosts());
158+
final HostSpec hostSpecAfterFailover = Utils.getWriter(this.pluginService.getAllHosts());
161159
if (hostSpecAfterFailover == null) {
162160
return;
163161
}
@@ -177,7 +175,7 @@ private void checkWriterChanged(boolean needRefreshHostLists) {
177175

178176
private void rememberWriter() {
179177
if (this.currentWriter == null || this.needUpdateCurrentWriter) {
180-
this.currentWriter = this.getWriter(this.pluginService.getAllHosts());
178+
this.currentWriter = Utils.getWriter(this.pluginService.getAllHosts());
181179
this.needUpdateCurrentWriter = false;
182180
}
183181
}
@@ -194,13 +192,4 @@ public void notifyNodeListChanged(final Map<String, EnumSet<NodeChangeOptions>>
194192
}
195193
}
196194
}
197-
198-
private HostSpec getWriter(final @NonNull List<HostSpec> hosts) {
199-
for (final HostSpec hostSpec : hosts) {
200-
if (hostSpec.getRole() == HostRole.WRITER) {
201-
return hostSpec;
202-
}
203-
}
204-
return null;
205-
}
206195
}

wrapper/src/main/java/software/amazon/jdbc/plugin/AuroraInitialConnectionStrategyPlugin.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import software.amazon.jdbc.util.Messages;
3838
import software.amazon.jdbc.util.RdsUrlType;
3939
import software.amazon.jdbc.util.RdsUtils;
40+
import software.amazon.jdbc.util.Utils;
4041
import software.amazon.jdbc.util.WrapperUtils;
4142

4243
public class AuroraInitialConnectionStrategyPlugin extends AbstractConnectionPlugin {
@@ -187,7 +188,7 @@ private Connection getVerifiedWriterConnection(
187188
writerCandidate = null;
188189

189190
try {
190-
writerCandidate = this.getWriter();
191+
writerCandidate = Utils.getWriter(this.pluginService.getAllHosts());
191192

192193
if (writerCandidate == null || this.rdsUtils.isRdsClusterDns(writerCandidate.getHost())) {
193194

@@ -362,15 +363,6 @@ private void delay(final long delayMs) {
362363
}
363364
}
364365

365-
private HostSpec getWriter() {
366-
for (final HostSpec host : this.pluginService.getAllHosts()) {
367-
if (host.getRole() == HostRole.WRITER) {
368-
return host;
369-
}
370-
}
371-
return null;
372-
}
373-
374366
private HostSpec getReader(final Properties props) throws SQLException {
375367

376368
final String strategy = READER_HOST_SELECTOR_STRATEGY.getString(props);

wrapper/src/main/java/software/amazon/jdbc/plugin/customendpoint/CustomEndpointPlugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ protected CustomEndpointMonitor createMonitorIfAbsent(Properties props) throws S
217217
this.customEndpointHostSpec.getUrl(),
218218
this.servicesContainer.getStorageService(),
219219
this.pluginService.getTelemetryFactory(),
220+
this.pluginService.getDefaultConnectionProvider(),
220221
this.pluginService.getOriginalUrl(),
221222
this.pluginService.getDriverProtocol(),
222223
this.pluginService.getTargetDriverDialect(),

wrapper/src/main/java/software/amazon/jdbc/plugin/efm2/HostMonitorServiceImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ protected HostMonitor getMonitor(
155155
monitorKey,
156156
this.serviceContainer.getStorageService(),
157157
this.telemetryFactory,
158+
this.pluginService.getDefaultConnectionProvider(),
158159
this.pluginService.getOriginalUrl(),
159160
this.pluginService.getDriverProtocol(),
160161
this.pluginService.getTargetDriverDialect(),

0 commit comments

Comments
 (0)