tzulitai commented on a change in pull request #7759: 
[FLINK-11485][FLINK-10897] POJO state schema evolution / migrate PojoSerializer 
to use new compatibility APIs
URL: https://github.com/apache/flink/pull/7759#discussion_r258839933
 
 

 ##########
 File path: 
flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoSerializerSnapshot.java
 ##########
 @@ -0,0 +1,401 @@
+/*
+ * 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
+ *
+ *     http://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.java.typeutils.runtime;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.CompositeTypeSerializerUtil;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.util.LinkedOptionalMap;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * Snapshot class for the {@link PojoSerializer}.
+ */
+@Internal
+public class PojoSerializerSnapshot<T> implements TypeSerializerSnapshot<T> {
+
+       /**
+        * We start from version {@code 2}. {@code 1} is retained for {@link 
PojoSerializer.PojoSerializerConfigSnapshot}.
+        */
+       private static final int VERSION = 2;
+
+       /**
+        * Contains the actual content for the serializer snapshot.
+        */
+       private PojoSerializerSnapshotData<T> snapshotData;
+
+       /**
+        * Constructor for reading the snapshot.
+        */
+       public PojoSerializerSnapshot() {}
+
+       /**
+        * Constructor for writing the snapshot.
+        *
+        * @param pojoClass the Pojo type class.
+        * @param fieldSerializers map of fields to their corresponding 
serializers.
+        * @param registeredSubclassSerializers map of registered subclasses to 
their corresponding serializers.
+        * @param nonRegisteredSubclassSerializers map of non-registered 
subclasses to their corresponding serializers.
+        */
+       PojoSerializerSnapshot(
+                       Class<T> pojoClass,
+                       LinkedHashMap<Field, TypeSerializer<?>> 
fieldSerializers,
+                       LinkedHashMap<Class<?>, TypeSerializer<?>> 
registeredSubclassSerializers,
+                       HashMap<Class<?>, TypeSerializer<?>> 
nonRegisteredSubclassSerializers) {
+
+               this.snapshotData = PojoSerializerSnapshotData.createFrom(
+                       pojoClass,
+                       fieldSerializers,
+                       registeredSubclassSerializers,
+                       nonRegisteredSubclassSerializers);
+       }
+
+       @Override
+       public int getCurrentVersion() {
+               return VERSION;
+       }
+
+       @Override
+       public void writeSnapshot(DataOutputView out) throws IOException {
+               this.snapshotData.writeSnapshotData(out);
+       }
+
+       @Override
+       public void readSnapshot(int readVersion, DataInputView in, ClassLoader 
userCodeClassLoader) throws IOException {
+               checkArgument(readVersion == 2, "unrecognized read version %d", 
readVersion);
+               this.snapshotData = PojoSerializerSnapshotData.createFrom(in, 
userCodeClassLoader);
+       }
+
+       @Override
+       @SuppressWarnings("unchecked")
+       public TypeSerializer<T> restoreSerializer() {
+               final int numFields = 
snapshotData.getFieldSerializerSnapshots().size();
+               final Field[] restoredFields = 
snapshotData.getFieldSerializerSnapshots().getKeysNoThrow().toArray(new 
Field[numFields]);
+               final TypeSerializer<Object>[] restoredFieldSerializers = 
snapshotData.getFieldSerializerSnapshots().getValues()
+                       .stream()
+                       .map(TypeSerializerSnapshot::restoreSerializer)
+                       .toArray(TypeSerializer[]::new);
+
+               final LinkedHashMap<Class<?>, TypeSerializer<?>> 
registeredSubclassSerializers = restoreSerializers(
+                       
snapshotData.getRegisteredSubclassSerializerSnapshots().unwrapOptionals());
+               final Tuple2<LinkedHashMap<Class<?>, Integer>, 
TypeSerializer<Object>[]> decomposedSubclassSerializerRegistry =
+                       
decomposeSubclassSerializerRegistry(registeredSubclassSerializers);
+
+               final LinkedHashMap<Class<?>, TypeSerializer<?>> 
nonRegisteredSubclassSerializers = restoreSerializers(
+                       
snapshotData.getNonRegisteredSubclassSerializerSnapshots().unwrapOptionals());
+
+               return new PojoSerializer<>(
+                       snapshotData.getPojoClass(),
+                       restoredFields,
+                       restoredFieldSerializers,
+                       decomposedSubclassSerializerRegistry.f0,
+                       decomposedSubclassSerializerRegistry.f1,
+                       nonRegisteredSubclassSerializers,
+                       null);
+       }
+
+       @Override
+       public TypeSerializerSchemaCompatibility<T> 
resolveSchemaCompatibility(TypeSerializer<T> newSerializer) {
+               if (newSerializer.getClass() != PojoSerializer.class) {
+                       return TypeSerializerSchemaCompatibility.incompatible();
+               }
+
+               final PojoSerializer<T> newPojoSerializer = (PojoSerializer<T>) 
newSerializer;
+
+               final Class<T> previousPojoClass = snapshotData.getPojoClass();
+               final LinkedOptionalMap<Field, TypeSerializerSnapshot<?>> 
fieldSerializerSnapshots = snapshotData.getFieldSerializerSnapshots();
+               final LinkedOptionalMap<Class<?>, TypeSerializerSnapshot<?>> 
registeredSubclassSerializerSnapshots = 
snapshotData.getRegisteredSubclassSerializerSnapshots();
+               final LinkedOptionalMap<Class<?>, TypeSerializerSnapshot<?>> 
nonRegisteredSubclassSerializerSnapshots = 
snapshotData.getNonRegisteredSubclassSerializerSnapshots();
+
+               if (previousPojoClass != newPojoSerializer.getPojoClass()) {
+                       return TypeSerializerSchemaCompatibility.incompatible();
+               }
+
+               if 
(!registeredSubclassSerializerSnapshots.absentKeysOrValues().isEmpty()) {
+                       return TypeSerializerSchemaCompatibility.incompatible();
+               }
+
+               if 
(!nonRegisteredSubclassSerializerSnapshots.absentKeysOrValues().isEmpty()) {
+                       return TypeSerializerSchemaCompatibility.incompatible();
+               }
+
+               final 
CompositeTypeSerializerUtil.IntermediateCompatibilityResult<T> 
fieldSerializerCompatibility =
+                       getCompatibilityOfPreExistingFields(newPojoSerializer, 
fieldSerializerSnapshots);
+
+               final 
CompositeTypeSerializerUtil.IntermediateCompatibilityResult<T> 
preExistingRegistrationsCompatibility =
+                       
getCompatibilityOfPreExistingRegisteredSubclasses(newPojoSerializer, 
registeredSubclassSerializerSnapshots);
+
+               if (fieldSerializerCompatibility.isIncompatible() || 
preExistingRegistrationsCompatibility.isIncompatible()) {
+                       return TypeSerializerSchemaCompatibility.incompatible();
+               }
+
+               if (newPojoHasNewOrRemovedFields(fieldSerializerSnapshots, 
newPojoSerializer)
+                               || 
fieldSerializerCompatibility.isCompatibleAfterMigration()
+                               || 
preExistingRegistrationsCompatibility.isCompatibleAfterMigration()) {
+
+                       return 
TypeSerializerSchemaCompatibility.compatibleAfterMigration();
+               } else if 
(newPojoHasDifferentSubclassRegistrationOrder(registeredSubclassSerializerSnapshots,
 newPojoSerializer)
+                               || 
previousSerializerHasNonRegisteredSubclasses(nonRegisteredSubclassSerializerSnapshots)
+                               || 
fieldSerializerCompatibility.isCompatibleWithReconfiguredSerializer()
+                               || 
preExistingRegistrationsCompatibility.isCompatibleWithReconfiguredSerializer()) 
{
+
+                       return 
TypeSerializerSchemaCompatibility.compatibleWithReconfiguredSerializer(
+                               constructReconfiguredPojoSerializer(
+                                       newPojoSerializer,
+                                       fieldSerializerCompatibility,
+                                       registeredSubclassSerializerSnapshots,
+                                       preExistingRegistrationsCompatibility,
+                                       
nonRegisteredSubclassSerializerSnapshots));
+               } else {
+                       return 
TypeSerializerSchemaCompatibility.compatibleAsIs();
+               }
 
 Review comment:
   I've added some unit tests that tests how removal / addition of Pojo fields 
and compatibility of field serializers affect which compatibility result is 
returned.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to