Repository: ignite
Updated Branches:
  refs/heads/ignite-5075 e88450df1 -> 4841b5587


IGNITE-4842 Now containsKey() respects isReadFromBackup() flag.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/d84fd29d
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/d84fd29d
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/d84fd29d

Branch: refs/heads/ignite-5075
Commit: d84fd29d924ea1f81ce2bfdb7f8e42c7b85b4c05
Parents: 6be8d97
Author: dkarachentsev <dkarachent...@gridgain.com>
Authored: Thu May 18 19:11:08 2017 +0300
Committer: dkarachentsev <dkarachent...@gridgain.com>
Committed: Thu May 18 19:11:08 2017 +0300

----------------------------------------------------------------------
 .../processors/cache/GridCacheAdapter.java      |   4 +-
 .../cache/IgniteCacheContainsKeyAtomicTest.java | 103 +++++++++++++++++++
 .../testsuites/IgniteCacheTestSuite4.java       |   3 +
 3 files changed, 108 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/d84fd29d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
index 2820bb7..307c5cf 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAdapter.java
@@ -635,7 +635,7 @@ public abstract class GridCacheAdapter<K, V> implements 
IgniteInternalCache<K, V
 
         return (IgniteInternalFuture)getAsync(
             key,
-            /*force primary*/false,
+            /*force primary*/ !ctx.config().isReadFromBackup(),
             /*skip tx*/false,
             /*subj id*/null,
             /*task name*/null,
@@ -664,7 +664,7 @@ public abstract class GridCacheAdapter<K, V> implements 
IgniteInternalCache<K, V
 
         return getAllAsync(
             keys,
-            /*force primary*/false,
+            /*force primary*/ !ctx.config().isReadFromBackup(),
             /*skip tx*/false,
             /*subj id*/null,
             /*task name*/null,

http://git-wip-us.apache.org/repos/asf/ignite/blob/d84fd29d/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheContainsKeyAtomicTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheContainsKeyAtomicTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheContainsKeyAtomicTest.java
new file mode 100644
index 0000000..981d245
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheContainsKeyAtomicTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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;
+
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.testframework.GridTestUtils;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.ATOMIC;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC;
+
+/**
+ * Verifies that containsKey() works as expected on atomic cache.
+ */
+public class IgniteCacheContainsKeyAtomicTest extends 
GridCacheAbstractSelfTest {
+    /** Cache name. */
+    public static final String CACHE_NAME = "replicated";
+
+    /** {@inheritDoc} */
+    @Override protected int gridCount() {
+        return 4;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        IgniteCache cache = ignite(0).cache(CACHE_NAME);
+
+        if (cache != null)
+            cache.clear();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testContainsPutIfAbsent() throws Exception {
+        checkPutIfAbsent(false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testContainsPutIfAbsentAll() throws Exception {
+        checkPutIfAbsent(true);
+    }
+
+    /**
+     * @param all Check for set of keys.
+     * @throws Exception If failed.
+     */
+    private void checkPutIfAbsent(final boolean all) throws Exception {
+        Ignite srv = ignite(0);
+
+        final IgniteCache<Integer, Integer> cache1 = 
srv.getOrCreateCache(replicatedCache());
+        final IgniteCache<Integer, Integer> cache2 = 
ignite(1).getOrCreateCache(replicatedCache());
+
+        final AtomicInteger fails = new AtomicInteger(0);
+
+        GridTestUtils.runMultiThreaded(new Runnable() {
+            @Override public void run() {
+                for (int i = 0; i < 100; i++) {
+                    if (!cache1.putIfAbsent(i, i)) {
+                        if (all ? 
!cache2.containsKeys(Collections.singleton(i)) : !cache2.containsKey(i))
+                            fails.incrementAndGet();
+                    }
+                }
+            }
+        }, 100, "put-if-abs");
+
+        assertEquals(0, fails.get());
+    }
+
+    /**
+     * @return replicated cache configuration.
+     */
+    private CacheConfiguration<Integer, Integer> replicatedCache() {
+        return new CacheConfiguration<Integer, Integer>(CACHE_NAME)
+            .setAtomicityMode(ATOMIC)
+            .setWriteSynchronizationMode(FULL_SYNC)
+            .setReadFromBackup(false) // containsKey() must respect this flag
+            .setCacheMode(REPLICATED);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/d84fd29d/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite4.java
----------------------------------------------------------------------
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 e423098..6370a10 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
@@ -58,6 +58,7 @@ import 
org.apache.ignite.internal.processors.cache.IgniteCacheAtomicPeekModesTes
 import 
org.apache.ignite.internal.processors.cache.IgniteCacheAtomicReplicatedPeekModesTest;
 import 
org.apache.ignite.internal.processors.cache.IgniteCacheConfigurationDefaultTemplateTest;
 import 
org.apache.ignite.internal.processors.cache.IgniteCacheConfigurationTemplateTest;
+import 
org.apache.ignite.internal.processors.cache.IgniteCacheContainsKeyAtomicTest;
 import 
org.apache.ignite.internal.processors.cache.IgniteCacheDynamicStopSelfTest;
 import 
org.apache.ignite.internal.processors.cache.IgniteCacheGetCustomCollectionsSelfTest;
 import 
org.apache.ignite.internal.processors.cache.IgniteCacheInvokeReadThroughSingleNodeTest;
@@ -326,6 +327,8 @@ public class IgniteCacheTestSuite4 extends TestSuite {
 
         suite.addTestSuite(CacheAtomicPrimarySyncBackPressureTest.class);
 
+        suite.addTestSuite(IgniteCacheContainsKeyAtomicTest.class);
+
         return suite;
     }
 }
\ No newline at end of file

Reply via email to