You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2020/02/03 15:31:46 UTC

[GitHub] [ignite] ibessonov commented on a change in pull request #7302: IGNITE-12568 MessageFactory refactoring

ibessonov commented on a change in pull request #7302: IGNITE-12568 MessageFactory refactoring
URL: https://github.com/apache/ignite/pull/7302#discussion_r374167891
 
 

 ##########
 File path: modules/core/src/main/java/org/apache/ignite/internal/managers/communication/IgniteMessageFactoryImpl.java
 ##########
 @@ -0,0 +1,166 @@
+/*
+ * 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.managers.communication;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Supplier;
+
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.plugin.extensions.communication.IgniteMessageFactory;
+import org.apache.ignite.plugin.extensions.communication.Message;
+import org.apache.ignite.plugin.extensions.communication.MessageFactory;
+import org.apache.ignite.plugin.extensions.communication.MessageFactoryProvider;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.annotations.TestOnly;
+
+/**
+ * Message factory implementation which is responsible for instantiation of all communication messages.
+ */
+public class IgniteMessageFactoryImpl implements IgniteMessageFactory {
+    /** Offset. */
+    private static final int OFF = -Short.MIN_VALUE;
+
+    /** Array size. */
+    private static final int ARR_SIZE = 1 << Short.SIZE;
+
+    /** Custom messages registry. Used for test purposes. */
+    private static final Map<Short, Supplier<Message>> CUSTOM = new ConcurrentHashMap<>();
+
+    /** Message suppliers. */
+    private final Supplier<Message>[] msgSuppliers = (Supplier<Message>[]) Array.newInstance(Supplier.class, ARR_SIZE);
+
+    /** Initialized flag. If {@code true} then new message type couldn't be registered. */
+    private boolean initialized;
+
+    /**
+     * Contructor.
+     *
+     * @param factories Concrete message factories or message factory providers. Cfn't be empty or {@code null}.
+     */
+    public IgniteMessageFactoryImpl(MessageFactory[] factories) {
+        if (factories == null || factories.length == 0)
+            throw new IllegalArgumentException("Message factory couldn't be initialized. Factories aren't provided.");
+
+        List<MessageFactory> old = new ArrayList<>(factories.length);
+
+        for (MessageFactory factory : factories) {
+            if (factory instanceof MessageFactoryProvider) {
+                MessageFactoryProvider p = (MessageFactoryProvider)factory;
+
+                p.registerAll(this);
+            }
+            else
+                old.add(factory);
+        }
+
+        if (!old.isEmpty()) {
+            for (int i = 0; i < ARR_SIZE; i++) {
+                Supplier<Message> curr = msgSuppliers[i];
+
+                if (curr == null) {
+                    short directType = indexToDirectType(i);
+
+                    for (MessageFactory factory : old) {
+                        Message msg = factory.create(directType);
 
 Review comment:
   Should you wrap this into try-catch?
   Specification isn't very clear - it says that method returns null for unresolved direct type but at the same time this class (IgniteMessageFactoryImpl) throws exceptions for that same reason. We should probably unify this behavior and correct tests correspondingly.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services