You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by il...@apache.org on 2020/04/07 16:55:48 UTC

[ignite] branch ignite-2.8.1 updated: IGNITE-12549 Fix query mapping on REPLICATED cache during rebalance (#7277)

This is an automated email from the ASF dual-hosted git repository.

ilyak pushed a commit to branch ignite-2.8.1
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/ignite-2.8.1 by this push:
     new bd67b31  IGNITE-12549 Fix query mapping on REPLICATED cache during rebalance (#7277)
bd67b31 is described below

commit bd67b3181988aacd4e59d941aa82d5598a0e0d00
Author: Sergey Kosarev <ma...@gmail.com>
AuthorDate: Fri Mar 6 11:45:08 2020 +0300

    IGNITE-12549 Fix query mapping on REPLICATED cache during rebalance (#7277)
---
 .../processors/cache/IgniteCacheProxyImpl.java     |   5 +-
 .../cache/query/GridCacheQueryAdapter.java         |  27 +-
 .../query/ScanQueriesTopologyMappingTest.java      | 275 +++++++++++++++++++++
 .../ignite/testsuites/IgniteCacheTestSuite4.java   |   2 +
 4 files changed, 302 insertions(+), 7 deletions(-)

diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxyImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxyImpl.java
index b75b08e..79bb80c 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxyImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxyImpl.java
@@ -624,12 +624,9 @@ public class IgniteCacheProxyImpl<K, V> extends AsyncSupportAdapter<IgniteCache<
     private ClusterGroup projection(boolean loc) {
         GridCacheContext<K, V> ctx = getContextSafe();
 
-        if (loc || ctx.isLocal() || ctx.isReplicatedAffinityNode())
+        if (loc || ctx.isLocal())
             return ctx.kernalContext().grid().cluster().forLocal();
 
-        if (ctx.isReplicated())
-            return ctx.kernalContext().grid().cluster().forDataNodes(cacheName).forRandom();
-
         return null;
     }
 
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheQueryAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheQueryAdapter.java
index bb3afc9..ee43a60 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheQueryAdapter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheQueryAdapter.java
@@ -24,6 +24,7 @@ import java.util.Deque;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Queue;
@@ -45,6 +46,7 @@ import org.apache.ignite.internal.cluster.ClusterTopologyCheckedException;
 import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
 import org.apache.ignite.internal.processors.cache.GridCacheContext;
 import org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtUnreservedPartitionException;
+import org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology;
 import org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal;
 import org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker;
 import org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot;
@@ -654,12 +656,31 @@ public class GridCacheQueryAdapter<T> implements CacheQuery<T> {
                 if (prj != null || part != null)
                     return nodes(cctx, prj, part);
 
-                if (cctx.affinityNode())
+                GridDhtPartitionTopology topology = cctx.topology();
+
+                if (cctx.affinityNode() && !topology.localPartitionMap().hasMovingPartitions())
                     return Collections.singletonList(cctx.localNode());
 
-                Collection<ClusterNode> affNodes = nodes(cctx, null, null);
+                topology.readLock();
+
+                try {
+
+                    Collection<ClusterNode> affNodes = nodes(cctx, null, null);
+
+                    List<ClusterNode> nodes = new ArrayList<>(affNodes);
 
-                return affNodes.isEmpty() ? affNodes : Collections.singletonList(F.rand(affNodes));
+                    Collections.shuffle(nodes);
+
+                    for (ClusterNode node : nodes) {
+                        if (!topology.partitions(node.id()).hasMovingPartitions())
+                            return Collections.singletonList(node);
+                    }
+
+                    return affNodes;
+                }
+                finally {
+                    topology.readUnlock();
+                }
 
             case PARTITIONED:
                 return nodes(cctx, prj, part);
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/query/ScanQueriesTopologyMappingTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/query/ScanQueriesTopologyMappingTest.java
new file mode 100644
index 0000000..ef65191
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/query/ScanQueriesTopologyMappingTest.java
@@ -0,0 +1,275 @@
+/*
+ * 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.query;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cache.query.ScanQuery;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.TestRecordingCommunicationSpi;
+import org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.lang.IgniteBiPredicate;
+import org.apache.ignite.plugin.extensions.communication.Message;
+import org.apache.ignite.testframework.junits.GridAbstractTest;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static org.apache.ignite.internal.IgniteNodeAttributes.ATTR_IGNITE_INSTANCE_NAME;
+
+/** */
+public class ScanQueriesTopologyMappingTest extends GridCommonAbstractTest {
+    /** */
+    private boolean client;
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopAllGrids();
+
+        super.afterTest();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        return super.getConfiguration(igniteInstanceName)
+            .setCommunicationSpi(new TestRecordingCommunicationSpi())
+            .setClientMode(client);
+    }
+
+    /** */
+    @Test
+    public void testPartitionedQueryWithRebalance() throws Exception {
+        checkQueryWithRebalance(CacheMode.PARTITIONED);
+    }
+
+    /** */
+    @Test
+    public void testReplicatedQueryWithRebalance() throws Exception {
+        checkQueryWithRebalance(CacheMode.REPLICATED);
+    }
+
+    /** */
+    @Test
+    public void testPartitionedQueryWithNodeFilter() throws Exception {
+        checkQueryWithNodeFilter(CacheMode.PARTITIONED);
+    }
+
+    /** */
+    @Test
+    public void testReplicatedQueryWithNodeFilter() throws Exception {
+        checkQueryWithNodeFilter(CacheMode.REPLICATED);
+    }
+
+    /** */
+    @Test
+    public void testLocalCacheQueryMapping() throws Exception {
+        IgniteEx ign0 = startGrid(0);
+
+        IgniteCache<Object, Object> cache = ign0.createCache(new CacheConfiguration<>(GridAbstractTest.DEFAULT_CACHE_NAME)
+            .setCacheMode(CacheMode.LOCAL));
+
+        cache.put(1, 2);
+
+        startGrid(1);
+
+        ScanQuery<Object, Object> qry = new ScanQuery<>();
+
+        {
+            List<Cache.Entry<Object, Object>> res0 = grid(0).cache(GridAbstractTest.DEFAULT_CACHE_NAME).query(qry).getAll();
+
+            assertEquals(1, res0.size());
+            assertEquals(1, res0.get(0).getKey());
+            assertEquals(2, res0.get(0).getValue());
+        }
+
+        {
+            List<Cache.Entry<Object, Object>> res1 = grid(1).cache(GridAbstractTest.DEFAULT_CACHE_NAME).query(qry).getAll();
+
+            assertTrue(res1.isEmpty());
+        }
+    }
+
+    /** */
+    private void checkQueryWithRebalance(CacheMode cacheMode) throws Exception {
+        IgniteEx ign0 = startGrid(0);
+
+        IgniteCache<Object, Object> cache0 = ign0.createCache(new CacheConfiguration<>(GridAbstractTest.DEFAULT_CACHE_NAME)
+                .setCacheMode(cacheMode));
+
+        cache0.put(1, 2);
+
+        blockRebalanceSupplyMessages(ign0, DEFAULT_CACHE_NAME, getTestIgniteInstanceName(1));
+
+        startGrid(1);
+
+        client = true;
+
+        startGrid(10);
+
+        int part = ign0.affinity(DEFAULT_CACHE_NAME).partition(1);
+
+        for (int i = 0; i < 100; i++) {
+            for (Ignite ign : G.allGrids()) {
+                IgniteCache<Object, Object> cache = ign.cache(DEFAULT_CACHE_NAME);
+
+                //check scan query
+                List<Cache.Entry<Object, Object>> res = cache.query(new ScanQuery<>()).getAll();
+
+                assertEquals(1, res.size());
+                assertEquals(1, res.get(0).getKey());
+                assertEquals(2, res.get(0).getValue());
+
+                res = new ArrayList<>();
+
+                //check iterator
+                for (Cache.Entry<Object, Object> entry : cache) {
+                    res.add(entry);
+                }
+
+                assertEquals(1, res.size());
+                assertEquals(1, res.get(0).getKey());
+                assertEquals(2, res.get(0).getValue());
+
+                // check scan query by partition
+                res = cache.query(new ScanQuery<>(part)).getAll();
+
+                assertEquals(1, res.size());
+                assertEquals(1, res.get(0).getKey());
+                assertEquals(2, res.get(0).getValue());
+            }
+
+            ScanQuery<Object, Object> qry = new ScanQuery<>().setLocal(true);
+
+            {
+                List<Cache.Entry<Object, Object>> res0 = grid(0).cache(DEFAULT_CACHE_NAME).query(qry).getAll();
+
+                assertEquals(1, res0.size());
+                assertEquals(1, res0.get(0).getKey());
+                assertEquals(2, res0.get(0).getValue());
+            }
+
+            {
+                List<Cache.Entry<Object, Object>> res1 = grid(1).cache(DEFAULT_CACHE_NAME).query(qry).getAll();
+
+                assertTrue(res1.isEmpty());
+            }
+
+            {
+                List<Cache.Entry<Object, Object>> res1 = grid(10).cache(DEFAULT_CACHE_NAME).query(qry).getAll();
+
+                assertTrue(res1.isEmpty());
+            }
+        }
+    }
+
+    /** */
+    private void checkQueryWithNodeFilter(CacheMode cacheMode) throws Exception {
+        IgniteEx ign0 = startGrid(0);
+        String name0 = ign0.name();
+
+        IgniteCache<Object, Object> cache0 = ign0.createCache(new CacheConfiguration<>(GridAbstractTest.DEFAULT_CACHE_NAME)
+                .setCacheMode(cacheMode)
+                .setNodeFilter(node -> name0.equals(node.attribute(ATTR_IGNITE_INSTANCE_NAME))));
+
+        cache0.put(1, 2);
+
+        startGrid(1);
+
+        client = true;
+
+        startGrid(10);
+
+        int part = ign0.affinity(DEFAULT_CACHE_NAME).partition(1);
+
+        for (int i = 0; i < 100; i++) {
+            for (Ignite ign : G.allGrids()) {
+                IgniteCache<Object, Object> cache = ign.cache(GridAbstractTest.DEFAULT_CACHE_NAME);
+
+                List<Cache.Entry<Object, Object>> res = cache.query(new ScanQuery<>()).getAll();
+
+                assertEquals(1, res.size());
+                assertEquals(1, res.get(0).getKey());
+                assertEquals(2, res.get(0).getValue());
+
+                res = new ArrayList<>();
+
+                for (Cache.Entry<Object, Object> entry : cache) {
+                    res.add(entry);
+                }
+
+                assertEquals(1, res.size());
+                assertEquals(1, res.get(0).getKey());
+                assertEquals(2, res.get(0).getValue());
+
+                res = cache.query(new ScanQuery<>(part)).getAll();
+
+                assertEquals(1, res.size());
+                assertEquals(1, res.get(0).getKey());
+                assertEquals(2, res.get(0).getValue());
+            }
+
+            ScanQuery<Object, Object> qry = new ScanQuery<>().setLocal(true);
+
+            {
+                List<Cache.Entry<Object, Object>> res0 = grid(0).cache(GridAbstractTest.DEFAULT_CACHE_NAME).query(qry).getAll();
+
+                assertEquals(1, res0.size());
+                assertEquals(1, res0.get(0).getKey());
+                assertEquals(2, res0.get(0).getValue());
+            }
+
+            {
+                List<Cache.Entry<Object, Object>> res1 = grid(1).cache(GridAbstractTest.DEFAULT_CACHE_NAME).query(qry).getAll();
+
+                assertTrue(res1.isEmpty());
+            }
+
+            {
+                List<Cache.Entry<Object, Object>> res1 = grid(10).cache(GridAbstractTest.DEFAULT_CACHE_NAME).query(qry).getAll();
+
+                assertTrue(res1.isEmpty());
+            }
+        }
+    }
+
+    /** */
+    private void blockRebalanceSupplyMessages(IgniteEx sndNode, String cacheName, String dstNodeName) {
+        int grpId = sndNode.cachex(cacheName).context().groupId();
+
+        TestRecordingCommunicationSpi comm0 = (TestRecordingCommunicationSpi)sndNode.configuration().getCommunicationSpi();
+        comm0.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {
+            @Override public boolean apply(ClusterNode node, Message msg) {
+                String dstName = node.attribute(ATTR_IGNITE_INSTANCE_NAME);
+
+                if (dstNodeName.equals(dstName) && msg instanceof GridDhtPartitionSupplyMessage) {
+                    GridDhtPartitionSupplyMessage msg0 = (GridDhtPartitionSupplyMessage)msg;
+                    return msg0.groupId() == grpId;
+                }
+
+                return false;
+            }
+        });
+    }
+}
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite4.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite4.java
index 6a81abc..fec1b9b 100644
--- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite4.java
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite4.java
@@ -159,6 +159,7 @@ import org.apache.ignite.internal.processors.cache.version.CacheVersionedEntryPa
 import org.apache.ignite.internal.processors.cache.version.CacheVersionedEntryPartitionedTransactionalSelfTest;
 import org.apache.ignite.internal.processors.cache.version.CacheVersionedEntryReplicatedAtomicSelfTest;
 import org.apache.ignite.internal.processors.cache.version.CacheVersionedEntryReplicatedTransactionalSelfTest;
+import org.apache.ignite.internal.processors.query.ScanQueriesTopologyMappingTest;
 import org.apache.ignite.testframework.GridTestUtils;
 import org.apache.ignite.testframework.junits.DynamicSuite;
 import org.junit.runner.RunWith;
@@ -264,6 +265,7 @@ public class IgniteCacheTestSuite4 {
         GridTestUtils.addTestIfNeeded(suite, IgniteDiscoDataHandlingInNewClusterTest.class, ignoredTests);
         GridTestUtils.addTestIfNeeded(suite, CacheDiscoveryDataConcurrentJoinTest.class, ignoredTests);
         GridTestUtils.addTestIfNeeded(suite, IgniteClientCacheInitializationFailTest.class, ignoredTests);
+        GridTestUtils.addTestIfNeeded(suite, ScanQueriesTopologyMappingTest.class, ignoredTests);
         GridTestUtils.addTestIfNeeded(suite, IgniteCacheFailedUpdateResponseTest.class, ignoredTests);
 
         GridTestUtils.addTestIfNeeded(suite, GridCacheTxLoadFromStoreOnLockSelfTest.class, ignoredTests);