Skip to content

Commit 2e42a60

Browse files
authored
skip failover on interrupted thread (#1284)
1 parent 29b46a9 commit 2e42a60

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

Diff for: docs/using-the-jdbc-driver/using-plugins/UsingTheFailover2Plugin.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ In addition to the parameters that you can configure for the underlying driver,
6363
| `clusterTopologyHighRefreshRateMs` | Integer | No | Interval of time in milliseconds to wait between attempts to update cluster topology after the writer has come back online following a failover event. It corresponds to the increased monitoring rate described earlier. Usually, the topology monitoring component uses this increased monitoring rate for 30s after a new writer was detected. | `100` |
6464
| `failoverReaderHostSelectorStrategy` | String | No | Strategy used to select a reader node during failover. For more information on the available reader selection strategies, see this [table](../ReaderSelectionStrategies.md). | `random` |
6565
| `clusterId` | String | No | A unique identifier for the cluster. Connections with the same cluster id share a cluster topology cache. | None |
66-
| `telemetryFailoverAdditionalTopTrace` | Boolean | No | Allows the driver to produce an additional telemetry span associated with failover. Such span helps to facilitate telemetry analysis in AWS CloudWatch. | `false` |
66+
| `telemetryFailoverAdditionalTopTrace` | Boolean | No | Allows the driver to produce an additional telemetry span associated with failover. Such span helps to facilitate telemetry analysis in AWS CloudWatch. | `false` |
67+
| `skipFailoverOnInterruptedThread` | Boolean | No | Enable to skip failover if the current thread is interrupted. This may leave the Connection in an invalid state so the Connection should be disposed. | `false` |
68+
6769

6870

6971
Please refer to the original [Failover Plugin](./UsingTheFailoverPlugin.md) for more details about error codes, configurations, connection pooling and sample codes.

Diff for: docs/using-the-jdbc-driver/using-plugins/UsingTheFailoverPlugin.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ In addition to the parameters that you can configure for the underlying driver,
3131
| `failoverTimeoutMs` | Integer | No | Maximum allowed time in milliseconds to attempt reconnecting to a new writer or reader instance after a cluster failover is initiated. | `300000` |
3232
| `failoverWriterReconnectIntervalMs` | Integer | No | Interval of time in milliseconds to wait between attempts to reconnect to a failed writer during a writer failover process. | `2000` |
3333
| `enableConnectFailover` | Boolean | No | Enables/disables cluster-aware failover if the initial connection to the database fails due to a network exception. Note that this may result in a connection to a different instance in the cluster than was specified by the URL. | `false` |
34+
| `skipFailoverOnInterruptedThread` | Boolean | No | Enable to skip failover if the current thread is interrupted. This may leave the Connection in an invalid state so the Connection should be disposed. | `false` |
3435
| ~~`keepSessionStateOnFailover`~~ | Boolean | No | This parameter is no longer available. If specified, it will be ignored by the driver. See [Session State](../SessionState.md) for more details. | `false` |
3536
| ~~`enableFailoverStrictReader`~~ | Boolean | No | This parameter is no longer available and, if specified, it will be ignored by the driver. See `failoverMode` (`reader-or-writer` or `strict-reader`) for more details. | |
3637

Diff for: wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java

+13
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,20 @@ public class FailoverConnectionPlugin extends AbstractConnectionPlugin {
164164
"telemetryFailoverAdditionalTopTrace", "false",
165165
"Post an additional top-level trace for failover process.");
166166

167+
public static final AwsWrapperProperty SKIP_FAILOVER_ON_INTERRUPTED_THREAD =
168+
new AwsWrapperProperty(
169+
"skipFailoverOnInterruptedThread", "false",
170+
"Enable to skip failover if the current thread is interrupted.");
171+
167172
private final TelemetryCounter failoverWriterTriggeredCounter;
168173
private final TelemetryCounter failoverWriterSuccessCounter;
169174
private final TelemetryCounter failoverWriterFailedCounter;
170175
private final TelemetryCounter failoverReaderTriggeredCounter;
171176
private final TelemetryCounter failoverReaderSuccessCounter;
172177
private final TelemetryCounter failoverReaderFailedCounter;
173178

179+
private boolean skipFailoverOnInterruptedThread;
180+
174181
static {
175182
PropertyDefinition.registerPluginProperties(FailoverConnectionPlugin.class);
176183
}
@@ -369,6 +376,7 @@ private void initSettings() {
369376
this.failoverReaderConnectTimeoutMsSetting = FAILOVER_READER_CONNECT_TIMEOUT_MS.getInteger(this.properties);
370377
this.telemetryFailoverAdditionalTopTraceSetting =
371378
TELEMETRY_FAILOVER_ADDITIONAL_TOP_TRACE.getBoolean(this.properties);
379+
this.skipFailoverOnInterruptedThread = SKIP_FAILOVER_ON_INTERRUPTED_THREAD.getBoolean(this.properties);
372380
}
373381

374382
protected void initFailoverMode() {
@@ -788,6 +796,11 @@ protected boolean shouldExceptionTriggerConnectionSwitch(final Throwable t) {
788796
return false;
789797
}
790798

799+
if (this.skipFailoverOnInterruptedThread && Thread.currentThread().isInterrupted()) {
800+
LOGGER.fine(() -> Messages.get("Failover.skipFailoverOnInterruptedThread"));
801+
return false;
802+
}
803+
791804
String sqlState = null;
792805
if (t instanceof SQLException) {
793806
sqlState = ((SQLException) t).getSQLState();

Diff for: wrapper/src/main/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPlugin.java

+13
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public class FailoverConnectionPlugin extends AbstractConnectionPlugin {
9898
+ "network exception. Note that this may result in a connection to a different instance in the cluster "
9999
+ "than was specified by the URL.");
100100

101+
public static final AwsWrapperProperty SKIP_FAILOVER_ON_INTERRUPTED_THREAD =
102+
new AwsWrapperProperty(
103+
"skipFailoverOnInterruptedThread", "false",
104+
"Enable to skip failover if the current thread is interrupted.");
105+
101106
private static final Set<String> subscribedMethods =
102107
Collections.unmodifiableSet(new HashSet<String>() {
103108
{
@@ -137,6 +142,8 @@ public class FailoverConnectionPlugin extends AbstractConnectionPlugin {
137142
protected final TelemetryCounter failoverReaderTriggeredCounter;
138143
protected final TelemetryCounter failoverReaderSuccessCounter;
139144
protected final TelemetryCounter failoverReaderFailedCounter;
145+
protected final boolean skipFailoverOnInterruptedThread;
146+
140147

141148
static {
142149
PropertyDefinition.registerPluginProperties(FailoverConnectionPlugin.class);
@@ -165,6 +172,7 @@ public FailoverConnectionPlugin(final PluginService pluginService, final Propert
165172
TELEMETRY_FAILOVER_ADDITIONAL_TOP_TRACE.getBoolean(this.properties);
166173
this.failoverReaderHostSelectorStrategySetting =
167174
FAILOVER_READER_HOST_SELECTOR_STRATEGY.getString(this.properties);
175+
this.skipFailoverOnInterruptedThread = SKIP_FAILOVER_ON_INTERRUPTED_THREAD.getBoolean(this.properties);
168176

169177
TelemetryFactory telemetryFactory = this.pluginService.getTelemetryFactory();
170178
this.failoverWriterTriggeredCounter = telemetryFactory.createCounter("writerFailover.triggered.count");
@@ -637,6 +645,11 @@ protected boolean shouldExceptionTriggerConnectionSwitch(final Throwable t) {
637645
return false;
638646
}
639647

648+
if (this.skipFailoverOnInterruptedThread && Thread.currentThread().isInterrupted()) {
649+
LOGGER.fine(() -> Messages.get("Failover.skipFailoverOnInterruptedThread"));
650+
return false;
651+
}
652+
640653
String sqlState = null;
641654
if (t instanceof SQLException) {
642655
sqlState = ((SQLException) t).getSQLState();

0 commit comments

Comments
 (0)