Class JdbcXaSinkFunction<T>
- java.lang.Object
-
- org.apache.flink.api.common.functions.AbstractRichFunction
-
- org.apache.flink.connector.jdbc.xa.JdbcXaSinkFunction<T>
-
- All Implemented Interfaces:
Serializable,AutoCloseable,org.apache.flink.api.common.functions.Function,org.apache.flink.api.common.functions.RichFunction,org.apache.flink.api.common.state.CheckpointListener,org.apache.flink.api.java.typeutils.InputTypeConfigurable,org.apache.flink.streaming.api.checkpoint.CheckpointedFunction,org.apache.flink.streaming.api.functions.sink.SinkFunction<T>
@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
JDBC sink function that uses XA transactions to provide exactly once guarantees. That is, if a checkpoint succeeds then all records emitted during it are committed in the database, and rolled back otherwise.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:
- transactions may be abandoned, holding resources (e.g. locks, versions of rows)
- abandoned transactions collide with the new transactions if their IDs repeat after recovery
- commit requests may be repeated after job recovery, resulting in error responses and job failure
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 - Use globally unique XIDs
- derive XID from: checkpoint id, subtask id, "job id", "run id" (see
SemanticXidGenerator).
ended > prepared snapshotState() Database keeps these transactions prepared forever ("in-doubt" state) - store ended transactions in state; rollback on job recovery (still doesn't cover all scenarios)
- call xa_recover() and xa_rollback() on job recovery; disabled by default in order not to affect transactions of other subtasks and apps
- setting transaction timeouts (not supported by most databases)
- manual recovery and rollback
prepared > committed open(), notifyCheckpointComplete() Upon job recovery state contains committed transactions; or JM may notifyCheckpointComplete again after recovery. Committing results in
XAER_NOTAerror.Distinguish between transactions created during this run and restored from state and ignore XAER_NOTAfor the latter.- Since:
- 1.13
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description JdbcXaSinkFunction(String sql, JdbcStatementBuilder<T> statementBuilder, XaFacade xaFacade, JdbcExecutionOptions executionOptions, JdbcExactlyOnceOptions options)Creates aJdbcXaSinkFunction.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 aJdbcXaSinkFunction.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidinitializeState(org.apache.flink.runtime.state.FunctionInitializationContext context)voidinvoke(T value, org.apache.flink.streaming.api.functions.sink.SinkFunction.Context context)voidnotifyCheckpointComplete(long checkpointId)voidopen(org.apache.flink.configuration.Configuration configuration)voidsetInputType(org.apache.flink.api.common.typeinfo.TypeInformation<?> type, org.apache.flink.api.common.ExecutionConfig executionConfig)voidsnapshotState(org.apache.flink.runtime.state.FunctionSnapshotContext context)-
Methods inherited from class org.apache.flink.api.common.functions.AbstractRichFunction
getIterationRuntimeContext, getRuntimeContext, setRuntimeContext
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
Constructor Detail
-
JdbcXaSinkFunction
public JdbcXaSinkFunction(String sql, JdbcStatementBuilder<T> statementBuilder, XaFacade xaFacade, JdbcExecutionOptions executionOptions, JdbcExactlyOnceOptions options)
Creates aJdbcXaSinkFunction.All parameters must be
serializable.Note:
JdbcExecutionOptionsmaxRetries setting must be strictly set to 0 for this sink to work properly and not to produce duplicates. See issue FLINK-22311 for details.- Parameters:
xaFacade-XaFacadeto manage XA transactions
-
JdbcXaSinkFunction
public 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 aJdbcXaSinkFunction.All parameters must be
serializable.- Parameters:
outputFormat-JdbcOutputFormatto write records withxaFacade-XaFacadeto manage XA transactionsxidGenerator-XidGeneratorto generate new transaction ids
-
-
Method Detail
-
initializeState
public void initializeState(org.apache.flink.runtime.state.FunctionInitializationContext context) throws Exception- Specified by:
initializeStatein interfaceorg.apache.flink.streaming.api.checkpoint.CheckpointedFunction- Throws:
Exception
-
open
public void open(org.apache.flink.configuration.Configuration configuration) throws Exception- Specified by:
openin interfaceorg.apache.flink.api.common.functions.RichFunction- Overrides:
openin classorg.apache.flink.api.common.functions.AbstractRichFunction- Throws:
Exception
-
snapshotState
public void snapshotState(org.apache.flink.runtime.state.FunctionSnapshotContext context) throws Exception- Specified by:
snapshotStatein interfaceorg.apache.flink.streaming.api.checkpoint.CheckpointedFunction- Throws:
Exception
-
notifyCheckpointComplete
public void notifyCheckpointComplete(long checkpointId)
- Specified by:
notifyCheckpointCompletein interfaceorg.apache.flink.api.common.state.CheckpointListener
-
invoke
public void invoke(T value, org.apache.flink.streaming.api.functions.sink.SinkFunction.Context context) throws IOException
- Specified by:
invokein interfaceorg.apache.flink.streaming.api.functions.sink.SinkFunction<T>- Throws:
IOException
-
close
public void close() throws Exception- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceorg.apache.flink.api.common.functions.RichFunction- Overrides:
closein classorg.apache.flink.api.common.functions.AbstractRichFunction- Throws:
Exception
-
setInputType
public void setInputType(org.apache.flink.api.common.typeinfo.TypeInformation<?> type, org.apache.flink.api.common.ExecutionConfig executionConfig)- Specified by:
setInputTypein interfaceorg.apache.flink.api.java.typeutils.InputTypeConfigurable
-
-