You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by al...@apache.org on 2021/10/15 07:10:31 UTC

[ignite] branch master updated: IGNITE-15659 Fix REST request failure when cache node filter is used - Fixes #9459.

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

alexpl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 368962a  IGNITE-15659 Fix REST request failure when cache node filter is used - Fixes #9459.
368962a is described below

commit 368962a467e51bef8d6fe76dc616e6e4865050fb
Author: Aleksey Plekhanov <pl...@gmail.com>
AuthorDate: Fri Oct 15 10:06:17 2021 +0300

    IGNITE-15659 Fix REST request failure when cache node filter is used - Fixes #9459.
    
    Signed-off-by: Aleksey Plekhanov <pl...@gmail.com>
---
 .../client/suite/IgniteClientTestSuite.java        |   2 +
 .../JettyRestProcessorCacheNodeFilterTest.java     | 135 +++++++++++++++++++++
 .../handlers/cache/GridCacheCommandHandler.java    |  24 ++--
 3 files changed, 146 insertions(+), 15 deletions(-)

diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/client/suite/IgniteClientTestSuite.java b/modules/clients/src/test/java/org/apache/ignite/internal/client/suite/IgniteClientTestSuite.java
index 9014955..c7ec89b 100644
--- a/modules/clients/src/test/java/org/apache/ignite/internal/client/suite/IgniteClientTestSuite.java
+++ b/modules/clients/src/test/java/org/apache/ignite/internal/client/suite/IgniteClientTestSuite.java
@@ -64,6 +64,7 @@ import org.apache.ignite.internal.processors.rest.JettyRestProcessorAuthenticati
 import org.apache.ignite.internal.processors.rest.JettyRestProcessorAuthenticatorUserManagementAuthorizationTest;
 import org.apache.ignite.internal.processors.rest.JettyRestProcessorBaselineSelfTest;
 import org.apache.ignite.internal.processors.rest.JettyRestProcessorBeforeNodeStartSelfTest;
+import org.apache.ignite.internal.processors.rest.JettyRestProcessorCacheNodeFilterTest;
 import org.apache.ignite.internal.processors.rest.JettyRestProcessorGetAllAsArrayTest;
 import org.apache.ignite.internal.processors.rest.JettyRestProcessorSignedSelfTest;
 import org.apache.ignite.internal.processors.rest.JettyRestProcessorUnsignedSelfTest;
@@ -108,6 +109,7 @@ import org.junit.runners.Suite;
     JettyRestProcessorGetAllAsArrayTest.class,
     JettyRestProcessorBaselineSelfTest.class,
     JettyRestProcessorBeforeNodeStartSelfTest.class,
+    JettyRestProcessorCacheNodeFilterTest.class,
 
     // Test TCP rest processor with original memcache client.
     ClientMemcachedProtocolSelfTest.class,
diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorCacheNodeFilterTest.java b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorCacheNodeFilterTest.java
new file mode 100644
index 0000000..407097d
--- /dev/null
+++ b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorCacheNodeFilterTest.java
@@ -0,0 +1,135 @@
+/*
+ * 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.rest;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.CachePeekMode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.lang.IgnitePredicate;
+import org.junit.Test;
+
+import static org.apache.ignite.internal.processors.rest.GridRestResponse.STATUS_SUCCESS;
+
+/**
+ * Test REST with cache node filter.
+ */
+public class JettyRestProcessorCacheNodeFilterTest extends JettyRestProcessorCommonSelfTest {
+    /** {@inheritDoc} */
+    @Override protected String signature() {
+        return null;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        return super.getConfiguration(igniteInstanceName).setConsistentId(igniteInstanceName).setCacheConfiguration();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected int gridCount() {
+        return 3;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        grid(0).destroyCache(DEFAULT_CACHE_NAME);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testPut() throws Exception {
+        IgniteCache<String, String> cache = createCacheWithNodeFilter();
+
+        // Send request to cache to node 0.
+        checkPut(cache, "k0", "v0");
+
+        // Send request to cache to node 0 with redirection to node 2.
+        checkPut(cache, "k0", "v0", "destId", grid(2).cluster().localNode().id().toString());
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testSize() throws Exception {
+        IgniteCache<String, String> cache = createCacheWithNodeFilter();
+
+        cache.put("k0", "v0");
+
+        // Send request to cache to node 0.
+        checkSize(cache);
+
+        // Send request to cache to node 0 with redirection to node 2.
+        checkSize(cache, "destId", grid(2).cluster().localNode().id().toString());
+    }
+
+    /** */
+    private IgniteCache<String, String> createCacheWithNodeFilter() throws InterruptedException {
+        // Start cache only on node 1.
+        IgnitePredicate<ClusterNode> nodeFilter = n -> n.consistentId().toString().endsWith("1");
+
+        IgniteCache<String, String> cache = grid(1).createCache(
+            new CacheConfiguration<String, String>(DEFAULT_CACHE_NAME).setNodeFilter(nodeFilter));
+
+        awaitPartitionMapExchange();
+
+        return cache;
+    }
+
+    /**
+     * @param cache Cache.
+     * @param key Key.
+     * @param val Value.
+     * @param pars Additional parameters.
+     */
+    private void checkPut(IgniteCache<String, String> cache, String key, String val, String... pars) throws Exception {
+        String ret = content(cache.getName(), GridRestCommand.CACHE_PUT, F.concat(pars, "key", key, "val", val));
+
+        info("Put command result: " + ret);
+
+        assertEquals(STATUS_SUCCESS, responseField(ret, "successStatus"));
+
+        assertEquals(val, cache.get(key));
+    }
+
+    /**
+     * @param cache Cache.
+     * @param pars Additional parameters.
+     */
+    private void checkSize(IgniteCache<String, String> cache, String... pars) throws Exception {
+        String ret = content(cache.getName(), GridRestCommand.CACHE_SIZE, pars);
+
+        info("Size command result: " + ret);
+
+        assertEquals(STATUS_SUCCESS, responseField(ret, "successStatus"));
+
+        assertEquals(cache.size(CachePeekMode.PRIMARY), responseField(ret, "response"));
+    }
+
+    /** */
+    private int responseField(String res, String field) throws JsonProcessingException {
+        return JSON_MAPPER.readTree(res).get(field).asInt();
+    }
+}
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
index cab9e4d0..4a30e76 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cache/GridCacheCommandHandler.java
@@ -343,15 +343,16 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter {
      * @return Instance on the named cache.
      * @throws IgniteCheckedException If cache not found.
      */
-    private static IgniteInternalCache<Object, Object> cache(Ignite ignite,
-        String cacheName) throws IgniteCheckedException {
-        IgniteInternalCache<Object, Object> cache = ((IgniteKernal)ignite).getCache(cacheName);
+    private static IgniteInternalCache<Object, Object> cache(
+        Ignite ignite,
+        String cacheName
+    ) throws IgniteCheckedException {
+        IgniteCacheProxy<Object, Object> cache = (IgniteCacheProxy<Object, Object>)ignite.cache(cacheName);
 
         if (cache == null)
-            throw new IgniteCheckedException(
-                "Failed to find cache for given cache name: " + cacheName);
+            throw new IgniteCheckedException("Failed to find cache for given cache name: " + cacheName);
 
-        return cache;
+        return cache.internalProxy();
     }
 
     /** {@inheritDoc} */
@@ -785,8 +786,7 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter {
         final String cacheName,
         final Object key,
         final CacheCommand op) throws IgniteCheckedException {
-        final boolean locExec = destId == null || destId.equals(ctx.localNodeId()) ||
-            ctx.cache().cache(cacheName) != null;
+        final boolean locExec = destId == null || destId.equals(ctx.localNodeId());
 
         if (locExec) {
             final IgniteInternalCache<Object, Object> cache = localCache(cacheName);
@@ -825,13 +825,7 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter {
      * @throws IgniteCheckedException If cache not found.
      */
     protected IgniteInternalCache<Object, Object> localCache(String cacheName) throws IgniteCheckedException {
-        IgniteInternalCache<Object, Object> cache = ctx.cache().cache(cacheName);
-
-        if (cache == null)
-            throw new IgniteCheckedException(
-                "Failed to find cache for given cache name: " + cacheName);
-
-        return cache;
+        return cache(ctx.grid(), cacheName);
     }
 
     /**