eskabetxe commented on code in PR #259:
URL:
https://github.com/apache/flink-connector-jdbc/pull/259#discussion_r4643559411
##########
flink-connector-jdbc-core/src/main/java/org/apache/flink/connector/jdbc/datasource/transactions/xa/XaTransaction.java:
##########
@@ -87,16 +85,24 @@ public void open(JdbcWriterState state) throws IOException {
try {
xaConnectionProvider.open();
recoverState(state);
- hangingXids = new
LinkedList<>(failOrRollback(hangingXids).getForRetry());
+ XaTransactionResult<TransactionId> rolledBack =
failOrRollback(hangingXids);
+ if (rolledBack.getTransientFailure().isPresent()) {
Review Comment:
The new check only fails on getTransientFailure().
A non-transient failure lands in XaTransactionResult.failed, gets only a
LOG.warn in failOrRollback, and is then dropped when hangingXids = new
LinkedList<>() on line 97, so that transaction holds its locks for the rest of
the run, which is exactly the outcome this commit set out to prevent.
Same argument as the comment on line 90. One line fixes it, before the
transient check:
rolledBack.throwIfAnyFailed("roll back");
(throwIfAnyFailed only maps non-transient failures, so it composes with the
existing branch.)
##########
flink-connector-jdbc-core/src/main/java/org/apache/flink/connector/jdbc/datasource/transactions/xa/XaTransaction.java:
##########
@@ -170,6 +249,7 @@ public void prepareTx() throws IOException {
ExceptionUtils.rethrowIOException(e);
}
currentTid = null;
+ return Optional.ofNullable(prepared);
}
public void commitTx() {
Review Comment:
After this PR, commitTx() is called only from open() (line 98), this is the
legacy v4.x preparedXids path. If one of those commits fails transiently at
open, commitXids puts it back into preparedXids (line 258), getState() keeps
it, and… nothing retries it until the next restore, while the transaction keeps
holding its locks for the whole run.
The old code retried on every checkpoint via commitTxUntil. Since these are
records of already-completed checkpoints, committing them late is always safe,
consider calling commitTx() from snapshotState() when preparedXids is non-empty
(and a test that the second attempt succeeds after a transient failure at open,
mirroring testRetryTransientFailureUntilMaxCommitAttempts).
##########
flink-connector-jdbc-core/src/main/java/org/apache/flink/connector/jdbc/core/datastream/sink/writer/JdbcWriter.java:
##########
@@ -127,7 +122,24 @@ public JdbcWriter(
() ->
JdbcBatchStatementExecutor.simple(
queryStatement.query(),
queryStatement::statement));
- this.jdbcOutput.open(outputSerializer);
+ try {
+ if (jdbcTransaction != null) {
+ this.jdbcTransaction.open(state);
+ this.jdbcTransaction.rollbackPreparedTxs(lastCheckpointId);
+ this.jdbcTransaction.createTx(lastCheckpointId);
+ }
+ this.jdbcOutput.open(outputSerializer);
+ } catch (IOException | RuntimeException e) {
Review Comment:
Nice symmetry on the XA side. The at-least-once path is still uncovered
though: if jdbcOutput.open() (line 131) fails inside
createAndOpenStatementExecutor after getOrEstablishConnection() succeeded, the
underlying JDBC connection leaks, pre-existing, not a regression, and
JdbcOutputFormat.close() isn't safe to call on a half-open format, so I'm not
asking for a change here. Mentioning it only in case you'd rather have the
invariant "a failed constructor releases its connections" hold for both
guarantees.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]