/** * Creates the restore serializer from the pos-th config snapshot. */ public <T> TypeSerializer<T> getRestoredNestedSerializer(int pos) { checkArgument(pos < nestedSnapshots.length); @SuppressWarnings("unchecked") TypeSerializerSnapshot<T> snapshot = (TypeSerializerSnapshot<T>) nestedSnapshots[pos]; return snapshot.restoreSerializer(); }
@SuppressWarnings("deprecation") @Test public void movingForward() throws IOException { TypeSerializerSnapshot<ElementT> previousSnapshot = snapshotUnderTest(); TypeSerializer<ElementT> restoredSerializer = previousSnapshot.restoreSerializer(); TypeSerializerSnapshot<ElementT> nextSnapshot = restoredSerializer.snapshotConfiguration(); assertThat(nextSnapshot, instanceOf(testSpecification.snapshotClass)); TypeSerializerSnapshot<ElementT> nextSnapshotDeserialized = writeAndThenReadTheSnapshot(restoredSerializer, nextSnapshot); assertThat(nextSnapshotDeserialized, allOf( notNullValue(), not(instanceOf(TypeSerializerConfigSnapshot.class)) )); }
@Test public void restoredSerializerIsAbleToDeserializePreviousData() throws IOException { TypeSerializerSnapshot<ElementT> snapshot = snapshotUnderTest(); TypeSerializer<ElementT> serializer = snapshot.restoreSerializer(); DataInputView input = dataUnderTest(); final Matcher<ElementT> matcher = testSpecification.testDataElementMatcher; for (int i = 0; i < testSpecification.testDataCount; i++) { final ElementT result = serializer.deserialize(input); assertThat(result, matcher); } }
@Test public void recordSerializedShouldBeDeserializeWithTheResortedSerializer() throws IOException { // user is an avro generated test object. final User user = TestDataGenerator.generateRandomUser(new Random()); final AvroSerializer<User> originalSerializer = new AvroSerializer<>(User.class); // // first serialize the record // ByteBuffer serializedUser = serialize(originalSerializer, user); // // then restore a serializer from the snapshot // TypeSerializer<User> restoredSerializer = originalSerializer.snapshotConfiguration().restoreSerializer(); // // now deserialize the user with the resorted serializer. // User restoredUser = deserialize(restoredSerializer, serializedUser); assertThat(user, is(restoredUser)); }
@Test public void testSnapshotConfigurationAndReconfigure() throws Exception { final TypeSerializer<T> serializer = getSerializer(); final 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(), getSerializer()); } TypeSerializerSchemaCompatibility<T> strategy = restoredConfig.resolveSchemaCompatibility(getSerializer()); assertTrue(strategy.isCompatibleAsIs()); TypeSerializer<T> restoreSerializer = restoredConfig.restoreSerializer(); assertEquals(serializer.getClass(), restoreSerializer.getClass()); }
@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 } }
@Nonnull public final TypeSerializer<T> previousSchemaSerializer() { if (cachedRestoredSerializer != null) { return cachedRestoredSerializer; } this.cachedRestoredSerializer = previousSerializerSnapshot.restoreSerializer(); return cachedRestoredSerializer; } }
@Nonnull public final TypeSerializer<T> previousSchemaSerializer() { if (cachedRestoredSerializer != null) { return cachedRestoredSerializer; } this.cachedRestoredSerializer = previousSerializerSnapshot.restoreSerializer(); return cachedRestoredSerializer; } }
/** * Creates the restore serializer from the pos-th config snapshot. */ public <T> TypeSerializer<T> getRestoreSerializer(int pos) { checkArgument(pos < nestedSnapshots.length); @SuppressWarnings("unchecked") TypeSerializerSnapshot<T> snapshot = (TypeSerializerSnapshot<T>) nestedSnapshots[pos]; return snapshot.restoreSerializer(); }
@SuppressWarnings({ "rawtypes", "unchecked" }) private <T> TypeSerializer<T> getKeySerializer() { return proxy != null ? (TypeSerializer) proxy.getKeySerializerConfigSnapshot().restoreSerializer() : (TypeSerializer) keySerializer; }
@SuppressWarnings("unchecked") public static <T> Optional<TypeSerializer<T>> getSerializer(KeyedBackendSerializationProxy<?> proxy, String stateName) { for (StateMetaInfoSnapshot snapshot : proxy.getStateMetaInfoSnapshots()) { if (snapshot.getName().equals(stateName)) { return Optional .of((TypeSerializer<T>) snapshot .getTypeSerializerSnapshot(CommonSerializerKeys.VALUE_SERIALIZER) .restoreSerializer()); } } return Optional.empty(); }
private TypeSerializer<?> getKeySerializer(KeyedBackendSerializationProxy<?> proxy) { TypeSerializer<?> keySerializer = proxy.getKeySerializerConfigSnapshot().restoreSerializer(); if (keySerializer instanceof TupleSerializerBase) { TupleSerializerBase ts = (TupleSerializerBase) keySerializer; if (ts.getTupleClass().equals(Tuple1.class)) { return ts.getFieldSerializers()[0]; } } return keySerializer; }