Class TwoPhaseCommitSinkFunction<IN,TXN,CONTEXT>
- java.lang.Object
-
- org.apache.flink.api.common.functions.AbstractRichFunction
-
- org.apache.flink.streaming.api.functions.sink.legacy.RichSinkFunction<IN>
-
- org.apache.flink.streaming.api.functions.sink.legacy.TwoPhaseCommitSinkFunction<IN,TXN,CONTEXT>
-
- Type Parameters:
IN- Input type forSinkFunction.TXN- Transaction to store all of the information required to handle a transaction.CONTEXT- Context that will be shared across all invocations for the givenTwoPhaseCommitSinkFunctioninstance. Context is created once
- All Implemented Interfaces:
Serializable,org.apache.flink.api.common.functions.Function,org.apache.flink.api.common.functions.RichFunction,org.apache.flink.api.common.state.CheckpointListener,org.apache.flink.streaming.api.checkpoint.CheckpointedFunction,org.apache.flink.streaming.api.functions.sink.legacy.SinkFunction<IN>
@Internal public abstract class TwoPhaseCommitSinkFunction<IN,TXN,CONTEXT> extends org.apache.flink.streaming.api.functions.sink.legacy.RichSinkFunction<IN> implements org.apache.flink.streaming.api.checkpoint.CheckpointedFunction, org.apache.flink.api.common.state.CheckpointListenerDeprecated.This interface will be removed in future versions. Use the newSinkinterface instead.This is a recommended base class for all of theSinkFunctionthat intend to implement exactly-once semantic. It does that by implementing two phase commit algorithm on top of theCheckpointedFunctionandCheckpointListener. User should provide customTXN(transaction handle) and implement abstract methods handling this transaction handle.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTwoPhaseCommitSinkFunction.State<TXN,CONTEXT>Deprecated.State POJO class coupling pendingTransaction, context and pendingCommitTransactions.static classTwoPhaseCommitSinkFunction.StateSerializer<TXN,CONTEXT>Deprecated.CustomTypeSerializerfor the sink state.static classTwoPhaseCommitSinkFunction.StateSerializerSnapshot<TXN,CONTEXT>Deprecated.Snapshot for theTwoPhaseCommitSinkFunction.StateSerializer.static classTwoPhaseCommitSinkFunction.TransactionHolder<TXN>Deprecated.Adds metadata (currently only the start time of the transaction) to the transaction object.
-
Field Summary
Fields Modifier and Type Field Description protected LinkedHashMap<Long,TwoPhaseCommitSinkFunction.TransactionHolder<TXN>>pendingCommitTransactionsDeprecated.protected org.apache.flink.api.common.state.ListState<TwoPhaseCommitSinkFunction.State<TXN,CONTEXT>>stateDeprecated.protected Optional<CONTEXT>userContextDeprecated.
-
Constructor Summary
Constructors Constructor Description TwoPhaseCommitSinkFunction(org.apache.flink.api.common.typeutils.TypeSerializer<TXN> transactionSerializer, org.apache.flink.api.common.typeutils.TypeSerializer<CONTEXT> contextSerializer)Deprecated.Use defaultListStateDescriptorfor internal state serialization.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected abstract voidabort(TXN transaction)Deprecated.Abort a transaction.protected abstract TXNbeginTransaction()Deprecated.Method that starts a new transaction.voidclose()Deprecated.protected abstract voidcommit(TXN transaction)Deprecated.Commit a pre-committed transaction.protected TXNcurrentTransaction()Deprecated.protected TwoPhaseCommitSinkFunction<IN,TXN,CONTEXT>enableTransactionTimeoutWarnings(double warningRatio)Deprecated.Enables logging of warnings if a transaction's elapsed time reaches a specified ratio of thetransactionTimeout.voidfinish()Deprecated.protected voidfinishProcessing(TXN transaction)Deprecated.This method is called at the end of data processing.protected voidfinishRecoveringContext(Collection<TXN> handledTransactions)Deprecated.Callback for subclasses which is called after restoring (each) user context.protected Optional<CONTEXT>getUserContext()Deprecated.protected TwoPhaseCommitSinkFunction<IN,TXN,CONTEXT>ignoreFailuresAfterTransactionTimeout()Deprecated.If called, the sink will only log but not propagate exceptions thrown inrecoverAndCommit(Object)if the transaction is older than a specified transaction timeout.voidinitializeState(org.apache.flink.runtime.state.FunctionInitializationContext context)Deprecated.protected Optional<CONTEXT>initializeUserContext()Deprecated.voidinvoke(IN value)Deprecated.This should not be implemented by subclasses.voidinvoke(IN value, org.apache.flink.streaming.api.functions.sink.legacy.SinkFunction.Context context)Deprecated.protected abstract voidinvoke(TXN transaction, IN value, org.apache.flink.streaming.api.functions.sink.legacy.SinkFunction.Context context)Deprecated.Write value within a transaction.voidnotifyCheckpointAborted(long checkpointId)Deprecated.voidnotifyCheckpointComplete(long checkpointId)Deprecated.protected Stream<Map.Entry<Long,TXN>>pendingTransactions()Deprecated.protected abstract voidpreCommit(TXN transaction)Deprecated.Pre commit previously created transaction.protected voidrecoverAndAbort(TXN transaction)Deprecated.Abort a transaction that was rejected by a coordinator after a failure.protected voidrecoverAndCommit(TXN transaction)Deprecated.Invoked on recovered transactions after a failure.protected TwoPhaseCommitSinkFunction<IN,TXN,CONTEXT>setTransactionTimeout(long transactionTimeout)Deprecated.Sets the transaction timeout.voidsnapshotState(org.apache.flink.runtime.state.FunctionSnapshotContext context)Deprecated.-
Methods inherited from class org.apache.flink.api.common.functions.AbstractRichFunction
getIterationRuntimeContext, getRuntimeContext, open, setRuntimeContext
-
-
-
-
Field Detail
-
pendingCommitTransactions
protected final LinkedHashMap<Long,TwoPhaseCommitSinkFunction.TransactionHolder<TXN>> pendingCommitTransactions
Deprecated.
-
state
protected transient org.apache.flink.api.common.state.ListState<TwoPhaseCommitSinkFunction.State<TXN,CONTEXT>> state
Deprecated.
-
-
Constructor Detail
-
TwoPhaseCommitSinkFunction
public TwoPhaseCommitSinkFunction(org.apache.flink.api.common.typeutils.TypeSerializer<TXN> transactionSerializer, org.apache.flink.api.common.typeutils.TypeSerializer<CONTEXT> contextSerializer)
Deprecated.Use defaultListStateDescriptorfor internal state serialization. Helpful utilities for using this constructor areTypeInformation.of(Class),TypeHintandTypeInformation.of(TypeHint). Example:TwoPhaseCommitSinkFunction(TypeInformation.of(new TypeHint<State<TXN, CONTEXT>>() {}));- Parameters:
transactionSerializer-TypeSerializerfor the transaction type of this sinkcontextSerializer-TypeSerializerfor the context type of this sink
-
-
Method Detail
-
currentTransaction
@Nullable protected TXN currentTransaction()
Deprecated.
-
pendingTransactions
@Nonnull protected Stream<Map.Entry<Long,TXN>> pendingTransactions()
Deprecated.
-
invoke
protected abstract void invoke(TXN transaction, IN value, org.apache.flink.streaming.api.functions.sink.legacy.SinkFunction.Context context) throws Exception
Deprecated.Write value within a transaction.- Throws:
Exception
-
beginTransaction
protected abstract TXN beginTransaction() throws Exception
Deprecated.Method that starts a new transaction.- Returns:
- newly created transaction.
- Throws:
Exception
-
preCommit
protected abstract void preCommit(TXN transaction) throws Exception
Deprecated.Pre commit previously created transaction. Pre commit must make all of the necessary steps to prepare the transaction for a commit that might happen in the future. After this point the transaction might still be aborted, but underlying implementation must ensure that commit calls on already pre committed transactions will always succeed.Usually implementation involves flushing the data.
- Throws:
Exception
-
commit
protected abstract void commit(TXN transaction)
Deprecated.Commit a pre-committed transaction. If this method fail, Flink application will be restarted andrecoverAndCommit(Object)will be called again for the same transaction.
-
recoverAndCommit
protected void recoverAndCommit(TXN transaction)
Deprecated.Invoked on recovered transactions after a failure. User implementation must ensure that this call will eventually succeed. If it fails, Flink application will be restarted and it will be invoked again. If it does not succeed eventually, a data loss will occur. Transactions will be recovered in an order in which they were created.
-
abort
protected abstract void abort(TXN transaction)
Deprecated.Abort a transaction.
-
recoverAndAbort
protected void recoverAndAbort(TXN transaction)
Deprecated.Abort a transaction that was rejected by a coordinator after a failure.
-
finishRecoveringContext
protected void finishRecoveringContext(Collection<TXN> handledTransactions)
Deprecated.Callback for subclasses which is called after restoring (each) user context.- Parameters:
handledTransactions- transactions which were already committed or aborted and do not need further handling
-
finishProcessing
protected void finishProcessing(@Nullable TXN transaction)Deprecated.This method is called at the end of data processing.The method is expected to flush all remaining buffered data. Exceptions will cause the pipeline to be recognized as failed, because the last data items are not processed properly. You may use this method to flush remaining buffered elements in the state into the current transaction which will be committed in the last checkpoint.
-
invoke
public final void invoke(IN value) throws Exception
Deprecated.This should not be implemented by subclasses.
-
invoke
public final void invoke(IN value, org.apache.flink.streaming.api.functions.sink.legacy.SinkFunction.Context context) throws Exception
Deprecated.
-
finish
public final void finish() throws ExceptionDeprecated.
-
notifyCheckpointComplete
public final void notifyCheckpointComplete(long checkpointId) throws ExceptionDeprecated.- Specified by:
notifyCheckpointCompletein interfaceorg.apache.flink.api.common.state.CheckpointListener- Throws:
Exception
-
notifyCheckpointAborted
public void notifyCheckpointAborted(long checkpointId)
Deprecated.- Specified by:
notifyCheckpointAbortedin interfaceorg.apache.flink.api.common.state.CheckpointListener
-
snapshotState
public void snapshotState(org.apache.flink.runtime.state.FunctionSnapshotContext context) throws ExceptionDeprecated.- Specified by:
snapshotStatein interfaceorg.apache.flink.streaming.api.checkpoint.CheckpointedFunction- Throws:
Exception
-
initializeState
public void initializeState(org.apache.flink.runtime.state.FunctionInitializationContext context) throws ExceptionDeprecated.- Specified by:
initializeStatein interfaceorg.apache.flink.streaming.api.checkpoint.CheckpointedFunction- Throws:
Exception
-
close
public void close() throws ExceptionDeprecated.- Specified by:
closein interfaceorg.apache.flink.api.common.functions.RichFunction- Overrides:
closein classorg.apache.flink.api.common.functions.AbstractRichFunction- Throws:
Exception
-
setTransactionTimeout
protected TwoPhaseCommitSinkFunction<IN,TXN,CONTEXT> setTransactionTimeout(long transactionTimeout)
Deprecated.Sets the transaction timeout. Setting only the transaction timeout has no effect in itself.- Parameters:
transactionTimeout- The transaction timeout in ms.- See Also:
ignoreFailuresAfterTransactionTimeout(),enableTransactionTimeoutWarnings(double)
-
ignoreFailuresAfterTransactionTimeout
protected TwoPhaseCommitSinkFunction<IN,TXN,CONTEXT> ignoreFailuresAfterTransactionTimeout()
Deprecated.If called, the sink will only log but not propagate exceptions thrown inrecoverAndCommit(Object)if the transaction is older than a specified transaction timeout. The start time of an transaction is determined bySystem.currentTimeMillis(). By default, failures are propagated.
-
enableTransactionTimeoutWarnings
protected TwoPhaseCommitSinkFunction<IN,TXN,CONTEXT> enableTransactionTimeoutWarnings(double warningRatio)
Deprecated.Enables logging of warnings if a transaction's elapsed time reaches a specified ratio of thetransactionTimeout. IfwarningRatiois 0, a warning will be always logged when committing the transaction.- Parameters:
warningRatio- A value in the range [0,1].- Returns:
-
-