Java Code Examples for org.apache.flink.api.common.typeutils.TypeSerializerSnapshotSerializationUtil

The following examples show how to use org.apache.flink.api.common.typeutils.TypeSerializerSnapshotSerializationUtil. These examples are extracted from open source projects. 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
@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 2
Source Project: flink   Source File: KryoSerializerCompatibilityTest.java    License: 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 Project: flink   Source File: KryoSerializerCompatibilityTest.java    License: 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 4
Source Project: Flink-CEPplus   Source File: PojoSerializerTest.java    License: 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 5
/**
 * 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 6
Source Project: Flink-CEPplus   Source File: EnumSerializerUpgradeTest.java    License: 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 7
Source Project: Flink-CEPplus   Source File: EnumSerializerTest.java    License: 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
@Nonnull
@Override
public StateMetaInfoSnapshot readStateMetaInfoSnapshot(
	@Nonnull DataInputView inputView,
	@Nonnull ClassLoader userCodeClassLoader) throws IOException {

	final String stateName = inputView.readUTF();
	final StateMetaInfoSnapshot.BackendStateType stateType =
		StateMetaInfoSnapshot.BackendStateType.values()[inputView.readInt()];
	final int numOptions = inputView.readInt();
	HashMap<String, String> optionsMap = new HashMap<>(numOptions);
	for (int i = 0; i < numOptions; ++i) {
		String key = inputView.readUTF();
		String value = inputView.readUTF();
		optionsMap.put(key, value);
	}

	final int numSerializerConfigSnapshots = inputView.readInt();
	final HashMap<String, TypeSerializerSnapshot<?>> serializerConfigsMap = new HashMap<>(numSerializerConfigSnapshots);

	for (int i = 0; i < numSerializerConfigSnapshots; ++i) {
		serializerConfigsMap.put(
			inputView.readUTF(),
			TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
				inputView, userCodeClassLoader, null));
	}

	return new StateMetaInfoSnapshot(stateName, stateType, optionsMap, serializerConfigsMap);
}
 
Example 9
@Override
public void write(DataOutputView out) throws IOException {
	super.write(out);

	// write the compression format used to write each key-group
	out.writeBoolean(usingKeyGroupCompression);

	TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(out, keySerializerSnapshot, keySerializer);

	// write individual registered keyed state metainfos
	out.writeShort(stateMetaInfoSnapshots.size());
	for (StateMetaInfoSnapshot metaInfoSnapshot : stateMetaInfoSnapshots) {
		StateMetaInfoSnapshotReadersWriters.getWriter().writeStateMetaInfoSnapshot(metaInfoSnapshot, out);
	}
}
 
Example 10
Source Project: flink   Source File: SerializerTestUtil.java    License: 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 11
Source Project: flink   Source File: PojoSerializerTest.java    License: 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 12
Source Project: flink   Source File: KryoSerializerCompatibilityTest.java    License: 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 13
Source Project: flink   Source File: EnumSerializerUpgradeTest.java    License: 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 14
Source Project: flink   Source File: EnumSerializerTest.java    License: 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 Project: flink   Source File: StateMetaInfoSnapshotReadersWriters.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public StateMetaInfoSnapshot readStateMetaInfoSnapshot(
	@Nonnull DataInputView inputView,
	@Nonnull ClassLoader userCodeClassLoader) throws IOException {

	final String stateName = inputView.readUTF();
	final StateMetaInfoSnapshot.BackendStateType stateType =
		StateMetaInfoSnapshot.BackendStateType.values()[inputView.readInt()];
	final int numOptions = inputView.readInt();
	HashMap<String, String> optionsMap = new HashMap<>(numOptions);
	for (int i = 0; i < numOptions; ++i) {
		String key = inputView.readUTF();
		String value = inputView.readUTF();
		optionsMap.put(key, value);
	}

	final int numSerializerConfigSnapshots = inputView.readInt();
	final HashMap<String, TypeSerializerSnapshot<?>> serializerConfigsMap = new HashMap<>(numSerializerConfigSnapshots);

	for (int i = 0; i < numSerializerConfigSnapshots; ++i) {
		serializerConfigsMap.put(
			inputView.readUTF(),
			TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
				inputView, userCodeClassLoader, null));
	}

	return new StateMetaInfoSnapshot(stateName, stateType, optionsMap, serializerConfigsMap);
}
 
Example 16
Source Project: flink   Source File: KeyedBackendSerializationProxy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void write(DataOutputView out) throws IOException {
	super.write(out);

	// write the compression format used to write each key-group
	out.writeBoolean(usingKeyGroupCompression);

	TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(out, keySerializerSnapshot, keySerializer);

	// write individual registered keyed state metainfos
	out.writeShort(stateMetaInfoSnapshots.size());
	for (StateMetaInfoSnapshot metaInfoSnapshot : stateMetaInfoSnapshots) {
		StateMetaInfoSnapshotReadersWriters.getWriter().writeStateMetaInfoSnapshot(metaInfoSnapshot, out);
	}
}
 
Example 17
Source Project: flink   Source File: SerializerTestUtil.java    License: 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 18
Source Project: flink   Source File: PojoSerializerTest.java    License: 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 19
Source Project: flink   Source File: KryoSerializerCompatibilityTest.java    License: 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 20
Source Project: flink   Source File: EnumSerializerCompatibilityTest.java    License: 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 21
Source Project: flink   Source File: EnumSerializerTest.java    License: 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 22
Source Project: flink   Source File: StateMetaInfoSnapshotReadersWriters.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public StateMetaInfoSnapshot readStateMetaInfoSnapshot(
	@Nonnull DataInputView inputView,
	@Nonnull ClassLoader userCodeClassLoader) throws IOException {

	final String stateName = inputView.readUTF();
	final StateMetaInfoSnapshot.BackendStateType stateType =
		StateMetaInfoSnapshot.BackendStateType.values()[inputView.readInt()];
	final int numOptions = inputView.readInt();
	HashMap<String, String> optionsMap = new HashMap<>(numOptions);
	for (int i = 0; i < numOptions; ++i) {
		String key = inputView.readUTF();
		String value = inputView.readUTF();
		optionsMap.put(key, value);
	}

	final int numSerializerConfigSnapshots = inputView.readInt();
	final HashMap<String, TypeSerializerSnapshot<?>> serializerConfigsMap = new HashMap<>(numSerializerConfigSnapshots);

	for (int i = 0; i < numSerializerConfigSnapshots; ++i) {
		serializerConfigsMap.put(
			inputView.readUTF(),
			TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
				inputView, userCodeClassLoader, null));
	}

	return new StateMetaInfoSnapshot(stateName, stateType, optionsMap, serializerConfigsMap);
}
 
Example 23
Source Project: flink   Source File: KeyedBackendSerializationProxy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void write(DataOutputView out) throws IOException {
	super.write(out);

	// write the compression format used to write each key-group
	out.writeBoolean(usingKeyGroupCompression);

	TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(out, keySerializerSnapshot, keySerializer);

	// write individual registered keyed state metainfos
	out.writeShort(stateMetaInfoSnapshots.size());
	for (StateMetaInfoSnapshot metaInfoSnapshot : stateMetaInfoSnapshots) {
		StateMetaInfoSnapshotReadersWriters.getWriter().writeStateMetaInfoSnapshot(metaInfoSnapshot, out);
	}
}
 
Example 24
Source Project: Flink-CEPplus   Source File: PojoSerializerTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Tests that reconfiguration repopulates previously cached subclass serializers.
 */
@Test
public void testReconfigureRepopulateNonregisteredSubclassSerializerCache() throws Exception {
	// don't register any subclasses
	PojoSerializer<TestUserClass> pojoSerializer = (PojoSerializer<TestUserClass>) type.createSerializer(new ExecutionConfig());

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

	assertEquals(2, pojoSerializer.getSubclassSerializerCache().size());
	assertTrue(pojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassA.class));
	assertTrue(pojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassB.class));

	// 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

	pojoSerializer = (PojoSerializer<TestUserClass>) type.createSerializer(new ExecutionConfig());

	// 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 subclass serializer cache is repopulated
	@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));
}
 
Example 25
Source Project: Flink-CEPplus   Source File: PojoSerializerTest.java    License: 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 26
@SuppressWarnings("unchecked")
@Override
public void read(DataInputView in) throws IOException {
	super.read(in);

	final int readVersion = getReadVersion();

	if (readVersion >= 4) {
		usingKeyGroupCompression = in.readBoolean();
	} else {
		usingKeyGroupCompression = false;
	}

	// only starting from version 3, we have the key serializer and its config snapshot written
	if (readVersion >= 6) {
		this.keySerializerSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			in, userCodeClassLoader, null);
	} else if (readVersion >= 3) {
		Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>> keySerializerAndConfig =
				TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader).get(0);
		this.keySerializerSnapshot = (TypeSerializerSnapshot<K>) keySerializerAndConfig.f1;
	} else {
		this.keySerializerSnapshot = new BackwardsCompatibleSerializerSnapshot<>(
			TypeSerializerSerializationUtil.tryReadSerializer(in, userCodeClassLoader, true));
	}
	this.keySerializer = null;

	Integer metaInfoSnapshotVersion = META_INFO_SNAPSHOT_FORMAT_VERSION_MAPPER.get(readVersion);
	if (metaInfoSnapshotVersion == null) {
		// this should not happen; guard for the future
		throw new IOException("Cannot determine corresponding meta info snapshot version for keyed backend serialization readVersion=" + readVersion);
	}
	final StateMetaInfoReader stateMetaInfoReader = StateMetaInfoSnapshotReadersWriters.getReader(
		metaInfoSnapshotVersion,
		StateMetaInfoSnapshotReadersWriters.StateTypeHint.KEYED_STATE);

	int numKvStates = in.readShort();
	stateMetaInfoSnapshots = new ArrayList<>(numKvStates);
	for (int i = 0; i < numKvStates; i++) {
		StateMetaInfoSnapshot snapshot = stateMetaInfoReader.readStateMetaInfoSnapshot(in, userCodeClassLoader);

		stateMetaInfoSnapshots.add(snapshot);
	}
}
 
Example 27
Source Project: flink   Source File: PojoSerializerTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Tests that reconfiguration repopulates previously cached subclass serializers.
 */
@Test
public void testReconfigureRepopulateNonregisteredSubclassSerializerCache() throws Exception {
	// don't register any subclasses
	PojoSerializer<TestUserClass> pojoSerializer = (PojoSerializer<TestUserClass>) type.createSerializer(new ExecutionConfig());

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

	assertEquals(2, pojoSerializer.getSubclassSerializerCache().size());
	assertTrue(pojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassA.class));
	assertTrue(pojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassB.class));

	// 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

	pojoSerializer = (PojoSerializer<TestUserClass>) type.createSerializer(new ExecutionConfig());

	// 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 subclass serializer cache is repopulated
	@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));
}
 
Example 28
Source Project: flink   Source File: PojoSerializerTest.java    License: 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 29
Source Project: flink   Source File: KeyedBackendSerializationProxy.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void read(DataInputView in) throws IOException {
	super.read(in);

	final int readVersion = getReadVersion();

	if (readVersion >= 4) {
		usingKeyGroupCompression = in.readBoolean();
	} else {
		usingKeyGroupCompression = false;
	}

	// only starting from version 3, we have the key serializer and its config snapshot written
	if (readVersion >= 6) {
		this.keySerializerSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			in, userCodeClassLoader, null);
	} else if (readVersion >= 3) {
		Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>> keySerializerAndConfig =
				TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader).get(0);
		this.keySerializerSnapshot = (TypeSerializerSnapshot<K>) keySerializerAndConfig.f1;
	} else {
		this.keySerializerSnapshot = new BackwardsCompatibleSerializerSnapshot<>(
			TypeSerializerSerializationUtil.tryReadSerializer(in, userCodeClassLoader, true));
	}
	this.keySerializer = null;

	Integer metaInfoSnapshotVersion = META_INFO_SNAPSHOT_FORMAT_VERSION_MAPPER.get(readVersion);
	if (metaInfoSnapshotVersion == null) {
		// this should not happen; guard for the future
		throw new IOException("Cannot determine corresponding meta info snapshot version for keyed backend serialization readVersion=" + readVersion);
	}
	final StateMetaInfoReader stateMetaInfoReader = StateMetaInfoSnapshotReadersWriters.getReader(
		metaInfoSnapshotVersion,
		StateMetaInfoSnapshotReadersWriters.StateTypeHint.KEYED_STATE);

	int numKvStates = in.readShort();
	stateMetaInfoSnapshots = new ArrayList<>(numKvStates);
	for (int i = 0; i < numKvStates; i++) {
		StateMetaInfoSnapshot snapshot = stateMetaInfoReader.readStateMetaInfoSnapshot(in, userCodeClassLoader);

		stateMetaInfoSnapshots.add(snapshot);
	}
}
 
Example 30
Source Project: flink   Source File: PojoSerializerTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Tests that reconfiguration repopulates previously cached subclass serializers.
 */
@Test
public void testReconfigureRepopulateNonregisteredSubclassSerializerCache() throws Exception {
	// don't register any subclasses
	PojoSerializer<TestUserClass> pojoSerializer = (PojoSerializer<TestUserClass>) type.createSerializer(new ExecutionConfig());

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

	assertEquals(2, pojoSerializer.getSubclassSerializerCache().size());
	assertTrue(pojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassA.class));
	assertTrue(pojoSerializer.getSubclassSerializerCache().containsKey(SubTestUserClassB.class));

	// 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

	pojoSerializer = (PojoSerializer<TestUserClass>) type.createSerializer(new ExecutionConfig());

	// 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 subclass serializer cache is repopulated
	@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));
}