You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2017/08/21 10:22:28 UTC

[05/10] ignite git commit: ignite-6124 Merge exchanges for multiple discovery events

http://git-wip-us.apache.org/repos/asf/ignite/blob/bebf2997/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/CacheGroupAffinityMessage.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/CacheGroupAffinityMessage.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/CacheGroupAffinityMessage.java
new file mode 100644
index 0000000..8a1ffb4
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/CacheGroupAffinityMessage.java
@@ -0,0 +1,339 @@
+/*
+ * 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.processors.cache.distributed.dht.preloader;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.internal.GridDirectCollection;
+import org.apache.ignite.internal.GridDirectMap;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache;
+import org.apache.ignite.internal.processors.cache.GridCacheSharedContext;
+import org.apache.ignite.internal.util.GridLongList;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.plugin.extensions.communication.Message;
+import org.apache.ignite.plugin.extensions.communication.MessageCollectionItemType;
+import org.apache.ignite.plugin.extensions.communication.MessageReader;
+import org.apache.ignite.plugin.extensions.communication.MessageWriter;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Information about affinity assignment.
+ */
+public class CacheGroupAffinityMessage implements Message {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** */
+    @GridDirectCollection(GridLongList.class)
+    private List<GridLongList> assigns;
+
+    /** */
+    @GridDirectCollection(GridLongList.class)
+    private List<GridLongList> idealAssigns;
+
+    /** */
+    @GridDirectMap(keyType = Integer.class, valueType = GridLongList.class)
+    private Map<Integer, GridLongList> assignsDiff;
+
+    /**
+     *
+     */
+    public CacheGroupAffinityMessage() {
+        // No-op.
+    }
+
+    /**
+     * @param assign0 Assignment.
+     * @param idealAssign0 Ideal assignment.
+     * @param assignDiff0 Difference with ideal affinity assignment.
+     */
+    private CacheGroupAffinityMessage(List<List<ClusterNode>> assign0,
+        List<List<ClusterNode>> idealAssign0,
+        Map<Integer, List<Long>> assignDiff0) {
+        if (assign0 != null)
+            assigns = createAssigns(assign0);
+
+        if (idealAssign0 != null)
+            idealAssigns = createAssigns(idealAssign0);
+
+        if (assignDiff0 != null) {
+            assignsDiff = U.newHashMap(assignDiff0.size());
+
+            for (Map.Entry<Integer, List<Long>> e : assignDiff0.entrySet()) {
+                List<Long> orders = e.getValue();
+
+                GridLongList l = new GridLongList(orders.size());
+
+                for (int n = 0; n < orders.size(); n++)
+                    l.add(orders.get(n));
+
+                assignsDiff.put(e.getKey(), l);
+            }
+        }
+    }
+
+    private List<GridLongList> createAssigns(List<List<ClusterNode>> assign0) {
+        if (assign0 != null) {
+            List<GridLongList> assigns = new ArrayList<>(assign0.size());
+
+            for (int i = 0; i < assign0.size(); i++) {
+                List<ClusterNode> nodes = assign0.get(i);
+
+                GridLongList l = new GridLongList(nodes.size());
+
+                for (int n = 0; n < nodes.size(); n++)
+                    l.add(nodes.get(n).order());
+
+                assigns.add(l);
+            }
+
+            return assigns;
+        }
+
+        return null;
+    }
+
+    /**
+     * @param affDiff Affinity diff.
+     * @return Affinity diff messages.
+     */
+    public static Map<Integer, CacheGroupAffinityMessage> createAffinityDiffMessages(
+        Map<Integer, Map<Integer, List<Long>>> affDiff) {
+        if (F.isEmpty(affDiff))
+            return null;
+
+        Map<Integer, CacheGroupAffinityMessage> map = U.newHashMap(affDiff.size());
+
+        for (Map.Entry<Integer, Map<Integer, List<Long>>> e : affDiff.entrySet())
+            map.put(e.getKey(), new CacheGroupAffinityMessage(null, null, e.getValue()));
+
+        return map;
+    }
+
+    /**
+     * @param cctx Context.
+     * @param topVer Topology version.
+     * @param affReq Cache group IDs.
+     * @param cachesAff Optional already prepared affinity.
+     * @return Affinity.
+     */
+    static Map<Integer, CacheGroupAffinityMessage> createAffinityMessages(
+        GridCacheSharedContext cctx,
+        AffinityTopologyVersion topVer,
+        Collection<Integer> affReq,
+        @Nullable Map<Integer, CacheGroupAffinityMessage> cachesAff) {
+        assert !F.isEmpty(affReq) : affReq;
+
+        if (cachesAff == null)
+            cachesAff = U.newHashMap(affReq.size());
+
+        for (Integer grpId : affReq) {
+            if (!cachesAff.containsKey(grpId)) {
+                GridAffinityAssignmentCache aff = cctx.affinity().affinity(grpId);
+
+                List<List<ClusterNode>> assign = aff.readyAssignments(topVer);
+
+                CacheGroupAffinityMessage msg = new CacheGroupAffinityMessage(assign,
+                    aff.centralizedAffinityFunction() ? aff.idealAssignment() : null,
+                    null);
+
+                cachesAff.put(grpId, msg);
+            }
+        }
+
+        return cachesAff;
+    }
+
+    /**
+     * @param assign Nodes orders.
+     * @param nodesByOrder Nodes by order cache.
+     * @param discoCache Discovery data cache.
+     * @return Nodes list.
+     */
+    public static List<ClusterNode> toNodes(GridLongList assign, Map<Long, ClusterNode> nodesByOrder, DiscoCache discoCache) {
+        List<ClusterNode> assign0 = new ArrayList<>(assign.size());
+
+        for (int n = 0; n < assign.size(); n++) {
+            long order = assign.get(n);
+
+            ClusterNode affNode = nodesByOrder.get(order);
+
+            if (affNode == null) {
+                affNode = discoCache.serverNodeByOrder(order);
+
+                assert affNode != null : "Failed to find node by order [order=" + order +
+                    ", topVer=" + discoCache.version() + ']';
+
+                nodesByOrder.put(order, affNode);
+            }
+
+            assign0.add(affNode);
+        }
+
+        return assign0;
+    }
+
+    /**
+     * @param nodesByOrder Nodes by order cache.
+     * @param discoCache Discovery data cache.
+     * @return Nodes list.
+     */
+    @Nullable public List<List<ClusterNode>> createIdealAssignments(Map<Long, ClusterNode> nodesByOrder,
+        DiscoCache discoCache) {
+        if (idealAssigns == null)
+            return null;
+
+        return createAssignments(idealAssigns, nodesByOrder, discoCache);
+    }
+
+    /**
+     * @param nodesByOrder Nodes by order cache.
+     * @param discoCache Discovery data cache.
+     * @return Assignments.
+     */
+    public List<List<ClusterNode>> createAssignments(Map<Long, ClusterNode> nodesByOrder, DiscoCache discoCache) {
+        return createAssignments(assigns, nodesByOrder, discoCache);
+    }
+
+    /**
+     * @param assigns Nodes orders.
+     * @param nodesByOrder Nodes by order cache.
+     * @param discoCache Discovery data cache.
+     * @return Nodes list.
+     */
+    private List<List<ClusterNode>> createAssignments(List<GridLongList> assigns,
+        Map<Long, ClusterNode> nodesByOrder,
+        DiscoCache discoCache) {
+        List<List<ClusterNode>> assignments0 = new ArrayList<>(assigns.size());
+
+        for (int p = 0; p < assigns.size(); p++) {
+            GridLongList assign = assigns.get(p);
+
+            assignments0.add(toNodes(assign, nodesByOrder, discoCache));
+        }
+
+        return assignments0;
+    }
+
+    /**
+     * @return Difference with ideal affinity assignment.
+     */
+    public Map<Integer, GridLongList> assignmentsDiff() {
+        return assignsDiff;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
+        writer.setBuffer(buf);
+
+        if (!writer.isHeaderWritten()) {
+            if (!writer.writeHeader(directType(), fieldsCount()))
+                return false;
+
+            writer.onHeaderWritten();
+        }
+
+        switch (writer.state()) {
+            case 0:
+                if (!writer.writeCollection("assigns", assigns, MessageCollectionItemType.MSG))
+                    return false;
+
+                writer.incrementState();
+
+            case 1:
+                if (!writer.writeMap("assignsDiff", assignsDiff, MessageCollectionItemType.INT, MessageCollectionItemType.MSG))
+                    return false;
+
+                writer.incrementState();
+
+            case 2:
+                if (!writer.writeCollection("idealAssigns", idealAssigns, MessageCollectionItemType.MSG))
+                    return false;
+
+                writer.incrementState();
+
+        }
+
+        return true;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
+        reader.setBuffer(buf);
+
+        if (!reader.beforeMessageRead())
+            return false;
+
+        switch (reader.state()) {
+            case 0:
+                assigns = reader.readCollection("assigns", MessageCollectionItemType.MSG);
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 1:
+                assignsDiff = reader.readMap("assignsDiff", MessageCollectionItemType.INT, MessageCollectionItemType.MSG, false);
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 2:
+                idealAssigns = reader.readCollection("idealAssigns", MessageCollectionItemType.MSG);
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+        }
+
+        return reader.afterMessageRead(CacheGroupAffinityMessage.class);
+    }
+
+    /** {@inheritDoc} */
+    @Override public short directType() {
+        return 128;
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte fieldsCount() {
+        return 3;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onAckReceived() {
+        // No-op.
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(CacheGroupAffinityMessage.class, this);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/bebf2997/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/ForceRebalanceExchangeTask.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/ForceRebalanceExchangeTask.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/ForceRebalanceExchangeTask.java
index c820175..dfa0e1e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/ForceRebalanceExchangeTask.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/ForceRebalanceExchangeTask.java
@@ -42,6 +42,11 @@ public class ForceRebalanceExchangeTask implements CachePartitionExchangeWorkerT
         this.forcedRebFut = forcedRebFut;
     }
 
+    /** {@inheritDoc} */
+    @Override public boolean skipForExchangeMerge() {
+        return true;
+    }
+
     /**
      * @return Exchange ID.
      */

http://git-wip-us.apache.org/repos/asf/ignite/blob/bebf2997/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionExchangeId.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionExchangeId.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionExchangeId.java
index 1a4dabf..33728d3 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionExchangeId.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionExchangeId.java
@@ -64,6 +64,17 @@ public class GridDhtPartitionExchangeId implements Message, Comparable<GridDhtPa
 
     /**
      * @param nodeId Node ID.
+     * @param evt Event type.
+     * @param topVer Topology version.
+     */
+    public GridDhtPartitionExchangeId(UUID nodeId, int evt, AffinityTopologyVersion topVer) {
+        this.nodeId = nodeId;
+        this.evt = evt;
+        this.topVer = topVer;
+    }
+
+    /**
+     * @param nodeId Node ID.
      * @param discoEvt Event.
      * @param topVer Topology version.
      */

http://git-wip-us.apache.org/repos/asf/ignite/blob/bebf2997/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionMap.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionMap.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionMap.java
index 735ca1e..410caf6 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionMap.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionMap.java
@@ -254,7 +254,7 @@ public class GridDhtPartitionMap implements Comparable<GridDhtPartitionMap>, Ext
             i++;
         }
 
-        assert i == size;
+        assert i == size : "Invalid size [size1=" + size + ", size2=" + i + ']';
 
         if (top != null) {
             out.writeLong(topologyVersion().topologyVersion());

http://git-wip-us.apache.org/repos/asf/ignite/blob/bebf2997/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionSupplier.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionSupplier.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionSupplier.java
index 3ead982..e25ace7 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionSupplier.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionSupplier.java
@@ -134,7 +134,7 @@ class GridDhtPartitionSupplier {
      * @param topVer Topology version.
      */
     @SuppressWarnings("ConstantConditions")
-    public void onTopologyChanged(AffinityTopologyVersion topVer) {
+    void onTopologyChanged(AffinityTopologyVersion topVer) {
         synchronized (scMap) {
             Iterator<T3<UUID, Integer, AffinityTopologyVersion>> it = scMap.keySet().iterator();
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/bebf2997/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionsAbstractMessage.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionsAbstractMessage.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionsAbstractMessage.java
index 441952d..95c1a4f 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionsAbstractMessage.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionsAbstractMessage.java
@@ -34,7 +34,10 @@ import org.jetbrains.annotations.Nullable;
  */
 public abstract class GridDhtPartitionsAbstractMessage extends GridCacheMessage {
     /** */
-    protected static final byte COMPRESSED_FLAG_MASK = 1;
+    private static final byte COMPRESSED_FLAG_MASK = 0x01;
+
+    /** */
+    private static final byte RESTORE_STATE_FLAG_MASK = 0x02;
 
     /** */
     private static final long serialVersionUID = 0L;
@@ -46,7 +49,7 @@ public abstract class GridDhtPartitionsAbstractMessage extends GridCacheMessage
     private GridCacheVersion lastVer;
 
     /** */
-    private byte flags;
+    protected byte flags;
 
     /**
      * Required by {@link Externalizable}.
@@ -64,6 +67,15 @@ public abstract class GridDhtPartitionsAbstractMessage extends GridCacheMessage
         this.lastVer = lastVer;
     }
 
+    /**
+     * @param msg Message.
+     */
+    void copyStateTo(GridDhtPartitionsAbstractMessage msg) {
+        msg.exchId = exchId;
+        msg.lastVer = lastVer;
+        msg.flags = flags;
+    }
+
     /** {@inheritDoc} */
     @Override public boolean cacheGroupMessage() {
         return false;
@@ -92,6 +104,13 @@ public abstract class GridDhtPartitionsAbstractMessage extends GridCacheMessage
     }
 
     /**
+     * @param exchId Exchange ID.
+     */
+    public void exchangeId(GridDhtPartitionExchangeId exchId) {
+        this.exchId = exchId;
+    }
+
+    /**
      * @param grpId Cache group ID.
      * @return Parition update counters.
      */
@@ -118,6 +137,20 @@ public abstract class GridDhtPartitionsAbstractMessage extends GridCacheMessage
         flags = compressed ? (byte)(flags | COMPRESSED_FLAG_MASK) : (byte)(flags & ~COMPRESSED_FLAG_MASK);
     }
 
+    /**
+     * @param restoreState Restore exchange state flag.
+     */
+    void restoreState(boolean restoreState) {
+        flags = restoreState ? (byte)(flags | RESTORE_STATE_FLAG_MASK) : (byte)(flags & ~RESTORE_STATE_FLAG_MASK);
+    }
+
+    /**
+     * @return Restore exchange state flag.
+     */
+    public boolean restoreState() {
+        return (flags & RESTORE_STATE_FLAG_MASK) != 0;
+    }
+
     /** {@inheritDoc} */
     @Override public byte fieldsCount() {
         return 5;