IGNITE-6053: IgniteCache.clear clears local caches with same names on all server nodes. - Fixes #2443.
Signed-off-by: shroman <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/6aa94a9a Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/6aa94a9a Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/6aa94a9a Branch: refs/heads/ignite-3478 Commit: 6aa94a9ae1eb52546385e5ba0027c09172873175 Parents: 7ecdd7d Author: shroman <[email protected]> Authored: Mon Sep 25 12:59:51 2017 +0900 Committer: shroman <[email protected]> Committed: Mon Sep 25 12:59:51 2017 +0900 ---------------------------------------------------------------------- .../processors/cache/GridCacheAdapter.java | 62 ++++++++++++---- .../local/GridCacheLocalFullApiSelfTest.java | 76 +++++++++++++++++++- 2 files changed, 123 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/6aa94a9a/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 32b1b99..c7e40ec 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 @@ -383,6 +383,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * Increments map public size. + * * @param e Map entry. */ public void incrementSize(GridCacheMapEntry e) { @@ -391,6 +392,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * Decrements map public size. + * * @param e Map entry. */ public void decrementSize(GridCacheMapEntry e) { @@ -1114,8 +1116,16 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V * @throws IgniteCheckedException In case of error. */ private void clear(@Nullable Set<? extends K> keys) throws IgniteCheckedException { - executeClearTask(keys, false).get(); - executeClearTask(keys, true).get(); + if (isLocal()) { + if (keys == null) + clearLocally(true, false, false); + else + clearLocallyAll(keys, true, false, false); + } + else { + executeClearTask(keys, false).get(); + executeClearTask(keys, true).get(); + } } /** @@ -1123,13 +1133,33 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V * @return Future. */ private IgniteInternalFuture<?> clearAsync(@Nullable final Set<? extends K> keys) { - return executeClearTask(keys, false).chain(new CX1<IgniteInternalFuture<?>, Object>() { - @Override public Object applyx(IgniteInternalFuture<?> fut) throws IgniteCheckedException { - executeClearTask(keys, true).get(); + if (isLocal()) + return clearLocallyAsync(keys); + else + return executeClearTask(keys, false).chain(new CX1<IgniteInternalFuture<?>, Object>() { + @Override public Object applyx(IgniteInternalFuture<?> fut) throws IgniteCheckedException { + executeClearTask(keys, true).get(); + + return null; + } + }); + } + + /** + * @param keys Keys to clear. + * @return Clear future. + */ + private IgniteInternalFuture<?> clearLocallyAsync(@Nullable final Set<? extends K> keys) { + return ctx.closures().callLocalSafe(new Callable<Object>() { + @Override public Object call() throws Exception { + if (keys == null) + clearLocally(true, false, false); + else + clearLocallyAll(keys, true, false, false); return null; } - }); + }, false); } /** @@ -3670,8 +3700,8 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * @param p Predicate. * @param args Arguments. - * @throws IgniteCheckedException If failed. * @return Load cache future. + * @throws IgniteCheckedException If failed. */ IgniteInternalFuture<?> globalLoadCacheAsync(@Nullable IgniteBiPredicate<K, V> p, @Nullable Object... args) throws IgniteCheckedException { @@ -3693,7 +3723,6 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V new LoadCacheJobV2<>(ctx.name(), ctx.affinity().affinityTopologyVersion(), p, args, plc, keepBinary)), nodes); - return fut; } @@ -5995,7 +6024,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * @param metrics Metrics. - * @param start Start time. + * @param start Start time. */ public UpdateTimeStatClosure(CacheMetricsImpl metrics, long start) { this.metrics = metrics; @@ -6031,7 +6060,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * @param metrics Metrics. - * @param start Start time. + * @param start Start time. */ public UpdateGetTimeStatClosure(CacheMetricsImpl metrics, long start) { super(metrics, start); @@ -6052,7 +6081,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * @param metrics Metrics. - * @param start Start time. + * @param start Start time. */ public UpdateRemoveTimeStatClosure(CacheMetricsImpl metrics, long start) { super(metrics, start); @@ -6073,7 +6102,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * @param metrics Metrics. - * @param start Start time. + * @param start Start time. */ public UpdatePutTimeStatClosure(CacheMetricsImpl metrics, long start) { super(metrics, start); @@ -6094,7 +6123,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * @param metrics Metrics. - * @param start Start time. + * @param start Start time. */ public UpdatePutAndGetTimeStatClosure(CacheMetricsImpl metrics, long start) { super(metrics, start); @@ -6493,6 +6522,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * Constructor. + * * @param internalIterator Internal iterator. * @param keepBinary Keep binary flag. */ @@ -6541,6 +6571,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * Constructor + * * @param internalSet Internal set. */ private KeySet(Set<GridCacheMapEntry> internalSet) { @@ -6585,6 +6616,7 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** * Constructor. + * * @param internalIterator Internal iterator. * @param keepBinary Keep binary. */ @@ -6632,7 +6664,9 @@ public abstract class GridCacheAdapter<K, V> implements IgniteInternalCache<K, V /** Keep binary flag. */ private final boolean keepBinary; - /** Constructor. + /** + * Constructor. + * * @param internalSet Internal set. * @param keepBinary Keep binary flag. */ http://git-wip-us.apache.org/repos/asf/ignite/blob/6aa94a9a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalFullApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalFullApiSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalFullApiSelfTest.java index 8cb9369..aaf69c9 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalFullApiSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/local/GridCacheLocalFullApiSelfTest.java @@ -17,7 +17,9 @@ package org.apache.ignite.internal.processors.cache.local; +import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; import java.util.Map; import org.apache.ignite.IgniteCache; import org.apache.ignite.cache.CacheMode; @@ -79,4 +81,76 @@ public class GridCacheLocalFullApiSelfTest extends GridCacheAbstractFullApiSelfT for (String key : keys) assert "key1".equals(key) || "key2".equals(key); } -} \ No newline at end of file + + /** + * @throws Exception If failed. + */ + public void testLocalClearAsync() throws Exception { + localCacheClear(true); + } + + /** + * @throws Exception If failed. + */ + public void testLocalClear() throws Exception { + localCacheClear(false); + } + + /** + * @param async If {@code true} uses async method. + * @throws Exception If failed. + */ + private void localCacheClear(boolean async) throws Exception { + // In addition to the existing tests, it confirms the data is cleared only on one node, + // not on all nodes that have local caches with same names. + try { + startGrid(1); + + IgniteCache<String, Integer> cache = jcache(); + + for (int i = 0; i < 5; i++) { + cache.put(String.valueOf(i), i); + jcache(1).put(String.valueOf(i), i); + } + + if (async) + cache.clearAsync("4").get(); + else + cache.clear("4"); + + assertNull(peek(cache, "4")); + assertNotNull(peek(jcache(1), "4")); + + if (async) + cache.clearAllAsync(new HashSet<>(Arrays.asList("2", "3"))).get(); + else + cache.clearAll(new HashSet<>(Arrays.asList("2", "3"))); + + for (int i = 2; i < 4; i++) { + assertNull(peek(cache, String.valueOf(i))); + assertNotNull(peek(jcache(1), String.valueOf(i))); + } + + if (async) + cache.clearAsync().get(); + else + cache.clear(); + + for (int i = 0; i < 2; i++) { + assertNull(peek(cache, String.valueOf(i))); + assertNotNull(peek(jcache(1), String.valueOf(i))); + } + + if (async) + jcache(1).clearAsync().get(); + else + jcache(1).clear(); + + for (int i = 0; i < 2; i++) + assert jcache(i).localSize() == 0; + } + finally { + stopGrid(1); + } + } +}
