Interface KeyedStateBackend<K>

Type Parameters:
K - The key by which state is keyed.
All Superinterfaces:
org.apache.flink.util.Disposable, KeyedStateFactory, PriorityQueueSetFactory
All Known Subinterfaces:
CheckpointableKeyedStateBackend<K>, TestableKeyedStateBackend<K>
All Known Implementing Classes:
AbstractKeyedStateBackend, BatchExecutionKeyedStateBackend, HeapKeyedStateBackend

public interface KeyedStateBackend<K> extends KeyedStateFactory, PriorityQueueSetFactory, org.apache.flink.util.Disposable
A keyed state backend provides methods for managing keyed state.
  • Method Details

    • setCurrentKey

      void setCurrentKey(K newKey)
      Sets the current key that is used for partitioned state.
      Parameters:
      newKey - The new current key.
    • getCurrentKey

      K getCurrentKey()
      Returns:
      Current key.
    • setCurrentKeyAndKeyGroup

      void setCurrentKeyAndKeyGroup(K newKey, int newKeyGroupIndex)
      Act as a fast path for setCurrentKey(K) when the key group is known.
    • getKeySerializer

      org.apache.flink.api.common.typeutils.TypeSerializer<K> getKeySerializer()
      Returns:
      Serializer of the key.
    • applyToAllKeys

      <N, S extends org.apache.flink.api.common.state.State, T> void applyToAllKeys(N namespace, org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, org.apache.flink.api.common.state.StateDescriptor<S,T> stateDescriptor, KeyedStateFunction<K,S> function) throws Exception
      Applies the provided KeyedStateFunction to the state with the provided StateDescriptor of all the currently active keys.
      Type Parameters:
      N - The type of the namespace.
      S - The type of the state.
      Parameters:
      namespace - the namespace of the state.
      namespaceSerializer - the serializer for the namespace.
      stateDescriptor - the descriptor of the state to which the function is going to be applied.
      function - the function to be applied to the keyed state.
      Throws:
      Exception
    • getKeys

      <N> Stream<K> getKeys(String state, N namespace)
      Parameters:
      state - State variable for which existing keys will be returned.
      namespace - Namespace for which existing keys will be returned.
      Returns:
      A stream of all keys for the given state and namespace. Modifications to the state during iterating over it keys are not supported.
    • getKeysAndNamespaces

      <N> Stream<org.apache.flink.api.java.tuple.Tuple2<K,N>> getKeysAndNamespaces(String state)
      Parameters:
      state - State variable for which existing keys will be returned.
      Returns:
      A stream of all keys for the given state and namespace. Modifications to the state during iterating over it keys are not supported. Implementations go not make any ordering guarantees about the returned tupes. Two records with the same key or namespace may not be returned near each other in the stream.
    • getOrCreateKeyedState

      <N, S extends org.apache.flink.api.common.state.State, T> S getOrCreateKeyedState(org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, org.apache.flink.api.common.state.StateDescriptor<S,T> stateDescriptor) throws Exception
      Creates or retrieves a keyed state backed by this state backend.
      Type Parameters:
      N - The type of the namespace.
      S - The type of the state.
      Parameters:
      namespaceSerializer - The serializer used for the namespace type of the state
      stateDescriptor - The identifier for the state. This contains name and can create a default state value.
      Returns:
      A new key/value state backed by this backend.
      Throws:
      Exception - Exceptions may occur during initialization of the state and should be forwarded.
    • getPartitionedState

      <N, S extends org.apache.flink.api.common.state.State> S getPartitionedState(N namespace, org.apache.flink.api.common.typeutils.TypeSerializer<N> namespaceSerializer, org.apache.flink.api.common.state.StateDescriptor<S,?> stateDescriptor) throws Exception
      Creates or retrieves a partitioned state backed by this state backend.

      TODO: NOTE: This method does a lot of work caching / retrieving states just to update the namespace. This method should be removed for the sake of namespaces being lazily fetched from the keyed state backend, or being set on the state directly.

      Type Parameters:
      N - The type of the namespace.
      S - The type of the state.
      Parameters:
      stateDescriptor - The identifier for the state. This contains name and can create a default state value.
      Returns:
      A new key/value state backed by this backend.
      Throws:
      Exception - Exceptions may occur during initialization of the state and should be forwarded.
    • dispose

      void dispose()
      Specified by:
      dispose in interface org.apache.flink.util.Disposable
    • registerKeySelectionListener

      void registerKeySelectionListener(KeyedStateBackend.KeySelectionListener<K> listener)
      State backend will call KeyedStateBackend.KeySelectionListener.keySelected(K) when key context is switched if supported.
    • deregisterKeySelectionListener

      boolean deregisterKeySelectionListener(KeyedStateBackend.KeySelectionListener<K> listener)
      Returns:
      returns true iff listener was registered before.
    • isSafeToReuseKVState

      default boolean isSafeToReuseKVState()
      Whether it's safe to reuse key-values from the state-backend, e.g for the purpose of optimization.

      NOTE: this method should not be used to check for InternalPriorityQueue, as the priority queue could be stored on different locations, e.g RocksDB state-backend could store that on JVM heap if configuring HEAP as the time-service factory.

      Returns:
      returns ture if safe to reuse the key-values from the state-backend.