Skip to content

Commit

Permalink
fix failover mode initialization (#1204)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiyvamz authored Nov 28, 2024
1 parent f811999 commit a8b6c5f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class FailoverConnectionPlugin extends AbstractConnectionPlugin {
private Throwable lastExceptionDealtWith = null;
private PluginManagerService pluginManagerService;
private boolean isInTransaction = false;
private RdsUrlType rdsUrlType;
private RdsUrlType rdsUrlType = null;
private HostListProviderService hostListProviderService;
private final AuroraStaleDnsHelper staleDnsHelper;

Expand Down Expand Up @@ -293,24 +293,6 @@ void initHostProvider(
this.writerFailoverHandler = writerFailoverHandlerSupplier.get();

initHostProviderFunc.call();

this.failoverMode = FailoverMode.fromValue(FAILOVER_MODE.getString(this.properties));
this.rdsUrlType = this.rdsHelper.identifyRdsType(initialUrl);

if (this.failoverMode == null) {
if (this.rdsUrlType.isRdsCluster()) {
this.failoverMode = (this.rdsUrlType == RdsUrlType.RDS_READER_CLUSTER)
? FailoverMode.READER_OR_WRITER
: FailoverMode.STRICT_WRITER;
} else {
this.failoverMode = FailoverMode.STRICT_WRITER;
}
}

LOGGER.finer(
() -> Messages.get(
"Failover.parameterValue",
new Object[]{"failoverMode", this.failoverMode}));
}

@Override
Expand Down Expand Up @@ -382,6 +364,29 @@ private void initSettings() {
TELEMETRY_FAILOVER_ADDITIONAL_TOP_TRACE.getBoolean(this.properties);
}

protected void initFailoverMode() {
if (this.rdsUrlType == null) {
this.failoverMode = FailoverMode.fromValue(FAILOVER_MODE.getString(this.properties));
final HostSpec initialHostSpec = this.hostListProviderService.getInitialConnectionHostSpec();
this.rdsUrlType = this.rdsHelper.identifyRdsType(initialHostSpec.getHost());

if (this.failoverMode == null) {
if (this.rdsUrlType.isRdsCluster()) {
this.failoverMode = (this.rdsUrlType == RdsUrlType.RDS_READER_CLUSTER)
? FailoverMode.READER_OR_WRITER
: FailoverMode.STRICT_WRITER;
} else {
this.failoverMode = FailoverMode.STRICT_WRITER;
}
}

LOGGER.finer(
() -> Messages.get(
"Failover.parameterValue",
new Object[]{"failoverMode", this.failoverMode}));
}
}

private void invalidInvocationOnClosedConnection() throws SQLException {
if (!this.closedExplicitly.get()) {
this.isClosed = false;
Expand Down Expand Up @@ -811,6 +816,7 @@ public Connection connect(
private Connection connectInternal(String driverProtocol, HostSpec hostSpec, Properties props,
boolean isInitialConnection, JdbcCallable<Connection, SQLException> connectFunc, boolean isForceConnect)
throws SQLException {
this.initFailoverMode();
Connection conn = null;
try {
conn =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,26 +229,7 @@ void initHostProvider(
final JdbcCallable<Void, SQLException> initHostProviderFunc)
throws SQLException {
this.hostListProviderService = hostListProviderService;

initHostProviderFunc.call();

this.failoverMode = FailoverMode.fromValue(FAILOVER_MODE.getString(this.properties));
this.rdsUrlType = this.rdsHelper.identifyRdsType(initialUrl);

if (this.failoverMode == null) {
if (this.rdsUrlType.isRdsCluster()) {
this.failoverMode = (this.rdsUrlType == RdsUrlType.RDS_READER_CLUSTER)
? FailoverMode.READER_OR_WRITER
: FailoverMode.STRICT_WRITER;
} else {
this.failoverMode = FailoverMode.STRICT_WRITER;
}
}

LOGGER.finer(
() -> Messages.get(
"Failover.parameterValue",
new Object[]{"failoverMode", this.failoverMode}));
}

protected boolean isFailoverEnabled() {
Expand Down Expand Up @@ -632,6 +613,29 @@ protected boolean canDirectExecute(final String methodName) {
|| methodName.equals(METHOD_ABORT));
}

protected void initFailoverMode() {
if (this.rdsUrlType == null) {
this.failoverMode = FailoverMode.fromValue(FAILOVER_MODE.getString(this.properties));
final HostSpec initialHostSpec = this.hostListProviderService.getInitialConnectionHostSpec();
this.rdsUrlType = this.rdsHelper.identifyRdsType(initialHostSpec.getHost());

if (this.failoverMode == null) {
if (this.rdsUrlType.isRdsCluster()) {
this.failoverMode = (this.rdsUrlType == RdsUrlType.RDS_READER_CLUSTER)
? FailoverMode.READER_OR_WRITER
: FailoverMode.STRICT_WRITER;
} else {
this.failoverMode = FailoverMode.STRICT_WRITER;
}
}

LOGGER.finer(
() -> Messages.get(
"Failover.parameterValue",
new Object[]{"failoverMode", this.failoverMode}));
}
}

@Override
public Connection connect(
final String driverProtocol,
Expand All @@ -640,11 +644,14 @@ public Connection connect(
final boolean isInitialConnection,
final JdbcCallable<Connection, SQLException> connectFunc)
throws SQLException {

// This call was initiated by this failover2 plugin and doesn't require any additional processing.
if (props.containsKey(INTERNAL_CONNECT_PROPERTY_NAME)) {
return connectFunc.call();
}

this.initFailoverMode();

Connection conn = null;

if (!ENABLE_CONNECT_FAILOVER.getBoolean(props)) {
Expand Down

0 comments on commit a8b6c5f

Please sign in to comment.