[
https://issues.apache.org/jira/browse/FLINK-41805?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
ASF GitHub Bot updated FLINK-41805:
-----------------------------------
Labels: pull-request-available (was: )
> JdbcOutputFormat#close throws after a failed flush and stops the TaskManager
> ----------------------------------------------------------------------------
>
> Key: FLINK-41805
> URL: https://issues.apache.org/jira/browse/FLINK-41805
> Project: Flink
> Issue Type: Bug
> Components: Connectors / JDBC
> Reporter: Mate Czagany
> Priority: Major
> Labels: pull-request-available
>
> When the database restarts under a running JDBC sink, the TaskManager exits.
> On Flink 2.3.0 with the connector from {{main}} (c70802e) and PostgreSQL 16,
> the TaskManager stopped with exit code 1 about two seconds after PostgreSQL
> went down. Cancelling a healthy job while the database is down does the
> same.
> h3. Cause
> Since FLINK-38180 (Flink 2.2), an exception thrown while a failed or
> cancelled task is cleaned up is a fatal error, see FLINK-41269.
> {{JdbcOutputFormat#close}} throws in that cleanup in three ways:
> # {{close()}} flushes the buffered records. With the database still down the
> flush fails, and {{close()}} throws {{RuntimeException: Writing records to
> JDBC failed.}}
> # After a flush on the timer thread has failed, {{close()}} rethrows the
> stored {{flushException}}.
> # When a flush attempt fails, {{updateExecutor}} closes the statements and
> then reconnects. If the reconnect is refused the statements stay null, and
> the flush in {{close()}} fails with a {{NullPointerException}}:
> {noformat}
> FATAL - exception in exception handler of task GroupAggregate[3] ->
> ConstraintEnforcer[4] -> Sink: snk[4] (1/1)#0
> java.lang.RuntimeException: Writing records to JDBC failed.
> at
> org.apache.flink.connector.jdbc.internal.JdbcOutputFormat.close(JdbcOutputFormat.java:236)
> at
> org.apache.flink.connector.jdbc.internal.GenericJdbcSinkFunction.close(GenericJdbcSinkFunction.java:72)
> ...
> Caused by: java.lang.NullPointerException: Cannot invoke
> "org.apache.flink.connector.jdbc.statement.FieldNamedPreparedStatement.setInt(int,
> int)" because "statement" is null
> at
> org.apache.flink.connector.jdbc.internal.executor.TableSimpleStatementExecutor.addToBatch(TableSimpleStatementExecutor.java:59)
> at
> org.apache.flink.connector.jdbc.internal.executor.TableBufferReducedStatementExecutor.executeBatch(TableBufferReducedStatementExecutor.java:97)
> at
> org.apache.flink.connector.jdbc.internal.JdbcOutputFormat.attemptFlush(JdbcOutputFormat.java:217)
> at
> org.apache.flink.connector.jdbc.internal.JdbcOutputFormat.flush(JdbcOutputFormat.java:191)
> at
> org.apache.flink.connector.jdbc.internal.JdbcOutputFormat.close(JdbcOutputFormat.java:233)
> {noformat}
> A second defect is why a short restart reaches the task at all: a refused
> reconnect is not retried. {{flush()}} throws {{IOException: Unable to update
> JDBC statement executor}} on the first refused reconnect, whatever
> {{sink.max-retries}} is set to.
> h3. How to reproduce
> * Flink 2.3.0 standalone cluster, PostgreSQL 16, checkpointing every 10 s.
> * A SQL job from {{datagen}} into a {{jdbc}} table with a primary key and
> {{'sink.buffer-flush.interval' = '1s'}}.
> * Stop PostgreSQL ({{pg_ctl stop -m fast}}).
> The TaskManager logs {{FATAL - exception in exception handler of task}} and
> exits with code 1. With {{'sink.buffer-flush.interval' = '0'}} and
> {{'sink.buffer-flush.max-rows' = '100'}} the flush runs on the task thread
> and the cause is the NullPointerException above.
> h3. Proposed change
> * {{JdbcOutputFormat#flush}}: prepare the statements again at the start of
> the next attempt, after the backoff, so a refused reconnect is a failed
> attempt and is retried.
> * {{JdbcOutputFormat#updateExecutor}}: connect before closing the old
> statements.
> * {{JdbcOutputFormat#close}}: release the statements and the connection. Do
> not flush and do not rethrow a flush failure. This also closes the connection
> in the cases of FLINK-30371 and FLINK-32659.
> * {{GenericJdbcSinkFunction}}: flush in {{finish()}}, so a bounded job still
> writes its last batch. {{JdbcWriter}} already flushes in
> {{flush(endOfInput)}}.
> * {{JdbcWriter#close}}: do not rethrow the flush failure in exactly-once
> mode. Today that also skips {{jdbcTransaction.close()}}.
> With these changes the TaskManager stays up through a 40 s outage and the job
> continues once PostgreSQL is back. With {{sink.max-retries = 10}} the sink
> rides out a 20 s outage without a restart.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)