igalshilman 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_r258824773
 
 

 ##########
 File path: 
flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoSerializerSnapshotData.java
 ##########
 @@ -0,0 +1,308 @@
+/*
+ * 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.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.api.common.typeutils.TypeSerializerUtils;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.util.InstantiationUtil;
+import org.apache.flink.util.LinkedOptionalMap;
+import org.apache.flink.util.function.BiConsumerWithException;
+import org.apache.flink.util.function.BiFunctionWithException;
+import org.apache.flink.util.function.FunctionWithException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import static org.apache.flink.util.LinkedOptionalMap.optionalMapOf;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * This class holds the snapshot content for the {@link PojoSerializer}.
+ *
+ * <h2>Serialization Format</hr>
+ *
+ * <p>The serialization format defined by this class is as follows:
+ *
+ * <pre>{@code
+ * 
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                                            POJO class name                
                          |
+ * 
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |      number of fields      |                (field name, field serializer 
snapshot)                 |
+ * |                            |                                pairs         
                          |
+ * 
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |         number of          |       (registered subclass name, subclass 
serializer snapshot)         |
+ * |   registered subclasses    |                                pairs         
                          |
+ * 
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |         number of          |     (non-registered subclass name, subclass 
serializer snapshot)       |
+ * | non-registered subclasses  |                                pairs         
                          |
+ * 
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * }</pre>
+ */
+@Internal
+final class PojoSerializerSnapshotData<T> {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(PojoSerializerSnapshotData.class);
+
+       // 
---------------------------------------------------------------------------------------------
+       //  Factory methods
+       // 
---------------------------------------------------------------------------------------------
+
+       /**
+        * Creates a {@link PojoSerializerSnapshotData} from configuration of a 
{@link PojoSerializer}.
+        *
+        * <p>This factory method is meant to be used in regular write paths, 
i.e. when taking a snapshot
+        * of the {@link PojoSerializer}. All POJO fields, registered subclass 
classes, and non-registered
+        * subclass classes are all present.
+        */
+       static <T> PojoSerializerSnapshotData<T> createFrom(
+                       Class<T> pojoClass,
+                       LinkedHashMap<Field, TypeSerializer<?>> 
fieldSerializers,
+                       LinkedHashMap<Class<?>, TypeSerializer<?>> 
registeredSubclassSerializers,
+                       Map<Class<?>, TypeSerializer<?>> 
nonRegisteredSubclassSerializers) {
+
+               LinkedHashMap<Field, TypeSerializerSnapshot<?>> 
fieldSerializerSnapshots = new LinkedHashMap<>(fieldSerializers.size());
+               fieldSerializers.forEach((k, v) -> 
fieldSerializerSnapshots.put(k, 
TypeSerializerUtils.snapshotBackwardsCompatible(v)));
+
+               LinkedHashMap<Class<?>, TypeSerializerSnapshot<?>> 
registeredSubclassSerializerSnapshots = new 
LinkedHashMap<>(registeredSubclassSerializers.size());
+               registeredSubclassSerializers.forEach((k, v) -> 
registeredSubclassSerializerSnapshots.put(k, 
TypeSerializerUtils.snapshotBackwardsCompatible(v)));
+
+               Map<Class<?>, TypeSerializerSnapshot<?>> 
nonRegisteredSubclassSerializerSnapshots = new 
HashMap<>(nonRegisteredSubclassSerializers.size());
+               nonRegisteredSubclassSerializers.forEach((k, v) -> 
nonRegisteredSubclassSerializerSnapshots.put(k, 
TypeSerializerUtils.snapshotBackwardsCompatible(v)));
+
+               return new PojoSerializerSnapshotData<>(
+                       pojoClass,
+                       optionalMapOf(fieldSerializerSnapshots, Field::getName),
+                       optionalMapOf(registeredSubclassSerializerSnapshots, 
Class::getName),
+                       optionalMapOf(nonRegisteredSubclassSerializerSnapshots, 
Class::getName));
+       }
+
+       /**
+        * Creates a {@link PojoSerializerSnapshotData} from serialized data 
stream.
+        *
+        * <p>This factory method is meant to be used in regular read paths, 
i.e. when reading back a snapshot
+        * of the {@link PojoSerializer}. POJO fields, registered subclass 
classes, and non-registered subclass
+        * classes may no longer be present anymore.
+        */
+       static <T> PojoSerializerSnapshotData<T> createFrom(DataInputView in, 
ClassLoader userCodeClassLoader) throws IOException {
+               return PojoSerializerSnapshotData.readSnapshotData(in, 
userCodeClassLoader);
+       }
+
+       /**
+        * Creates a {@link PojoSerializerSnapshotData} from existing 
snapshotted configuration of a {@link PojoSerializer}.
+        *
+        * <p>This factory method is meant to be used for backwards compatible 
read paths.
+        */
+       static <T> PojoSerializerSnapshotData<T> createFrom(
+               Class<T> pojoClass,
+               LinkedHashMap<Field, TypeSerializerSnapshot<?>> 
existingFieldSerializerSnapshots,
+               LinkedHashMap<Class<?>, TypeSerializerSnapshot<?>> 
existingRegisteredSubclassSerializerSnapshots,
+               LinkedHashMap<Class<?>, TypeSerializerSnapshot<?>> 
existingNonRegisteredSubclassSerializerSnapshots) {
+
+               return new PojoSerializerSnapshotData<>(
+                       pojoClass,
+                       optionalMapOf(existingFieldSerializerSnapshots, 
Field::getName),
+                       
optionalMapOf(existingRegisteredSubclassSerializerSnapshots, Class::getName),
+                       
optionalMapOf(existingNonRegisteredSubclassSerializerSnapshots, 
Class::getName));
+       }
+
+       private Class<T> pojoClass;
+       private LinkedOptionalMap<Field, TypeSerializerSnapshot<?>> 
fieldSerializerSnapshots;
+       private LinkedOptionalMap<Class<?>, TypeSerializerSnapshot<?>> 
registeredSubclassSerializerSnapshots;
+       private LinkedOptionalMap<Class<?>, TypeSerializerSnapshot<?>> 
nonRegisteredSubclassSerializerSnapshots;
+
+       private PojoSerializerSnapshotData(
+                       Class<T> typeClass,
+                       LinkedOptionalMap<Field, TypeSerializerSnapshot<?>> 
fieldSerializerSnapshots,
+                       LinkedOptionalMap<Class<?>, TypeSerializerSnapshot<?>> 
registeredSubclassSerializerSnapshots,
+                       LinkedOptionalMap<Class<?>, TypeSerializerSnapshot<?>> 
nonRegisteredSubclassSerializerSnapshots) {
+
+               this.pojoClass = checkNotNull(typeClass);
+               this.fieldSerializerSnapshots = 
checkNotNull(fieldSerializerSnapshots);
+               this.registeredSubclassSerializerSnapshots = 
checkNotNull(registeredSubclassSerializerSnapshots);
+               this.nonRegisteredSubclassSerializerSnapshots = 
checkNotNull(nonRegisteredSubclassSerializerSnapshots);
+       }
+
+       // 
---------------------------------------------------------------------------------------------
+       //  Snapshot data read / write methods
+       // 
---------------------------------------------------------------------------------------------
+
+       void writeSnapshotData(DataOutputView out) throws IOException {
+               out.writeUTF(pojoClass.getName());
+               writeOptionalMap(out, fieldSerializerSnapshots, 
PojoFieldUtils::writeField, TypeSerializerSnapshot::writeVersionedSnapshot);
+               writeOptionalMap(out, registeredSubclassSerializerSnapshots, 
NoOpWriter.noopWriter(), TypeSerializerSnapshot::writeVersionedSnapshot);
+               writeOptionalMap(out, nonRegisteredSubclassSerializerSnapshots, 
NoOpWriter.noopWriter(), TypeSerializerSnapshot::writeVersionedSnapshot);
+       }
+
+       private static <T> PojoSerializerSnapshotData<T> 
readSnapshotData(DataInputView in, ClassLoader userCodeClassLoader) throws 
IOException {
+               Class<T> pojoClass = InstantiationUtil.resolveClassByName(in, 
userCodeClassLoader);
+
+               LinkedOptionalMap<Field, TypeSerializerSnapshot<?>> 
fieldSerializerSnapshots = readOptionalMap(
+                       in,
+                       fieldReader(userCodeClassLoader),
+                       snapshotReader(userCodeClassLoader));
+               LinkedOptionalMap<Class<?>, TypeSerializerSnapshot<?>> 
registeredSubclassSerializerSnapshots = readOptionalMap(
+                       in,
+                       classReader(userCodeClassLoader),
+                       snapshotReader(userCodeClassLoader));
+               LinkedOptionalMap<Class<?>, TypeSerializerSnapshot<?>> 
nonRegisteredSubclassSerializerSnapshots = readOptionalMap(
+                       in,
+                       classReader(userCodeClassLoader),
+                       snapshotReader(userCodeClassLoader));
+
+               return new PojoSerializerSnapshotData<>(pojoClass, 
fieldSerializerSnapshots, registeredSubclassSerializerSnapshots, 
nonRegisteredSubclassSerializerSnapshots);
+       }
+
+       // 
---------------------------------------------------------------------------------------------
+       //  Snapshot data accessors
+       // 
---------------------------------------------------------------------------------------------
+
+       Class<T> getPojoClass() {
+               return pojoClass;
+       }
+
+       LinkedOptionalMap<Field, TypeSerializerSnapshot<?>> 
getFieldSerializerSnapshots() {
+               return fieldSerializerSnapshots;
+       }
+
+       LinkedOptionalMap<Class<?>, TypeSerializerSnapshot<?>> 
getRegisteredSubclassSerializerSnapshots() {
+               return registeredSubclassSerializerSnapshots;
+       }
+
+       LinkedOptionalMap<Class<?>, TypeSerializerSnapshot<?>> 
getNonRegisteredSubclassSerializerSnapshots() {
+               return nonRegisteredSubclassSerializerSnapshots;
+       }
+
+       // 
---------------------------------------------------------------------------------------------
+       //  Utilities
+       // 
---------------------------------------------------------------------------------------------
+
+       private static <K, V> void writeOptionalMap(
+               DataOutputView out,
+               LinkedOptionalMap<K, V> map,
+               BiConsumerWithException<DataOutputView, K, IOException> 
keyWriter,
+               BiConsumerWithException<DataOutputView, V, IOException> 
valueWriter) throws IOException {
+
+               out.writeInt(map.size());
+               map.forEach(((keyName, key, value) -> {
+                       out.writeUTF(keyName);
+                       if (key == null) {
+                               out.writeBoolean(false);
+                               }
 
 Review comment:
   nit: I think the formatting here are a bit funky

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to