org.apache.flink.runtime.state.OperatorStateBackend Java Examples

The following examples show how to use org.apache.flink.runtime.state.OperatorStateBackend. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NotifyCheckpointAbortedITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws BackendBuildingException {
	if (operatorIdentifier.contains(DECLINE_SINK_NAME)) {
		return new DeclineSinkFailingOperatorStateBackend(
			env.getExecutionConfig(),
			cancelStreamRegistry,
			new DeclineSinkFailingSnapshotStrategy());
	} else {
		return new DefaultOperatorStateBackendBuilder(
			env.getUserClassLoader(),
			env.getExecutionConfig(),
			false,
			stateHandles,
			cancelStreamRegistry).build();
	}
}
 
Example #2
Source File: RocksDBStateBackend.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws Exception {

	//the default for RocksDB; eventually there can be a operator state backend based on RocksDB, too.
	final boolean asyncSnapshots = true;
	return new DefaultOperatorStateBackendBuilder(
		env.getUserClassLoader(),
		env.getExecutionConfig(),
		asyncSnapshots,
		stateHandles,
		cancelStreamRegistry).build();
}
 
Example #3
Source File: RocksDBStateBackend.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws Exception {

	//the default for RocksDB; eventually there can be a operator state backend based on RocksDB, too.
	final boolean asyncSnapshots = true;
	return new DefaultOperatorStateBackendBuilder(
		env.getUserClassLoader(),
		env.getExecutionConfig(),
		asyncSnapshots,
		stateHandles,
		cancelStreamRegistry).build();
}
 
Example #4
Source File: StreamingFunctionUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
public static void snapshotFunctionState(
		StateSnapshotContext context,
		OperatorStateBackend backend,
		Function userFunction) throws Exception {

	Preconditions.checkNotNull(context);
	Preconditions.checkNotNull(backend);

	while (true) {

		if (trySnapshotFunctionState(context, backend, userFunction)) {
			break;
		}

		// inspect if the user function is wrapped, then unwrap and try again if we can snapshot the inner function
		if (userFunction instanceof WrappingFunction) {
			userFunction = ((WrappingFunction<?>) userFunction).getWrappedFunction();
		} else {
			break;
		}
	}
}
 
Example #5
Source File: DoFnOperator.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public BufferedOutputManager<OutputT> create(
    Output<StreamRecord<WindowedValue<OutputT>>> output,
    Lock bufferLock,
    OperatorStateBackend operatorStateBackend)
    throws Exception {
  Preconditions.checkNotNull(output);
  Preconditions.checkNotNull(bufferLock);
  Preconditions.checkNotNull(operatorStateBackend);

  TaggedKvCoder taggedKvCoder = buildTaggedKvCoder();
  ListStateDescriptor<KV<Integer, WindowedValue<?>>> taggedOutputPushbackStateDescriptor =
      new ListStateDescriptor<>("bundle-buffer-tag", new CoderTypeSerializer<>(taggedKvCoder));
  ListState<KV<Integer, WindowedValue<?>>> listStateBuffer =
      operatorStateBackend.getListState(taggedOutputPushbackStateDescriptor);
  PushedBackElementsHandler<KV<Integer, WindowedValue<?>>> pushedBackElementsHandler =
      NonKeyedPushedBackElementsHandler.create(listStateBuffer);

  return new BufferedOutputManager<>(
      output, mainTag, tagsToOutputTags, tagsToIds, bufferLock, pushedBackElementsHandler);
}
 
Example #6
Source File: OperatorStateInputFormat.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void open(OperatorStateInputSplit split) throws IOException {
	registry = new CloseableRegistry();

	final BackendRestorerProcedure<OperatorStateBackend, OperatorStateHandle> backendRestorer =
		new BackendRestorerProcedure<>(
			(handles) -> createOperatorStateBackend(getRuntimeContext(), handles, registry),
			registry,
			operatorState.getOperatorID().toString()
		);

	try {
		restoredBackend = backendRestorer.createAndRestore(split.getPrioritizedManagedOperatorState());
	} catch (Exception exception) {
		throw new IOException("Failed to restore state backend", exception);
	}

	try {
		elements = getElements(restoredBackend).iterator();
	} catch (Exception e) {
		throw new IOException("Failed to read operator state from restored state backend", e);
	}
}
 
Example #7
Source File: OperatorStateInputFormat.java    From flink with Apache License 2.0 6 votes vote down vote up
private static OperatorStateBackend createOperatorStateBackend(
	RuntimeContext runtimeContext,
	Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry){

	try {
		return new DefaultOperatorStateBackendBuilder(
			runtimeContext.getUserCodeClassLoader(),
			runtimeContext.getExecutionConfig(),
			false,
			stateHandles,
			cancelStreamRegistry
		).build();
	} catch (BackendBuildingException e) {
		throw new RuntimeException(e);
	}
}
 
Example #8
Source File: BufferingDoFnRunner.java    From beam with Apache License 2.0 6 votes vote down vote up
public static <InputT, OutputT> BufferingDoFnRunner<InputT, OutputT> create(
    DoFnRunner<InputT, OutputT> doFnRunner,
    String stateName,
    org.apache.beam.sdk.coders.Coder windowedInputCoder,
    org.apache.beam.sdk.coders.Coder windowCoder,
    OperatorStateBackend operatorStateBackend,
    @Nullable KeyedStateBackend<Object> keyedStateBackend,
    int maxConcurrentCheckpoints)
    throws Exception {
  return new BufferingDoFnRunner<>(
      doFnRunner,
      stateName,
      windowedInputCoder,
      windowCoder,
      operatorStateBackend,
      keyedStateBackend,
      maxConcurrentCheckpoints);
}
 
Example #9
Source File: RocksDBStateBackend.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws Exception {

	//the default for RocksDB; eventually there can be a operator state backend based on RocksDB, too.
	final boolean asyncSnapshots = true;
	return new DefaultOperatorStateBackendBuilder(
		env.getUserClassLoader(),
		env.getExecutionConfig(),
		asyncSnapshots,
		stateHandles,
		cancelStreamRegistry).build();
}
 
Example #10
Source File: FlinkBroadcastStateInternals.java    From beam with Apache License 2.0 6 votes vote down vote up
FlinkCombiningStateWithContext(
    OperatorStateBackend flinkStateBackend,
    StateTag<CombiningState<InputT, AccumT, OutputT>> address,
    CombineWithContext.CombineFnWithContext<InputT, AccumT, OutputT> combineFn,
    StateNamespace namespace,
    Coder<AccumT> accumCoder,
    FlinkBroadcastStateInternals<K2> flinkStateInternals,
    CombineWithContext.Context context) {
  super(flinkStateBackend, address.getId(), namespace, accumCoder);

  this.namespace = namespace;
  this.address = address;
  this.combineFn = combineFn;
  this.flinkStateInternals = flinkStateInternals;
  this.context = context;
}
 
Example #11
Source File: FlinkBroadcastStateInternals.java    From beam with Apache License 2.0 6 votes vote down vote up
AbstractBroadcastState(
    OperatorStateBackend flinkStateBackend,
    String name,
    StateNamespace namespace,
    Coder<T> coder) {
  this.name = name;

  this.namespace = namespace;
  this.flinkStateBackend = flinkStateBackend;

  CoderTypeInformation<Map<String, T>> typeInfo =
      new CoderTypeInformation<>(MapCoder.of(StringUtf8Coder.of(), coder));

  flinkStateDescriptor =
      new ListStateDescriptor<>(name, typeInfo.createSerializer(new ExecutionConfig()));
}
 
Example #12
Source File: OperatorStateInputFormat.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void open(OperatorStateInputSplit split) throws IOException {
	registry = new CloseableRegistry();

	final BackendRestorerProcedure<OperatorStateBackend, OperatorStateHandle> backendRestorer =
		new BackendRestorerProcedure<>(
			(handles) -> createOperatorStateBackend(getRuntimeContext(), handles, registry),
			registry,
			operatorState.getOperatorID().toString()
		);

	try {
		restoredBackend = backendRestorer.createAndRestore(split.getPrioritizedManagedOperatorState());
	} catch (Exception exception) {
		throw new IOException("Failed to restore state backend", exception);
	}

	try {
		elements = getElements(restoredBackend).iterator();
	} catch (Exception e) {
		throw new IOException("Failed to read operator state from restored state backend", e);
	}
}
 
Example #13
Source File: TaskCheckpointingBehaviourTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws Exception {
	return new DefaultOperatorStateBackendBuilder(
		env.getUserClassLoader(),
		env.getExecutionConfig(),
		true,
		stateHandles,
		cancelStreamRegistry) {
		@Override
		@SuppressWarnings("unchecked")
		public DefaultOperatorStateBackend build() {
			return new DefaultOperatorStateBackend(
				executionConfig,
				cancelStreamRegistry,
				new HashMap<>(),
				new HashMap<>(),
				new HashMap<>(),
				new HashMap<>(),
				mock(AbstractSnapshotStrategy.class)
			) {
				@Nonnull
				@Override
				public RunnableFuture<SnapshotResult<OperatorStateHandle>> snapshot(
					long checkpointId,
					long timestamp,
					@Nonnull CheckpointStreamFactory streamFactory,
					@Nonnull CheckpointOptions checkpointOptions) throws Exception {

					return new FutureTask<>(() -> {
						throw new Exception("Async part snapshot exception.");
					});
				}
			};
		}
	}.build();
}
 
Example #14
Source File: TaskCheckpointingBehaviourTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws Exception {
	return new DefaultOperatorStateBackendBuilder(
		env.getUserClassLoader(),
		env.getExecutionConfig(),
		true,
		stateHandles,
		cancelStreamRegistry) {
		@Override
		@SuppressWarnings("unchecked")
		public DefaultOperatorStateBackend build() {
			return new DefaultOperatorStateBackend(
				executionConfig,
				cancelStreamRegistry,
				new HashMap<>(),
				new HashMap<>(),
				new HashMap<>(),
				new HashMap<>(),
				mock(AbstractSnapshotStrategy.class)
			) {
				@Nonnull
				@Override
				public RunnableFuture<SnapshotResult<OperatorStateHandle>> snapshot(
					long checkpointId,
					long timestamp,
					@Nonnull CheckpointStreamFactory streamFactory,
					@Nonnull CheckpointOptions checkpointOptions) throws Exception {

					throw new Exception("Sync part snapshot exception.");
				}
			};
		}
	}.build();
}
 
Example #15
Source File: StubStateBackend.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws Exception {
	return backend.createOperatorStateBackend(env, operatorIdentifier, stateHandles, cancelStreamRegistry);
}
 
Example #16
Source File: OperatorStateWriter.java    From bravo with Apache License 2.0 5 votes vote down vote up
private StateObjectCollection<OperatorStateHandle> transformSubtaskOpState(Path outDir, Integer subtaskId,
		StateObjectCollection<OperatorStateHandle> baseState) {

	if (transformer == null) {
		return baseState;
	}

	StateObjectCollection<OperatorStateHandle> opHandle = baseState;
	try (OperatorStateBackend opBackend = OperatorStateReader
			.restoreOperatorStateBackend(opHandle)) {

		transformer.accept(subtaskId, opBackend);

		OperatorStateHandle newSnapshot = opBackend
				.snapshot(checkpointId, System.currentTimeMillis(), new CheckpointStreamFactory() {
					@Override
					public CheckpointStateOutputStream createCheckpointStateOutputStream(
							CheckpointedStateScope scope)
							throws IOException {
						return new FileBasedStateOutputStream(outDir.getFileSystem(),
								new Path(outDir, String.valueOf(UUID.randomUUID())));
					}
				}, null).get().getJobManagerOwnedSnapshot();
		return new StateObjectCollection<>(Lists.newArrayList(newSnapshot));
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #17
Source File: OperatorStateReader.java    From bravo with Apache License 2.0 5 votes vote down vote up
/**
 * Restores the OperatorStateBackends corresponding to the different
 * subtasks. The backends are completely restored in-memory.
 */
public Map<Integer, OperatorStateBackend> createOperatorStateBackendsFromSnapshot() throws Exception {
	return Maps.transformValues(opState.getSubtaskStates(),
			sst -> {
				try {
					return restoreOperatorStateBackend(sst.getManagedOperatorState());
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			});
}
 
Example #18
Source File: OperatorStateReader.java    From bravo with Apache License 2.0 5 votes vote down vote up
/**
 * Read the serializableListState stored in the checkpoint for the given
 * operator subtask
 */
public List<Serializable> getSerializableListState(int subtask) throws Exception {
	OperatorStateBackend backend = createOperatorStateBackendFromSnapshot(subtask);
	@SuppressWarnings("deprecation")
	ListState<Serializable> listState = backend
			.getSerializableListState(DefaultOperatorStateBackend.DEFAULT_OPERATOR_STATE_NAME);

	List<Serializable> list = new ArrayList<>();

	for (Serializable serializable : listState.get()) {
		list.add(serializable);
	}

	return list;
}
 
Example #19
Source File: BroadcastStateTransformationTest.java    From bravo with Apache License 2.0 5 votes vote down vote up
private void validateSecondSavepoint(Savepoint savepoint) throws Exception {
	// Validate the contents of the broadcast state
	OperatorStateReader reader = new OperatorStateReader(ExecutionEnvironment.createLocalEnvironment(), savepoint,
			"stateful");
	OperatorStateBackend backend = reader.createOperatorStateBackendFromSnapshot(1);
	BroadcastState<Boolean, List<Integer>> state = backend.getBroadcastState(bcstate);
	assertEquals(Lists.newArrayList(2, 3), state.get(true));
}
 
Example #20
Source File: StreamingFunctionUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static boolean trySnapshotFunctionState(
		StateSnapshotContext context,
		OperatorStateBackend backend,
		Function userFunction) throws Exception {

	if (userFunction instanceof CheckpointedFunction) {
		((CheckpointedFunction) userFunction).snapshotState(context);

		return true;
	}

	if (userFunction instanceof ListCheckpointed) {
		@SuppressWarnings("unchecked")
		List<Serializable> partitionableState = ((ListCheckpointed<Serializable>) userFunction).
				snapshotState(context.getCheckpointId(), context.getCheckpointTimestamp());

		ListState<Serializable> listState = backend.
				getSerializableListState(DefaultOperatorStateBackend.DEFAULT_OPERATOR_STATE_NAME);

		listState.clear();

		if (null != partitionableState) {
			try {
				for (Serializable statePartition : partitionableState) {
					listState.add(statePartition);
				}
			} catch (Exception e) {
				listState.clear();

				throw new Exception("Could not write partitionable state to operator " +
					"state backend.", e);
			}
		}

		return true;
	}

	return false;
}
 
Example #21
Source File: TaskCheckpointingBehaviourTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws Exception {
	return new DefaultOperatorStateBackendBuilder(
		env.getUserClassLoader(),
		env.getExecutionConfig(),
		true,
		stateHandles,
		cancelStreamRegistry) {
		@Override
		@SuppressWarnings("unchecked")
		public DefaultOperatorStateBackend build() {
			return new DefaultOperatorStateBackend(
				executionConfig,
				cancelStreamRegistry,
				new HashMap<>(),
				new HashMap<>(),
				new HashMap<>(),
				new HashMap<>(),
				mock(AbstractSnapshotStrategy.class)
			) {
				@Nonnull
				@Override
				public RunnableFuture<SnapshotResult<OperatorStateHandle>> snapshot(
					long checkpointId,
					long timestamp,
					@Nonnull CheckpointStreamFactory streamFactory,
					@Nonnull CheckpointOptions checkpointOptions) throws Exception {

					throw new Exception("Sync part snapshot exception.");
				}
			};
		}
	}.build();
}
 
Example #22
Source File: FlinkBroadcastStateInternals.java    From beam with Apache License 2.0 5 votes vote down vote up
FlinkBroadcastBagState(
    OperatorStateBackend flinkStateBackend,
    StateTag<BagState<T>> address,
    StateNamespace namespace,
    Coder<T> coder) {
  super(flinkStateBackend, address.getId(), namespace, ListCoder.of(coder));

  this.namespace = namespace;
  this.address = address;
}
 
Example #23
Source File: FlinkBroadcastStateInternals.java    From beam with Apache License 2.0 5 votes vote down vote up
FlinkCombiningState(
    OperatorStateBackend flinkStateBackend,
    StateTag<CombiningState<InputT, AccumT, OutputT>> address,
    Combine.CombineFn<InputT, AccumT, OutputT> combineFn,
    StateNamespace namespace,
    Coder<AccumT> accumCoder) {
  super(flinkStateBackend, address.getId(), namespace, accumCoder);

  this.namespace = namespace;
  this.address = address;
  this.combineFn = combineFn;
}
 
Example #24
Source File: StateBackendITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws Exception {

	throw new SuccessException();
}
 
Example #25
Source File: StreamTaskStateInitializerImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
StreamOperatorStateContextImpl(
	boolean restored,
	OperatorStateBackend operatorStateBackend,
	AbstractKeyedStateBackend<?> keyedStateBackend,
	InternalTimeServiceManager<?> internalTimeServiceManager,
	CloseableIterable<StatePartitionStreamProvider> rawOperatorStateInputs,
	CloseableIterable<KeyGroupStatePartitionStreamProvider> rawKeyedStateInputs) {

	this.restored = restored;
	this.operatorStateBackend = operatorStateBackend;
	this.keyedStateBackend = keyedStateBackend;
	this.internalTimeServiceManager = internalTimeServiceManager;
	this.rawOperatorStateInputs = rawOperatorStateInputs;
	this.rawKeyedStateInputs = rawKeyedStateInputs;
}
 
Example #26
Source File: StreamTaskStateInitializerImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
protected OperatorStateBackend operatorStateBackend(
	String operatorIdentifierText,
	PrioritizedOperatorSubtaskState prioritizedOperatorSubtaskStates,
	CloseableRegistry backendCloseableRegistry) throws Exception {

	String logDescription = "operator state backend for " + operatorIdentifierText;

	// Now restore processing is included in backend building/constructing process, so we need to make sure
	// each stream constructed in restore could also be closed in case of task cancel, for example the data
	// input stream opened for serDe during restore.
	CloseableRegistry cancelStreamRegistryForRestore = new CloseableRegistry();
	backendCloseableRegistry.registerCloseable(cancelStreamRegistryForRestore);
	BackendRestorerProcedure<OperatorStateBackend, OperatorStateHandle> backendRestorer =
		new BackendRestorerProcedure<>(
			(stateHandles) -> stateBackend.createOperatorStateBackend(
				environment,
				operatorIdentifierText,
				stateHandles,
				cancelStreamRegistryForRestore),
			backendCloseableRegistry,
			logDescription);

	try {
		return backendRestorer.createAndRestore(
			prioritizedOperatorSubtaskStates.getPrioritizedManagedOperatorState());
	} finally {
		if (backendCloseableRegistry.unregisterCloseable(cancelStreamRegistryForRestore)) {
			IOUtils.closeQuietly(cancelStreamRegistryForRestore);
		}
	}
}
 
Example #27
Source File: FlinkBroadcastStateInternals.java    From beam with Apache License 2.0 5 votes vote down vote up
FlinkKeyedCombiningState(
    OperatorStateBackend flinkStateBackend,
    StateTag<CombiningState<InputT, AccumT, OutputT>> address,
    Combine.CombineFn<InputT, AccumT, OutputT> combineFn,
    StateNamespace namespace,
    Coder<AccumT> accumCoder,
    FlinkBroadcastStateInternals<K2> flinkStateInternals) {
  super(flinkStateBackend, address.getId(), namespace, accumCoder);

  this.namespace = namespace;
  this.address = address;
  this.combineFn = combineFn;
  this.flinkStateInternals = flinkStateInternals;
}
 
Example #28
Source File: MemoryStateBackend.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws Exception {

	return new DefaultOperatorStateBackendBuilder(
		env.getUserClassLoader(),
		env.getExecutionConfig(),
		isUsingAsynchronousSnapshots(),
		stateHandles,
		cancelStreamRegistry).build();
}
 
Example #29
Source File: CheckpointSettingsSerializableTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws Exception {
	throw new UnsupportedOperationException();
}
 
Example #30
Source File: FsStateBackend.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public OperatorStateBackend createOperatorStateBackend(
	Environment env,
	String operatorIdentifier,
	@Nonnull Collection<OperatorStateHandle> stateHandles,
	CloseableRegistry cancelStreamRegistry) throws BackendBuildingException {

	return new DefaultOperatorStateBackendBuilder(
		env.getUserClassLoader(),
		env.getExecutionConfig(),
		isUsingAsynchronousSnapshots(),
		stateHandles,
		cancelStreamRegistry).build();
}