Package org.apache.flink.runtime.state
Interface KeyedStateBackend<K>
-
- Type Parameters:
K
- The key by which state is keyed.
- All Superinterfaces:
Disposable
,KeyedStateFactory
,PriorityQueueSetFactory
- All Known Subinterfaces:
CheckpointableKeyedStateBackend<K>
,TestableKeyedStateBackend<K>
- All Known Implementing Classes:
AbstractKeyedStateBackend
,BatchExecutionKeyedStateBackend
,ChangelogKeyedStateBackend
,ForStSyncKeyedStateBackend
,HeapKeyedStateBackend
,RocksDBKeyedStateBackend
public interface KeyedStateBackend<K> extends KeyedStateFactory, PriorityQueueSetFactory, Disposable
A keyed state backend provides methods for managing keyed state.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
KeyedStateBackend.KeySelectionListener<K>
Listener is given a callback whensetCurrentKey(K)
is called (key context changes).
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description <N,S extends State,T>
voidapplyToAllKeys(N namespace, TypeSerializer<N> namespaceSerializer, StateDescriptor<S,T> stateDescriptor, KeyedStateFunction<K,S> function)
Applies the providedKeyedStateFunction
to the state with the providedStateDescriptor
of all the currently active keys.boolean
deregisterKeySelectionListener(KeyedStateBackend.KeySelectionListener<K> listener)
Stop calling listener registered inregisterKeySelectionListener(org.apache.flink.runtime.state.KeyedStateBackend.KeySelectionListener<K>)
.void
dispose()
Disposes the object and releases all resources.K
getCurrentKey()
<N> Stream<K>
getKeys(String state, N namespace)
<N> Stream<Tuple2<K,N>>
getKeysAndNamespaces(String state)
TypeSerializer<K>
getKeySerializer()
<N,S extends State,T>
SgetOrCreateKeyedState(TypeSerializer<N> namespaceSerializer, StateDescriptor<S,T> stateDescriptor)
Creates or retrieves a keyed state backed by this state backend.<N,S extends State>
SgetPartitionedState(N namespace, TypeSerializer<N> namespaceSerializer, StateDescriptor<S,?> stateDescriptor)
Creates or retrieves a partitioned state backed by this state backend.default boolean
isSafeToReuseKVState()
Whether it's safe to reuse key-values from the state-backend, e.g for the purpose of optimization.void
registerKeySelectionListener(KeyedStateBackend.KeySelectionListener<K> listener)
State backend will callKeyedStateBackend.KeySelectionListener.keySelected(K)
when key context is switched if supported.void
setCurrentKey(K newKey)
Sets the current key that is used for partitioned state.void
setCurrentKeyAndKeyGroup(K newKey, int newKeyGroupIndex)
Act as a fast path forsetCurrentKey(K)
when the key group is known.-
Methods inherited from interface org.apache.flink.runtime.state.KeyedStateFactory
createOrUpdateInternalState, createOrUpdateInternalState, createOrUpdateInternalState
-
Methods inherited from interface org.apache.flink.runtime.state.PriorityQueueSetFactory
create, create
-
-
-
-
Method Detail
-
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 forsetCurrentKey(K)
when the key group is known.
-
getKeySerializer
TypeSerializer<K> getKeySerializer()
- Returns:
- Serializer of the key.
-
applyToAllKeys
<N,S extends State,T> void applyToAllKeys(N namespace, TypeSerializer<N> namespaceSerializer, StateDescriptor<S,T> stateDescriptor, KeyedStateFunction<K,S> function) throws Exception
Applies the providedKeyedStateFunction
to the state with the providedStateDescriptor
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<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 State,T> S getOrCreateKeyedState(TypeSerializer<N> namespaceSerializer, 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 statestateDescriptor
- 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 State> S getPartitionedState(N namespace, TypeSerializer<N> namespaceSerializer, 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()
Description copied from interface:Disposable
Disposes the object and releases all resources. After calling this method, calling any methods on the object may result in undefined behavior.- Specified by:
dispose
in interfaceDisposable
-
registerKeySelectionListener
void registerKeySelectionListener(KeyedStateBackend.KeySelectionListener<K> listener)
State backend will callKeyedStateBackend.KeySelectionListener.keySelected(K)
when key context is switched if supported.
-
deregisterKeySelectionListener
boolean deregisterKeySelectionListener(KeyedStateBackend.KeySelectionListener<K> listener)
Stop calling listener registered inregisterKeySelectionListener(org.apache.flink.runtime.state.KeyedStateBackend.KeySelectionListener<K>)
.- 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.
-
-