Java Code Examples for org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility#isIncompatible()

The following examples show how to use org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility#isIncompatible() . 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: AbstractRocksDBRestoreOperation.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
KeyedBackendSerializationProxy<K> readMetaData(DataInputView dataInputView)
	throws IOException, StateMigrationException {
	// isSerializerPresenceRequired flag is set to false, since for the RocksDB state backend,
	// deserialization of state happens lazily during runtime; we depend on the fact
	// that the new serializer for states could be compatible, and therefore the restore can continue
	// without old serializers required to be present.
	KeyedBackendSerializationProxy<K> serializationProxy =
		new KeyedBackendSerializationProxy<>(userCodeClassLoader);
	serializationProxy.read(dataInputView);
	if (!isKeySerializerCompatibilityChecked) {
		// check for key serializer compatibility; this also reconfigures the
		// key serializer to be compatible, if it is required and is possible
		TypeSerializerSchemaCompatibility<K> keySerializerSchemaCompat =
			keySerializerProvider.setPreviousSerializerSnapshotForRestoredState(serializationProxy.getKeySerializerSnapshot());
		if (keySerializerSchemaCompat.isCompatibleAfterMigration() || keySerializerSchemaCompat.isIncompatible()) {
			throw new StateMigrationException("The new key serializer must be compatible.");
		}

		isKeySerializerCompatibilityChecked = true;
	}

	return serializationProxy;
}
 
Example 2
Source File: StateSerializerProvider.java    From flink with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
@SuppressWarnings("ConstantConditions")
public TypeSerializerSchemaCompatibility<T> registerNewSerializerForRestoredState(TypeSerializer<T> newSerializer) {
	checkNotNull(newSerializer);
	if (registeredSerializer != null) {
		throw new UnsupportedOperationException("A serializer has already been registered for the state; re-registration is not allowed.");
	}

	TypeSerializerSchemaCompatibility<T> result = previousSerializerSnapshot.resolveSchemaCompatibility(newSerializer);
	if (result.isIncompatible()) {
		invalidateCurrentSchemaSerializerAccess();
	}
	if (result.isCompatibleWithReconfiguredSerializer()) {
		this.registeredSerializer = result.getReconfiguredSerializer();
	} else {
		this.registeredSerializer = newSerializer;
	}
	return result;
}
 
Example 3
Source File: StateSerializerProvider.java    From flink with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public TypeSerializerSchemaCompatibility<T> setPreviousSerializerSnapshotForRestoredState(TypeSerializerSnapshot<T> previousSerializerSnapshot) {
	checkNotNull(previousSerializerSnapshot);
	if (this.previousSerializerSnapshot != null) {
		throw new UnsupportedOperationException("The snapshot of the state's previous serializer has already been set; cannot reset.");
	}

	this.previousSerializerSnapshot = previousSerializerSnapshot;

	TypeSerializerSchemaCompatibility<T> result = previousSerializerSnapshot.resolveSchemaCompatibility(registeredSerializer);
	if (result.isIncompatible()) {
		invalidateCurrentSchemaSerializerAccess();
	}
	if (result.isCompatibleWithReconfiguredSerializer()) {
		this.registeredSerializer = result.getReconfiguredSerializer();
	}
	return result;
}
 
Example 4
Source File: AbstractRocksDBRestoreOperation.java    From flink with Apache License 2.0 6 votes vote down vote up
KeyedBackendSerializationProxy<K> readMetaData(DataInputView dataInputView)
	throws IOException, StateMigrationException {
	// isSerializerPresenceRequired flag is set to false, since for the RocksDB state backend,
	// deserialization of state happens lazily during runtime; we depend on the fact
	// that the new serializer for states could be compatible, and therefore the restore can continue
	// without old serializers required to be present.
	KeyedBackendSerializationProxy<K> serializationProxy =
		new KeyedBackendSerializationProxy<>(userCodeClassLoader);
	serializationProxy.read(dataInputView);
	if (!isKeySerializerCompatibilityChecked) {
		// check for key serializer compatibility; this also reconfigures the
		// key serializer to be compatible, if it is required and is possible
		TypeSerializerSchemaCompatibility<K> keySerializerSchemaCompat =
			keySerializerProvider.setPreviousSerializerSnapshotForRestoredState(serializationProxy.getKeySerializerSnapshot());
		if (keySerializerSchemaCompat.isCompatibleAfterMigration() || keySerializerSchemaCompat.isIncompatible()) {
			throw new StateMigrationException("The new key serializer must be compatible.");
		}

		isKeySerializerCompatibilityChecked = true;
	}

	return serializationProxy;
}
 
Example 5
Source File: RocksDBKeyedStateBackend.java    From flink with Apache License 2.0 6 votes vote down vote up
private <N, S extends State, SV> RegisteredKeyValueStateBackendMetaInfo<N, SV> updateRestoredStateMetaInfo(
	Tuple2<ColumnFamilyHandle, RegisteredKeyValueStateBackendMetaInfo<N, SV>> oldStateInfo,
	StateDescriptor<S, SV> stateDesc,
	TypeSerializer<N> namespaceSerializer,
	TypeSerializer<SV> stateSerializer) throws Exception {

	@SuppressWarnings("unchecked")
	RegisteredKeyValueStateBackendMetaInfo<N, SV> restoredKvStateMetaInfo = oldStateInfo.f1;

	TypeSerializerSchemaCompatibility<N> s = restoredKvStateMetaInfo.updateNamespaceSerializer(namespaceSerializer);
	if (s.isCompatibleAfterMigration() || s.isIncompatible()) {
		throw new StateMigrationException("The new namespace serializer must be compatible.");
	}

	restoredKvStateMetaInfo.checkStateMetaInfo(stateDesc);

	TypeSerializerSchemaCompatibility<SV> newStateSerializerCompatibility =
		restoredKvStateMetaInfo.updateStateSerializer(stateSerializer);
	if (newStateSerializerCompatibility.isCompatibleAfterMigration()) {
		migrateStateValues(stateDesc, oldStateInfo);
	} else if (newStateSerializerCompatibility.isIncompatible()) {
		throw new StateMigrationException("The new state serializer cannot be incompatible.");
	}

	return restoredKvStateMetaInfo;
}
 
Example 6
Source File: AbstractRocksDBRestoreOperation.java    From flink with Apache License 2.0 6 votes vote down vote up
KeyedBackendSerializationProxy<K> readMetaData(DataInputView dataInputView)
	throws IOException, StateMigrationException {
	// isSerializerPresenceRequired flag is set to false, since for the RocksDB state backend,
	// deserialization of state happens lazily during runtime; we depend on the fact
	// that the new serializer for states could be compatible, and therefore the restore can continue
	// without old serializers required to be present.
	KeyedBackendSerializationProxy<K> serializationProxy =
		new KeyedBackendSerializationProxy<>(userCodeClassLoader);
	serializationProxy.read(dataInputView);
	if (!isKeySerializerCompatibilityChecked) {
		// check for key serializer compatibility; this also reconfigures the
		// key serializer to be compatible, if it is required and is possible
		TypeSerializerSchemaCompatibility<K> keySerializerSchemaCompat =
			keySerializerProvider.setPreviousSerializerSnapshotForRestoredState(serializationProxy.getKeySerializerSnapshot());
		if (keySerializerSchemaCompat.isCompatibleAfterMigration() || keySerializerSchemaCompat.isIncompatible()) {
			throw new StateMigrationException("The new key serializer must be compatible.");
		}

		isKeySerializerCompatibilityChecked = true;
	}

	return serializationProxy;
}
 
Example 7
Source File: StateSerializerProvider.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public TypeSerializerSchemaCompatibility<T> setPreviousSerializerSnapshotForRestoredState(TypeSerializerSnapshot<T> previousSerializerSnapshot) {
	checkNotNull(previousSerializerSnapshot);
	if (this.previousSerializerSnapshot != null) {
		throw new UnsupportedOperationException("The snapshot of the state's previous serializer has already been set; cannot reset.");
	}

	this.previousSerializerSnapshot = previousSerializerSnapshot;

	TypeSerializerSchemaCompatibility<T> result = previousSerializerSnapshot.resolveSchemaCompatibility(registeredSerializer);
	if (result.isIncompatible()) {
		invalidateCurrentSchemaSerializerAccess();
	}
	if (result.isCompatibleWithReconfiguredSerializer()) {
		this.registeredSerializer = result.getReconfiguredSerializer();
	}
	return result;
}
 
Example 8
Source File: StateSerializerProvider.java    From flink with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
@SuppressWarnings("ConstantConditions")
public TypeSerializerSchemaCompatibility<T> registerNewSerializerForRestoredState(TypeSerializer<T> newSerializer) {
	checkNotNull(newSerializer);
	if (registeredSerializer != null) {
		throw new UnsupportedOperationException("A serializer has already been registered for the state; re-registration is not allowed.");
	}

	TypeSerializerSchemaCompatibility<T> result = previousSerializerSnapshot.resolveSchemaCompatibility(newSerializer);
	if (result.isIncompatible()) {
		invalidateCurrentSchemaSerializerAccess();
	}
	if (result.isCompatibleWithReconfiguredSerializer()) {
		this.registeredSerializer = result.getReconfiguredSerializer();
	} else {
		this.registeredSerializer = newSerializer;
	}
	return result;
}
 
Example 9
Source File: RocksDBKeyedStateBackend.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private <N, S extends State, SV> RegisteredKeyValueStateBackendMetaInfo<N, SV> updateRestoredStateMetaInfo(
	Tuple2<ColumnFamilyHandle, RegisteredKeyValueStateBackendMetaInfo<N, SV>> oldStateInfo,
	StateDescriptor<S, SV> stateDesc,
	TypeSerializer<N> namespaceSerializer,
	TypeSerializer<SV> stateSerializer) throws Exception {

	@SuppressWarnings("unchecked")
	RegisteredKeyValueStateBackendMetaInfo<N, SV> restoredKvStateMetaInfo = oldStateInfo.f1;

	TypeSerializerSchemaCompatibility<N> s = restoredKvStateMetaInfo.updateNamespaceSerializer(namespaceSerializer);
	if (s.isCompatibleAfterMigration() || s.isIncompatible()) {
		throw new StateMigrationException("The new namespace serializer must be compatible.");
	}

	restoredKvStateMetaInfo.checkStateMetaInfo(stateDesc);

	TypeSerializerSchemaCompatibility<SV> newStateSerializerCompatibility =
		restoredKvStateMetaInfo.updateStateSerializer(stateSerializer);
	if (newStateSerializerCompatibility.isCompatibleAfterMigration()) {
		migrateStateValues(stateDesc, oldStateInfo);
	} else if (newStateSerializerCompatibility.isIncompatible()) {
		throw new StateMigrationException("The new state serializer cannot be incompatible.");
	}

	return restoredKvStateMetaInfo;
}
 
Example 10
Source File: RocksDBKeyedStateBackend.java    From flink with Apache License 2.0 6 votes vote down vote up
private <N, S extends State, SV> RegisteredKeyValueStateBackendMetaInfo<N, SV> updateRestoredStateMetaInfo(
	Tuple2<ColumnFamilyHandle, RegisteredKeyValueStateBackendMetaInfo<N, SV>> oldStateInfo,
	StateDescriptor<S, SV> stateDesc,
	TypeSerializer<N> namespaceSerializer,
	TypeSerializer<SV> stateSerializer) throws Exception {

	@SuppressWarnings("unchecked")
	RegisteredKeyValueStateBackendMetaInfo<N, SV> restoredKvStateMetaInfo = oldStateInfo.f1;

	TypeSerializerSchemaCompatibility<N> s = restoredKvStateMetaInfo.updateNamespaceSerializer(namespaceSerializer);
	if (s.isCompatibleAfterMigration() || s.isIncompatible()) {
		throw new StateMigrationException("The new namespace serializer must be compatible.");
	}

	restoredKvStateMetaInfo.checkStateMetaInfo(stateDesc);

	TypeSerializerSchemaCompatibility<SV> newStateSerializerCompatibility =
		restoredKvStateMetaInfo.updateStateSerializer(stateSerializer);
	if (newStateSerializerCompatibility.isCompatibleAfterMigration()) {
		migrateStateValues(stateDesc, oldStateInfo);
	} else if (newStateSerializerCompatibility.isIncompatible()) {
		throw new StateMigrationException("The new state serializer cannot be incompatible.");
	}

	return restoredKvStateMetaInfo;
}
 
Example 11
Source File: HeapKeyedStateBackend.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nonnull
@Override
public <T extends HeapPriorityQueueElement & PriorityComparable & Keyed> KeyGroupedInternalPriorityQueue<T> create(
	@Nonnull String stateName,
	@Nonnull TypeSerializer<T> byteOrderedElementSerializer) {

	final HeapPriorityQueueSnapshotRestoreWrapper existingState = registeredPQStates.get(stateName);

	if (existingState != null) {
		// TODO we implement the simple way of supporting the current functionality, mimicking keyed state
		// because this should be reworked in FLINK-9376 and then we should have a common algorithm over
		// StateMetaInfoSnapshot that avoids this code duplication.

		TypeSerializerSchemaCompatibility<T> compatibilityResult =
			existingState.getMetaInfo().updateElementSerializer(byteOrderedElementSerializer);

		if (compatibilityResult.isIncompatible()) {
			throw new FlinkRuntimeException(new StateMigrationException("For heap backends, the new priority queue serializer must not be incompatible."));
		} else {
			registeredPQStates.put(
				stateName,
				existingState.forUpdatedSerializer(byteOrderedElementSerializer));
		}

		return existingState.getPriorityQueue();
	} else {
		final RegisteredPriorityQueueStateBackendMetaInfo<T> metaInfo =
			new RegisteredPriorityQueueStateBackendMetaInfo<>(stateName, byteOrderedElementSerializer);
		return createInternal(metaInfo);
	}
}
 
Example 12
Source File: DefaultOperatorStateBackend.java    From flink with Apache License 2.0 4 votes vote down vote up
private <S> ListState<S> getListState(
		ListStateDescriptor<S> stateDescriptor,
		OperatorStateHandle.Mode mode) throws StateMigrationException {

	Preconditions.checkNotNull(stateDescriptor);
	String name = Preconditions.checkNotNull(stateDescriptor.getName());

	@SuppressWarnings("unchecked")
	PartitionableListState<S> previous = (PartitionableListState<S>) accessedStatesByName.get(name);
	if (previous != null) {
		checkStateNameAndMode(
				previous.getStateMetaInfo().getName(),
				name,
				previous.getStateMetaInfo().getAssignmentMode(),
				mode);
		return previous;
	}

	// end up here if its the first time access after execution for the
	// provided state name; check compatibility of restored state, if any
	// TODO with eager registration in place, these checks should be moved to restore()

	stateDescriptor.initializeSerializerUnlessSet(getExecutionConfig());
	TypeSerializer<S> partitionStateSerializer = Preconditions.checkNotNull(stateDescriptor.getElementSerializer());

	@SuppressWarnings("unchecked")
	PartitionableListState<S> partitionableListState = (PartitionableListState<S>) registeredOperatorStates.get(name);

	if (null == partitionableListState) {
		// no restored state for the state name; simply create new state holder

		partitionableListState = new PartitionableListState<>(
			new RegisteredOperatorStateBackendMetaInfo<>(
				name,
				partitionStateSerializer,
				mode));

		registeredOperatorStates.put(name, partitionableListState);
	} else {
		// has restored state; check compatibility of new state access

		checkStateNameAndMode(
				partitionableListState.getStateMetaInfo().getName(),
				name,
				partitionableListState.getStateMetaInfo().getAssignmentMode(),
				mode);

		RegisteredOperatorStateBackendMetaInfo<S> restoredPartitionableListStateMetaInfo =
			partitionableListState.getStateMetaInfo();

		// check compatibility to determine if new serializers are incompatible
		TypeSerializer<S> newPartitionStateSerializer = partitionStateSerializer.duplicate();

		TypeSerializerSchemaCompatibility<S> stateCompatibility =
			restoredPartitionableListStateMetaInfo.updatePartitionStateSerializer(newPartitionStateSerializer);
		if (stateCompatibility.isIncompatible()) {
			throw new StateMigrationException("The new state typeSerializer for operator state must not be incompatible.");
		}

		partitionableListState.setStateMetaInfo(restoredPartitionableListStateMetaInfo);
	}

	accessedStatesByName.put(name, partitionableListState);
	return partitionableListState;
}
 
Example 13
Source File: InternalTimerServiceImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Starts the local {@link InternalTimerServiceImpl} by:
 * <ol>
 *     <li>Setting the {@code keySerialized} and {@code namespaceSerializer} for the timers it will contain.</li>
 *     <li>Setting the {@code triggerTarget} which contains the action to be performed when a timer fires.</li>
 *     <li>Re-registering timers that were retrieved after recovering from a node failure, if any.</li>
 * </ol>
 * This method can be called multiple times, as long as it is called with the same serializers.
 */
public void startTimerService(
		TypeSerializer<K> keySerializer,
		TypeSerializer<N> namespaceSerializer,
		Triggerable<K, N> triggerTarget) {

	if (!isInitialized) {

		if (keySerializer == null || namespaceSerializer == null) {
			throw new IllegalArgumentException("The TimersService serializers cannot be null.");
		}

		if (this.keySerializer != null || this.namespaceSerializer != null || this.triggerTarget != null) {
			throw new IllegalStateException("The TimerService has already been initialized.");
		}

		// the following is the case where we restore
		if (restoredTimersSnapshot != null) {
			TypeSerializerSchemaCompatibility<K> keySerializerCompatibility =
				restoredTimersSnapshot.getKeySerializerSnapshot().resolveSchemaCompatibility(keySerializer);

			if (keySerializerCompatibility.isIncompatible() || keySerializerCompatibility.isCompatibleAfterMigration()) {
				throw new IllegalStateException(
					"Tried to initialize restored TimerService with new key serializer that requires migration or is incompatible.");
			}

			TypeSerializerSchemaCompatibility<N> namespaceSerializerCompatibility =
				restoredTimersSnapshot.getNamespaceSerializerSnapshot().resolveSchemaCompatibility(namespaceSerializer);

			restoredTimersSnapshot = null;

			if (namespaceSerializerCompatibility.isIncompatible() || namespaceSerializerCompatibility.isCompatibleAfterMigration()) {
				throw new IllegalStateException(
					"Tried to initialize restored TimerService with new namespace serializer that requires migration or is incompatible.");
			}

			this.keySerializer = keySerializerCompatibility.isCompatibleAsIs()
				? keySerializer : keySerializerCompatibility.getReconfiguredSerializer();
			this.namespaceSerializer = namespaceSerializerCompatibility.isCompatibleAsIs()
				? namespaceSerializer : namespaceSerializerCompatibility.getReconfiguredSerializer();
		} else {
			this.keySerializer = keySerializer;
			this.namespaceSerializer = namespaceSerializer;
		}

		this.keyDeserializer = null;
		this.namespaceDeserializer = null;

		this.triggerTarget = Preconditions.checkNotNull(triggerTarget);

		// re-register the restored timers (if any)
		final InternalTimer<K, N> headTimer = processingTimeTimersQueue.peek();
		if (headTimer != null) {
			nextTimer = processingTimeService.registerTimer(headTimer.getTimestamp(), this::onProcessingTime);
		}
		this.isInitialized = true;
	} else {
		if (!(this.keySerializer.equals(keySerializer) && this.namespaceSerializer.equals(namespaceSerializer))) {
			throw new IllegalArgumentException("Already initialized Timer Service " +
				"tried to be initialized with different key and namespace serializers.");
		}
	}
}
 
Example 14
Source File: HeapRestoreOperation.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public Void restore() throws Exception {

	registeredKVStates.clear();
	registeredPQStates.clear();

	boolean keySerializerRestored = false;

	for (KeyedStateHandle keyedStateHandle : restoreStateHandles) {

		if (keyedStateHandle == null) {
			continue;
		}

		if (!(keyedStateHandle instanceof KeyGroupsStateHandle)) {
			throw new IllegalStateException("Unexpected state handle type, " +
				"expected: " + KeyGroupsStateHandle.class +
				", but found: " + keyedStateHandle.getClass());
		}

		KeyGroupsStateHandle keyGroupsStateHandle = (KeyGroupsStateHandle) keyedStateHandle;
		FSDataInputStream fsDataInputStream = keyGroupsStateHandle.openInputStream();
		cancelStreamRegistry.registerCloseable(fsDataInputStream);

		try {
			DataInputViewStreamWrapper inView = new DataInputViewStreamWrapper(fsDataInputStream);

			KeyedBackendSerializationProxy<K> serializationProxy =
				new KeyedBackendSerializationProxy<>(userCodeClassLoader);

			serializationProxy.read(inView);

			if (!keySerializerRestored) {
				// check for key serializer compatibility; this also reconfigures the
				// key serializer to be compatible, if it is required and is possible
				TypeSerializerSchemaCompatibility<K> keySerializerSchemaCompat =
					keySerializerProvider.setPreviousSerializerSnapshotForRestoredState(serializationProxy.getKeySerializerSnapshot());
				if (keySerializerSchemaCompat.isCompatibleAfterMigration() || keySerializerSchemaCompat.isIncompatible()) {
					throw new StateMigrationException("The new key serializer must be compatible.");
				}

				keySerializerRestored = true;
			}

			List<StateMetaInfoSnapshot> restoredMetaInfos =
				serializationProxy.getStateMetaInfoSnapshots();

			final Map<Integer, StateMetaInfoSnapshot> kvStatesById = new HashMap<>();

			createOrCheckStateForMetaInfo(restoredMetaInfos, kvStatesById);

			readStateHandleStateData(
				fsDataInputStream,
				inView,
				keyGroupsStateHandle.getGroupRangeOffsets(),
				kvStatesById, restoredMetaInfos.size(),
				serializationProxy.getReadVersion(),
				serializationProxy.isUsingKeyGroupCompression());
		} finally {
			if (cancelStreamRegistry.unregisterCloseable(fsDataInputStream)) {
				IOUtils.closeQuietly(fsDataInputStream);
			}
		}
	}
	return null;
}
 
Example 15
Source File: InternalTimerServiceImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Starts the local {@link InternalTimerServiceImpl} by:
 * <ol>
 *     <li>Setting the {@code keySerialized} and {@code namespaceSerializer} for the timers it will contain.</li>
 *     <li>Setting the {@code triggerTarget} which contains the action to be performed when a timer fires.</li>
 *     <li>Re-registering timers that were retrieved after recovering from a node failure, if any.</li>
 * </ol>
 * This method can be called multiple times, as long as it is called with the same serializers.
 */
public void startTimerService(
		TypeSerializer<K> keySerializer,
		TypeSerializer<N> namespaceSerializer,
		Triggerable<K, N> triggerTarget) {

	if (!isInitialized) {

		if (keySerializer == null || namespaceSerializer == null) {
			throw new IllegalArgumentException("The TimersService serializers cannot be null.");
		}

		if (this.keySerializer != null || this.namespaceSerializer != null || this.triggerTarget != null) {
			throw new IllegalStateException("The TimerService has already been initialized.");
		}

		// the following is the case where we restore
		if (restoredTimersSnapshot != null) {
			TypeSerializerSchemaCompatibility<K> keySerializerCompatibility =
				restoredTimersSnapshot.getKeySerializerSnapshot().resolveSchemaCompatibility(keySerializer);

			if (keySerializerCompatibility.isIncompatible() || keySerializerCompatibility.isCompatibleAfterMigration()) {
				throw new IllegalStateException(
					"Tried to initialize restored TimerService with new key serializer that requires migration or is incompatible.");
			}

			TypeSerializerSchemaCompatibility<N> namespaceSerializerCompatibility =
				restoredTimersSnapshot.getNamespaceSerializerSnapshot().resolveSchemaCompatibility(namespaceSerializer);

			if (namespaceSerializerCompatibility.isIncompatible() || namespaceSerializerCompatibility.isCompatibleAfterMigration()) {
				throw new IllegalStateException(
					"Tried to initialize restored TimerService with new namespace serializer that requires migration or is incompatible.");
			}

			this.keySerializer = keySerializerCompatibility.isCompatibleAsIs()
				? keySerializer : keySerializerCompatibility.getReconfiguredSerializer();
			this.namespaceSerializer = namespaceSerializerCompatibility.isCompatibleAsIs()
				? namespaceSerializer : namespaceSerializerCompatibility.getReconfiguredSerializer();
		} else {
			this.keySerializer = keySerializer;
			this.namespaceSerializer = namespaceSerializer;
		}

		this.keyDeserializer = null;
		this.namespaceDeserializer = null;

		this.triggerTarget = Preconditions.checkNotNull(triggerTarget);

		// re-register the restored timers (if any)
		final InternalTimer<K, N> headTimer = processingTimeTimersQueue.peek();
		if (headTimer != null) {
			nextTimer = processingTimeService.registerTimer(headTimer.getTimestamp(), this);
		}
		this.isInitialized = true;
	} else {
		if (!(this.keySerializer.equals(keySerializer) && this.namespaceSerializer.equals(namespaceSerializer))) {
			throw new IllegalArgumentException("Already initialized Timer Service " +
				"tried to be initialized with different key and namespace serializers.");
		}
	}
}
 
Example 16
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 17
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 18
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 19
Source File: DefaultOperatorStateBackend.java    From flink with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <K, V> BroadcastState<K, V> getBroadcastState(final MapStateDescriptor<K, V> stateDescriptor) throws StateMigrationException {

	Preconditions.checkNotNull(stateDescriptor);
	String name = Preconditions.checkNotNull(stateDescriptor.getName());

	BackendWritableBroadcastState<K, V> previous =
		(BackendWritableBroadcastState<K, V>) accessedBroadcastStatesByName.get(name);

	if (previous != null) {
		checkStateNameAndMode(
				previous.getStateMetaInfo().getName(),
				name,
				previous.getStateMetaInfo().getAssignmentMode(),
				OperatorStateHandle.Mode.BROADCAST);
		return previous;
	}

	stateDescriptor.initializeSerializerUnlessSet(getExecutionConfig());
	TypeSerializer<K> broadcastStateKeySerializer = Preconditions.checkNotNull(stateDescriptor.getKeySerializer());
	TypeSerializer<V> broadcastStateValueSerializer = Preconditions.checkNotNull(stateDescriptor.getValueSerializer());

	BackendWritableBroadcastState<K, V> broadcastState =
		(BackendWritableBroadcastState<K, V>) registeredBroadcastStates.get(name);

	if (broadcastState == null) {
		broadcastState = new HeapBroadcastState<>(
				new RegisteredBroadcastStateBackendMetaInfo<>(
						name,
						OperatorStateHandle.Mode.BROADCAST,
						broadcastStateKeySerializer,
						broadcastStateValueSerializer));
		registeredBroadcastStates.put(name, broadcastState);
	} else {
		// has restored state; check compatibility of new state access

		checkStateNameAndMode(
				broadcastState.getStateMetaInfo().getName(),
				name,
				broadcastState.getStateMetaInfo().getAssignmentMode(),
				OperatorStateHandle.Mode.BROADCAST);

		RegisteredBroadcastStateBackendMetaInfo<K, V> restoredBroadcastStateMetaInfo = broadcastState.getStateMetaInfo();

		// check whether new serializers are incompatible
		TypeSerializerSchemaCompatibility<K> keyCompatibility =
			restoredBroadcastStateMetaInfo.updateKeySerializer(broadcastStateKeySerializer);
		if (keyCompatibility.isIncompatible()) {
			throw new StateMigrationException("The new key typeSerializer for broadcast state must not be incompatible.");
		}

		TypeSerializerSchemaCompatibility<V> valueCompatibility =
			restoredBroadcastStateMetaInfo.updateValueSerializer(broadcastStateValueSerializer);
		if (valueCompatibility.isIncompatible()) {
			throw new StateMigrationException("The new value typeSerializer for broadcast state must not be incompatible.");
		}

		broadcastState.setStateMetaInfo(restoredBroadcastStateMetaInfo);
	}

	accessedBroadcastStatesByName.put(name, broadcastState);
	return broadcastState;
}
 
Example 20
Source File: DefaultOperatorStateBackend.java    From flink with Apache License 2.0 4 votes vote down vote up
private <S> ListState<S> getListState(
		ListStateDescriptor<S> stateDescriptor,
		OperatorStateHandle.Mode mode) throws StateMigrationException {

	Preconditions.checkNotNull(stateDescriptor);
	String name = Preconditions.checkNotNull(stateDescriptor.getName());

	@SuppressWarnings("unchecked")
	PartitionableListState<S> previous = (PartitionableListState<S>) accessedStatesByName.get(name);
	if (previous != null) {
		checkStateNameAndMode(
				previous.getStateMetaInfo().getName(),
				name,
				previous.getStateMetaInfo().getAssignmentMode(),
				mode);
		return previous;
	}

	// end up here if its the first time access after execution for the
	// provided state name; check compatibility of restored state, if any
	// TODO with eager registration in place, these checks should be moved to restore()

	stateDescriptor.initializeSerializerUnlessSet(getExecutionConfig());
	TypeSerializer<S> partitionStateSerializer = Preconditions.checkNotNull(stateDescriptor.getElementSerializer());

	@SuppressWarnings("unchecked")
	PartitionableListState<S> partitionableListState = (PartitionableListState<S>) registeredOperatorStates.get(name);

	if (null == partitionableListState) {
		// no restored state for the state name; simply create new state holder

		partitionableListState = new PartitionableListState<>(
			new RegisteredOperatorStateBackendMetaInfo<>(
				name,
				partitionStateSerializer,
				mode));

		registeredOperatorStates.put(name, partitionableListState);
	} else {
		// has restored state; check compatibility of new state access

		checkStateNameAndMode(
				partitionableListState.getStateMetaInfo().getName(),
				name,
				partitionableListState.getStateMetaInfo().getAssignmentMode(),
				mode);

		RegisteredOperatorStateBackendMetaInfo<S> restoredPartitionableListStateMetaInfo =
			partitionableListState.getStateMetaInfo();

		// check compatibility to determine if new serializers are incompatible
		TypeSerializer<S> newPartitionStateSerializer = partitionStateSerializer.duplicate();

		TypeSerializerSchemaCompatibility<S> stateCompatibility =
			restoredPartitionableListStateMetaInfo.updatePartitionStateSerializer(newPartitionStateSerializer);
		if (stateCompatibility.isIncompatible()) {
			throw new StateMigrationException("The new state typeSerializer for operator state must not be incompatible.");
		}

		partitionableListState.setStateMetaInfo(restoredPartitionableListStateMetaInfo);
	}

	accessedStatesByName.put(name, partitionableListState);
	return partitionableListState;
}