@Internal public class JdbcXaSinkFunction<T> extends org.apache.flink.api.common.functions.AbstractRichFunction implements org.apache.flink.streaming.api.checkpoint.CheckpointedFunction, org.apache.flink.api.common.state.CheckpointListener, org.apache.flink.streaming.api.functions.sink.SinkFunction<T>, AutoCloseable, org.apache.flink.api.java.typeutils.InputTypeConfigurable
Each parallel subtask has it's own transactions, independent from other subtasks. Therefore, consistency is only guaranteed within partitions.
XA uses a two-phase commit protocol, which solves the consistency problem, but leaves the following issues:
The following table summarizes effects of failures during transaction state transitions and ways to mitigate them:
| Transition | Methods | What happens if transition lost | Ways to mitigate |
|---|---|---|---|
| none > started, started > ended | open(), snapshotState() | Database eventually discards these transactions |
|
| ended > prepared | snapshotState() | Database keeps these transactions prepared forever ("in-doubt" state) |
|
| prepared > committed | open(), notifyCheckpointComplete() |
Upon job recovery state contains committed transactions; or JM may notifyCheckpointComplete again after recovery.
Committing results in |
Distinguish between transactions created during this run and restored from state and ignore XAER_NOTA for the latter.
|
| Constructor and Description |
|---|
JdbcXaSinkFunction(JdbcOutputFormat<T,T,JdbcBatchStatementExecutor<T>> outputFormat,
XaFacade xaFacade,
XidGenerator xidGenerator,
org.apache.flink.connector.jdbc.xa.XaSinkStateHandler stateHandler,
JdbcExactlyOnceOptions options,
org.apache.flink.connector.jdbc.xa.XaGroupOps xaGroupOps)
Creates a
JdbcXaSinkFunction. |
JdbcXaSinkFunction(String sql,
JdbcStatementBuilder<T> statementBuilder,
XaFacade xaFacade,
JdbcExecutionOptions executionOptions,
JdbcExactlyOnceOptions options)
Creates a
JdbcXaSinkFunction. |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
void |
initializeState(org.apache.flink.runtime.state.FunctionInitializationContext context) |
void |
invoke(T value,
org.apache.flink.streaming.api.functions.sink.SinkFunction.Context context) |
void |
notifyCheckpointComplete(long checkpointId) |
void |
open(org.apache.flink.configuration.Configuration configuration) |
void |
setInputType(org.apache.flink.api.common.typeinfo.TypeInformation<?> type,
org.apache.flink.api.common.ExecutionConfig executionConfig) |
void |
snapshotState(org.apache.flink.runtime.state.FunctionSnapshotContext context) |
getIterationRuntimeContext, getRuntimeContext, setRuntimeContextclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitpublic JdbcXaSinkFunction(String sql, JdbcStatementBuilder<T> statementBuilder, XaFacade xaFacade, JdbcExecutionOptions executionOptions, JdbcExactlyOnceOptions options)
JdbcXaSinkFunction.
All parameters must be serializable.
Note: JdbcExecutionOptions maxRetries setting must be strictly set to 0 for this
sink to work properly and not to produce duplicates. See issue FLINK-22311 for details.
xaFacade - XaFacade to manage XA transactionspublic JdbcXaSinkFunction(JdbcOutputFormat<T,T,JdbcBatchStatementExecutor<T>> outputFormat, XaFacade xaFacade, XidGenerator xidGenerator, org.apache.flink.connector.jdbc.xa.XaSinkStateHandler stateHandler, JdbcExactlyOnceOptions options, org.apache.flink.connector.jdbc.xa.XaGroupOps xaGroupOps)
JdbcXaSinkFunction.
All parameters must be serializable.
outputFormat - JdbcOutputFormat to write records withxaFacade - XaFacade to manage XA transactionsxidGenerator - XidGenerator to generate new transaction idspublic void initializeState(org.apache.flink.runtime.state.FunctionInitializationContext context)
throws Exception
initializeState in interface org.apache.flink.streaming.api.checkpoint.CheckpointedFunctionExceptionpublic void open(org.apache.flink.configuration.Configuration configuration)
throws Exception
open in interface org.apache.flink.api.common.functions.RichFunctionopen in class org.apache.flink.api.common.functions.AbstractRichFunctionExceptionpublic void snapshotState(org.apache.flink.runtime.state.FunctionSnapshotContext context)
throws Exception
snapshotState in interface org.apache.flink.streaming.api.checkpoint.CheckpointedFunctionExceptionpublic void notifyCheckpointComplete(long checkpointId)
notifyCheckpointComplete in interface org.apache.flink.api.common.state.CheckpointListenerpublic void invoke(T value, org.apache.flink.streaming.api.functions.sink.SinkFunction.Context context) throws IOException
invoke in interface org.apache.flink.streaming.api.functions.sink.SinkFunction<T>IOExceptionpublic void close()
throws Exception
close in interface AutoCloseableclose in interface org.apache.flink.api.common.functions.RichFunctionclose in class org.apache.flink.api.common.functions.AbstractRichFunctionExceptionpublic void setInputType(org.apache.flink.api.common.typeinfo.TypeInformation<?> type,
org.apache.flink.api.common.ExecutionConfig executionConfig)
setInputType in interface org.apache.flink.api.java.typeutils.InputTypeConfigurableCopyright © 2022–2024 The Apache Software Foundation. All rights reserved.