use of org.apache.flink.api.common.state.State in project flink by apache.
the class TtlVerifyUpdateFunction method performUpdate.
private TtlUpdateContext<?, ?> performUpdate(TtlStateVerifier<?, ?> verifier, Object update) throws Exception {
return MonotonicTTLTimeProvider.doWithFrozenTime(frozenTimestamp -> {
State state = states.get(verifier.getId());
Object valueBeforeUpdate = verifier.get(state);
verifier.update(state, update);
Object updatedValue = verifier.get(state);
return new TtlUpdateContext<>(valueBeforeUpdate, update, updatedValue, frozenTimestamp);
});
}
use of org.apache.flink.api.common.state.State in project flink by apache.
the class BatchExecutionKeyedStateBackend method setCurrentKey.
@Override
public void setCurrentKey(K newKey) {
if (!Objects.equals(newKey, currentKey)) {
notifyKeySelected(newKey);
for (State value : states.values()) {
((AbstractBatchExecutionKeyState<?, ?, ?>) value).clearAllNamespaces();
}
for (KeyGroupedInternalPriorityQueue<?> value : priorityQueues.values()) {
while (value.poll() != null) {
// remove everything for the key
}
}
this.currentKey = newKey;
}
}
use of org.apache.flink.api.common.state.State in project flink by apache.
the class AbstractQueryableStateTestBase method testDuplicateRegistrationFailsJob.
/**
* Tests that duplicate query registrations fail the job at the JobManager.
*/
@Test(timeout = 60_000)
public void testDuplicateRegistrationFailsJob() throws Exception {
final int numKeys = 256;
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStateBackend(stateBackend);
env.setParallelism(maxParallelism);
// Very important, because cluster is shared between tests and we
// don't explicitly check that all slots are available before
// submitting.
env.setRestartStrategy(RestartStrategies.fixedDelayRestart(Integer.MAX_VALUE, 1000L));
DataStream<Tuple2<Integer, Long>> source = env.addSource(new TestKeyRangeSource(numKeys));
// Reducing state
ReducingStateDescriptor<Tuple2<Integer, Long>> reducingState = new ReducingStateDescriptor<>("any-name", new SumReduce(), source.getType());
final String queryName = "duplicate-me";
final QueryableStateStream<Integer, Tuple2<Integer, Long>> queryableState = source.keyBy(new KeySelector<Tuple2<Integer, Long>, Integer>() {
private static final long serialVersionUID = -4126824763829132959L;
@Override
public Integer getKey(Tuple2<Integer, Long> value) {
return value.f0;
}
}).asQueryableState(queryName, reducingState);
final QueryableStateStream<Integer, Tuple2<Integer, Long>> duplicate = source.keyBy(new KeySelector<Tuple2<Integer, Long>, Integer>() {
private static final long serialVersionUID = -6265024000462809436L;
@Override
public Integer getKey(Tuple2<Integer, Long> value) {
return value.f0;
}
}).asQueryableState(queryName);
// Submit the job graph
final JobGraph jobGraph = env.getStreamGraph().getJobGraph();
clusterClient.submitJob(jobGraph).thenCompose(clusterClient::requestJobResult).thenApply(JobResult::getSerializedThrowable).thenAccept(serializedThrowable -> {
assertTrue(serializedThrowable.isPresent());
final Throwable t = serializedThrowable.get().deserializeError(getClass().getClassLoader());
final String failureCause = ExceptionUtils.stringifyException(t);
assertThat(failureCause, containsString("KvState with name '" + queryName + "' has already been registered by another operator"));
}).get();
}
use of org.apache.flink.api.common.state.State in project flink by apache.
the class MultiStateKeyIterator method remove.
/**
* Removes the current key from <b>ALL</b> known states in the state backend.
*/
@Override
public void remove() {
if (currentKey == null) {
return;
}
for (StateDescriptor<?, ?> descriptor : descriptors) {
try {
State state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, descriptor);
state.clear();
} catch (Exception e) {
throw new RuntimeException("Failed to drop partitioned state from state backend", e);
}
}
}
use of org.apache.flink.api.common.state.State in project flink by apache.
the class MockKeyedStateBackend method createInternalState.
@Override
@SuppressWarnings("unchecked")
@Nonnull
public <N, SV, SEV, S extends State, IS extends S> IS createInternalState(@Nonnull TypeSerializer<N> namespaceSerializer, @Nonnull StateDescriptor<S, SV> stateDesc, @Nonnull StateSnapshotTransformFactory<SEV> snapshotTransformFactory) throws Exception {
StateFactory stateFactory = STATE_FACTORIES.get(stateDesc.getType());
if (stateFactory == null) {
String message = String.format("State %s is not supported by %s", stateDesc.getClass(), TtlStateFactory.class);
throw new FlinkRuntimeException(message);
}
IS state = stateFactory.createInternalState(namespaceSerializer, stateDesc);
stateSnapshotFilters.put(stateDesc.getName(), (StateSnapshotTransformer<Object>) getStateSnapshotTransformer(stateDesc, snapshotTransformFactory));
((MockInternalKvState<K, N, SV>) state).values = () -> stateValues.computeIfAbsent(stateDesc.getName(), n -> new HashMap<>()).computeIfAbsent(getCurrentKey(), k -> new HashMap<>());
return state;
}
Aggregations