MartijnVisser commented on code in PR #262:
URL:
https://github.com/apache/flink-connector-jdbc/pull/262#discussion_r4670866400
##########
flink-connector-jdbc-core/src/main/java/org/apache/flink/connector/jdbc/datasource/transactions/xa/XaTransaction.java:
##########
@@ -122,16 +112,41 @@ public void close() throws Exception {
xaConnectionProvider.close();
currentTid = null;
hangingXids = null;
+ unownedHangingXids = null;
preparedXids = null;
}
+ /**
+ * Keeps only the hanging transactions of this job, which {@link #open}
rolls back. Those of
+ * another job may belong to a job that still runs and is about to commit
them, for example the
+ * job that took the savepoint this one restores. With a prefix it keeps
those under the prefix
+ * that it owns, see {@link #isLeftoverFromCheckpoint}, which also covers
earlier jobs; rolling
+ * them back here needs no listing of the prepared transactions.
+ */
public void recoverState(JdbcWriterState state) {
- hangingXids = new LinkedList<>(state.getHanging());
+ hangingXids = new LinkedList<>();
+ unownedHangingXids = new ArrayList<>();
+ for (TransactionId hanging : state.getHanging()) {
+ if (baseTransaction.getPrefixBytes() == null
Review Comment:
Agreed, the two tests you describe are in. I've limited the job id match to
xids without a prefix, though: job ids can be pinned or derived from the HA
cluster id, so with `jobId || owned` a green job under blue's job id would roll
back blue's xids under blue's prefix. A blue without a prefix under the same
job id isn't protected, so the docs say such jobs need a prefix each.
`testRestoreWithNewPrefixUnderSameJobId` covers the adoption path.
##########
flink-connector-jdbc-core/src/main/java/org/apache/flink/connector/jdbc/datasource/transactions/xa/XaTransaction.java:
##########
@@ -141,6 +156,90 @@ public void checkState() {
"inconsistent internal state");
}
+ /**
+ * Rolls back the prepared transactions left behind from the given
checkpoint on. Their records
+ * are replayed, and the committer only commits transactions of earlier
checkpoints.
+ */
+ public void rollbackPreparedTxs(long checkpointId) {
+ Collection<Xid> recovered;
+ try {
+ recovered = xaConnectionProvider.recover();
+ } catch (Exception e) {
+ LOG.warn(
+ "unable to recover prepared transactions, those left
behind that aren't in the"
+ + " restored state keep their locks",
+ e);
+
hangingXids.addAll(failOrRollback(unownedHangingXids).getForRetry());
+ return;
+ }
+ for (Xid xid : recovered) {
+ if (isLeftoverFromCheckpoint(xid, checkpointId)) {
+ LOG.info("rolling back a leftover transaction, xid={}", xid);
+ try {
+ xaConnectionProvider.rollback(xid);
+ } catch (RuntimeException e) {
+ if (isPrepared(xid)) {
+ throw e;
+ }
+ LOG.info("leftover transaction is gone already, xid={}",
xid);
+ }
+ }
+ }
+ }
+
+ /**
+ * A transaction id names the job, the subtask and the parallelism, and
the prefix if there is
+ * one. Without a prefix this writer only rolls back its own transactions:
another exactly-once
+ * sink of the job at the same parallelism can't run, as it starts the
same xids, and at another
+ * parallelism its xids differ only in the parallelism.
+ *
+ * <p>With a prefix it also rolls back those of earlier jobs and
parallelisms under the prefix,
+ * of the subtasks whose index is its own modulo the parallelism, so each
has a single owner as
+ * long as all writers can list them. At the same job and parallelism that
is only its own, as
+ * in BATCH the other writers may have prepared their transactions for the
committer already.
+ */
+ private boolean isLeftoverFromCheckpoint(Xid xid, long checkpointId) {
+ TransactionId transactionId;
+ try {
+ transactionId =
+ TransactionId.createFromXid(
+ xid.getFormatId(),
+ xid.getGlobalTransactionId(),
+ xid.getBranchQualifier());
+ } catch (IOException e) {
+ return false;
+ }
+ if (transactionId.getCheckpointId() < checkpointId) {
+ return false;
+ }
+ if (xid.getFormatId() == baseTransaction.getFormatId()
+ && Arrays.equals(
+ xid.getGlobalTransactionId(),
baseTransaction.getGlobalTransactionId())
+ && transactionId.getNumberOfSubtasks() ==
baseTransaction.getNumberOfSubtasks()) {
+ return true;
+ }
+ return baseTransaction.getPrefixBytes() != null &&
isOwnedUnderPrefix(transactionId);
Review Comment:
Done as suggested, in the same `owns()` check as the restored hanging xids.
`testOwnership` covers leftovers of each prefix, job, subtask, parallelism and
checkpoint for a writer at subtask 1 of 2, from both the listing and the
restored state.
##########
flink-connector-jdbc-core/src/main/java/org/apache/flink/connector/jdbc/core/datastream/sink/writer/JdbcWriter.java:
##########
@@ -113,7 +117,16 @@ public JdbcWriter(
exactlyOnceOptions,
transactionId,
((XaConnectionProvider) connectionProvider));
+ if (!exactlyOnceOptions.getTransactionIdPrefix().isPresent()
+ && transactionId.getSubtaskId() == 0) {
Review Comment:
Moved to `rollbackPreparedTxs`. It now only warns when it leaves prepared
xids alone that have no prefix and are of another job or parallelism, from the
restored checkpoint on, from the first subtask, and names them. A fresh start
logs nothing (`testNoWarningWithoutLeftovers`), and the example has its
argument now.
--
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]