# ignite-333 review
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/cf6677bb Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/cf6677bb Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/cf6677bb Branch: refs/heads/ignite-45 Commit: cf6677bb044e6969c67230d5b79b97876989b471 Parents: 62be9d1 Author: sboikov <sboi...@gridgain.com> Authored: Fri Mar 13 17:18:24 2015 +0300 Committer: sboikov <sboi...@gridgain.com> Committed: Fri Mar 13 17:35:20 2015 +0300 ---------------------------------------------------------------------- .../java/org/apache/ignite/IgniteCache.java | 33 +++++---- .../processors/cache/GridCacheAdapter.java | 46 ++++++------ .../cache/GridCacheAbstractFullApiSelfTest.java | 73 ++++++++++---------- 3 files changed, 77 insertions(+), 75 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cf6677bb/modules/core/src/main/java/org/apache/ignite/IgniteCache.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCache.java b/modules/core/src/main/java/org/apache/ignite/IgniteCache.java index 99a8934..042d811 100644 --- a/modules/core/src/main/java/org/apache/ignite/IgniteCache.java +++ b/modules/core/src/main/java/org/apache/ignite/IgniteCache.java @@ -18,10 +18,10 @@ package org.apache.ignite; import org.apache.ignite.cache.*; -import org.apache.ignite.cache.query.*; -import org.apache.ignite.cache.store.*; import org.apache.ignite.cache.affinity.*; import org.apache.ignite.cache.affinity.rendezvous.*; +import org.apache.ignite.cache.query.*; +import org.apache.ignite.cache.store.*; import org.apache.ignite.configuration.*; import org.apache.ignite.internal.processors.cache.*; import org.apache.ignite.lang.*; @@ -31,6 +31,7 @@ import org.jetbrains.annotations.*; import javax.cache.*; import javax.cache.configuration.*; import javax.cache.expiry.*; +import javax.cache.integration.*; import javax.cache.processor.*; import java.util.*; import java.util.concurrent.*; @@ -380,8 +381,9 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS @Override public void clear(); /** - * Clear entry from the cache, without notifying listeners or - * {@link javax.cache.integration.CacheWriter}s. + * Clear entry from the cache and swap storage, without notifying listeners or + * {@link CacheWriter}s. Entry is cleared only if it is not currently locked, + * and is not participating in a transaction. * * @param key Key to clear. * @throws IllegalStateException if the cache is {@link #isClosed()} @@ -391,8 +393,9 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS public void clear(K key); /** - * Clear entries from the cache, without notifying listeners or - * {@link javax.cache.integration.CacheWriter}s. + * Clear entries from the cache and swap storage, without notifying listeners or + * {@link CacheWriter}s. Entry is cleared only if it is not currently locked, + * and is not participating in a transaction. * * @param keys Keys to clear. * @throws IllegalStateException if the cache is {@link #isClosed()} @@ -402,24 +405,26 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS public void clearAll(Set<K> keys); /** - * Clears an entry from this cache and swap storage only if the entry - * is not currently locked, and is not participating in a transaction. + * Clear entry from the cache and swap storage, without notifying listeners or + * {@link CacheWriter}s. Entry is cleared only if it is not currently locked, + * and is not participating in a transaction. * <p/> * Note that this operation is local as it merely clears - * an entry from local cache. It does not remove entries from - * remote caches or from underlying persistent storage. + * an entry from local cache, it does not remove entries from + * remote caches. * * @param key Key to clear. */ public void localClear(K key); /** - * Clears an entry from this cache and swap storage only if the entry - * is not currently locked, and is not participating in a transaction. + * Clear entries from the cache and swap storage, without notifying listeners or + * {@link CacheWriter}s. Entry is cleared only if it is not currently locked, + * and is not participating in a transaction. * <p/> * Note that this operation is local as it merely clears - * an entry from local cache. It does not remove entries from - * remote caches or from underlying persistent storage. + * an entry from local cache, it does not remove entries from + * remote caches. * * @param keys Keys to clear. */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cf6677bb/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 2f68e58..9b1cc57 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 @@ -81,8 +81,8 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, /** clearLocally() split threshold. */ public static final int CLEAR_ALL_SPLIT_THRESHOLD = 10000; - /** Distribution modes to include into global size calculation. */ - private static final Set<CacheDistributionMode> SIZE_NODES = EnumSet.of( + /** */ + private static final Set<CacheDistributionMode> NEAR_AND_DATA_NODES = EnumSet.of( CacheDistributionMode.NEAR_PARTITIONED, CacheDistributionMode.PARTITIONED_ONLY, CacheDistributionMode.NEAR_ONLY); @@ -1414,21 +1414,17 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, * @param filter Optional filter. * @return {@code True} if cleared. */ - private boolean clearLocally(GridCacheVersion obsoleteVer, K key, - @Nullable CacheEntryPredicate[] filter) { + private boolean clearLocally(GridCacheVersion obsoleteVer, K key, @Nullable CacheEntryPredicate[] filter) { try { KeyCacheObject cacheKey = ctx.toCacheKeyObject(key); - GridCacheEntryEx e = peekEx(cacheKey); - - boolean removed = true; - - if (!ctx.isNear() && ctx.isSwapOrOffheapEnabled() && ctx.swap().containsKey(ctx.toCacheKeyObject(key))) - removed = entryEx(ctx.toCacheKeyObject(key)).clear(obsoleteVer, false, CU.empty0()); - - removed &= e != null && e.clear(obsoleteVer, false, filter); + GridCacheEntryEx entry = ctx.isSwapOrOffheapEnabled() ? entryEx(cacheKey) : peekEx(cacheKey); - return removed; + if (entry != null) + return entry.clear(obsoleteVer, false, filter); + } + catch (GridDhtInvalidPartitionException e) { + return false; } catch (IgniteCheckedException ex) { U.error(log, "Failed to clearLocally entry for key: " + key, ex); @@ -1479,20 +1475,21 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, /** * @param timeout Timeout for clearLocally all task in milliseconds (0 for never). * Set it to larger value for large caches. - * @param clearCallable Global clear callable object. + * @param clearCall Global clear callable object. * @throws IgniteCheckedException In case of cache could not be cleared on any of the nodes. */ - private void clearRemotes(long timeout, GlobalClearCallable clearCallable) throws IgniteCheckedException { + private void clearRemotes(long timeout, GlobalClearCallable clearCall) throws IgniteCheckedException { try { // Send job to remote nodes only. - Collection<ClusterNode> nodes = ctx.grid().cluster().forCacheNodes(name()).forRemotes().nodes(); + Collection<ClusterNode> nodes = + ctx.grid().cluster().forCacheNodes(name(), NEAR_AND_DATA_NODES).forRemotes().nodes(); IgniteInternalFuture<Object> fut = null; if (!nodes.isEmpty()) { ctx.kernalContext().task().setThreadContext(TC_TIMEOUT, timeout); - fut = ctx.closures().callAsyncNoFailover(BROADCAST, clearCallable, nodes, true); + fut = ctx.closures().callAsyncNoFailover(BROADCAST, clearCall, nodes, true); } if (fut != null) @@ -1516,15 +1513,15 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, } /** - * @param clearCallable Global clear callable object. + * @param clearCall Global clear callable object. * @return Future. */ - private IgniteInternalFuture<?> clearAsync(GlobalClearCallable clearCallable) { - Collection<ClusterNode> nodes = ctx.grid().cluster().forCacheNodes(name()).nodes(); + private IgniteInternalFuture<?> clearAsync(GlobalClearCallable clearCall) { + Collection<ClusterNode> nodes = ctx.grid().cluster().forCacheNodes(name(), NEAR_AND_DATA_NODES).nodes(); if (!nodes.isEmpty()) { IgniteInternalFuture<Object> fut = - ctx.closures().callAsyncNoFailover(BROADCAST, clearCallable, nodes, true); + ctx.closures().callAsyncNoFailover(BROADCAST, clearCall, nodes, true); return fut.chain(new CX1<IgniteInternalFuture<Object>, Object>() { @Override public Object applyx(IgniteInternalFuture<Object> fut) throws IgniteCheckedException { @@ -4067,7 +4064,8 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, IgniteClusterEx cluster = ctx.grid().cluster(); - ClusterGroup grp = modes.near ? cluster.forCacheNodes(name(), SIZE_NODES) : cluster.forDataNodes(name()); + ClusterGroup grp = + modes.near ? cluster.forCacheNodes(name(), NEAR_AND_DATA_NODES) : cluster.forDataNodes(name()); Collection<ClusterNode> nodes = grp.nodes(); @@ -5509,12 +5507,10 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, } /** - * Internal callable which performs {@link CacheProjection#clearLocally()} - * operation on a cache with the given name. + * Internal callable which performs clear operation on a cache with the given name. */ @GridInternal private static abstract class GlobalClearCallable implements Callable<Object>, Externalizable { - /** Cache name. */ protected String cacheName; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/cf6677bb/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java index f20b571..8def3a3 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java @@ -406,7 +406,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract assert 2 == map1.size() : "Invalid map: " + map1; - assertEquals(1, (int) map1.get("key1")); + assertEquals(1, (int)map1.get("key1")); assertEquals(2, (int)map1.get("key2")); assertNull(map1.get("key9999")); @@ -1024,7 +1024,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract tx.close(); } - assertEquals((Integer) 3, cache.get("key")); + assertEquals((Integer)3, cache.get("key")); } /** @@ -1188,11 +1188,11 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract assertEquals("null", cache.invoke("k0", INCR_PROCESSOR)); - assertEquals((Integer) 1, cache.get("k0")); + assertEquals((Integer)1, cache.get("k0")); assertEquals("1", cache.invoke("k0", INCR_PROCESSOR)); - assertEquals((Integer) 2, cache.get("k0")); + assertEquals((Integer)2, cache.get("k0")); cache.put("k1", 1); @@ -1225,8 +1225,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract }; GridTestUtils.assertThrows(log, new Callable<Void>() { - @Override - public Void call() throws Exception { + @Override public Void call() throws Exception { cache.invoke("k1", errProcessor); return null; @@ -1641,7 +1640,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract grid(i).jcache(null).localPeek("key", CachePeekMode.ONHEAP) + ']'); } - assertEquals((Integer) 1, cache.getAndPutIfAbsent("key", 2)); + assertEquals((Integer)1, cache.getAndPutIfAbsent("key", 2)); assert cache.get("key") != null; assert cache.get("key") == 1; @@ -1651,7 +1650,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract cache.localEvict(Collections.singleton("key2")); - assertEquals((Integer) 1, cache.getAndPutIfAbsent("key2", 3)); + assertEquals((Integer)1, cache.getAndPutIfAbsent("key2", 3)); // Check db. putToStore("key3", 3); @@ -2115,7 +2114,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract info("Finished replace."); - assertEquals((Integer) 2, cache.get("key")); + assertEquals((Integer)2, cache.get("key")); cacheAsync.replace("wrond", 2); @@ -3000,7 +2999,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract cache.put(key, 1); - assertEquals((Integer) 1, cache.get(key)); + assertEquals((Integer)1, cache.get(key)); long ttl = 500; @@ -3972,8 +3971,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract iter.remove(); GridTestUtils.assertThrows(log, new Callable<Object>() { - @Override - public Void call() throws Exception { + @Override public Void call() throws Exception { iter.remove(); return null; @@ -4102,15 +4100,15 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract public void testLocalClearKey() throws Exception { addKeys(); - String keyToRemove = "key" + 25; + String keyToRmv = "key" + 25; - Ignite g = primaryIgnite(keyToRemove); + Ignite g = primaryIgnite(keyToRmv); - g.<String, Integer>jcache(null).localClear(keyToRemove); + g.<String, Integer>jcache(null).localClear(keyToRmv); - checkLocalRemovedKey(keyToRemove); + checkLocalRemovedKey(keyToRmv); - g.<String, Integer>jcache(null).put(keyToRemove, 1); + g.<String, Integer>jcache(null).put(keyToRmv, 1); String keyToEvict = "key" + 30; @@ -4124,16 +4122,16 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract } /** - * @param keyToRemove Removed key. + * @param keyToRmv Removed key. */ - private void checkLocalRemovedKey(String keyToRemove) { + private void checkLocalRemovedKey(String keyToRmv) { for (int i = 0; i < 500; ++i) { String key = "key" + i; boolean found = primaryIgnite(key).jcache(null).localPeek(key) != null; - if (keyToRemove.equals(key)) { - Collection<ClusterNode> nodes =grid(0).affinity(null).mapKeyToPrimaryAndBackups(key); + if (keyToRmv.equals(key)) { + Collection<ClusterNode> nodes = grid(0).affinity(null).mapKeyToPrimaryAndBackups(key); for (int j = 0; j < gridCount(); ++j) { if (nodes.contains(grid(j).localNode()) && grid(j) != primaryIgnite(key)) @@ -4155,15 +4153,15 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract Ignite g = grid(0); - Set<String> keysToRemove = new HashSet<>(); + Set<String> keysToRmv = new HashSet<>(); for (int i = 0; i < gridCount(); ++i) { List<String> gridKeys = keys.get(grid(i).name()); if (gridKeys.size() > 2) { - keysToRemove.add(gridKeys.get(0)); + keysToRmv.add(gridKeys.get(0)); - keysToRemove.add(gridKeys.get(1)); + keysToRmv.add(gridKeys.get(1)); g = grid(i); @@ -4171,14 +4169,16 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract } } - g.<String, Integer>jcache(null).localClearAll(keysToRemove); + assert keysToRmv.size() > 1; + + g.<String, Integer>jcache(null).localClearAll(keysToRmv); for (int i = 0; i < 500; ++i) { String key = "key" + i; boolean found = primaryIgnite(key).jcache(null).localPeek(key) != null; - if (keysToRemove.contains(key)) + if (keysToRmv.contains(key)) assertFalse("Found removed key " + key, found); else assertTrue("Not found key " + key, found); @@ -4241,10 +4241,10 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract /** * @param async If {@code true} uses async method. - * @param keysToRemove Keys to remove. + * @param keysToRmv Keys to remove. * @throws Exception If failed. */ - protected void testGlobalClearKey(boolean async, Collection<String> keysToRemove) throws Exception { + protected void testGlobalClearKey(boolean async, Collection<String> keysToRmv) throws Exception { // Save entries only on their primary nodes. If we didn't do so, clearLocally() will not remove all entries // because some of them were blocked due to having readers. for (int i = 0; i < 500; ++i) { @@ -4258,18 +4258,18 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract if (async) { IgniteCache<String, Integer> asyncCache = jcache().withAsync(); - if (keysToRemove.size() == 1) - asyncCache.clear(F.first(keysToRemove)); + if (keysToRmv.size() == 1) + asyncCache.clear(F.first(keysToRmv)); else - asyncCache.clearAll(new HashSet(keysToRemove)); + asyncCache.clearAll(new HashSet<>(keysToRmv)); asyncCache.future().get(); } else { - if (keysToRemove.size() == 1) - jcache().clear(F.first(keysToRemove)); + if (keysToRmv.size() == 1) + jcache().clear(F.first(keysToRmv)); else - jcache().clearAll(new HashSet(keysToRemove)); + jcache().clearAll(new HashSet<>(keysToRmv)); } for (int i = 0; i < 500; ++i) { @@ -4277,11 +4277,12 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract boolean found = false; - for (int j = 0; j < gridCount(); j++) + for (int j = 0; j < gridCount(); j++) { if (jcache(j).localPeek(key) != null) found = true; + } - if (!keysToRemove.contains(key)) + if (!keysToRmv.contains(key)) assertTrue("Not found key " + key, found); else assertFalse("Found removed key " + key, found);