SEPURI-SAI-KRISHNA opened a new pull request, #13365:
URL: https://github.com/apache/seatunnel/pull/13365
Yes, glad to contribute to Apache SeaTunnel.
### Purpose of this pull request
Closes #13341.
When a JDBC batch fails with a row-level error (SQLState class `22` or `23`)
and an error handler is configured, `JdbcSinkWriter.handleRowLevelBatchFailure`
handed every row of the batch to the row-error collector. With 3 valid rows and
1 duplicate key in one batch, all 4 rows went to the error table and none was
written. `SinkErrorToMysqlIT` asserted exactly that, while `error-handling.md`
said only the bad record is handed to the error handler.
Following the direction in #13341, the change is separated by transaction
mode.
**Manual commit (`auto_commit = false`, or Oracle).** The failed batch is
cleared and rolled back first, as before. Its rows are then written again one
by one only when it is known that none of them is in the database and that one
failing row can be undone alone:
- the rollback succeeded (to the last savepoint, or the whole transaction).
With checkpointing, the rows of the batch were only in the open transaction, so
the rollback removed all of them. Without checkpointing, a flush commits only
after `executeBatch` succeeded, so the failed batch was never committed; a
failed commit is a system error, never a row error, so it never reaches this
path;
- the shared transaction did not lose work of another writer
(`JdbcOutputFormat.isPendingWorkLost()`, new). Otherwise the commit is refused
anyway and the job recovers, as before;
- the batch has more than one row;
- checkpointing is disabled (each replayed row is committed by its own
flush, so a failing row is rolled back alone), or the driver supports
savepoints (a savepoint is taken before each replayed row).
A replayed row that fails with a row-level error is rolled back to its
savepoint (or alone) and reported with its own error. The written rows get a
savepoint boundary like an auto-flushed batch and are reported as written: in
`write()` and the timer flush right away, as for an auto-flushed batch, and in
`prepareCommit()` and `close()` after the commit. Any other failure during the
replay rolls the transaction back and fails the writer.
**Auto-commit.** Unchanged. Rows of the failed batch before the failing
statement can already be committed (the new unit test shows the row before the
duplicate in the table), so writing them again could write a row twice while it
is also reported as an error. A separate change can handle auto-commit once its
partial-apply behavior is proven per driver.
**Savepoint boundary order (found while testing on real SQLite).**
`markSuccessfulAutoFlushBoundaryIfNeeded` took the new savepoint and then
released the previous one. In SQLite, and in PostgreSQL ("releases the named
savepoint and all active savepoints that were created after the named
savepoint", [RELEASE
SAVEPOINT](https://www.postgresql.org/docs/current/sql-release-savepoint.html)),
that also destroys the new savepoint. On dev, a row error after two flushed
batches therefore failed with `no such savepoint: SQLITE_SAVEPOINT_2` instead
of being handled. The boundary is now moved by releasing first
(`moveSavepointBoundary`), which the replay also relies on.
Docs: the batch section of `error-handling.md` (EN/ZH) now describes each
mode and the recommendations, and `incompatible-changes.md` (EN/ZH) has an
entry, because error tables now hold only the failing rows.
### Does this PR introduce _any_ user-facing change?
Yes. With an error handler and a manual-commit connection (driver with
savepoints, or checkpointing disabled), only the failing rows of a failed batch
reach the error handler and the other rows are written. No option, default or
API changes. Auto-commit connections, single-row batches and jobs without an
error handler behave as before.
### How was this patch tested?
`JdbcSinkWriterTest`, new cases on a real writer, output format and
statement executor over a fake table (primary key, name up to 10 characters,
transactions, savepoints that behave as in SQLite and PostgreSQL; only the
dialect, provider and row converter are mocked). Each asserts the exact
committed rows, the exact reported errors (phase, row, SQLState) and the
reported writes:
- valid, duplicate, valid at close, with checkpointing: rows 1 and 2
committed, only the duplicate reported; one full rollback, one rollback to a
savepoint, one commit;
- the same with checkpointing disabled: no savepoint taken, each row
committed by its flush;
- a batch failing in `write()` after a flushed batch: rolled back to that
batch's savepoint (no full rollback), all valid rows committed at
`prepareCommit`;
- two failed batches in one checkpoint interval: the rows of the first
replay survive the second rollback;
- unchanged behavior: auto-commit (all 3 rows reported, the row before the
duplicate already committed, one `executeBatch`), no savepoint support, a
rollback that discarded another writer's batch (all rows reported, commit
refused), and no error handler (the writer fails, nothing committed);
- a replayed row failing with a non-row error: rollback, the writer fails,
nothing committed.
On the `JdbcSinkWriter` of dev, the 4 replay cases fail (for example
`expected: <[1 ok1, 2 ok2]> but was: <[]>`) and the 4 unchanged-behavior cases
pass. Mutations, each caught: no lost-work check, no savepoint-support check,
no boundary after the replay, replay under auto-commit, no per-row savepoint,
and the old savepoint order.
`JdbcMultiTableReconnectTest` (real SQLite): the connection proxy can now
fail every batch that contains a given id, after executing it, as a real
constraint violation does (`failRowWith`). Two tests used the one-shot
`failNextBatchWith` as a row error; a replayed row would then succeed, which no
real constraint error does, so they now use `failRowWith` and keep their
intent: the shared-queue savepoint test now reports only row 4 and commits rows
1 to 3, and the consecutive-errors test makes both rows of the first batch
fail, so the second failure still rolls back the reported rows and the
checkpoint is still rejected. New:
`rowErrorAfterTwoFlushedBatchesRollsBackToTheLatestBoundary`, which fails on
dev with `no such savepoint`.
`SinkErrorToMysqlIT` (Zeta, MySQL 8.0.43): the two batch-close tests now
assert the exact target rows and the routed rows (data error: rows 1 and 2
written, the 2 long names routed; constraint: rows 1, 2 and 3 written, only the
duplicate routed). New
`testSinkBatchConstraintErrorWithCheckpointRollsBackToSavepoint` runs with
checkpointing and `batch_size = 2`, so the failing batch follows two flushed
batches and is rolled back to a savepoint on MySQL.
- `SinkErrorToMysqlIT` on Zeta: 19 run, 0 failures, 1 skipped
(`testSinkQueueOverflowFailPolicy`, already `@Disabled` on dev). With the
`JdbcSinkWriter` and `JdbcOutputFormat` of dev, the 3 batch tests fail:
`expected: <[1,ok1,18, 2,ok2,20, 3,ok3,21]> but was: <[]>` (duplicate key),
`expected: <[1,ok1,18, 2,ok2,20]> but was: <[]>` (data too long), and with
checkpointing `expected: <[1,a,1, 2,b,2, 3,c,3, 4,d,4, 5,e,5]> but was:
<[1,a,1, 2,b,2, 3,c,3, 4,d,4]>`. In that last case the job of dev still
succeeded, so on MySQL 8.0.43 the rollback to the savepoint worked with the old
order; the savepoint order problem shows on SQLite (and, per its documentation,
PostgreSQL), not on MySQL.
- connector-jdbc unit tests: all 1303 pass (6 skipped: the catalog tests
disabled with "Please Test it in your local environment"), including the Oracle
and Kingbase container tests.
- `spotless:check`, `git diff --check`.
### Check list
* [x] If any new Jar binary package adding in your PR, please add License
Notice according
[New License
Guide](https://github.com/apache/seatunnel/blob/dev/docs/en/developer/new-license.md)
(no new jar)
* [x] If necessary, please update the documentation to describe the new
feature. https://github.com/apache/seatunnel/tree/dev/docs (`error-handling.md`
EN/ZH)
* [x] If necessary, please update `incompatible-changes.md` to describe the
incompatibility caused by this PR. (EN/ZH)
* [ ] If you are contributing the connector code, please check that the
following files are updated: (not a new connector)
--
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]