Class SavepointRuntimeContext
- java.lang.Object
-
- org.apache.flink.state.api.runtime.SavepointRuntimeContext
-
- All Implemented Interfaces:
RuntimeContext
@Internal public final class SavepointRuntimeContext extends Object implements RuntimeContext
A streamingRuntimeContext
which delegates to the underlying batchRuntimeContext
along with a specifiedKeyedStateStore
.This
RuntimeContext
has the ability to force eager state registration by throwing an exception if state is registered outside of open.
-
-
Constructor Summary
Constructors Constructor Description SavepointRuntimeContext(RuntimeContext ctx, KeyedStateStore keyedStateStore)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <V,A extends Serializable>
voidaddAccumulator(String name, Accumulator<V,A> accumulator)
Add this accumulator.<T> TypeSerializer<T>
createSerializer(TypeInformation<T> typeInformation)
Create a serializer for a given type.void
disableStateRegistration()
<V,A extends Serializable>
Accumulator<V,A>getAccumulator(String name)
Get an existing accumulator object.<IN,ACC,OUT>
AggregatingState<IN,OUT>getAggregatingState(AggregatingStateDescriptor<IN,ACC,OUT> stateProperties)
Gets a handle to the system's key/value aggregating state.<RT> List<RT>
getBroadcastVariable(String name)
Returns the result bound to the broadcast variable identified by the givenname
.<T,C>
CgetBroadcastVariableWithInitializer(String name, BroadcastVariableInitializer<T,C> initializer)
Returns the result bound to the broadcast variable identified by the givenname
.DistributedCache
getDistributedCache()
Returns theDistributedCache
to get the local temporary file copies of files otherwise not locally accessible.DoubleCounter
getDoubleCounter(String name)
Convenience function to create a counter object for doubles.Set<ExternalResourceInfo>
getExternalResourceInfos(String resourceName)
Get the specific external resource information by the resourceName.Map<String,String>
getGlobalJobParameters()
Get global job parameters.Histogram
getHistogram(String name)
Convenience function to create a counter object for histograms.IntCounter
getIntCounter(String name)
Convenience function to create a counter object for integers.JobInfo
getJobInfo()
Get the meta information of current job.<T> ListState<T>
getListState(ListStateDescriptor<T> stateProperties)
Gets a handle to the system's key/value list state.LongCounter
getLongCounter(String name)
Convenience function to create a counter object for longs.<UK,UV>
MapState<UK,UV>getMapState(MapStateDescriptor<UK,UV> stateProperties)
Gets a handle to the system's key/value map state.OperatorMetricGroup
getMetricGroup()
Returns the metric group for this parallel subtask.<T> ReducingState<T>
getReducingState(ReducingStateDescriptor<T> stateProperties)
Gets a handle to the system's key/value reducing state.<T> ValueState<T>
getState(ValueStateDescriptor<T> stateProperties)
Gets a handle to the system's key/value state.List<StateDescriptor<?,?>>
getStateDescriptors()
TaskInfo
getTaskInfo()
Get the meta information of current task.ClassLoader
getUserCodeClassLoader()
Gets the ClassLoader to load classes that are not in system's classpath, but are part of the jar file of a user job.boolean
hasBroadcastVariable(String name)
Tests for the existence of the broadcast variable identified by the givenname
.boolean
isObjectReuseEnabled()
Check if object reuse is enabled.void
registerUserCodeClassLoaderReleaseHookIfAbsent(String releaseHookName, Runnable releaseHook)
Registers a custom hook for the user code class loader release.
-
-
-
Constructor Detail
-
SavepointRuntimeContext
public SavepointRuntimeContext(RuntimeContext ctx, KeyedStateStore keyedStateStore)
-
-
Method Detail
-
getJobInfo
public JobInfo getJobInfo()
Description copied from interface:RuntimeContext
Get the meta information of current job.- Specified by:
getJobInfo
in interfaceRuntimeContext
- Returns:
- the job meta information.
-
getTaskInfo
public TaskInfo getTaskInfo()
Description copied from interface:RuntimeContext
Get the meta information of current task.- Specified by:
getTaskInfo
in interfaceRuntimeContext
- Returns:
- the task meta information.
-
getMetricGroup
public OperatorMetricGroup getMetricGroup()
Description copied from interface:RuntimeContext
Returns the metric group for this parallel subtask.- Specified by:
getMetricGroup
in interfaceRuntimeContext
- Returns:
- The metric group for this parallel subtask.
-
createSerializer
public <T> TypeSerializer<T> createSerializer(TypeInformation<T> typeInformation)
Description copied from interface:RuntimeContext
Create a serializer for a given type.- Specified by:
createSerializer
in interfaceRuntimeContext
- Parameters:
typeInformation
- the type information of the object to be serialized- Returns:
- the serializer for the given type
-
getGlobalJobParameters
public Map<String,String> getGlobalJobParameters()
Description copied from interface:RuntimeContext
Get global job parameters.- Specified by:
getGlobalJobParameters
in interfaceRuntimeContext
- Returns:
- the global job parameters
-
isObjectReuseEnabled
public boolean isObjectReuseEnabled()
Description copied from interface:RuntimeContext
Check if object reuse is enabled.- Specified by:
isObjectReuseEnabled
in interfaceRuntimeContext
- Returns:
- true if object reuse is enabled, false otherwise
-
getUserCodeClassLoader
public ClassLoader getUserCodeClassLoader()
Description copied from interface:RuntimeContext
Gets the ClassLoader to load classes that are not in system's classpath, but are part of the jar file of a user job.- Specified by:
getUserCodeClassLoader
in interfaceRuntimeContext
- Returns:
- The ClassLoader for user code classes.
-
registerUserCodeClassLoaderReleaseHookIfAbsent
public void registerUserCodeClassLoaderReleaseHookIfAbsent(String releaseHookName, Runnable releaseHook)
Description copied from interface:RuntimeContext
Registers a custom hook for the user code class loader release.The release hook is executed just before the user code class loader is being released. Registration only happens if no hook has been registered under this name already.
- Specified by:
registerUserCodeClassLoaderReleaseHookIfAbsent
in interfaceRuntimeContext
- Parameters:
releaseHookName
- name of the release hook.releaseHook
- release hook which is executed just before the user code class loader is being released
-
addAccumulator
public <V,A extends Serializable> void addAccumulator(String name, Accumulator<V,A> accumulator)
Description copied from interface:RuntimeContext
Add this accumulator. Throws an exception if the accumulator already exists in the same Task. Note that the Accumulator name must have an unique name across the Flink job. Otherwise you will get an error when incompatible accumulators from different Tasks are combined at the JobManager upon job completion.- Specified by:
addAccumulator
in interfaceRuntimeContext
-
getAccumulator
public <V,A extends Serializable> Accumulator<V,A> getAccumulator(String name)
Description copied from interface:RuntimeContext
Get an existing accumulator object. The accumulator must have been added previously in this local runtime context.Throws an exception if the accumulator does not exist or if the accumulator exists, but with different type.
- Specified by:
getAccumulator
in interfaceRuntimeContext
-
getIntCounter
public IntCounter getIntCounter(String name)
Description copied from interface:RuntimeContext
Convenience function to create a counter object for integers.- Specified by:
getIntCounter
in interfaceRuntimeContext
-
getLongCounter
public LongCounter getLongCounter(String name)
Description copied from interface:RuntimeContext
Convenience function to create a counter object for longs.- Specified by:
getLongCounter
in interfaceRuntimeContext
-
getDoubleCounter
public DoubleCounter getDoubleCounter(String name)
Description copied from interface:RuntimeContext
Convenience function to create a counter object for doubles.- Specified by:
getDoubleCounter
in interfaceRuntimeContext
-
getHistogram
public Histogram getHistogram(String name)
Description copied from interface:RuntimeContext
Convenience function to create a counter object for histograms.- Specified by:
getHistogram
in interfaceRuntimeContext
-
getExternalResourceInfos
public Set<ExternalResourceInfo> getExternalResourceInfos(String resourceName)
Description copied from interface:RuntimeContext
Get the specific external resource information by the resourceName.- Specified by:
getExternalResourceInfos
in interfaceRuntimeContext
- Parameters:
resourceName
- of the required external resource- Returns:
- information set of the external resource identified by the resourceName
-
hasBroadcastVariable
public boolean hasBroadcastVariable(String name)
Description copied from interface:RuntimeContext
Tests for the existence of the broadcast variable identified by the givenname
.- Specified by:
hasBroadcastVariable
in interfaceRuntimeContext
- Parameters:
name
- The name under which the broadcast variable is registered;- Returns:
- Whether a broadcast variable exists for the given name.
-
getBroadcastVariable
public <RT> List<RT> getBroadcastVariable(String name)
Description copied from interface:RuntimeContext
Returns the result bound to the broadcast variable identified by the givenname
.IMPORTANT: The broadcast variable data structure is shared between the parallel tasks on one machine. Any access that modifies its internal state needs to be manually synchronized by the caller.
- Specified by:
getBroadcastVariable
in interfaceRuntimeContext
- Parameters:
name
- The name under which the broadcast variable is registered;- Returns:
- The broadcast variable, materialized as a list of elements.
-
getBroadcastVariableWithInitializer
public <T,C> C getBroadcastVariableWithInitializer(String name, BroadcastVariableInitializer<T,C> initializer)
Description copied from interface:RuntimeContext
Returns the result bound to the broadcast variable identified by the givenname
. The broadcast variable is returned as a shared data structure that is initialized with the givenBroadcastVariableInitializer
.IMPORTANT: The broadcast variable data structure is shared between the parallel tasks on one machine. Any access that modifies its internal state needs to be manually synchronized by the caller.
- Specified by:
getBroadcastVariableWithInitializer
in interfaceRuntimeContext
- Parameters:
name
- The name under which the broadcast variable is registered;initializer
- The initializer that creates the shared data structure of the broadcast variable from the sequence of elements.- Returns:
- The broadcast variable, materialized as a list of elements.
-
getDistributedCache
public DistributedCache getDistributedCache()
Description copied from interface:RuntimeContext
Returns theDistributedCache
to get the local temporary file copies of files otherwise not locally accessible.- Specified by:
getDistributedCache
in interfaceRuntimeContext
- Returns:
- The distributed cache of the worker executing this instance.
-
getState
public <T> ValueState<T> getState(ValueStateDescriptor<T> stateProperties)
Description copied from interface:RuntimeContext
Gets a handle to the system's key/value state. The key/value state is only accessible if the function is executed on a KeyedStream. On each access, the state exposes the value for the key of the element currently processed by the function. Each function may have multiple partitioned states, addressed with different names.Because the scope of each value is the key of the currently processed element, and the elements are distributed by the Flink runtime, the system can transparently scale out and redistribute the state and KeyedStream.
The following code example shows how to implement a continuous counter that counts how many times elements of a certain key occur, and emits an updated count for that element on each occurrence.
DataStream<MyType> stream = ...; KeyedStream<MyType> keyedStream = stream.keyBy("id"); keyedStream.map(new RichMapFunction<MyType, Tuple2<MyType, Long>>() { private ValueState<Long> state; public void open(OpenContext ctx) { state = getRuntimeContext().getState( new ValueStateDescriptor<Long>("count", LongSerializer.INSTANCE, 0L)); } public Tuple2<MyType, Long> map(MyType value) { long count = state.value() + 1; state.update(count); return new Tuple2<>(value, count); } });
- Specified by:
getState
in interfaceRuntimeContext
- Type Parameters:
T
- The type of value stored in the state.- Parameters:
stateProperties
- The descriptor defining the properties of the stats.- Returns:
- The partitioned state object.
-
getListState
public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties)
Description copied from interface:RuntimeContext
Gets a handle to the system's key/value list state. This state is similar to the state accessed viaRuntimeContext.getState(ValueStateDescriptor)
, but is optimized for state that holds lists. One can add elements to the list, or retrieve the list as a whole.This state is only accessible if the function is executed on a KeyedStream.
DataStream<MyType> stream = ...; KeyedStream<MyType> keyedStream = stream.keyBy("id"); keyedStream.map(new RichFlatMapFunction<MyType, List<MyType>>() { private ListState<MyType> state; public void open(OpenContext ctx) { state = getRuntimeContext().getListState( new ListStateDescriptor<>("myState", MyType.class)); } public void flatMap(MyType value, Collector<MyType> out) { if (value.isDivider()) { for (MyType t : state.get()) { out.collect(t); } } else { state.add(value); } } });
- Specified by:
getListState
in interfaceRuntimeContext
- Type Parameters:
T
- The type of value stored in the state.- Parameters:
stateProperties
- The descriptor defining the properties of the stats.- Returns:
- The partitioned state object.
-
getReducingState
public <T> ReducingState<T> getReducingState(ReducingStateDescriptor<T> stateProperties)
Description copied from interface:RuntimeContext
Gets a handle to the system's key/value reducing state. This state is similar to the state accessed viaRuntimeContext.getState(ValueStateDescriptor)
, but is optimized for state that aggregates values.This state is only accessible if the function is executed on a KeyedStream.
DataStream<MyType> stream = ...; KeyedStream<MyType> keyedStream = stream.keyBy("id"); keyedStream.map(new RichMapFunction<MyType, List<MyType>>() { private ReducingState<Long> state; public void open(OpenContext ctx) { state = getRuntimeContext().getReducingState( new ReducingStateDescriptor<>("sum", (a, b) -> a + b, Long.class)); } public Tuple2<MyType, Long> map(MyType value) { state.add(value.count()); return new Tuple2<>(value, state.get()); } });
- Specified by:
getReducingState
in interfaceRuntimeContext
- Type Parameters:
T
- The type of value stored in the state.- Parameters:
stateProperties
- The descriptor defining the properties of the stats.- Returns:
- The partitioned state object.
-
getAggregatingState
public <IN,ACC,OUT> AggregatingState<IN,OUT> getAggregatingState(AggregatingStateDescriptor<IN,ACC,OUT> stateProperties)
Description copied from interface:RuntimeContext
Gets a handle to the system's key/value aggregating state. This state is similar to the state accessed viaRuntimeContext.getState(ValueStateDescriptor)
, but is optimized for state that aggregates values with different types.This state is only accessible if the function is executed on a KeyedStream.
DataStream<MyType> stream = ...; KeyedStream<MyType> keyedStream = stream.keyBy("id"); AggregateFunction<...> aggregateFunction = ... keyedStream.map(new RichMapFunction<MyType, List<MyType>>() { private AggregatingState<MyType, Long> state; public void open(OpenContext ctx) { state = getRuntimeContext().getAggregatingState( new AggregatingStateDescriptor<>("sum", aggregateFunction, Long.class)); } public Tuple2<MyType, Long> map(MyType value) { state.add(value); return new Tuple2<>(value, state.get()); } });
- Specified by:
getAggregatingState
in interfaceRuntimeContext
- Type Parameters:
IN
- The type of the values that are added to the state.ACC
- The type of the accumulator (intermediate aggregation state).OUT
- The type of the values that are returned from the state.- Parameters:
stateProperties
- The descriptor defining the properties of the stats.- Returns:
- The partitioned state object.
-
getMapState
public <UK,UV> MapState<UK,UV> getMapState(MapStateDescriptor<UK,UV> stateProperties)
Description copied from interface:RuntimeContext
Gets a handle to the system's key/value map state. This state is similar to the state accessed viaRuntimeContext.getState(ValueStateDescriptor)
, but is optimized for state that is composed of user-defined key-value pairsThis state is only accessible if the function is executed on a KeyedStream.
DataStream<MyType> stream = ...; KeyedStream<MyType> keyedStream = stream.keyBy("id"); keyedStream.map(new RichMapFunction<MyType, List<MyType>>() { private MapState<MyType, Long> state; public void open(OpenContext ctx) { state = getRuntimeContext().getMapState( new MapStateDescriptor<>("sum", MyType.class, Long.class)); } public Tuple2<MyType, Long> map(MyType value) { return new Tuple2<>(value, state.get(value)); } });
- Specified by:
getMapState
in interfaceRuntimeContext
- Type Parameters:
UK
- The type of the user keys stored in the state.UV
- The type of the user values stored in the state.- Parameters:
stateProperties
- The descriptor defining the properties of the stats.- Returns:
- The partitioned state object.
-
getStateDescriptors
public List<StateDescriptor<?,?>> getStateDescriptors()
-
-