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

The following examples show how to use org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility#isCompatibleAsIs() . 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: SerializerTestUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Snapshot and restore the given serializer. Returns the restored serializer.
 */
public static <T> TypeSerializer<T> snapshotAndReconfigure(
	TypeSerializer<T> serializer, SerializerGetter<T> serializerGetter) throws IOException {
	TypeSerializerSnapshot<T> configSnapshot = serializer.snapshotConfiguration();

	byte[] serializedConfig;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			new DataOutputViewStreamWrapper(out), configSnapshot, serializer);
		serializedConfig = out.toByteArray();
	}

	TypeSerializerSnapshot<T> restoredConfig;
	try (ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
		restoredConfig = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in),
			Thread.currentThread().getContextClassLoader(),
			serializerGetter.getSerializer());
	}

	TypeSerializerSchemaCompatibility<T> strategy =
		restoredConfig.resolveSchemaCompatibility(serializerGetter.getSerializer());
	final TypeSerializer<T> restoredSerializer;
	if (strategy.isCompatibleAsIs()) {
		restoredSerializer = restoredConfig.restoreSerializer();
	}
	else if (strategy.isCompatibleWithReconfiguredSerializer()) {
		restoredSerializer = strategy.getReconfiguredSerializer();
	}
	else {
		throw new AssertionError("Unable to restore serializer with " + strategy);
	}
	assertEquals(serializer.getClass(), restoredSerializer.getClass());

	return restoredSerializer;
}
 
Example 2
Source File: RocksDBKeyedStateBackend.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <UK> boolean checkMapStateKeySchemaCompatibility(
	MapSerializerSnapshot<?, ?> mapStateSerializerSnapshot,
	MapSerializer<?, ?> newMapStateSerializer) {
	TypeSerializerSnapshot<UK> previousKeySerializerSnapshot = (TypeSerializerSnapshot<UK>) mapStateSerializerSnapshot.getKeySerializerSnapshot();
	TypeSerializer<UK> newUserKeySerializer = (TypeSerializer<UK>) newMapStateSerializer.getKeySerializer();

	TypeSerializerSchemaCompatibility<UK> keyCompatibility = previousKeySerializerSnapshot.resolveSchemaCompatibility(newUserKeySerializer);
	return keyCompatibility.isCompatibleAsIs();
}
 
Example 3
Source File: SerializerTestUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Snapshot and restore the given serializer. Returns the restored serializer.
 */
public static <T> TypeSerializer<T> snapshotAndReconfigure(
	TypeSerializer<T> serializer, SerializerGetter<T> serializerGetter) throws IOException {
	TypeSerializerSnapshot<T> configSnapshot = serializer.snapshotConfiguration();

	byte[] serializedConfig;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			new DataOutputViewStreamWrapper(out), configSnapshot, serializer);
		serializedConfig = out.toByteArray();
	}

	TypeSerializerSnapshot<T> restoredConfig;
	try (ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
		restoredConfig = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in),
			Thread.currentThread().getContextClassLoader(),
			serializerGetter.getSerializer());
	}

	TypeSerializerSchemaCompatibility<T> strategy =
		restoredConfig.resolveSchemaCompatibility(serializerGetter.getSerializer());
	final TypeSerializer<T> restoredSerializer;
	if (strategy.isCompatibleAsIs()) {
		restoredSerializer = restoredConfig.restoreSerializer();
	}
	else if (strategy.isCompatibleWithReconfiguredSerializer()) {
		restoredSerializer = strategy.getReconfiguredSerializer();
	}
	else {
		throw new AssertionError("Unable to restore serializer with " + strategy);
	}
	assertEquals(serializer.getClass(), restoredSerializer.getClass());

	return restoredSerializer;
}
 
Example 4
Source File: RocksDBKeyedStateBackend.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <UK> boolean checkMapStateKeySchemaCompatibility(
	MapSerializerSnapshot<?, ?> mapStateSerializerSnapshot,
	MapSerializer<?, ?> newMapStateSerializer) {
	TypeSerializerSnapshot<UK> previousKeySerializerSnapshot = (TypeSerializerSnapshot<UK>) mapStateSerializerSnapshot.getKeySerializerSnapshot();
	TypeSerializer<UK> newUserKeySerializer = (TypeSerializer<UK>) newMapStateSerializer.getKeySerializer();

	TypeSerializerSchemaCompatibility<UK> keyCompatibility = previousKeySerializerSnapshot.resolveSchemaCompatibility(newUserKeySerializer);
	return keyCompatibility.isCompatibleAsIs();
}
 
Example 5
Source File: InternalTimerServiceImpl.java    From Flink-CEPplus 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 6
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 7
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.");
		}
	}
}