spuru9 commented on code in PR #259:
URL:
https://github.com/apache/flink-connector-jdbc/pull/259#discussion_r4711186225
##########
flink-connector-jdbc-core/src/main/java/org/apache/flink/connector/jdbc/core/datastream/sink/JdbcSink.java:
##########
@@ -102,6 +111,36 @@ public SimpleVersionedSerializer<JdbcCommitable>
getCommittableSerializer() {
return new JdbcCommitableSerializer();
}
+ @Override
+ @Internal
+ public DataStream<CommittableMessage<JdbcCommitable>> addPreCommitTopology(
+ DataStream<CommittableMessage<JdbcCommitable>> committables) {
+ if (deliveryGuarantee != DeliveryGuarantee.EXACTLY_ONCE) {
+ return committables;
+ }
+ // A prepared transaction keeps its locks until it is committed, so a
writer chained to the
+ // committer could block on one and keep the committer from running.
The map is chained to
+ // the writer, so the writer keeps its generated operator id and
savepoints without a uid
Review Comment:
This keeps the writer's id, but without a uid the committer's generated id
still changes, since its input is now this map. The committer always has state
in the savepoint, so restoring a 4.1 savepoint without uids would need
`--allowNonRestoredState`. Could we document that and add a committer-id check
next to `testWriterKeepsItsOperatorIdWithoutUid`?
##########
flink-connector-jdbc-core/src/main/java/org/apache/flink/connector/jdbc/core/datastream/sink/committer/JdbcCommitter.java:
##########
@@ -64,16 +75,54 @@ public void
commit(Collection<CommitRequest<JdbcCommitable>> committables)
if (DeliveryGuarantee.EXACTLY_ONCE != this.deliveryGuarantee &&
!committables.isEmpty()) {
throw new FlinkRuntimeException("Non XA sink with commitables");
}
+ if (!committables.isEmpty() && !opened) {
+ try {
+ xaConnectionProvider.open();
+ } catch (Exception e) {
+ // close() skips the provider, so release what open() got
before it failed
+ try {
+ xaConnectionProvider.close();
+ } catch (Exception closeException) {
+ e.addSuppressed(closeException);
+ }
+ ExceptionUtils.rethrowIOException(e);
+ }
+ opened = true;
+ }
for (CommitRequest<JdbcCommitable> request : committables) {
-
request.getCommittable().getTransaction().orElse(this.jdbcTransaction).commitTx();
+ Xid xid = request.getCommittable().getXid();
+ try {
+ xaConnectionProvider.commit(xid, isRestored(xid));
+ } catch (TransientXaException e) {
+ if (request.getNumberOfRetries() + 1 >= maxCommitAttempts) {
+ request.signalFailedWithUnknownReason(e);
+ } else {
+ LOG.warn("failed to commit transaction {} (retrying)",
xid, e);
+ request.retryLater();
+ }
+ }
+ }
+ }
+
+ /**
+ * A transaction from before the restored checkpoint may have been
committed before the failure.
+ * Any other transaction unknown to the database was not committed.
+ */
+ private boolean isRestored(Xid xid) throws IOException {
+ if (!restoredCheckpointId.isPresent()) {
+ return false;
}
+ TransactionId transactionId =
+ TransactionId.createFromXid(
+ xid.getFormatId(), xid.getGlobalTransactionId(),
xid.getBranchQualifier());
+ return transactionId.getCheckpointId() <
restoredCheckpointId.getAsLong();
Review Comment:
Could we add a test for an unknown xid whose checkpoint id equals the
restored one? It should fail, and nothing would catch it if this `<` became
`<=`.
##########
flink-connector-jdbc-core/src/main/java/org/apache/flink/connector/jdbc/core/datastream/sink/committer/JdbcCommitter.java:
##########
@@ -64,16 +75,54 @@ public void
commit(Collection<CommitRequest<JdbcCommitable>> committables)
if (DeliveryGuarantee.EXACTLY_ONCE != this.deliveryGuarantee &&
!committables.isEmpty()) {
throw new FlinkRuntimeException("Non XA sink with commitables");
}
+ if (!committables.isEmpty() && !opened) {
+ try {
+ xaConnectionProvider.open();
+ } catch (Exception e) {
+ // close() skips the provider, so release what open() got
before it failed
+ try {
+ xaConnectionProvider.close();
+ } catch (Exception closeException) {
+ e.addSuppressed(closeException);
+ }
+ ExceptionUtils.rethrowIOException(e);
+ }
+ opened = true;
+ }
for (CommitRequest<JdbcCommitable> request : committables) {
-
request.getCommittable().getTransaction().orElse(this.jdbcTransaction).commitTx();
+ Xid xid = request.getCommittable().getXid();
+ try {
+ xaConnectionProvider.commit(xid, isRestored(xid));
+ } catch (TransientXaException e) {
+ if (request.getNumberOfRetries() + 1 >= maxCommitAttempts) {
+ request.signalFailedWithUnknownReason(e);
+ } else {
+ LOG.warn("failed to commit transaction {} (retrying)",
xid, e);
+ request.retryLater();
Review Comment:
The committer's connection is only used when there's something to commit, so
it can sit idle through a quiet stream and get dropped by `wait_timeout` or a
firewall. Nothing reopens it, so the retries here run on the same broken
connection and the job fails over. Could we reopen the provider on a transient
failure before `retryLater()`?
##########
docs/content/docs/connectors/datastream/jdbc.md:
##########
@@ -448,12 +448,26 @@ JdbcExactlyOnceOptions.builder()
.build();
```
-This will make Flink use a separate connection for every XA transaction. This
may require adjusting connection limits.
-For PostgreSQL and MySQL, this can be done by increasing `max_connections`.
+This will make Flink start every XA transaction on a connection from a pool,
and return the connection once its transaction is prepared, so each sink
subtask writes on one connection at a time.
Furthermore, XA needs to be enabled and/or configured in some databases.
-For PostgreSQL, you should set `max_prepared_transactions` to some value
greater than zero.
-For MySQL v8+, you should grant `XA_RECOVER_ADMIN` to Flink DB user.
+For PostgreSQL, you should set `max_prepared_transactions` to at least twice
the sink parallelism: every sink subtask keeps its transaction prepared until
the checkpoint completes, and can prepare the next one before the previous one
is committed. Each failed or concurrent checkpoint in between adds one more per
sink subtask.
+For MySQL v8+, you should grant `XA_RECOVER_ADMIN` to Flink DB user. On
Oracle, grant `SELECT` on `DBA_PENDING_TRANSACTIONS` and `EXECUTE` on
`SYS.DBMS_XA`. The sink lists the prepared transactions at every start, and
fails if it can't.
+
+The sink commits a prepared XA transaction once the checkpoint completes, on a
different connection than the one that prepared it.
+For MySQL, this requires `xa_detach_on_prepare` to be `ON`, which is the
default since MySQL 8.0.29.
+
+Records become visible once the checkpoint completes. A single committer
subtask commits the transactions of all sink subtasks, one after another, which
takes about one database round trip per sink subtask.
+A prepared transaction keeps its locks until it is committed, so a record that
touches a row of the previous checkpoint waits until that commit.
Review Comment:
One case where that commit never comes: if a checkpoint is aborted but
tolerated, its transaction is only committed after the next checkpoint. A
record that updates one of its rows blocks the writer before that barrier, so
the wait only ends when the lock timeout fails the job. Could we mention that
case here?
--
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]