/** * Reads the composite snapshot of all the contained serializers in a way that is compatible * with Version 1 of the deprecated {@link CompositeTypeSerializerConfigSnapshot}. */ public static NestedSerializersSnapshotDelegate legacyReadNestedSerializerSnapshots(DataInputView in, ClassLoader cl) throws IOException { @SuppressWarnings("deprecation") final List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndSnapshots = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, cl); final TypeSerializerSnapshot<?>[] nestedSnapshots = serializersAndSnapshots.stream() .map(t -> t.f1) .toArray(TypeSerializerSnapshot<?>[]::new); return new NestedSerializersSnapshotDelegate(nestedSnapshots); }
@SuppressWarnings("unchecked") @Override protected void restoreKeyAndNamespaceSerializers( InternalTimersSnapshot<K, N> restoredTimersSnapshot, DataInputView in) throws IOException { List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); restoredTimersSnapshot.setKeySerializer((TypeSerializer<K>) serializersAndConfigs.get(0).f0); restoredTimersSnapshot.setKeySerializerConfigSnapshot((TypeSerializerSnapshot<K>) serializersAndConfigs.get(0).f1); restoredTimersSnapshot.setNamespaceSerializer((TypeSerializer<N>) serializersAndConfigs.get(1).f0); restoredTimersSnapshot.setNamespaceSerializerConfigSnapshot((TypeSerializerSnapshot<N>) serializersAndConfigs.get(1).f1); } }
@Override public void read(DataInputView in) throws IOException { super.read(in); this.nestedSerializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, getUserCodeClassLoader()); }
@SuppressWarnings({"unchecked", "deprecation"}) private TypeSerializerSnapshot<ElementT> readPre17SnapshotFormat(DataInputView input) throws IOException { final ClassLoader cl = Thread.currentThread().getContextClassLoader(); List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializers = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(input, cl); return (TypeSerializerSnapshot<ElementT>) serializers.get(0).f1; }
/** * Verifies resilience to serializer deserialization failures when writing and reading * serializer and config snapshot pairs. */ @Test public void testSerializerAndConfigPairsSerializationWithSerializerDeserializationFailures() throws Exception { TestIntSerializer serializer = new TestIntSerializer(); List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = Arrays.asList( new Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>( serializer, serializer.snapshotConfiguration())); byte[] serializedSerializersAndConfigs; try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { TypeSerializerSerializationUtil.writeSerializersAndConfigsWithResilience( new DataOutputViewStreamWrapper(out), serializersAndConfigs); serializedSerializersAndConfigs = out.toByteArray(); } Set<String> cnfThrowingClassnames = new HashSet<>(); cnfThrowingClassnames.add(TestIntSerializer.class.getName()); List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> restored; try (ByteArrayInputStream in = new ByteArrayInputStream(serializedSerializersAndConfigs)) { restored = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience( new DataInputViewStreamWrapper(in), new ArtificialCNFExceptionThrowingClassLoader( Thread.currentThread().getContextClassLoader(), cnfThrowingClassnames)); } Assert.assertEquals(1, restored.size()); Assert.assertTrue(restored.get(0).f0 instanceof UnloadableDummyTypeSerializer); Assert.assertThat(restored.get(0).f1, Matchers.instanceOf(SimpleTypeSerializerSnapshot.class)); }
/** * Reads the composite snapshot of all the contained serializers in a way that is compatible * with Version 1 of the deprecated {@link CompositeTypeSerializerConfigSnapshot}. */ public static CompositeSerializerSnapshot legacyReadProductSnapshots(DataInputView in, ClassLoader cl) throws IOException { @SuppressWarnings("deprecation") final List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndSnapshots = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, cl); final TypeSerializerSnapshot<?>[] nestedSnapshots = serializersAndSnapshots.stream() .map(t -> t.f1) .toArray(TypeSerializerSnapshot<?>[]::new); return new CompositeSerializerSnapshot(nestedSnapshots); }
@Override public void read(DataInputView in) throws IOException { super.read(in); this.nestedSerializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, getUserCodeClassLoader()); }
@Override public void read(DataInputView in) throws IOException { super.read(in); this.nestedSerializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, getUserCodeClassLoader()); }
@SuppressWarnings("unchecked") @Override protected void restoreKeyAndNamespaceSerializers( InternalTimersSnapshot<K, N> restoredTimersSnapshot, DataInputView in) throws IOException { List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); restoredTimersSnapshot.setKeySerializer((TypeSerializer<K>) serializersAndConfigs.get(0).f0); restoredTimersSnapshot.setKeySerializerConfigSnapshot((TypeSerializerSnapshot<K>) serializersAndConfigs.get(0).f1); restoredTimersSnapshot.setNamespaceSerializer((TypeSerializer<N>) serializersAndConfigs.get(1).f0); restoredTimersSnapshot.setNamespaceSerializerConfigSnapshot((TypeSerializerSnapshot<N>) serializersAndConfigs.get(1).f1); } }
@SuppressWarnings("unchecked") @Override protected void restoreKeyAndNamespaceSerializers( InternalTimersSnapshot<K, N> restoredTimersSnapshot, DataInputView in) throws IOException { List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); restoredTimersSnapshot.setKeySerializer((TypeSerializer<K>) serializersAndConfigs.get(0).f0); restoredTimersSnapshot.setKeySerializerConfigSnapshot((TypeSerializerSnapshot<K>) serializersAndConfigs.get(0).f1); restoredTimersSnapshot.setNamespaceSerializer((TypeSerializer<N>) serializersAndConfigs.get(1).f0); restoredTimersSnapshot.setNamespaceSerializerConfigSnapshot((TypeSerializerSnapshot<N>) serializersAndConfigs.get(1).f1); } }
@Nonnull @Override public StateMetaInfoSnapshot readStateMetaInfoSnapshot( @Nonnull DataInputView in, @Nonnull ClassLoader userCodeClassLoader) throws IOException { final StateDescriptor.Type stateDescType = StateDescriptor.Type.values()[in.readInt()]; final String stateName = in.readUTF(); List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); Map<String, String> optionsMap = Collections.singletonMap( StateMetaInfoSnapshot.CommonOptionsKeys.KEYED_STATE_TYPE.toString(), stateDescType.toString()); Map<String, TypeSerializerSnapshot<?>> serializerConfigSnapshotMap = new HashMap<>(2); serializerConfigSnapshotMap.put(StateMetaInfoSnapshot.CommonSerializerKeys.NAMESPACE_SERIALIZER.toString(), serializersAndConfigs.get(0).f1); serializerConfigSnapshotMap.put(StateMetaInfoSnapshot.CommonSerializerKeys.VALUE_SERIALIZER.toString(), serializersAndConfigs.get(1).f1); return new StateMetaInfoSnapshot( stateName, StateMetaInfoSnapshot.BackendStateType.KEY_VALUE, optionsMap, serializerConfigSnapshotMap); } }
@Nonnull @Override public StateMetaInfoSnapshot readStateMetaInfoSnapshot( @Nonnull DataInputView in, @Nonnull ClassLoader userCodeClassLoader) throws IOException { final StateDescriptor.Type stateDescType = StateDescriptor.Type.values()[in.readInt()]; final String stateName = in.readUTF(); List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); Map<String, String> optionsMap = Collections.singletonMap( StateMetaInfoSnapshot.CommonOptionsKeys.KEYED_STATE_TYPE.toString(), stateDescType.toString()); Map<String, TypeSerializerSnapshot<?>> serializerConfigSnapshotMap = new HashMap<>(2); serializerConfigSnapshotMap.put(StateMetaInfoSnapshot.CommonSerializerKeys.NAMESPACE_SERIALIZER.toString(), serializersAndConfigs.get(0).f1); serializerConfigSnapshotMap.put(StateMetaInfoSnapshot.CommonSerializerKeys.VALUE_SERIALIZER.toString(), serializersAndConfigs.get(1).f1); return new StateMetaInfoSnapshot( stateName, StateMetaInfoSnapshot.BackendStateType.KEY_VALUE, optionsMap, serializerConfigSnapshotMap); } }
TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(inputView, userCodeClassLoader);
TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(inputView, userCodeClassLoader);
@Override public RegisteredKeyedBackendStateMetaInfo.Snapshot<N, S> readStateMetaInfo(DataInputView in) throws IOException { RegisteredKeyedBackendStateMetaInfo.Snapshot<N, S> metaInfo = new RegisteredKeyedBackendStateMetaInfo.Snapshot<>(); metaInfo.setStateType(StateDescriptor.Type.values()[in.readInt()]); metaInfo.setName(in.readUTF()); List<Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot>> serializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); metaInfo.setNamespaceSerializer((TypeSerializer<N>) serializersAndConfigs.get(0).f0); metaInfo.setNamespaceSerializerConfigSnapshot(serializersAndConfigs.get(0).f1); metaInfo.setStateSerializer((TypeSerializer<S>) serializersAndConfigs.get(1).f0); metaInfo.setStateSerializerConfigSnapshot(serializersAndConfigs.get(1).f1); return metaInfo; } }
@Override public RegisteredKeyedBackendStateMetaInfo.Snapshot<N, S> readStateMetaInfo(DataInputView in) throws IOException { RegisteredKeyedBackendStateMetaInfo.Snapshot<N, S> metaInfo = new RegisteredKeyedBackendStateMetaInfo.Snapshot<>(); metaInfo.setStateType(StateDescriptor.Type.values()[in.readInt()]); metaInfo.setName(in.readUTF()); List<Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot>> serializersAndConfigs = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); metaInfo.setNamespaceSerializer((TypeSerializer<N>) serializersAndConfigs.get(0).f0); metaInfo.setNamespaceSerializerConfigSnapshot(serializersAndConfigs.get(0).f1); metaInfo.setStateSerializer((TypeSerializer<S>) serializersAndConfigs.get(1).f0); metaInfo.setStateSerializerConfigSnapshot(serializersAndConfigs.get(1).f1); return metaInfo; } }
@SuppressWarnings("unchecked") @Override public void read(DataInputView in) throws IOException { super.read(in); // only starting from version 3, we have the key serializer and its config snapshot written if (getReadVersion() >= 3) { Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot> keySerializerAndConfig = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader).get(0); this.keySerializer = (TypeSerializer<K>) keySerializerAndConfig.f0; this.keySerializerConfigSnapshot = keySerializerAndConfig.f1; } else { this.keySerializer = TypeSerializerSerializationUtil.tryReadSerializer(in, userCodeClassLoader); this.keySerializerConfigSnapshot = null; } int numKvStates = in.readShort(); stateMetaInfoSnapshots = new ArrayList<>(numKvStates); for (int i = 0; i < numKvStates; i++) { stateMetaInfoSnapshots.add( KeyedBackendStateMetaInfoSnapshotReaderWriters .getReaderForVersion(getReadVersion(), userCodeClassLoader) .readStateMetaInfo(in)); } } }
@Override public RegisteredOperatorBackendStateMetaInfo.Snapshot<S> readStateMetaInfo(DataInputView in) throws IOException { RegisteredOperatorBackendStateMetaInfo.Snapshot<S> stateMetaInfo = new RegisteredOperatorBackendStateMetaInfo.Snapshot<>(); stateMetaInfo.setName(in.readUTF()); stateMetaInfo.setAssignmentMode(OperatorStateHandle.Mode.values()[in.readByte()]); Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot> stateSerializerAndConfig = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader).get(0); stateMetaInfo.setPartitionStateSerializer((TypeSerializer<S>) stateSerializerAndConfig.f0); stateMetaInfo.setPartitionStateSerializerConfigSnapshot(stateSerializerAndConfig.f1); return stateMetaInfo; } }
@Override public RegisteredOperatorBackendStateMetaInfo.Snapshot<S> readOperatorStateMetaInfo(DataInputView in) throws IOException { RegisteredOperatorBackendStateMetaInfo.Snapshot<S> stateMetaInfo = new RegisteredOperatorBackendStateMetaInfo.Snapshot<>(); stateMetaInfo.setName(in.readUTF()); stateMetaInfo.setAssignmentMode(OperatorStateHandle.Mode.values()[in.readByte()]); Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot> stateSerializerAndConfig = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader).get(0); stateMetaInfo.setPartitionStateSerializer((TypeSerializer<S>) stateSerializerAndConfig.f0); stateMetaInfo.setPartitionStateSerializerConfigSnapshot(stateSerializerAndConfig.f1); return stateMetaInfo; } }
@Override public RegisteredBroadcastBackendStateMetaInfo.Snapshot<K, V> readBroadcastStateMetaInfo(final DataInputView in) throws IOException { RegisteredBroadcastBackendStateMetaInfo.Snapshot<K, V> stateMetaInfo = new RegisteredBroadcastBackendStateMetaInfo.Snapshot<>(); stateMetaInfo.setName(in.readUTF()); stateMetaInfo.setAssignmentMode(OperatorStateHandle.Mode.values()[in.readByte()]); List<Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot>> serializers = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader); Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot> keySerializerAndConfig = serializers.get(0); Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot> valueSerializerAndConfig = serializers.get(1); stateMetaInfo.setKeySerializer((TypeSerializer<K>) keySerializerAndConfig.f0); stateMetaInfo.setKeySerializerConfigSnapshot(keySerializerAndConfig.f1); stateMetaInfo.setValueSerializer((TypeSerializer<V>) valueSerializerAndConfig.f0); stateMetaInfo.setValueSerializerConfigSnapshot(valueSerializerAndConfig.f1); return stateMetaInfo; } }