Java Code Examples for org.apache.flink.api.common.state.StateDescriptor#getName()

The following examples show how to use org.apache.flink.api.common.state.StateDescriptor#getName() . 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: AbstractKeyedStateBackend.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 *
 * @see KeyedStateBackend
 */
@SuppressWarnings("unchecked")
@Override
public <N, S extends State> S getPartitionedState(
		final N namespace,
		final TypeSerializer<N> namespaceSerializer,
		final StateDescriptor<S, ?> stateDescriptor) throws Exception {

	checkNotNull(namespace, "Namespace");

	if (lastName != null && lastName.equals(stateDescriptor.getName())) {
		lastState.setCurrentNamespace(namespace);
		return (S) lastState;
	}

	InternalKvState<K, ?, ?> previous = keyValueStatesByName.get(stateDescriptor.getName());
	if (previous != null) {
		lastState = previous;
		lastState.setCurrentNamespace(namespace);
		lastName = stateDescriptor.getName();
		return (S) previous;
	}

	final S state = getOrCreateKeyedState(namespaceSerializer, stateDescriptor);
	final InternalKvState<K, N, ?> kvState = (InternalKvState<K, N, ?>) state;

	lastName = stateDescriptor.getName();
	lastState = kvState;
	kvState.setCurrentNamespace(namespace);

	return state;
}
 
Example 2
Source File: AbstractKeyedStateBackend.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 *
 * @see KeyedStateBackend
 */
@SuppressWarnings("unchecked")
@Override
public <N, S extends State> S getPartitionedState(
		final N namespace,
		final TypeSerializer<N> namespaceSerializer,
		final StateDescriptor<S, ?> stateDescriptor) throws Exception {

	checkNotNull(namespace, "Namespace");

	if (lastName != null && lastName.equals(stateDescriptor.getName())) {
		lastState.setCurrentNamespace(namespace);
		return (S) lastState;
	}

	InternalKvState<K, ?, ?> previous = keyValueStatesByName.get(stateDescriptor.getName());
	if (previous != null) {
		lastState = previous;
		lastState.setCurrentNamespace(namespace);
		lastName = stateDescriptor.getName();
		return (S) previous;
	}

	final S state = getOrCreateKeyedState(namespaceSerializer, stateDescriptor);
	final InternalKvState<K, N, ?> kvState = (InternalKvState<K, N, ?>) state;

	lastName = stateDescriptor.getName();
	lastState = kvState;
	kvState.setCurrentNamespace(namespace);

	return state;
}
 
Example 3
Source File: AbstractKeyedStateBackend.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 *
 * @see KeyedStateBackend
 */
@SuppressWarnings("unchecked")
@Override
public <N, S extends State> S getPartitionedState(
		final N namespace,
		final TypeSerializer<N> namespaceSerializer,
		final StateDescriptor<S, ?> stateDescriptor) throws Exception {

	checkNotNull(namespace, "Namespace");

	if (lastName != null && lastName.equals(stateDescriptor.getName())) {
		lastState.setCurrentNamespace(namespace);
		return (S) lastState;
	}

	InternalKvState<K, ?, ?> previous = keyValueStatesByName.get(stateDescriptor.getName());
	if (previous != null) {
		lastState = previous;
		lastState.setCurrentNamespace(namespace);
		lastName = stateDescriptor.getName();
		return (S) previous;
	}

	final S state = getOrCreateKeyedState(namespaceSerializer, stateDescriptor);
	final InternalKvState<K, N, ?> kvState = (InternalKvState<K, N, ?>) state;

	lastName = stateDescriptor.getName();
	lastState = kvState;
	kvState.setCurrentNamespace(namespace);

	return state;
}
 
Example 4
Source File: RocksDBKeyedStateBackend.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Registers a k/v state information, which includes its state id, type, RocksDB column family handle, and serializers.
 *
 * <p>When restoring from a snapshot, we don’t restore the individual k/v states, just the global RocksDB database and
 * the list of k/v state information. When a k/v state is first requested we check here whether we
 * already have a registered entry for that and return it (after some necessary state compatibility checks)
 * or create a new one if it does not exist.
 */
private <N, S extends State, SV, SEV> Tuple2<ColumnFamilyHandle, RegisteredKeyValueStateBackendMetaInfo<N, SV>> tryRegisterKvStateInformation(
	StateDescriptor<S, SV> stateDesc,
	TypeSerializer<N> namespaceSerializer,
	@Nonnull StateSnapshotTransformFactory<SEV> snapshotTransformFactory) throws Exception {

	RocksDbKvStateInfo oldStateInfo = kvStateInformation.get(stateDesc.getName());

	TypeSerializer<SV> stateSerializer = stateDesc.getSerializer();

	RocksDbKvStateInfo newRocksStateInfo;
	RegisteredKeyValueStateBackendMetaInfo<N, SV> newMetaInfo;
	if (oldStateInfo != null) {
		@SuppressWarnings("unchecked")
		RegisteredKeyValueStateBackendMetaInfo<N, SV> castedMetaInfo =
			(RegisteredKeyValueStateBackendMetaInfo<N, SV>) oldStateInfo.metaInfo;

		newMetaInfo = updateRestoredStateMetaInfo(
			Tuple2.of(oldStateInfo.columnFamilyHandle, castedMetaInfo),
			stateDesc,
			namespaceSerializer,
			stateSerializer);

		newRocksStateInfo = new RocksDbKvStateInfo(oldStateInfo.columnFamilyHandle, newMetaInfo);
		kvStateInformation.put(stateDesc.getName(), newRocksStateInfo);
	} else {
		newMetaInfo = new RegisteredKeyValueStateBackendMetaInfo<>(
			stateDesc.getType(),
			stateDesc.getName(),
			namespaceSerializer,
			stateSerializer,
			StateSnapshotTransformFactory.noTransform());

		newRocksStateInfo = RocksDBOperationUtils.createStateInfo(
			newMetaInfo, db, columnFamilyOptionsFactory, ttlCompactFiltersManager);
		RocksDBOperationUtils.registerKvStateInformation(this.kvStateInformation, this.nativeMetricMonitor,
			stateDesc.getName(), newRocksStateInfo);
	}

	StateSnapshotTransformFactory<SV> wrappedSnapshotTransformFactory = wrapStateSnapshotTransformFactory(
		stateDesc, snapshotTransformFactory, newMetaInfo.getStateSerializer());
	newMetaInfo.updateSnapshotTransformFactory(wrappedSnapshotTransformFactory);

	ttlCompactFiltersManager.configCompactFilter(stateDesc, newMetaInfo.getStateSerializer());

	return Tuple2.of(newRocksStateInfo.columnFamilyHandle, newMetaInfo);
}
 
Example 5
Source File: HeapKeyedStateBackend.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private <N, V> StateTable<K, N, V> tryRegisterStateTable(
	TypeSerializer<N> namespaceSerializer,
	StateDescriptor<?, V> stateDesc,
	@Nonnull StateSnapshotTransformFactory<V> snapshotTransformFactory) throws StateMigrationException {

	@SuppressWarnings("unchecked")
	StateTable<K, N, V> stateTable = (StateTable<K, N, V>) registeredKVStates.get(stateDesc.getName());

	TypeSerializer<V> newStateSerializer = stateDesc.getSerializer();

	if (stateTable != null) {
		RegisteredKeyValueStateBackendMetaInfo<N, V> restoredKvMetaInfo = stateTable.getMetaInfo();

		restoredKvMetaInfo.updateSnapshotTransformFactory(snapshotTransformFactory);

		TypeSerializerSchemaCompatibility<N> namespaceCompatibility =
			restoredKvMetaInfo.updateNamespaceSerializer(namespaceSerializer);
		if (namespaceCompatibility.isCompatibleAfterMigration() || namespaceCompatibility.isIncompatible()) {
			throw new StateMigrationException("For heap backends, the new namespace serializer must be compatible.");
		}

		restoredKvMetaInfo.checkStateMetaInfo(stateDesc);

		TypeSerializerSchemaCompatibility<V> stateCompatibility =
			restoredKvMetaInfo.updateStateSerializer(newStateSerializer);

		if (stateCompatibility.isIncompatible()) {
			throw new StateMigrationException("For heap backends, the new state serializer must not be incompatible.");
		}

		stateTable.setMetaInfo(restoredKvMetaInfo);
	} else {
		RegisteredKeyValueStateBackendMetaInfo<N, V> newMetaInfo = new RegisteredKeyValueStateBackendMetaInfo<>(
			stateDesc.getType(),
			stateDesc.getName(),
			namespaceSerializer,
			newStateSerializer,
			snapshotTransformFactory);

		stateTable = snapshotStrategy.newStateTable(this, newMetaInfo);
		registeredKVStates.put(stateDesc.getName(), stateTable);
	}

	return stateTable;
}
 
Example 6
Source File: RocksDBKeyedStateBackend.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Registers a k/v state information, which includes its state id, type, RocksDB column family handle, and serializers.
 *
 * <p>When restoring from a snapshot, we don’t restore the individual k/v states, just the global RocksDB database and
 * the list of k/v state information. When a k/v state is first requested we check here whether we
 * already have a registered entry for that and return it (after some necessary state compatibility checks)
 * or create a new one if it does not exist.
 */
private <N, S extends State, SV, SEV> Tuple2<ColumnFamilyHandle, RegisteredKeyValueStateBackendMetaInfo<N, SV>> tryRegisterKvStateInformation(
	StateDescriptor<S, SV> stateDesc,
	TypeSerializer<N> namespaceSerializer,
	@Nonnull StateSnapshotTransformFactory<SEV> snapshotTransformFactory) throws Exception {

	RocksDbKvStateInfo oldStateInfo = kvStateInformation.get(stateDesc.getName());

	TypeSerializer<SV> stateSerializer = stateDesc.getSerializer();

	RocksDbKvStateInfo newRocksStateInfo;
	RegisteredKeyValueStateBackendMetaInfo<N, SV> newMetaInfo;
	if (oldStateInfo != null) {
		@SuppressWarnings("unchecked")
		RegisteredKeyValueStateBackendMetaInfo<N, SV> castedMetaInfo =
			(RegisteredKeyValueStateBackendMetaInfo<N, SV>) oldStateInfo.metaInfo;

		newMetaInfo = updateRestoredStateMetaInfo(
			Tuple2.of(oldStateInfo.columnFamilyHandle, castedMetaInfo),
			stateDesc,
			namespaceSerializer,
			stateSerializer);

		newRocksStateInfo = new RocksDbKvStateInfo(oldStateInfo.columnFamilyHandle, newMetaInfo);
		kvStateInformation.put(stateDesc.getName(), newRocksStateInfo);
	} else {
		newMetaInfo = new RegisteredKeyValueStateBackendMetaInfo<>(
			stateDesc.getType(),
			stateDesc.getName(),
			namespaceSerializer,
			stateSerializer,
			StateSnapshotTransformFactory.noTransform());

		newRocksStateInfo = RocksDBOperationUtils.createStateInfo(
			newMetaInfo, db, columnFamilyOptionsFactory, ttlCompactFiltersManager);
		RocksDBOperationUtils.registerKvStateInformation(this.kvStateInformation, this.nativeMetricMonitor,
			stateDesc.getName(), newRocksStateInfo);
	}

	StateSnapshotTransformFactory<SV> wrappedSnapshotTransformFactory = wrapStateSnapshotTransformFactory(
		stateDesc, snapshotTransformFactory, newMetaInfo.getStateSerializer());
	newMetaInfo.updateSnapshotTransformFactory(wrappedSnapshotTransformFactory);

	ttlCompactFiltersManager.configCompactFilter(stateDesc, newMetaInfo.getStateSerializer());

	return Tuple2.of(newRocksStateInfo.columnFamilyHandle, newMetaInfo);
}
 
Example 7
Source File: HeapKeyedStateBackend.java    From flink with Apache License 2.0 4 votes vote down vote up
private <N, V> StateTable<K, N, V> tryRegisterStateTable(
	TypeSerializer<N> namespaceSerializer,
	StateDescriptor<?, V> stateDesc,
	@Nonnull StateSnapshotTransformFactory<V> snapshotTransformFactory) throws StateMigrationException {

	@SuppressWarnings("unchecked")
	StateTable<K, N, V> stateTable = (StateTable<K, N, V>) registeredKVStates.get(stateDesc.getName());

	TypeSerializer<V> newStateSerializer = stateDesc.getSerializer();

	if (stateTable != null) {
		RegisteredKeyValueStateBackendMetaInfo<N, V> restoredKvMetaInfo = stateTable.getMetaInfo();

		restoredKvMetaInfo.updateSnapshotTransformFactory(snapshotTransformFactory);

		TypeSerializerSchemaCompatibility<N> namespaceCompatibility =
			restoredKvMetaInfo.updateNamespaceSerializer(namespaceSerializer);
		if (namespaceCompatibility.isCompatibleAfterMigration() || namespaceCompatibility.isIncompatible()) {
			throw new StateMigrationException("For heap backends, the new namespace serializer must be compatible.");
		}

		restoredKvMetaInfo.checkStateMetaInfo(stateDesc);

		TypeSerializerSchemaCompatibility<V> stateCompatibility =
			restoredKvMetaInfo.updateStateSerializer(newStateSerializer);

		if (stateCompatibility.isIncompatible()) {
			throw new StateMigrationException("For heap backends, the new state serializer must not be incompatible.");
		}

		stateTable.setMetaInfo(restoredKvMetaInfo);
	} else {
		RegisteredKeyValueStateBackendMetaInfo<N, V> newMetaInfo = new RegisteredKeyValueStateBackendMetaInfo<>(
			stateDesc.getType(),
			stateDesc.getName(),
			namespaceSerializer,
			newStateSerializer,
			snapshotTransformFactory);

		stateTable = snapshotStrategy.newStateTable(keyContext, newMetaInfo, keySerializer);
		registeredKVStates.put(stateDesc.getName(), stateTable);
	}

	return stateTable;
}
 
Example 8
Source File: RocksDBKeyedStateBackend.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Registers a k/v state information, which includes its state id, type, RocksDB column family handle, and serializers.
 *
 * <p>When restoring from a snapshot, we don’t restore the individual k/v states, just the global RocksDB database and
 * the list of k/v state information. When a k/v state is first requested we check here whether we
 * already have a registered entry for that and return it (after some necessary state compatibility checks)
 * or create a new one if it does not exist.
 */
private <N, S extends State, SV, SEV> Tuple2<ColumnFamilyHandle, RegisteredKeyValueStateBackendMetaInfo<N, SV>> tryRegisterKvStateInformation(
	StateDescriptor<S, SV> stateDesc,
	TypeSerializer<N> namespaceSerializer,
	@Nonnull StateSnapshotTransformFactory<SEV> snapshotTransformFactory) throws Exception {

	RocksDbKvStateInfo oldStateInfo = kvStateInformation.get(stateDesc.getName());

	TypeSerializer<SV> stateSerializer = stateDesc.getSerializer();

	RocksDbKvStateInfo newRocksStateInfo;
	RegisteredKeyValueStateBackendMetaInfo<N, SV> newMetaInfo;
	if (oldStateInfo != null) {
		@SuppressWarnings("unchecked")
		RegisteredKeyValueStateBackendMetaInfo<N, SV> castedMetaInfo =
			(RegisteredKeyValueStateBackendMetaInfo<N, SV>) oldStateInfo.metaInfo;

		newMetaInfo = updateRestoredStateMetaInfo(
			Tuple2.of(oldStateInfo.columnFamilyHandle, castedMetaInfo),
			stateDesc,
			namespaceSerializer,
			stateSerializer);

		newRocksStateInfo = new RocksDbKvStateInfo(oldStateInfo.columnFamilyHandle, newMetaInfo);
		kvStateInformation.put(stateDesc.getName(), newRocksStateInfo);
	} else {
		newMetaInfo = new RegisteredKeyValueStateBackendMetaInfo<>(
			stateDesc.getType(),
			stateDesc.getName(),
			namespaceSerializer,
			stateSerializer,
			StateSnapshotTransformFactory.noTransform());

		newRocksStateInfo = RocksDBOperationUtils.createStateInfo(
			newMetaInfo, db, columnFamilyOptionsFactory, ttlCompactFiltersManager);
		RocksDBOperationUtils.registerKvStateInformation(this.kvStateInformation, this.nativeMetricMonitor,
			stateDesc.getName(), newRocksStateInfo);
	}

	StateSnapshotTransformFactory<SV> wrappedSnapshotTransformFactory = wrapStateSnapshotTransformFactory(
		stateDesc, snapshotTransformFactory, newMetaInfo.getStateSerializer());
	newMetaInfo.updateSnapshotTransformFactory(wrappedSnapshotTransformFactory);

	ttlCompactFiltersManager.configCompactFilter(stateDesc, newMetaInfo.getStateSerializer());

	return Tuple2.of(newRocksStateInfo.columnFamilyHandle, newMetaInfo);
}
 
Example 9
Source File: HeapKeyedStateBackend.java    From flink with Apache License 2.0 4 votes vote down vote up
private <N, V> StateTable<K, N, V> tryRegisterStateTable(
	TypeSerializer<N> namespaceSerializer,
	StateDescriptor<?, V> stateDesc,
	@Nonnull StateSnapshotTransformFactory<V> snapshotTransformFactory) throws StateMigrationException {

	@SuppressWarnings("unchecked")
	StateTable<K, N, V> stateTable = (StateTable<K, N, V>) registeredKVStates.get(stateDesc.getName());

	TypeSerializer<V> newStateSerializer = stateDesc.getSerializer();

	if (stateTable != null) {
		RegisteredKeyValueStateBackendMetaInfo<N, V> restoredKvMetaInfo = stateTable.getMetaInfo();

		restoredKvMetaInfo.updateSnapshotTransformFactory(snapshotTransformFactory);

		TypeSerializerSchemaCompatibility<N> namespaceCompatibility =
			restoredKvMetaInfo.updateNamespaceSerializer(namespaceSerializer);
		if (namespaceCompatibility.isCompatibleAfterMigration() || namespaceCompatibility.isIncompatible()) {
			throw new StateMigrationException("For heap backends, the new namespace serializer must be compatible.");
		}

		restoredKvMetaInfo.checkStateMetaInfo(stateDesc);

		TypeSerializerSchemaCompatibility<V> stateCompatibility =
			restoredKvMetaInfo.updateStateSerializer(newStateSerializer);

		if (stateCompatibility.isIncompatible()) {
			throw new StateMigrationException("For heap backends, the new state serializer must not be incompatible.");
		}

		stateTable.setMetaInfo(restoredKvMetaInfo);
	} else {
		RegisteredKeyValueStateBackendMetaInfo<N, V> newMetaInfo = new RegisteredKeyValueStateBackendMetaInfo<>(
			stateDesc.getType(),
			stateDesc.getName(),
			namespaceSerializer,
			newStateSerializer,
			snapshotTransformFactory);

		stateTable = snapshotStrategy.newStateTable(keyContext, newMetaInfo, keySerializer);
		registeredKVStates.put(stateDesc.getName(), stateTable);
	}

	return stateTable;
}