/** * Utility method to bind the serializer and serializer snapshot to a common * generic type variable. */ @SuppressWarnings({"unchecked", "deprecation"}) private static <T> TypeSerializerSnapshot<T> configureForBackwardsCompatibility( TypeSerializerSnapshot<?> snapshot, TypeSerializer<?> serializer) { TypeSerializerSnapshot<T> typedSnapshot = (TypeSerializerSnapshot<T>) snapshot; TypeSerializer<T> typedSerializer = (TypeSerializer<T>) serializer; if (snapshot instanceof TypeSerializerConfigSnapshot) { ((TypeSerializerConfigSnapshot<T>) typedSnapshot).setPriorSerializer(typedSerializer); } return typedSnapshot; }
@SuppressWarnings("deprecation") private static <T> void setSerializerForWriteIfOldPath( TypeSerializerSnapshot<T> serializerSnapshot, TypeSerializer<T> serializer) { // for compatibility with non-upgraded serializers, put the serializer into the // config snapshot if it of the old version if (serializerSnapshot instanceof TypeSerializerConfigSnapshot) { checkState(serializer != null); ((TypeSerializerConfigSnapshot<T>) serializerSnapshot).setPriorSerializer(serializer); } } }
/** * Deserialization path for Flink versions in [1.4, 1.6]. */ @VisibleForTesting @SuppressWarnings("deprecation") static <T> TypeSerializerSnapshot<T> deserializeV1( DataInputView in, ClassLoader cl, @Nullable TypeSerializer<T> serializer) throws IOException { TypeSerializerSnapshot<T> snapshot = readAndInstantiateSnapshotClass(in, cl); // if the snapshot was created before Flink 1.7, we need to distinguish the following cases: // - old snapshot type that needs serializer from the outside // - new snapshot type that understands the old format and can produce a restore serializer from it if (snapshot instanceof TypeSerializerConfigSnapshot) { TypeSerializerConfigSnapshot<T> oldTypeSnapshot = (TypeSerializerConfigSnapshot<T>) snapshot; oldTypeSnapshot.setPriorSerializer(serializer); oldTypeSnapshot.setUserCodeClassLoader(cl); oldTypeSnapshot.read(in); } else { // new type, simple case int readVersion = in.readInt(); snapshot.readSnapshot(readVersion, in, cl); } return snapshot; }
@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 } }
/** * Utility method to bind the serializer and serializer snapshot to a common * generic type variable. */ @SuppressWarnings({"unchecked", "deprecation"}) private static <T> TypeSerializerSnapshot<T> configureForBackwardsCompatibility( TypeSerializerSnapshot<?> snapshot, TypeSerializer<?> serializer) { TypeSerializerSnapshot<T> typedSnapshot = (TypeSerializerSnapshot<T>) snapshot; TypeSerializer<T> typedSerializer = (TypeSerializer<T>) serializer; if (snapshot instanceof TypeSerializerConfigSnapshot) { ((TypeSerializerConfigSnapshot<T>) typedSnapshot).setPriorSerializer(typedSerializer); } return typedSnapshot; }
@SuppressWarnings("deprecation") private static <T> void setSerializerForWriteIfOldPath( TypeSerializerSnapshot<T> serializerSnapshot, TypeSerializer<T> serializer) { // for compatibility with non-upgraded serializers, put the serializer into the // config snapshot if it of the old version if (serializerSnapshot instanceof TypeSerializerConfigSnapshot) { checkState(serializer != null); ((TypeSerializerConfigSnapshot<T>) serializerSnapshot).setPriorSerializer(serializer); } } }
/** * Deserialization path for Flink versions in [1.4, 1.6]. */ @VisibleForTesting @SuppressWarnings("deprecation") static <T> TypeSerializerSnapshot<T> deserializeV1( DataInputView in, ClassLoader cl, @Nullable TypeSerializer<T> serializer) throws IOException { TypeSerializerSnapshot<T> snapshot = readAndInstantiateSnapshotClass(in, cl); // if the snapshot was created before Flink 1.7, we need to distinguish the following cases: // - old snapshot type that needs serializer from the outside // - new snapshot type that understands the old format and can produce a restore serializer from it if (snapshot instanceof TypeSerializerConfigSnapshot) { TypeSerializerConfigSnapshot<T> oldTypeSnapshot = (TypeSerializerConfigSnapshot<T>) snapshot; oldTypeSnapshot.setPriorSerializer(serializer); oldTypeSnapshot.setUserCodeClassLoader(cl); oldTypeSnapshot.read(in); } else { // new type, simple case int readVersion = in.readInt(); snapshot.readSnapshot(readVersion, in, cl); } return snapshot; }