Java Code Examples for org.apache.flink.api.common.typeutils.TypeSerializerSnapshot#resolveSchemaCompatibility()

The following examples show how to use org.apache.flink.api.common.typeutils.TypeSerializerSnapshot#resolveSchemaCompatibility() . 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: 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 2
Source File: KryoSerializerCompatibilityTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testMigrationStrategyForRemovedAvroDependency() throws Exception {
	KryoSerializer<TestClass> kryoSerializerForA = new KryoSerializer<>(TestClass.class, new ExecutionConfig());

	// read configuration again from bytes
	TypeSerializerSnapshot kryoSerializerConfigSnapshot;
	try (InputStream in = getClass().getResourceAsStream("/kryo-serializer-flink1.3-snapshot")) {
		kryoSerializerConfigSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader(), kryoSerializerForA);
	}

	@SuppressWarnings("unchecked")
	TypeSerializerSchemaCompatibility<TestClass> compatResult =
		kryoSerializerConfigSnapshot.resolveSchemaCompatibility(kryoSerializerForA);
	assertTrue(compatResult.isCompatibleAsIs());
}
 
Example 3
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 4
Source File: KryoSerializerCompatibilityTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testMigrationStrategyForRemovedAvroDependency() throws Exception {
	KryoSerializer<TestClass> kryoSerializerForA = new KryoSerializer<>(TestClass.class, new ExecutionConfig());

	// read configuration again from bytes
	TypeSerializerSnapshot kryoSerializerConfigSnapshot;
	try (InputStream in = getClass().getResourceAsStream("/kryo-serializer-flink1.3-snapshot")) {
		kryoSerializerConfigSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader(), kryoSerializerForA);
	}

	@SuppressWarnings("unchecked")
	TypeSerializerSchemaCompatibility<TestClass> compatResult =
		kryoSerializerConfigSnapshot.resolveSchemaCompatibility(kryoSerializerForA);
	assertTrue(compatResult.isCompatibleAsIs());
}
 
Example 5
Source File: KryoSerializerCompatibilityTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testMigrationStrategyForRemovedAvroDependency() throws Exception {
	KryoSerializer<TestClass> kryoSerializerForA = new KryoSerializer<>(TestClass.class, new ExecutionConfig());

	// read configuration again from bytes
	TypeSerializerSnapshot kryoSerializerConfigSnapshot;
	try (InputStream in = getClass().getResourceAsStream("/kryo-serializer-flink1.3-snapshot")) {
		kryoSerializerConfigSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader(), kryoSerializerForA);
	}

	@SuppressWarnings("unchecked")
	TypeSerializerSchemaCompatibility<TestClass> compatResult =
		kryoSerializerConfigSnapshot.resolveSchemaCompatibility(kryoSerializerForA);
	assertTrue(compatResult.isCompatibleAsIs());
}
 
Example 6
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 7
Source File: EnumSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigurationSnapshotSerialization() throws Exception {
	EnumSerializer<PublicEnum> serializer = new EnumSerializer<>(PublicEnum.class);

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

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

	TypeSerializerSchemaCompatibility<PublicEnum> compatResult = restoredConfig.resolveSchemaCompatibility(serializer);
	assertTrue(compatResult.isCompatibleAsIs());

	assertEquals(PublicEnum.FOO.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.FOO).intValue());
	assertEquals(PublicEnum.BAR.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.BAR).intValue());
	assertEquals(PublicEnum.PETER.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PETER).intValue());
	assertEquals(PublicEnum.NATHANIEL.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.NATHANIEL).intValue());
	assertEquals(PublicEnum.EMMA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.EMMA).intValue());
	assertEquals(PublicEnum.PAULA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PAULA).intValue());
	assertTrue(Arrays.equals(PublicEnum.values(), serializer.getValues()));
}
 
Example 8
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 9
Source File: EnumSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigurationSnapshotSerialization() throws Exception {
	EnumSerializer<PublicEnum> serializer = new EnumSerializer<>(PublicEnum.class);

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

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

	TypeSerializerSchemaCompatibility<PublicEnum> compatResult = restoredConfig.resolveSchemaCompatibility(serializer);
	assertTrue(compatResult.isCompatibleAsIs());

	assertEquals(PublicEnum.FOO.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.FOO).intValue());
	assertEquals(PublicEnum.BAR.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.BAR).intValue());
	assertEquals(PublicEnum.PETER.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PETER).intValue());
	assertEquals(PublicEnum.NATHANIEL.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.NATHANIEL).intValue());
	assertEquals(PublicEnum.EMMA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.EMMA).intValue());
	assertEquals(PublicEnum.PAULA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PAULA).intValue());
	assertTrue(Arrays.equals(PublicEnum.values(), serializer.getValues()));
}
 
Example 10
Source File: EnumSerializerUpgradeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static TypeSerializerSchemaCompatibility checkCompatibility(String enumSourceA, String enumSourceB)
	throws IOException, ClassNotFoundException {

	ClassLoader classLoader = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceA);

	EnumSerializer enumSerializer = new EnumSerializer(classLoader.loadClass(ENUM_NAME));

	TypeSerializerSnapshot snapshot = enumSerializer.snapshotConfiguration();
	byte[] snapshotBytes;
	try (
		ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
		DataOutputViewStreamWrapper outputViewStreamWrapper = new DataOutputViewStreamWrapper(outBuffer)) {

		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			outputViewStreamWrapper, snapshot, enumSerializer);
		snapshotBytes = outBuffer.toByteArray();
	}

	ClassLoader classLoader2 = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceB);

	TypeSerializerSnapshot restoredSnapshot;
	try (
		ByteArrayInputStream inBuffer = new ByteArrayInputStream(snapshotBytes);
		DataInputViewStreamWrapper inputViewStreamWrapper = new DataInputViewStreamWrapper(inBuffer)) {

		restoredSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			inputViewStreamWrapper, classLoader2, enumSerializer);
	}

	EnumSerializer enumSerializer2 = new EnumSerializer(classLoader2.loadClass(ENUM_NAME));
	return restoredSnapshot.resolveSchemaCompatibility(enumSerializer2);
}
 
Example 11
Source File: KryoSerializerCompatibilityTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that reconfiguration result is INCOMPATIBLE if data type has changed.
 */
@Test
public void testMigrationStrategyWithDifferentKryoType() throws Exception {
	KryoSerializer<TestClassA> kryoSerializerForA = new KryoSerializer<>(TestClassA.class, new ExecutionConfig());

	// snapshot configuration and serialize to bytes
	TypeSerializerSnapshot kryoSerializerConfigSnapshot = kryoSerializerForA.snapshotConfiguration();
	byte[] serializedConfig;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			new DataOutputViewStreamWrapper(out), kryoSerializerConfigSnapshot, kryoSerializerForA);
		serializedConfig = out.toByteArray();
	}

	KryoSerializer<TestClassB> kryoSerializerForB = new KryoSerializer<>(TestClassB.class, new ExecutionConfig());

	// read configuration again from bytes
	try (ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
		kryoSerializerConfigSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader(), kryoSerializerForB);
	}

	@SuppressWarnings("unchecked")
	TypeSerializerSchemaCompatibility<TestClassB> compatResult =
		kryoSerializerConfigSnapshot.resolveSchemaCompatibility(kryoSerializerForB);
	assertTrue(compatResult.isIncompatible());
}
 
Example 12
Source File: PojoSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that reconfiguring with a config snapshot of a preceding POJO serializer
 * with different POJO type will result in INCOMPATIBLE.
 */
@Test
public void testReconfigureWithDifferentPojoType() throws Exception {
	PojoSerializer<SubTestUserClassB> pojoSerializer1 = (PojoSerializer<SubTestUserClassB>)
		TypeExtractor.getForClass(SubTestUserClassB.class).createSerializer(new ExecutionConfig());

	// snapshot configuration and serialize to bytes
	TypeSerializerSnapshot pojoSerializerConfigSnapshot = pojoSerializer1.snapshotConfiguration();
	byte[] serializedConfig;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			new DataOutputViewStreamWrapper(out), pojoSerializerConfigSnapshot, pojoSerializer1);
		serializedConfig = out.toByteArray();
	}

	PojoSerializer<SubTestUserClassA> pojoSerializer2 = (PojoSerializer<SubTestUserClassA>)
		TypeExtractor.getForClass(SubTestUserClassA.class).createSerializer(new ExecutionConfig());

	// read configuration again from bytes
	try(ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
		pojoSerializerConfigSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader(), pojoSerializer2);
	}

	@SuppressWarnings("unchecked")
	TypeSerializerSchemaCompatibility<SubTestUserClassA> compatResult =
		pojoSerializerConfigSnapshot.resolveSchemaCompatibility(pojoSerializer2);
	assertTrue(compatResult.isIncompatible());
}
 
Example 13
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 14
Source File: EnumSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigurationSnapshotSerialization() throws Exception {
	EnumSerializer<PublicEnum> serializer = new EnumSerializer<>(PublicEnum.class);

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

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

	TypeSerializerSchemaCompatibility<PublicEnum> compatResult = restoredConfig.resolveSchemaCompatibility(serializer);
	assertTrue(compatResult.isCompatibleAsIs());

	assertEquals(PublicEnum.FOO.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.FOO).intValue());
	assertEquals(PublicEnum.BAR.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.BAR).intValue());
	assertEquals(PublicEnum.PETER.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PETER).intValue());
	assertEquals(PublicEnum.NATHANIEL.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.NATHANIEL).intValue());
	assertEquals(PublicEnum.EMMA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.EMMA).intValue());
	assertEquals(PublicEnum.PAULA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PAULA).intValue());
	assertTrue(Arrays.equals(PublicEnum.values(), serializer.getValues()));
}
 
Example 15
Source File: EnumSerializerCompatibilityTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static TypeSerializerSchemaCompatibility checkCompatibility(String enumSourceA, String enumSourceB)
	throws IOException, ClassNotFoundException {

	ClassLoader classLoader = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceA);

	EnumSerializer enumSerializer = new EnumSerializer(classLoader.loadClass(ENUM_NAME));

	TypeSerializerSnapshot snapshot = enumSerializer.snapshotConfiguration();
	byte[] snapshotBytes;
	try (
		ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
		DataOutputViewStreamWrapper outputViewStreamWrapper = new DataOutputViewStreamWrapper(outBuffer)) {

		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			outputViewStreamWrapper, snapshot, enumSerializer);
		snapshotBytes = outBuffer.toByteArray();
	}

	ClassLoader classLoader2 = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceB);

	TypeSerializerSnapshot restoredSnapshot;
	try (
		ByteArrayInputStream inBuffer = new ByteArrayInputStream(snapshotBytes);
		DataInputViewStreamWrapper inputViewStreamWrapper = new DataInputViewStreamWrapper(inBuffer)) {

		restoredSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			inputViewStreamWrapper, classLoader2, enumSerializer);
	}

	EnumSerializer enumSerializer2 = new EnumSerializer(classLoader2.loadClass(ENUM_NAME));
	return restoredSnapshot.resolveSchemaCompatibility(enumSerializer2);
}
 
Example 16
Source File: KryoSerializerCompatibilityTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that reconfiguration result is INCOMPATIBLE if data type has changed.
 */
@Test
public void testMigrationStrategyWithDifferentKryoType() throws Exception {
	KryoSerializer<TestClassA> kryoSerializerForA = new KryoSerializer<>(TestClassA.class, new ExecutionConfig());

	// snapshot configuration and serialize to bytes
	TypeSerializerSnapshot kryoSerializerConfigSnapshot = kryoSerializerForA.snapshotConfiguration();
	byte[] serializedConfig;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			new DataOutputViewStreamWrapper(out), kryoSerializerConfigSnapshot, kryoSerializerForA);
		serializedConfig = out.toByteArray();
	}

	KryoSerializer<TestClassB> kryoSerializerForB = new KryoSerializer<>(TestClassB.class, new ExecutionConfig());

	// read configuration again from bytes
	try (ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
		kryoSerializerConfigSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader(), kryoSerializerForB);
	}

	@SuppressWarnings("unchecked")
	TypeSerializerSchemaCompatibility<TestClassB> compatResult =
		kryoSerializerConfigSnapshot.resolveSchemaCompatibility(kryoSerializerForB);
	assertTrue(compatResult.isIncompatible());
}
 
Example 17
Source File: KryoSerializerSnapshotTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static TypeSerializerSchemaCompatibility<Animal> resolveKryoCompatibility(ExecutionConfig previous, ExecutionConfig current) {
	KryoSerializer<Animal> previousSerializer = new KryoSerializer<>(Animal.class, previous);
	TypeSerializerSnapshot<Animal> previousSnapshot = previousSerializer.snapshotConfiguration();

	TypeSerializer<Animal> currentSerializer = new KryoSerializer<>(Animal.class, current);
	return previousSnapshot.resolveSchemaCompatibility(currentSerializer);
}
 
Example 18
Source File: KryoSerializerCompatibilityTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that reconfiguration result is INCOMPATIBLE if data type has changed.
 */
@Test
public void testMigrationStrategyWithDifferentKryoType() throws Exception {
	KryoSerializer<TestClassA> kryoSerializerForA = new KryoSerializer<>(TestClassA.class, new ExecutionConfig());

	// snapshot configuration and serialize to bytes
	TypeSerializerSnapshot kryoSerializerConfigSnapshot = kryoSerializerForA.snapshotConfiguration();
	byte[] serializedConfig;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			new DataOutputViewStreamWrapper(out), kryoSerializerConfigSnapshot, kryoSerializerForA);
		serializedConfig = out.toByteArray();
	}

	KryoSerializer<TestClassB> kryoSerializerForB = new KryoSerializer<>(TestClassB.class, new ExecutionConfig());

	// read configuration again from bytes
	try (ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
		kryoSerializerConfigSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader(), kryoSerializerForB);
	}

	@SuppressWarnings("unchecked")
	TypeSerializerSchemaCompatibility<TestClassB> compatResult =
		kryoSerializerConfigSnapshot.resolveSchemaCompatibility(kryoSerializerForB);
	assertTrue(compatResult.isIncompatible());
}
 
Example 19
Source File: PojoSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that:
 *  - Previous Pojo serializer did not have registrations, and created cached serializers for subclasses
 *  - On restore, it had those subclasses registered
 *
 * In this case, after reconfiguration, the cache should be repopulated, and registrations should
 * also exist for the subclasses.
 *
 * Note: the cache still needs to be repopulated because previous data of those subclasses were
 * written with the cached serializers. In this case, the repopulated cache has reconfigured serializers
 * for the subclasses so that previous written data can be read, but the registered serializers
 * for the subclasses do not necessarily need to be reconfigured since they will only be used to
 * write new data.
 */
@Test
public void testReconfigureWithPreviouslyNonregisteredSubclasses() throws Exception {
	// don't register any subclasses at first
	PojoSerializer<TestUserClass> pojoSerializer = (PojoSerializer<TestUserClass>) type.createSerializer(new ExecutionConfig());

	// create cached serializers for SubTestUserClassA and SubTestUserClassB
	pojoSerializer.getSubclassSerializer(SubTestUserClassA.class);
	pojoSerializer.getSubclassSerializer(SubTestUserClassB.class);

	// make sure serializers are in cache
	assertEquals(2, pojoSerializer.getSubclassSerializerCache().size());
	assertTrue(pojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassA.class));
	assertTrue(pojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassB.class));

	// make sure that registrations are empty
	assertTrue(pojoSerializer.getRegisteredClasses().isEmpty());
	assertEquals(0, pojoSerializer.getRegisteredSerializers().length);

	// snapshot configuration and serialize to bytes
	TypeSerializerSnapshot pojoSerializerConfigSnapshot = pojoSerializer.snapshotConfiguration();
	byte[] serializedConfig;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			new DataOutputViewStreamWrapper(out), pojoSerializerConfigSnapshot, pojoSerializer);
		serializedConfig = out.toByteArray();
	}

	// instantiate new PojoSerializer, with new execution config that has the subclass registrations
	ExecutionConfig newExecutionConfig = new ExecutionConfig();
	newExecutionConfig.registerPojoType(SubTestUserClassA.class);
	newExecutionConfig.registerPojoType(SubTestUserClassB.class);
	pojoSerializer = (PojoSerializer<TestUserClass>) type.createSerializer(newExecutionConfig);

	// read configuration from bytes
	try(ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
		pojoSerializerConfigSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader(), pojoSerializer);
	}

	// reconfigure - check reconfiguration result and that
	// 1) subclass serializer cache is repopulated
	// 2) registrations also contain the now registered subclasses
	@SuppressWarnings("unchecked")
	TypeSerializerSchemaCompatibility<TestUserClass> compatResult =
		pojoSerializerConfigSnapshot.resolveSchemaCompatibility(pojoSerializer);
	assertTrue(compatResult.isCompatibleWithReconfiguredSerializer());
	assertTrue(compatResult.getReconfiguredSerializer() instanceof PojoSerializer);

	PojoSerializer<TestUserClass> reconfiguredPojoSerializer = (PojoSerializer<TestUserClass>) compatResult.getReconfiguredSerializer();
	assertEquals(2, reconfiguredPojoSerializer.getSubclassSerializerCache().size());
	assertTrue(reconfiguredPojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassA.class));
	assertTrue(reconfiguredPojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassB.class));
	assertEquals(2, reconfiguredPojoSerializer.getRegisteredClasses().size());
	assertTrue(reconfiguredPojoSerializer.getRegisteredClasses().containsKey(SubTestUserClassA.class));
	assertTrue(reconfiguredPojoSerializer.getRegisteredClasses().containsKey(SubTestUserClassB.class));
}
 
Example 20
Source File: PojoSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that:
 *  - Previous Pojo serializer did not have registrations, and created cached serializers for subclasses
 *  - On restore, it had those subclasses registered
 *
 * In this case, after reconfiguration, the cache should be repopulated, and registrations should
 * also exist for the subclasses.
 *
 * Note: the cache still needs to be repopulated because previous data of those subclasses were
 * written with the cached serializers. In this case, the repopulated cache has reconfigured serializers
 * for the subclasses so that previous written data can be read, but the registered serializers
 * for the subclasses do not necessarily need to be reconfigured since they will only be used to
 * write new data.
 */
@Test
public void testReconfigureWithPreviouslyNonregisteredSubclasses() throws Exception {
	// don't register any subclasses at first
	PojoSerializer<TestUserClass> pojoSerializer = (PojoSerializer<TestUserClass>) type.createSerializer(new ExecutionConfig());

	// create cached serializers for SubTestUserClassA and SubTestUserClassB
	pojoSerializer.getSubclassSerializer(SubTestUserClassA.class);
	pojoSerializer.getSubclassSerializer(SubTestUserClassB.class);

	// make sure serializers are in cache
	assertEquals(2, pojoSerializer.getSubclassSerializerCache().size());
	assertTrue(pojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassA.class));
	assertTrue(pojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassB.class));

	// make sure that registrations are empty
	assertTrue(pojoSerializer.getRegisteredClasses().isEmpty());
	assertEquals(0, pojoSerializer.getRegisteredSerializers().length);

	// snapshot configuration and serialize to bytes
	TypeSerializerSnapshot pojoSerializerConfigSnapshot = pojoSerializer.snapshotConfiguration();
	byte[] serializedConfig;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			new DataOutputViewStreamWrapper(out), pojoSerializerConfigSnapshot, pojoSerializer);
		serializedConfig = out.toByteArray();
	}

	// instantiate new PojoSerializer, with new execution config that has the subclass registrations
	ExecutionConfig newExecutionConfig = new ExecutionConfig();
	newExecutionConfig.registerPojoType(SubTestUserClassA.class);
	newExecutionConfig.registerPojoType(SubTestUserClassB.class);
	pojoSerializer = (PojoSerializer<TestUserClass>) type.createSerializer(newExecutionConfig);

	// read configuration from bytes
	try(ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
		pojoSerializerConfigSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader(), pojoSerializer);
	}

	// reconfigure - check reconfiguration result and that
	// 1) subclass serializer cache is repopulated
	// 2) registrations also contain the now registered subclasses
	@SuppressWarnings("unchecked")
	TypeSerializerSchemaCompatibility<TestUserClass> compatResult =
		pojoSerializerConfigSnapshot.resolveSchemaCompatibility(pojoSerializer);
	assertTrue(compatResult.isCompatibleWithReconfiguredSerializer());
	assertTrue(compatResult.getReconfiguredSerializer() instanceof PojoSerializer);

	PojoSerializer<TestUserClass> reconfiguredPojoSerializer = (PojoSerializer<TestUserClass>) compatResult.getReconfiguredSerializer();
	assertEquals(2, reconfiguredPojoSerializer.getSubclassSerializerCache().size());
	assertTrue(reconfiguredPojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassA.class));
	assertTrue(reconfiguredPojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassB.class));
	assertEquals(2, reconfiguredPojoSerializer.getRegisteredClasses().size());
	assertTrue(reconfiguredPojoSerializer.getRegisteredClasses().containsKey(SubTestUserClassA.class));
	assertTrue(reconfiguredPojoSerializer.getRegisteredClasses().containsKey(SubTestUserClassB.class));
}