/** * Deserialization path for Flink versions 1.7+. */ @VisibleForTesting static <T> TypeSerializerSnapshot<T> deserializeV2(DataInputView in, ClassLoader cl) throws IOException { return TypeSerializerSnapshot.readVersionedSnapshot(in, cl); }
/** * Reads the composite snapshot of all the contained serializers. */ public static NestedSerializersSnapshotDelegate readNestedSerializerSnapshots(DataInputView in, ClassLoader cl) throws IOException { final int magicNumber = in.readInt(); if (magicNumber != MAGIC_NUMBER) { throw new IOException(String.format("Corrupt data, magic number mismatch. Expected %8x, found %8x", MAGIC_NUMBER, magicNumber)); } final int version = in.readInt(); if (version != VERSION) { throw new IOException("Unrecognized version: " + version); } final int numSnapshots = in.readInt(); final TypeSerializerSnapshot<?>[] nestedSnapshots = new TypeSerializerSnapshot<?>[numSnapshots]; for (int i = 0; i < numSnapshots; i++) { nestedSnapshots[i] = TypeSerializerSnapshot.readVersionedSnapshot(in, cl); } return new NestedSerializersSnapshotDelegate(nestedSnapshots); }
private TypeSerializerSchemaCompatibility<String> snapshotCompositeSerializerAndGetSchemaCompatibilityAfterRestore( TypeSerializer<?>[] testNestedSerializers, String initialOuterConfiguration, String newOuterConfiguration) throws IOException { TestCompositeTypeSerializer testSerializer = new TestCompositeTypeSerializer(initialOuterConfiguration, testNestedSerializers); TypeSerializerSnapshot<String> testSerializerSnapshot = testSerializer.snapshotConfiguration(); DataOutputSerializer out = new DataOutputSerializer(128); TypeSerializerSnapshot.writeVersionedSnapshot(out, testSerializerSnapshot); DataInputDeserializer in = new DataInputDeserializer(out.getCopyOfBuffer()); testSerializerSnapshot = TypeSerializerSnapshot.readVersionedSnapshot( in, Thread.currentThread().getContextClassLoader()); TestCompositeTypeSerializer newTestSerializer = new TestCompositeTypeSerializer(newOuterConfiguration, testNestedSerializers); return testSerializerSnapshot.resolveSchemaCompatibility(newTestSerializer); }
@Test public void testRestoreCompositeTypeSerializer() throws IOException { // the target compatibilities of the nested serializers doesn't matter, // because we're only testing the restore serializer TypeSerializer<?>[] testNestedSerializers = { new NestedSerializer(TargetCompatibility.COMPATIBLE_AS_IS), new NestedSerializer(TargetCompatibility.INCOMPATIBLE), new NestedSerializer(TargetCompatibility.COMPATIBLE_AFTER_MIGRATION) }; TestCompositeTypeSerializer testSerializer = new TestCompositeTypeSerializer("outer-config", testNestedSerializers); TypeSerializerSnapshot<String> testSerializerSnapshot = testSerializer.snapshotConfiguration(); DataOutputSerializer out = new DataOutputSerializer(128); TypeSerializerSnapshot.writeVersionedSnapshot(out, testSerializerSnapshot); DataInputDeserializer in = new DataInputDeserializer(out.getCopyOfBuffer()); testSerializerSnapshot = TypeSerializerSnapshot.readVersionedSnapshot( in, Thread.currentThread().getContextClassLoader()); // now, restore the composite type serializer; // the restored nested serializer should be a RestoredNestedSerializer testSerializer = (TestCompositeTypeSerializer) testSerializerSnapshot.restoreSerializer(); Assert.assertTrue(testSerializer.getNestedSerializers()[0].getClass() == RestoredNestedSerializer.class); Assert.assertTrue(testSerializer.getNestedSerializers()[1].getClass() == RestoredNestedSerializer.class); Assert.assertTrue(testSerializer.getNestedSerializers()[2].getClass() == RestoredNestedSerializer.class); }
@Test public void testSerializerDeserializationFailure() throws Exception { TestSerializer ser = new TestSerializer(); TypeSerializerConfigSnapshot<Object> snap = (TypeSerializerConfigSnapshot<Object>) ser.snapshotConfiguration(); snap.setPriorSerializer(ser); DataOutputSerializer out = new DataOutputSerializer(64); TypeSerializerSnapshot.writeVersionedSnapshot(out, snap); TypeSerializerSnapshot<Object> readBack = TypeSerializerSnapshot.readVersionedSnapshot( new DataInputDeserializer(out.getCopyOfBuffer()), getClass().getClassLoader()); assertNotNull(readBack); try { readBack.restoreSerializer(); fail("expected exception"); } catch (IllegalStateException e) { // expected } ((TypeSerializerConfigSnapshot<Object>) readBack).setPriorSerializer( new UnloadableDummyTypeSerializer<>(new byte[0])); try { readBack.restoreSerializer(); fail("expected exception"); } catch (IllegalStateException e) { // expected } }
/** * Deserialization path for Flink versions 1.7+. */ @VisibleForTesting static <T> TypeSerializerSnapshot<T> deserializeV2(DataInputView in, ClassLoader cl) throws IOException { return TypeSerializerSnapshot.readVersionedSnapshot(in, cl); }
/** * Reads the composite snapshot of all the contained serializers. */ public static CompositeSerializerSnapshot readCompositeSnapshot(DataInputView in, ClassLoader cl) throws IOException { final int magicNumber = in.readInt(); if (magicNumber != MAGIC_NUMBER) { throw new IOException(String.format("Corrupt data, magic number mismatch. Expected %8x, found %8x", MAGIC_NUMBER, magicNumber)); } final int version = in.readInt(); if (version != VERSION) { throw new IOException("Unrecognized version: " + version); } final int numSnapshots = in.readInt(); final TypeSerializerSnapshot<?>[] nestedSnapshots = new TypeSerializerSnapshot<?>[numSnapshots]; for (int i = 0; i < numSnapshots; i++) { nestedSnapshots[i] = TypeSerializerSnapshot.readVersionedSnapshot(in, cl); } return new CompositeSerializerSnapshot(nestedSnapshots); }