rpuch commented on a change in pull request #523:
URL: https://github.com/apache/ignite-3/pull/523#discussion_r774975390



##########
File path: 
modules/network/src/main/java/org/apache/ignite/internal/network/serialization/PerSessionSerializationService.java
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.ignite.internal.network.serialization;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.stream.Collectors;
+import org.apache.ignite.internal.network.NetworkMessagesFactory;
+import org.apache.ignite.internal.network.message.ClassDescriptorMessage;
+import org.apache.ignite.internal.network.message.FieldDescriptorMessage;
+import org.apache.ignite.network.NetworkMessage;
+import org.apache.ignite.network.serialization.MessageDeserializer;
+import org.apache.ignite.network.serialization.MessageSerializer;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.TestOnly;
+
+/**
+ * Per-session serialization service.
+ * Handles (de-)serialization of messages, object (de-)serialization and class 
descriptor merging.
+ */
+public class PerSessionSerializationService {
+    /** Network messages factory. */
+    private static final NetworkMessagesFactory MSG_FACTORY = new 
NetworkMessagesFactory();
+
+    /** Global serialization service. */
+    @NotNull
+    private final SerializationService serializationService;
+
+    /**
+     * Map with merged class descriptors. They are the result of the merging 
of a local and a remote descriptor.
+     * The key in this map is a <b>remote</b> descriptor id.
+     */
+    private final ConcurrentMap<Integer, ClassDescriptor> mergedDescriptorMap 
= new ConcurrentHashMap<>();
+
+    /**
+     * Immutable view over {@link #mergedDescriptorMap}. Used by {@link 
#serializationService}.
+     */
+    private final Map<Integer, ClassDescriptor> descriptorMapView = 
Collections.unmodifiableMap(mergedDescriptorMap);
+
+    public PerSessionSerializationService(@NotNull SerializationService 
serializationService) {
+        this.serializationService = serializationService;
+    }
+
+    /**
+     * Creates a message serializer.
+     *
+     * @see SerializationService#createSerializer(short, short)
+     */
+    public <T extends NetworkMessage> MessageSerializer<T> 
createMessageSerializer(short groupType, short messageType) {
+        return serializationService.createSerializer(groupType, messageType);
+    }
+
+    /**
+     * Creates a message deserializer.
+     *
+     * @see SerializationService#createDeserializer(short, short)
+     */
+    public <T extends NetworkMessage> MessageDeserializer<T> 
createMessageDeserializer(short groupType, short messageType) {
+        return serializationService.createDeserializer(groupType, messageType);
+    }
+
+    /**
+     * Serializes a marshallable object to a byte array.
+     *
+     * @see SerializationService#writeMarshallable(Object)
+     */
+    public <T> SerializationResult writeMarshallable(T marshallable) {
+        return serializationService.writeMarshallable(marshallable);
+    }
+
+    /**
+     * Deserializes a marshallable object from a byte array.
+     *
+     * @see SerializationService#readMarshallable(Map, byte[])
+     */
+    public <T> T readMarshallable(List<ClassDescriptorMessage> 
missingDescriptors, byte[] marshallableData) {
+        mergeDescriptors(missingDescriptors);
+
+        return serializationService.readMarshallable(descriptorMapView, 
marshallableData);
+    }
+
+    /**
+     * Creates a list of messages holding class descriptors.
+     *
+     * @param descriptorIds Ids of class descriptors.
+     * @return List of class descriptor network messages.
+     */
+    public List<ClassDescriptorMessage> 
createClassDescriptorsMessages(List<Integer> descriptorIds) {
+        return descriptorIds.stream().map(id -> {
+            ClassDescriptor descriptor = 
serializationService.getClassDescriptor(id);
+
+            List<FieldDescriptorMessage> fields = descriptor.fields().stream()
+                    .map(d -> {
+                        return MSG_FACTORY.fieldDescriptorMessage()
+                                .name(d.name())
+                                .typeDescriptorId(d.typeDescriptorId())
+                                .className(d.clazz().getName())
+                                .build();
+                    })
+                    .collect(Collectors.toList());
+
+            Serialization serialization = descriptor.serialization();
+
+            return MSG_FACTORY.classDescriptorMessage()
+                    .fields(fields)
+                    .isFinal(descriptor.isFinal())
+                    .serializationType(serialization.type().value())
+                    
.hasSerializationOverride(serialization.hasSerializationOverride())
+                    .hasWriteReplace(serialization.hasWriteReplace())
+                    .hasReadResolve(serialization.hasReadResolve())
+                    .descriptorId(descriptor.descriptorId())
+                    .className(descriptor.className())
+                    .build();
+        }).collect(Collectors.toList());
+    }
+
+    private void mergeDescriptors(List<ClassDescriptorMessage> 
remoteDescriptors) {
+        for (ClassDescriptorMessage clsMsg : remoteDescriptors) {
+            int clsDescriptorId = clsMsg.descriptorId();
+
+            boolean isClsBuiltin = 
serializationService.isBuiltin(clsDescriptorId);
+
+            if (isClsBuiltin) {
+                continue;
+            }
+
+            ClassDescriptor mergedDescriptor = 
mergedDescriptorMap.get(clsDescriptorId);
+
+            if (mergedDescriptor != null) {

Review comment:
       Because it would clearly communicate the intent to 'put a value to the 
map only if there is no such value there yet'

##########
File path: 
modules/network/src/integrationTest/java/org/apache/ignite/internal/network/recovery/ItRecoveryHandshakeTest.java
##########
@@ -399,6 +400,7 @@ private ConnectionManager startManager(
             ClientStageFail clientHandshakeFailAt
     ) {
         var registry = new TestMessageSerializationRegistryImpl();
+        var serializationService = new SerializationService(registry, null);

Review comment:
       Ok




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to