Skip to content

Commit

Permalink
[FLINK-9377] [core] (part 4) Introduce BackwardsCompatibleConfigSnapshot
Browse files Browse the repository at this point in the history
The BackwardsCompatibleConfigSnapshot is a wrapper, dummy config
snapshot which wraps an actual config snapshot, as well as a
pre-existing serializer instance.

In previous versions, since the config snapshot wasn't a serializer
factory but simply a container for serializer parameters, previous
serializers didn't necessarily have config snapshots that are capable of
correctly creating a correct corresponding restore serializer.

In this case, since previous serializers still have serializers written
in the checkpoint, the backwards compatible solution would be to wrap
the written serializer and the config snapshot within the
BackwardsCompatibleConfigSnapshot dummy. When attempting to restore the
serializer, the wrapped serializer instance is returned instead of
actually calling the restoreSerializer method of the wrapped config
snapshot.
  • Loading branch information
tzulitai committed Oct 10, 2018
1 parent db65567 commit 1878691
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.api.common.typeutils;

import org.apache.flink.annotation.Internal;
import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.core.memory.DataInputView;
import org.apache.flink.core.memory.DataOutputView;
import org.apache.flink.util.Preconditions;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Objects;

/**
* A utility {@link TypeSerializerConfigSnapshot} that is used for backwards compatibility purposes.
*
* <p>In older versions of Flink (<= 1.6), we used to write state serializers into checkpoints, along
* with the serializer's configuration snapshot. Since 1.7.0, we no longer wrote the serializers, but
* instead used the configuration snapshot as a factory to instantiate serializers for restoring state.
* However, since some outdated implementations of configuration snapshots did not contain sufficient
* information to serve as a factory, the backwards compatible path for restoring from these older
* savepoints would be to just use the written serializer.
*
* <p>Therefore, when restoring from older savepoints which still contained both the config snapshot
* and the serializer, they are both wrapped within this utility class. When the caller intends
* to instantiate a restore serializer, we simply return the wrapped serializer instance.
*
* @param <T> the data type that the wrapped serializer instance serializes.
*/
@Internal
public class BackwardsCompatibleConfigSnapshot<T> extends TypeSerializerConfigSnapshot<T> {

/**
* The actual serializer config snapshot. This may be {@code null} when reading a
* savepoint from Flink <= 1.2.
*/
@Nullable
private TypeSerializerConfigSnapshot<?> wrappedConfigSnapshot;

/**
* The serializer instance written in savepoints.
*/
@Nonnull
private TypeSerializer<T> serializerInstance;

public BackwardsCompatibleConfigSnapshot(
@Nullable TypeSerializerConfigSnapshot<?> wrappedConfigSnapshot,
TypeSerializer<T> serializerInstance) {

this.wrappedConfigSnapshot = wrappedConfigSnapshot;
this.serializerInstance = Preconditions.checkNotNull(serializerInstance);
}

@Override
public void write(DataOutputView out) throws IOException {
throw new UnsupportedOperationException(
"This is a dummy config snapshot used only for backwards compatibility.");
}

@Override
public void read(DataInputView in) throws IOException {
throw new UnsupportedOperationException(
"This is a dummy config snapshot used only for backwards compatibility.");
}

@Override
public int getVersion() {
throw new UnsupportedOperationException(
"This is a dummy config snapshot used only for backwards compatibility.");
}

@Override
public TypeSerializer<T> restoreSerializer() {
return serializerInstance;
}

@Override
@SuppressWarnings("unchecked")
public TypeSerializerSchemaCompatibility<T> resolveSchemaCompatibility(TypeSerializer<?> newSerializer) {
if (wrappedConfigSnapshot != null) {
return (TypeSerializerSchemaCompatibility<T>) wrappedConfigSnapshot.resolveSchemaCompatibility(newSerializer);
} else {
// if there is no configuration snapshot to check against,
// then we can only assume that the new serializer is compatible as is
return TypeSerializerSchemaCompatibility.compatibleAsIs();
}
}

@Override
public int hashCode() {
int result = (wrappedConfigSnapshot != null) ? wrappedConfigSnapshot.hashCode() : 0;
result = 31 * result + serializerInstance.hashCode();
return result;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

BackwardsCompatibleConfigSnapshot<?> that = (BackwardsCompatibleConfigSnapshot<?>) o;

return Objects.equals(that.wrappedConfigSnapshot, wrappedConfigSnapshot)
&& that.serializerInstance.equals(serializerInstance);
}

@VisibleForTesting
public TypeSerializerConfigSnapshot<?> getWrappedConfigSnapshot() {
return wrappedConfigSnapshot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,16 @@ public static <T> CompatibilityResult<T> resolveCompatibilityResult(
TypeSerializerConfigSnapshot precedingSerializerConfigSnapshot,
TypeSerializer<T> newSerializer) {

if (precedingSerializerConfigSnapshot != null) {
CompatibilityResult<T> initialResult = newSerializer.ensureCompatibility(precedingSerializerConfigSnapshot);
TypeSerializerConfigSnapshot<?> actualConfigSnapshot;
if (precedingSerializerConfigSnapshot instanceof BackwardsCompatibleConfigSnapshot) {
actualConfigSnapshot =
((BackwardsCompatibleConfigSnapshot) precedingSerializerConfigSnapshot).getWrappedConfigSnapshot();
} else {
actualConfigSnapshot = precedingSerializerConfigSnapshot;
}

if (actualConfigSnapshot != null) {
CompatibilityResult<T> initialResult = newSerializer.ensureCompatibility(actualConfigSnapshot);

if (!initialResult.isRequiresMigration()) {
return initialResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static List<Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot>> read
new ArrayList<>(numSerializersAndConfigSnapshots);

TypeSerializer<?> serializer;
TypeSerializerConfigSnapshot configSnapshot;
TypeSerializerConfigSnapshot<?> configSnapshot;
try (
ByteArrayInputStreamWithPos bufferWithPos = new ByteArrayInputStreamWithPos(buffer);
DataInputViewStreamWrapper bufferWrapper = new DataInputViewStreamWrapper(bufferWithPos)) {
Expand All @@ -214,10 +214,17 @@ public static List<Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot>> read
serializer = tryReadSerializer(bufferWrapper, userCodeClassLoader, true);

bufferWithPos.setPosition(offsets[i * 2 + 1]);
configSnapshot = TypeSerializerConfigSnapshotSerializationUtil.readSerializerConfigSnapshot(bufferWrapper, userCodeClassLoader);

serializersAndConfigSnapshots.add(
new Tuple2<TypeSerializer<?>, TypeSerializerConfigSnapshot>(serializer, configSnapshot));
// the config snapshot is replaced with a dummy one, which wraps
// the actual config snapshot and the deserialized serializer.
// this is for backwards compatibility reasons, since before Flink 1.6, some serializers
// do not return config snapshots that can be used as a factory for themselves.
configSnapshot = new BackwardsCompatibleConfigSnapshot<>(
TypeSerializerConfigSnapshotSerializationUtil.readSerializerConfigSnapshot(
bufferWrapper, userCodeClassLoader),
serializer);

serializersAndConfigSnapshots.add(new Tuple2<>(serializer, configSnapshot));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,13 @@ public void testSerializerAndConfigPairsSerializationWithSerializerDeserializati

Assert.assertEquals(2, restored.size());
Assert.assertTrue(restored.get(0).f0 instanceof UnloadableDummyTypeSerializer);
Assert.assertEquals(IntSerializer.INSTANCE.snapshotConfiguration(), restored.get(0).f1);
Assert.assertEquals(
IntSerializer.INSTANCE.snapshotConfiguration(),
((BackwardsCompatibleConfigSnapshot) restored.get(0).f1).getWrappedConfigSnapshot());
Assert.assertTrue(restored.get(1).f0 instanceof UnloadableDummyTypeSerializer);
Assert.assertEquals(DoubleSerializer.INSTANCE.snapshotConfiguration(), restored.get(1).f1);
Assert.assertEquals(
DoubleSerializer.INSTANCE.snapshotConfiguration(),
((BackwardsCompatibleConfigSnapshot) restored.get(1).f1).getWrappedConfigSnapshot());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
package org.apache.flink.formats.avro.typeutils;

import org.apache.flink.api.common.ExecutionConfig;
import org.apache.flink.api.common.typeutils.BackwardsCompatibleConfigSnapshot;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.api.common.typeutils.TypeSerializerConfigSnapshot;
import org.apache.flink.api.common.typeutils.TypeSerializerSerializationUtil;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.typeutils.runtime.PojoSerializer;
import org.apache.flink.api.java.typeutils.runtime.PojoSerializer.PojoSerializerConfigSnapshot;
import org.apache.flink.core.memory.DataInputViewStreamWrapper;
import org.apache.flink.formats.avro.generated.SimpleUser;
import org.apache.flink.formats.avro.utils.TestDataGenerator;
Expand Down Expand Up @@ -100,16 +100,19 @@ public void testCompatibilityWithPojoSerializer() throws Exception {
assertNotNull(configSnapshot);

assertTrue(serializer instanceof PojoSerializer);
assertTrue(configSnapshot instanceof PojoSerializerConfigSnapshot);
assertTrue(configSnapshot instanceof BackwardsCompatibleConfigSnapshot);

TypeSerializerConfigSnapshot<?> wrappedConfigSnapshot =
((BackwardsCompatibleConfigSnapshot) configSnapshot).getWrappedConfigSnapshot();

// sanity check for the test: check that the test data works with the original serializer
validateDeserialization(serializer);

// sanity check for the test: check that a PoJoSerializer and the original serializer work together
assertFalse(serializer.ensureCompatibility(configSnapshot).isRequiresMigration());
assertFalse(serializer.ensureCompatibility(wrappedConfigSnapshot).isRequiresMigration());

final TypeSerializer<SimpleUser> newSerializer = new AvroTypeInfo<>(SimpleUser.class, true).createSerializer(new ExecutionConfig());
assertFalse(newSerializer.ensureCompatibility(configSnapshot).isRequiresMigration());
assertFalse(newSerializer.ensureCompatibility(wrappedConfigSnapshot).isRequiresMigration());

// deserialize the data and make sure this still works
validateDeserialization(newSerializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package org.apache.flink.api.scala.runtime
import java.io.InputStream

import org.apache.flink.api.common.ExecutionConfig
import org.apache.flink.api.common.typeutils.TypeSerializerSerializationUtil
import org.apache.flink.api.common.typeutils.{BackwardsCompatibleConfigSnapshot, TypeSerializerSerializationUtil}
import org.apache.flink.api.java.typeutils.runtime.TupleSerializerConfigSnapshot
import org.apache.flink.api.scala.createTypeInformation
import org.apache.flink.api.scala.runtime.TupleSerializerCompatibilityTestGenerator._
Expand Down Expand Up @@ -55,11 +55,19 @@ class TupleSerializerCompatibilityTest {
assertNotNull(oldSerializer)
assertNotNull(oldConfigSnapshot)
assertTrue(oldSerializer.isInstanceOf[CaseClassSerializer[_]])
assertTrue(oldConfigSnapshot.isInstanceOf[TupleSerializerConfigSnapshot[_]])
assertTrue(oldConfigSnapshot.isInstanceOf[BackwardsCompatibleConfigSnapshot[_]])

val wrappedOldConfigSnapshot = oldConfigSnapshot
.asInstanceOf[BackwardsCompatibleConfigSnapshot[_]]
.getWrappedConfigSnapshot

assertTrue(wrappedOldConfigSnapshot.isInstanceOf[TupleSerializerConfigSnapshot[_]])

val currentSerializer = createTypeInformation[TestCaseClass]
.createSerializer(new ExecutionConfig())
assertFalse(currentSerializer.ensureCompatibility(oldConfigSnapshot).isRequiresMigration)
assertFalse(currentSerializer
.ensureCompatibility(wrappedOldConfigSnapshot)
.isRequiresMigration)

// test old data serialization
is.close()
Expand Down

0 comments on commit 1878691

Please sign in to comment.