Class AbstractAsyncStateStreamOperatorV2<OUT>

java.lang.Object
org.apache.flink.streaming.api.operators.AbstractStreamOperatorV2<OUT>
org.apache.flink.runtime.asyncprocessing.operators.AbstractAsyncStateStreamOperatorV2<OUT>
All Implemented Interfaces:
Serializable, org.apache.flink.api.common.state.CheckpointListener, KeyContext, StreamOperator<OUT>, StreamOperatorStateHandler.CheckpointedStreamOperator, AsyncStateProcessing, AsyncStateProcessingOperator

@Internal public abstract class AbstractAsyncStateStreamOperatorV2<OUT> extends AbstractStreamOperatorV2<OUT> implements AsyncStateProcessingOperator
This operator is an abstract class that give the AbstractStreamOperatorV2 the ability to perform AsyncStateProcessing. The aim is to make any subclass of AbstractStreamOperatorV2 could manipulate async state with only a change of base class.
See Also:
  • Field Details

  • Constructor Details

    • AbstractAsyncStateStreamOperatorV2

      public AbstractAsyncStateStreamOperatorV2(StreamOperatorParameters<OUT> parameters, int numberOfInputs)
  • Method Details

    • beforeInitializeStateHandler

      public final void beforeInitializeStateHandler()
      Initialize necessary state components for AbstractStreamOperatorV2.
      Overrides:
      beforeInitializeStateHandler in class AbstractStreamOperatorV2<OUT>
    • isAsyncStateProcessingEnabled

      public boolean isAsyncStateProcessingEnabled()
      Description copied from class: AbstractStreamOperatorV2
      Indicates whether this operator is enabling the async state. Can be overridden by subclasses.
      Specified by:
      isAsyncStateProcessingEnabled in interface AsyncStateProcessing
      Overrides:
      isAsyncStateProcessingEnabled in class AbstractStreamOperatorV2<OUT>
      Returns:
      ture if async state processing is enabled.
    • getElementOrder

      public ElementOrder getElementOrder()
      Description copied from interface: AsyncStateProcessingOperator
      Get the ElementOrder of this operator.
      Specified by:
      getElementOrder in interface AsyncStateProcessingOperator
    • setAsyncKeyedContextElement

      public final <T> void setAsyncKeyedContextElement(StreamRecord<T> record, org.apache.flink.api.java.functions.KeySelector<T,?> keySelector) throws Exception
      Description copied from interface: AsyncStateProcessingOperator
      Set key context for async state processing.
      Specified by:
      setAsyncKeyedContextElement in interface AsyncStateProcessingOperator
      Type Parameters:
      T - the type of the record.
      Parameters:
      record - the record.
      keySelector - the key selector to select a key from record.
      Throws:
      Exception
    • newKeySelected

      public void newKeySelected(Object newKey)
      A hook that will be invoked after a new key is selected. It is not recommended to perform async state here. Only some synchronous logic is suggested.
      Parameters:
      newKey - the new key selected.
    • internalSetKeyContextElement

      protected <T> void internalSetKeyContextElement(StreamRecord<T> record, org.apache.flink.api.java.functions.KeySelector<T,?> selector) throws Exception
      Overrides:
      internalSetKeyContextElement in class AbstractStreamOperatorV2<OUT>
      Throws:
      Exception
    • getCurrentKey

      public Object getCurrentKey()
      Specified by:
      getCurrentKey in interface KeyContext
      Overrides:
      getCurrentKey in class AbstractStreamOperatorV2<OUT>
    • postProcessElement

      public final void postProcessElement()
      Description copied from interface: AsyncStateProcessingOperator
      A callback that will be triggered after an element finishes processElement.
      Specified by:
      postProcessElement in interface AsyncStateProcessingOperator
    • preserveRecordOrderAndProcess

      public final void preserveRecordOrderAndProcess(org.apache.flink.util.function.ThrowingRunnable<Exception> processing)
      Description copied from interface: AsyncStateProcessingOperator
      Check the order of same-key record, and then process the record. Mainly used when the AsyncStateProcessingOperator.getElementOrder() returns ElementOrder.RECORD_ORDER.
      Specified by:
      preserveRecordOrderAndProcess in interface AsyncStateProcessingOperator
      Parameters:
      processing - the record processing logic.
    • asyncProcessWithKey

      public <K> void asyncProcessWithKey(K key, org.apache.flink.util.function.ThrowingRunnable<Exception> processing)
      Description copied from interface: AsyncStateProcessingOperator
      Asynchronously process a code with a key provided.
      Specified by:
      asyncProcessWithKey in interface AsyncStateProcessingOperator
      Type Parameters:
      K - the type of key.
      Parameters:
      key - the specified key.
      processing - the process logic.
    • getDeclarationManager

      public final DeclarationManager getDeclarationManager()
      Description copied from interface: AsyncStateProcessingOperator
      Get the declaration manager for user-logic declaring.
      Specified by:
      getDeclarationManager in interface AsyncStateProcessingOperator
    • getRecordProcessor

      public final <T> org.apache.flink.util.function.ThrowingConsumer<StreamRecord<T>,Exception> getRecordProcessor(int inputId)
      Description copied from interface: AsyncStateProcessing
      Get the record processor that could process record from input, which is the only entry for async processing.
      Specified by:
      getRecordProcessor in interface AsyncStateProcessing
      Parameters:
      inputId - the input identifier, start from 1. Borrow the design from org.apache.flink.streaming.api.operators.AbstractInput#inputId. This is only relevant if there is multiple inputs for the instance.
    • getOrCreateKeyedState

      protected <N, S extends org.apache.flink.api.common.state.v2.State, T> S getOrCreateKeyedState(@Nonnull N defaultNamespace, @Nonnull org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, @Nonnull org.apache.flink.api.common.state.v2.StateDescriptor<T> stateDescriptor) throws Exception
      Create new state (v2) based on new state descriptor.
      Throws:
      Exception
    • prepareSnapshotPreBarrier

      public void prepareSnapshotPreBarrier(long checkpointId) throws Exception
      Description copied from interface: StreamOperator
      This method is called when the operator should do a snapshot, before it emits its own checkpoint barrier.

      This method is intended not for any actual state persistence, but only for emitting some data before emitting the checkpoint barrier. Operators that maintain some small transient state that is inefficient to checkpoint (especially when it would need to be checkpointed in a re-scalable way) but can simply be sent downstream before the checkpoint. An example are opportunistic pre-aggregation operators, which have small the pre-aggregation state that is frequently flushed downstream.

      Important: This method should not be used for any actual state snapshot logic, because it will inherently be within the synchronous part of the operator's checkpoint. If heavy work is done within this method, it will affect latency and downstream checkpoint alignments.

      Specified by:
      prepareSnapshotPreBarrier in interface StreamOperator<OUT>
      Overrides:
      prepareSnapshotPreBarrier in class AbstractStreamOperatorV2<OUT>
      Parameters:
      checkpointId - The ID of the checkpoint.
      Throws:
      Exception - Throwing an exception here causes the operator to fail and go into recovery.
    • getInternalTimerService

      public <K, N> InternalTimerService<N> getInternalTimerService(String name, org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, Triggerable<K,N> triggerable)
      Description copied from class: AbstractStreamOperatorV2
      Returns a InternalTimerService that can be used to query current processing time and event time and to set timers. An operator can have several timer services, where each has its own namespace serializer. Timer services are differentiated by the string key that is given when requesting them, if you call this method with the same key multiple times you will get the same timer service instance in subsequent requests.

      Timers are always scoped to a key, the currently active key of a keyed stream operation. When a timer fires, this key will also be set as the currently active key.

      Each timer has attached metadata, the namespace. Different timer services can have a different namespace type. If you don't need namespace differentiation you can use VoidNamespaceSerializer as the namespace serializer.

      Overrides:
      getInternalTimerService in class AbstractStreamOperatorV2<OUT>
      N - The type of the timer namespace.
      Parameters:
      name - The name of the requested timer service. If no service exists under the given name a new one will be created and returned.
      namespaceSerializer - TypeSerializer for the timer namespace.
      triggerable - The Triggerable that should be invoked when timers fire
    • reportOrForwardLatencyMarker

      protected void reportOrForwardLatencyMarker(LatencyMarker marker)
      Overrides:
      reportOrForwardLatencyMarker in class AbstractStreamOperatorV2<OUT>
    • preProcessWatermark

      public Watermark preProcessWatermark(Watermark watermark) throws Exception
      A hook that will be triggered when receiving a watermark. Some async state can safely go within this method. Return the watermark that should be normally processed.
      Parameters:
      watermark - the receiving watermark.
      Returns:
      the watermark that should be processed. Null if there is no need for following processing.
      Throws:
      Exception
    • postProcessWatermark

      public void postProcessWatermark(Watermark watermark) throws Exception
      A hook that will be invoked after finishing advancing the watermark. It is not recommended to perform async state here. Only some synchronous logic is suggested.
      Parameters:
      watermark - the advanced watermark.
      Throws:
      Exception
    • processWatermark

      public final void processWatermark(Watermark mark) throws Exception
      Process a watermark when receiving it. Do not override this method since the async processing is difficult to write. Please override the hooks, see preProcessWatermark(Watermark) and postProcessWatermark(Watermark). The basic logic of processWatermark with hooks in sync form would be:
                   Watermark watermark = preProcessWatermark(mark);
                   if (watermark != null) {
                       super.processWatermark(watermark);
                       postProcessWatermark(watermark);
                   }
       
      Overrides:
      processWatermark in class AbstractStreamOperatorV2<OUT>
      Throws:
      Exception
    • processWatermarkStatus

      public void processWatermarkStatus(WatermarkStatus watermarkStatus, int inputId) throws Exception
      Overrides:
      processWatermarkStatus in class AbstractStreamOperatorV2<OUT>
      Throws:
      Exception
    • processRecordAttributes

      public void processRecordAttributes(RecordAttributes recordAttributes, int inputId) throws Exception
      Overrides:
      processRecordAttributes in class AbstractStreamOperatorV2<OUT>
      Throws:
      Exception
    • getAsyncExecutionController

      @VisibleForTesting public AsyncExecutionController<?> getAsyncExecutionController()
    • getCurrentProcessingContext

      @VisibleForTesting public RecordContext getCurrentProcessingContext()
    • finish

      public void finish() throws Exception
      Description copied from interface: StreamOperator
      This method is called at the end of data processing.

      The method is expected to flush all remaining buffered data. Exceptions during this flushing of buffered data should be propagated, in order to cause the operation to be recognized as failed, because the last data items are not processed properly.

      After this method is called, no more records can be produced for the downstream operators.

      WARNING: It is not safe to use this method to commit any transactions or other side effects! You can use this method to flush any buffered data that can later on be committed e.g. in a CheckpointListener.notifyCheckpointComplete(long).

      NOTE:This method does not need to close any resources. You should release external resources in the StreamOperator.close() method.

      Specified by:
      finish in interface StreamOperator<OUT>
      Overrides:
      finish in class AbstractStreamOperatorV2<OUT>
      Throws:
      Exception - An exception in this method causes the operator to fail.
    • close

      public void close() throws Exception
      Description copied from interface: StreamOperator
      This method is called at the very end of the operator's life, both in the case of a successful completion of the operation, and in the case of a failure and canceling.

      This method is expected to make a thorough effort to release all resources that the operator has acquired.

      NOTE:It can not emit any records! If you need to emit records at the end of processing, do so in the StreamOperator.finish() method.

      Specified by:
      close in interface StreamOperator<OUT>
      Overrides:
      close in class AbstractStreamOperatorV2<OUT>
      Throws:
      Exception