ignite-96 fixed evictions
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/db9d607e Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/db9d607e Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/db9d607e Branch: refs/heads/ignite-82 Commit: db9d607e2139293ecb9246421d0a5a5f16b233db Parents: 7e8b6c7 Author: Yakov Zhdanov <yzhda...@gridgain.com> Authored: Wed Feb 11 15:23:03 2015 +0300 Committer: Yakov Zhdanov <yzhda...@gridgain.com> Committed: Wed Feb 11 15:23:03 2015 +0300 ---------------------------------------------------------------------- .../ignite/cache/eviction/EvictableEntry.java | 18 +- .../CacheIgniteFsPerBlockLruEvictionPolicy.java | 2 +- .../random/CacheRandomEvictionPolicy.java | 26 +- .../processors/cache/EvictableEntryImpl.java | 186 ++++++ .../processors/cache/GridCacheAdapter.java | 2 +- .../cache/GridCacheEvictionEntry.java | 164 ----- .../processors/cache/GridCacheMapEntry.java | 14 +- .../GridCachePreloadingEvictionsSelfTest.java | 2 - .../GridCacheDistributedEvictionsSelfTest.java | 4 +- .../GridCacheEmptyEntriesAbstractSelfTest.java | 4 +- .../eviction/GridCacheEvictionAbstractTest.java | 46 +- .../cache/eviction/GridCacheMockEntry.java | 72 ++- .../GridCacheFifoEvictionPolicySelfTest.java | 631 +++++++++---------- .../lru/GridCacheLruEvictionPolicySelfTest.java | 558 ++++++++-------- .../GridCacheRandomEvictionPolicySelfTest.java | 104 ++- 15 files changed, 968 insertions(+), 865 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/main/java/org/apache/ignite/cache/eviction/EvictableEntry.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/EvictableEntry.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/EvictableEntry.java index c306125..14e3497 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/EvictableEntry.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/EvictableEntry.java @@ -17,6 +17,7 @@ package org.apache.ignite.cache.eviction; +import org.apache.ignite.*; import org.jetbrains.annotations.*; import javax.cache.*; @@ -28,12 +29,25 @@ import javax.cache.*; * @version @java.version */ public interface EvictableEntry<K, V> extends Cache.Entry<K, V> { + /** + * Evicts entry associated with given key from cache. Note, that entry will be evicted + * only if it's not used (not participating in any locks or transactions). + * + * @return {@code True} if entry could be evicted, {@code false} otherwise. + */ public boolean evict(); + /** + * Checks whether entry is currently present in cache or not. If entry is not in + * cache (e.g. has been removed) {@code false} is returned. In this case all + * operations on this entry will cause creation of a new entry in cache. + * + * @return {@code True} if entry is in cache, {@code false} otherwise. + */ public boolean isCached(); /** - * Gets metadata by name. + * Gets metadata added by eviction policy. * * @return Metadata value or {@code null}. */ @@ -43,7 +57,7 @@ public interface EvictableEntry<K, V> extends Cache.Entry<K, V> { * Adds a new metadata. * * @param val Metadata value. - * @return Metadata previously associated with given name, or + * @return Metadata previously added, or * {@code null} if there was none. */ @Nullable public <T> T addMeta(T val); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/main/java/org/apache/ignite/cache/eviction/ignitefs/CacheIgniteFsPerBlockLruEvictionPolicy.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/ignitefs/CacheIgniteFsPerBlockLruEvictionPolicy.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/ignitefs/CacheIgniteFsPerBlockLruEvictionPolicy.java index cec59ce..510cae1 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/ignitefs/CacheIgniteFsPerBlockLruEvictionPolicy.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/ignitefs/CacheIgniteFsPerBlockLruEvictionPolicy.java @@ -184,7 +184,7 @@ public class CacheIgniteFsPerBlockLruEvictionPolicy implements CacheEvictionPoli * @return Peeked value. */ @Nullable private byte[] peek(EvictableEntry<GridGgfsBlockKey, byte[]> entry) { - return (byte[])((GridCacheEvictionEntry)entry).peek(); + return (byte[])((EvictableEntryImpl)entry).peek(); } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/main/java/org/apache/ignite/cache/eviction/random/CacheRandomEvictionPolicy.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/random/CacheRandomEvictionPolicy.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/random/CacheRandomEvictionPolicy.java index c63bf61..2a9fb9e 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/random/CacheRandomEvictionPolicy.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/random/CacheRandomEvictionPolicy.java @@ -17,10 +17,13 @@ package org.apache.ignite.cache.eviction.random; +import org.apache.ignite.*; import org.apache.ignite.cache.eviction.*; import org.apache.ignite.configuration.*; import org.apache.ignite.internal.util.typedef.internal.*; +import javax.cache.*; + /** * Cache eviction policy which will select random cache entry for eviction if cache * size exceeds the {@link #getMaxSize()} parameter. This implementation is @@ -74,22 +77,21 @@ public class CacheRandomEvictionPolicy<K, V> implements CacheEvictionPolicy<K, V } /** {@inheritDoc} */ + @SuppressWarnings("unchecked") @Override public void onEntryAccessed(boolean rmv, EvictableEntry<K, V> entry) { if (!entry.isCached()) return; - assert false : "ignite-96"; -// TODO ignite-96 -// GridCache<K, V> cache = entry.projection().cache(); -// -// int size = cache.size(); -// -// for (int i = max; i < size; i++) { -// Entry<K, V> e = cache.randomEntry(); -// -// if (e != null) -// e.evict(); -// } + IgniteCache<K, V> cache = entry.unwrap(IgniteCache.class); + + int size = cache.size(); + + for (int i = max; i < size; i++) { + Cache.Entry<K, V> e = cache.randomEntry(); + + if (e != null) + e.unwrap(EvictableEntry.class).evict(); + } } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/EvictableEntryImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/EvictableEntryImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/EvictableEntryImpl.java new file mode 100644 index 0000000..eec358b --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/EvictableEntryImpl.java @@ -0,0 +1,186 @@ +/* + * 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 org.apache.ignite.*; +import org.apache.ignite.cache.eviction.*; +import org.apache.ignite.internal.processors.cache.transactions.*; +import org.apache.ignite.internal.util.lang.*; +import org.apache.ignite.internal.util.tostring.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.jetbrains.annotations.*; + +/** + * Entry wrapper that never obscures obsolete entries from user. + */ +public class EvictableEntryImpl<K, V> implements EvictableEntry<K, V> { + /** */ + private static final String META_KEY = "ignite-eviction-entry-meta"; + + /** Cached entry. */ + @GridToStringInclude + protected GridCacheEntryEx<K, V> cached; + + /** + * @param cached Cached entry. + */ + @SuppressWarnings({"TypeMayBeWeakened"}) + protected EvictableEntryImpl(GridCacheEntryEx<K, V> cached) { + this.cached = cached; + } + + /** {@inheritDoc} */ + @Override public K getKey() throws IllegalStateException { + return cached.key(); + } + + @Override public boolean isCached() { + return !cached.obsoleteOrDeleted(); + } + + @Override public boolean evict() { + GridCacheContext<K, V> ctx = cached.context(); + + try { + assert ctx != null; + assert ctx.evicts() != null; + + return ctx.evicts().evict(cached, null, false, null); + } + catch (IgniteCheckedException e) { + U.error(ctx.grid().log(), "Failed to evict entry from cache: " + cached, e); + + return false; + } + } + + /** + * @return Peeks value. + */ + @SuppressWarnings("unchecked") + @Nullable public V peek() { + try { + return cached.peek(GridCachePeekMode.GLOBAL); + } + catch (GridCacheEntryRemovedException e) { + return null; + } + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override public V getValue() { + try { + IgniteInternalTx<K, V> tx = cached.context().tm().userTx(); + + if (tx != null) { + GridTuple<V> peek = tx.peek(cached.context(), false, cached.key(), null); + + if (peek != null) + return peek.get(); + } + + if (cached.detached()) + return cached.rawGet(); + + for (;;) { + GridCacheEntryEx<K, V> e = cached.context().cache().peekEx(cached.key()); + + if (e == null) + return null; + + try { + return e.peek(GridCachePeekMode.GLOBAL, CU.<K, V>empty()); + } + catch (GridCacheEntryRemovedException ignored) { + // No-op. + } + } + } + catch (GridCacheFilterFailedException ignored) { + throw new IgniteException("Should never happen."); + } + } + + /** {@inheritDoc} */ + @Nullable @Override public <T> T addMeta(T val) { + return cached.addMeta(META_KEY, val); + } + + /** {@inheritDoc} */ + @Nullable @Override public <T> T meta() { + return cached.meta(META_KEY); + } + + /** {@inheritDoc} */ + @Nullable @Override public <T> T removeMeta() { + return cached.removeMeta(META_KEY); + } + + /** {@inheritDoc} */ + @Override public <T> boolean removeMeta(T val) { + return cached.removeMeta(META_KEY, val); + } + + /** {@inheritDoc} */ + @Nullable @Override public <T> T putMetaIfAbsent(T val) { + return cached.putMetaIfAbsent(META_KEY, val); + } + + /** {@inheritDoc} */ + @Override public <T> boolean replaceMeta(T curVal, T newVal) { + return cached.replaceMeta(META_KEY,curVal, newVal); + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override public <T> T unwrap(Class<T> clazz) { + if (clazz.isAssignableFrom(IgniteCache.class)) + return (T)cached.context().grid().jcache(cached.context().name()); + + if(clazz.isAssignableFrom(getClass())) + return clazz.cast(this); + + throw new IllegalArgumentException(); + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return cached.key().hashCode(); + } + + /** {@inheritDoc} */ + @SuppressWarnings({"unchecked"}) + @Override public boolean equals(Object obj) { + if (obj == this) + return true; + + if (obj instanceof EvictableEntryImpl) { + EvictableEntryImpl<K, V> other = (EvictableEntryImpl<K, V>)obj; + + return cached.key().equals(other.getKey()); + } + + return false; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(EvictableEntryImpl.class, this); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/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 f5b4e07..c564b89 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 @@ -4057,7 +4057,7 @@ public abstract class GridCacheAdapter<K, V> implements GridCache<K, V>, @Nullable @Override public Cache.Entry<K, V> randomEntry() { GridCacheMapEntry<K, V> e = map.randomEntry(); - return e == null || e.obsolete() ? null : e.wrap(); + return e == null || e.obsolete() ? null : e.wrapLazyValue(); } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheEvictionEntry.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheEvictionEntry.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheEvictionEntry.java deleted file mode 100644 index ab60f36..0000000 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheEvictionEntry.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * 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 org.apache.ignite.*; -import org.apache.ignite.cache.eviction.*; -import org.apache.ignite.internal.util.tostring.*; -import org.apache.ignite.internal.util.typedef.internal.*; -import org.jetbrains.annotations.*; - -import java.io.*; - -/** - * Entry wrapper that never obscures obsolete entries from user. - */ -public class GridCacheEvictionEntry<K, V> implements EvictableEntry<K, V> { - /** */ - private static final String META_KEY = "ignite-eviction-entry-meta"; - - /** Cached entry. */ - @GridToStringInclude - protected GridCacheEntryEx<K, V> cached; - - /** - * Empty constructor required for {@link Externalizable}. - */ - public GridCacheEvictionEntry() { - // No-op. - } - - /** - * @param cached Cached entry. - */ - @SuppressWarnings({"TypeMayBeWeakened"}) - protected GridCacheEvictionEntry(GridCacheEntryEx<K, V> cached) { - this.cached = cached; - } - - /** {@inheritDoc} */ - @Override public K getKey() throws IllegalStateException { - return cached.key(); - } - - @Override public boolean isCached() { - return !cached.obsoleteOrDeleted(); - } - - @Override public boolean evict() { - GridCacheContext<K, V> ctx = cached.context(); - - try { - assert ctx != null; - assert ctx.evicts() != null; - - return ctx.evicts().evict(cached, null, false, null); - } - catch (IgniteCheckedException e) { - U.error(ctx.grid().log(), "Failed to evict entry from cache: " + cached, e); - - return false; - } - } - - /** - * @return Peeks value. - */ - @Nullable public V peek() { - try { - return cached.peek(GridCachePeekMode.GLOBAL); - } - catch (GridCacheEntryRemovedException e) { - return null; - } - } - - /** {@inheritDoc} */ - @Nullable @Override public V getValue() throws IllegalStateException { - throw new UnsupportedOperationException("Operation not supported during eviction."); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"unchecked"}) - @Nullable @Override public <T> T addMeta(T val) { - return cached.addMeta(META_KEY, val); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"unchecked"}) - @Nullable @Override public <T> T meta() { - return cached.meta(META_KEY); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"unchecked"}) - @Nullable @Override public <T> T removeMeta() { - return cached.removeMeta(META_KEY); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"unchecked"}) - @Override public <T> boolean removeMeta(T val) { - return cached.removeMeta(META_KEY, val); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"unchecked"}) - @Nullable @Override public <T> T putMetaIfAbsent(T val) { - return cached.putMetaIfAbsent(META_KEY, val); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"RedundantTypeArguments"}) - @Override public <T> boolean replaceMeta(T curVal, T newVal) { - return cached.replaceMeta(META_KEY,curVal, newVal); - } - - /** {@inheritDoc} */ - @Override public <T> T unwrap(Class<T> clazz) { - if(clazz.isAssignableFrom(getClass())) - return clazz.cast(this); - - throw new IllegalArgumentException(); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - return cached.key().hashCode(); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"unchecked"}) - @Override public boolean equals(Object obj) { - if (obj == this) - return true; - - if (obj instanceof GridCacheEvictionEntry) { - GridCacheEvictionEntry<K, V> other = (GridCacheEvictionEntry<K, V>)obj; - - return cached.key().equals(other.getKey()); - } - - return false; - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(GridCacheEvictionEntry.class, this); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java index 9a18247..6e6d0bf 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java @@ -3707,7 +3707,7 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V> /** {@inheritDoc} */ @Override public Cache.Entry<K, V> wrapLazyValue() { - return new IteratorEntry(key); + return new LazyValueEntry(key); } /** {@inheritDoc} */ @@ -3717,7 +3717,7 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V> /** {@inheritDoc} */ @Override public EvictableEntry<K, V> wrapEviction() { - return new GridCacheEvictionEntry<>(this); + return new EvictableEntryImpl<>(this); } /** {@inheritDoc} */ @@ -4319,14 +4319,14 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V> /** * */ - private class IteratorEntry implements Cache.Entry<K, V> { + private class LazyValueEntry implements Cache.Entry<K, V> { /** */ private final K key; /** * @param key Key. */ - private IteratorEntry(K key) { + private LazyValueEntry(K key) { this.key = key; } @@ -4373,6 +4373,12 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V> /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public <T> T unwrap(Class<T> cls) { + if (cls.isAssignableFrom(IgniteCache.class)) + return (T)cctx.grid().jcache(cctx.name()); + + if (cls.isAssignableFrom(EvictableEntry.class)) + return (T)wrapEviction(); + if (!cls.equals(getClass())) throw new IllegalArgumentException("Unwrapping to class is not supported: " + cls); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCachePreloadingEvictionsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCachePreloadingEvictionsSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCachePreloadingEvictionsSelfTest.java index ff4086e..3ea54ba 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCachePreloadingEvictionsSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCachePreloadingEvictionsSelfTest.java @@ -114,8 +114,6 @@ public class GridCachePreloadingEvictionsSelfTest extends GridCommonAbstractTest int oldSize = cache1.localSize(); - assert false; - IgniteInternalFuture fut = multithreadedAsync( new Callable<Object>() { @Nullable @Override public Object call() throws Exception { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheDistributedEvictionsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheDistributedEvictionsSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheDistributedEvictionsSelfTest.java index e3c9698..1d45e6c 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheDistributedEvictionsSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheDistributedEvictionsSelfTest.java @@ -228,8 +228,8 @@ public class GridCacheDistributedEvictionsSelfTest extends GridCommonAbstractTes info("Near node near key set: " + new TreeSet<>(this.<Integer, Integer>near(2).keySet())); try { - assert cache.size() == 10 : "Invalid cache size [size=" + cache.size() + ']'; - assert cache.size() == 10 : "Invalid key size [size=" + cache.size() + ']'; + assert cache.localSize() == 10 : "Invalid cache size [size=" + cache.localSize() + ']'; + assert cache.localSize() == 10 : "Invalid key size [size=" + cache.localSize() + ']'; assert jcache(2).localSize() == 0; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEmptyEntriesAbstractSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEmptyEntriesAbstractSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEmptyEntriesAbstractSelfTest.java index 05c2512..de80679 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEmptyEntriesAbstractSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEmptyEntriesAbstractSelfTest.java @@ -208,7 +208,7 @@ public abstract class GridCacheEmptyEntriesAbstractSelfTest extends GridCommonAb asyncCache.getAll(F.asSet("key5", "key6")); - assertTrue(((Collection)asyncCache.future().get()).isEmpty()); + assertTrue(((Map)asyncCache.future().get()).isEmpty()); cache.put("key7", "key7"); cache.remove("key7", "key7"); @@ -266,7 +266,7 @@ public abstract class GridCacheEmptyEntriesAbstractSelfTest extends GridCommonAb try { asyncCache.getAll(F.asSet("key5", "key6")); - assertTrue(((Collection)asyncCache.future().get()).isEmpty()); + assertTrue(((Map)asyncCache.future().get()).isEmpty()); tx.commit(); } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionAbstractTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionAbstractTest.java index 9de57d1..84da7e7 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionAbstractTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionAbstractTest.java @@ -23,6 +23,7 @@ import org.apache.ignite.cache.eviction.*; import org.apache.ignite.configuration.*; import org.apache.ignite.internal.*; import org.apache.ignite.internal.util.typedef.*; +import org.apache.ignite.internal.util.typedef.internal.*; import org.apache.ignite.spi.discovery.tcp.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; @@ -131,8 +132,8 @@ public abstract class GridCacheEvictionAbstractTest<T extends CacheEvictionPolic protected MockEntry entry(MockEntry[] arr, int idx) { MockEntry e = arr[idx]; -// if (e.isEvicted()) // TODO ignite-96 -// e = arr[idx] = new MockEntry(e.getKey()); + if (e.isEvicted()) + e = arr[idx] = new MockEntry(e.getKey()); return e; } @@ -154,7 +155,7 @@ public abstract class GridCacheEvictionAbstractTest<T extends CacheEvictionPolic * @param c1 Policy collection. * @param c2 Expected list. */ - protected void check(Collection<Cache.Entry<String, String>> c1, MockEntry... c2) { + protected void check(Collection<EvictableEntry<String, String>> c1, MockEntry... c2) { check(c1, F.asList(c2)); } @@ -186,7 +187,7 @@ public abstract class GridCacheEvictionAbstractTest<T extends CacheEvictionPolic * @param c1 Policy collection. * @param c2 Expected list. */ - protected void check(Collection<Cache.Entry<String, String>> c1, List<MockEntry> c2) { + protected void check(Collection<EvictableEntry<String, String>> c1, List<MockEntry> c2) { assert c1.size() == c2.size() : "Mismatch [actual=" + string(c1) + ", expected=" + string(c2) + ']'; assert c1.containsAll(c2) : "Mismatch [actual=" + string(c1) + ", expected=" + string(c2) + ']'; @@ -202,12 +203,18 @@ public abstract class GridCacheEvictionAbstractTest<T extends CacheEvictionPolic * @param c Collection. * @return String. */ + @SuppressWarnings("unchecked") protected String string(Iterable<? extends Cache.Entry> c) { - return "[" + F.fold(c, "", new C2<Cache.Entry, String, String>() { - @Override public String apply(Cache.Entry e, String b) { - return b.isEmpty() ? e.getKey().toString() : b + ", " + e.getKey(); - } - }) + "]]"; + return "[" + + F.fold( + c, + "", + new C2<Cache.Entry, String, String>() { + @Override public String apply(Cache.Entry e, String b) { + return b.isEmpty() ? e.getKey().toString() : b + ", " + e.getKey(); + } + }) + + "]]"; } /** @throws Exception If failed. */ @@ -412,7 +419,7 @@ public abstract class GridCacheEvictionAbstractTest<T extends CacheEvictionPolic @SuppressWarnings({"PublicConstructorInNonPublicClass"}) protected static class MockEntry extends GridCacheMockEntry<String, String> { /** */ - private final CacheProjection<String, String> parent; + private IgniteCache<String, String> parent; /** Entry value. */ private String val; @@ -420,8 +427,6 @@ public abstract class GridCacheEvictionAbstractTest<T extends CacheEvictionPolic /** @param key Key. */ public MockEntry(String key) { super(key); - - parent = null; } /** @@ -432,22 +437,35 @@ public abstract class GridCacheEvictionAbstractTest<T extends CacheEvictionPolic super(key); this.val = val; - parent = null; } /** * @param key Key. * @param parent Parent. */ - public MockEntry(String key, @Nullable CacheProjection<String, String> parent) { + public MockEntry(String key, @Nullable IgniteCache<String, String> parent) { super(key); this.parent = parent; } /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override public <T> T unwrap(Class<T> clazz) { + if (clazz.isAssignableFrom(IgniteCache.class)) + return (T)parent; + + return super.unwrap(clazz); + } + + /** {@inheritDoc} */ @Override public String getValue() throws IllegalStateException { return val; } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(MockEntry.class, this, super.toString()); + } } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheMockEntry.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheMockEntry.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheMockEntry.java index 74e00c6..73e3024 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheMockEntry.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheMockEntry.java @@ -17,9 +17,11 @@ package org.apache.ignite.internal.processors.cache.eviction; +import org.apache.ignite.cache.eviction.*; import org.apache.ignite.internal.util.lang.*; import org.apache.ignite.internal.util.tostring.*; import org.apache.ignite.internal.util.typedef.internal.*; +import org.jetbrains.annotations.*; import javax.cache.*; @@ -27,11 +29,19 @@ import javax.cache.*; /** * Mock cache entry. */ -public class GridCacheMockEntry<K, V> extends GridMetadataAwareAdapter implements Cache.Entry<K, V> { +public class GridCacheMockEntry<K, V> extends GridMetadataAwareAdapter implements Cache.Entry<K, V>, + EvictableEntry<K, V> { + /** */ + private static final String META_KEY = "ignite-mock-eviction-entry-meta"; + /** */ @GridToStringInclude private K key; + /** */ + @GridToStringInclude + private boolean evicted; + /** * Constructor. * @@ -52,6 +62,66 @@ public class GridCacheMockEntry<K, V> extends GridMetadataAwareAdapter implement } /** {@inheritDoc} */ + @Override public boolean evict() { + evicted = true; + + onEvicted(); + + return true; + } + + /** + * + */ + private void onEvicted() { + for (String key : allMeta().keySet()) + removeMeta(key); + } + + /** {@inheritDoc} */ + @Override public boolean isCached() { + return !evicted; + } + + /** + * + * @return Evicted or not. + */ + public boolean isEvicted() { + return evicted; + } + + /** {@inheritDoc} */ + @Nullable @Override public <T> T addMeta(T val) { + return addMeta(META_KEY, val); + } + + /** {@inheritDoc} */ + @Nullable @Override public <T> T meta() { + return meta(META_KEY); + } + + /** {@inheritDoc} */ + @Nullable @Override public <T> T removeMeta() { + return removeMeta(META_KEY); + } + + /** {@inheritDoc} */ + @Override public <T> boolean removeMeta(T val) { + return removeMeta(META_KEY, val); + } + + /** {@inheritDoc} */ + @Nullable @Override public <T> T putMetaIfAbsent(T val) { + return putMetaIfAbsent(META_KEY, val); + } + + /** {@inheritDoc} */ + @Override public <T> boolean replaceMeta(T curVal, T newVal) { + return replaceMeta(META_KEY,curVal, newVal); + } + + /** {@inheritDoc} */ @Override public <T> T unwrap(Class<T> clazz) { if(clazz.isAssignableFrom(getClass())) return clazz.cast(this); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoEvictionPolicySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoEvictionPolicySelfTest.java index 9931663..9dcd736 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoEvictionPolicySelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoEvictionPolicySelfTest.java @@ -17,10 +17,15 @@ package org.apache.ignite.internal.processors.cache.eviction.fifo; +import org.apache.ignite.*; import org.apache.ignite.cache.eviction.*; import org.apache.ignite.cache.eviction.fifo.*; import org.apache.ignite.internal.processors.cache.eviction.*; +import java.util.*; + +import static org.apache.ignite.cache.CacheMode.*; + /** * FIFO Eviction test. */ @@ -31,326 +36,316 @@ public class GridCacheFifoEvictionPolicySelfTest extends * @throws Exception If failed. */ public void testPolicy() throws Exception { - assert false : "ignite-96"; - -// try { -// startGrid(); -// -// MockEntry e1 = new MockEntry("1", "1"); -// MockEntry e2 = new MockEntry("2", "2"); -// MockEntry e3 = new MockEntry("3", "3"); -// MockEntry e4 = new MockEntry("4", "4"); -// MockEntry e5 = new MockEntry("5", "5"); -// -// CacheFifoEvictionPolicy<String, String> p = policy(); -// -// p.setMaxSize(3); -// -// p.onEntryAccessed(false, e1); -// -// check(p.queue(), e1); -// -// p.onEntryAccessed(false, e2); -// -// check(p.queue(), e1, e2); -// -// p.onEntryAccessed(false, e3); -// -// check(p.queue(), e1, e2, e3); -// -// assert !e1.isEvicted(); -// assert !e2.isEvicted(); -// assert !e3.isEvicted(); -// -// assertEquals(3, p.getCurrentSize()); -// -// p.onEntryAccessed(false, e4); -// -// check(p.queue(), e2, e3, e4); -// -// assertEquals(3, p.getCurrentSize()); -// -// assert e1.isEvicted(); -// assert !e2.isEvicted(); -// assert !e3.isEvicted(); -// assert !e4.isEvicted(); -// -// p.onEntryAccessed(false, e5); -// -// check(p.queue(), e3, e4, e5); -// -// assertEquals(3, p.getCurrentSize()); -// -// assert e2.isEvicted(); -// assert !e3.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); -// -// check(p.queue(), e4, e5, e1); -// -// assertEquals(3, p.getCurrentSize()); -// -// assert e3.isEvicted(); -// assert !e1.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(false, e5); -// -// check(p.queue(), e4, e5, e1); -// -// assert !e1.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(false, e1); -// -// assertEquals(3, p.getCurrentSize()); -// -// check(p.queue(), e4, e5, e1); -// -// assert !e1.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(false, e5); -// -// assertEquals(3, p.getCurrentSize()); -// -// check(p.queue(), e4, e5, e1); -// -// assert !e1.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(true, e1); -// -// assertEquals(2, p.getCurrentSize()); -// -// assert !e1.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(true, e4); -// -// assertEquals(1, p.getCurrentSize()); -// -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(true, e5); -// -// assertEquals(0, p.getCurrentSize()); -// -// assert !e5.isEvicted(); -// -// info(p); -// } -// finally { -// stopAllGrids(); -// } + try { + startGrid(); + + MockEntry e1 = new MockEntry("1", "1"); + MockEntry e2 = new MockEntry("2", "2"); + MockEntry e3 = new MockEntry("3", "3"); + MockEntry e4 = new MockEntry("4", "4"); + MockEntry e5 = new MockEntry("5", "5"); + + CacheFifoEvictionPolicy<String, String> p = policy(); + + p.setMaxSize(3); + + p.onEntryAccessed(false, e1); + + check(p.queue(), e1); + + p.onEntryAccessed(false, e2); + + check(p.queue(), e1, e2); + + p.onEntryAccessed(false, e3); + + check(p.queue(), e1, e2, e3); + + assert !e1.isEvicted(); + assert !e2.isEvicted(); + assert !e3.isEvicted(); + + assertEquals(3, p.getCurrentSize()); + + p.onEntryAccessed(false, e4); + + check(p.queue(), e2, e3, e4); + + assertEquals(3, p.getCurrentSize()); + + assert e1.isEvicted(); + assert !e2.isEvicted(); + assert !e3.isEvicted(); + assert !e4.isEvicted(); + + p.onEntryAccessed(false, e5); + + check(p.queue(), e3, e4, e5); + + assertEquals(3, p.getCurrentSize()); + + assert e2.isEvicted(); + assert !e3.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); + + check(p.queue(), e4, e5, e1); + + assertEquals(3, p.getCurrentSize()); + + assert e3.isEvicted(); + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e5); + + check(p.queue(), e4, e5, e1); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e1); + + assertEquals(3, p.getCurrentSize()); + + check(p.queue(), e4, e5, e1); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e5); + + assertEquals(3, p.getCurrentSize()); + + check(p.queue(), e4, e5, e1); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(true, e1); + + assertEquals(2, p.getCurrentSize()); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(true, e4); + + assertEquals(1, p.getCurrentSize()); + + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(true, e5); + + assertEquals(0, p.getCurrentSize()); + + assert !e5.isEvicted(); + + info(p); + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + public void testMemory() throws Exception { + try { + startGrid(); + + CacheFifoEvictionPolicy<String, String> p = policy(); + + int max = 10; + + p.setMaxSize(max); + + int cnt = 11; + + for (int i = 0; i < cnt; i++) + p.onEntryAccessed(false, new MockEntry(Integer.toString(i), Integer.toString(i))); + + info(p); + + assertEquals(max, p.getCurrentSize()); + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + public void testRandom() throws Exception { + try { + startGrid(); + + CacheFifoEvictionPolicy<String, String> p = policy(); + + int max = 10; + + p.setMaxSize(max); + + Random rand = new Random(); + + int keys = 31; + + MockEntry[] fifos = new MockEntry[keys]; + + for (int i = 0; i < fifos.length; i++) + fifos[i] = new MockEntry(Integer.toString(i)); + + int runs = 5000000; + + for (int i = 0; i < runs; i++) { + boolean rmv = rand.nextBoolean(); + + int j = rand.nextInt(fifos.length); + + MockEntry e = entry(fifos, j); + + if (rmv) + fifos[j] = new MockEntry(Integer.toString(j)); + + p.onEntryAccessed(rmv, e); + } + + info(p); + + int curSize = p.getCurrentSize(); + + assert curSize <= max : "curSize <= max [curSize=" + curSize + ", max=" + max + ']'; + } + finally { + stopAllGrids(); + } } -// /** -// * @throws Exception If failed. -// */ -// public void testMemory() throws Exception { -// try { -// startGrid(); -// -// CacheFifoEvictionPolicy<String, String> p = policy(); -// -// int max = 10; -// -// p.setMaxSize(max); -// -// int cnt = 11; -// -// for (int i = 0; i < cnt; i++) -// p.onEntryAccessed(false, new MockEntry(Integer.toString(i), Integer.toString(i))); -// -// info(p); -// -// assertEquals(max, p.getCurrentSize()); -// } -// finally { -// stopAllGrids(); -// } -// } -// -// /** -// * @throws Exception If failed. -// */ -// public void testRandom() throws Exception { -// try { -// startGrid(); -// -// CacheFifoEvictionPolicy<String, String> p = policy(); -// -// int max = 10; -// -// p.setMaxSize(max); -// -// Random rand = new Random(); -// -// int keys = 31; -// -// MockEntry[] fifos = new MockEntry[keys]; -// -// for (int i = 0; i < fifos.length; i++) -// fifos[i] = new MockEntry(Integer.toString(i)); -// -// int runs = 5000000; -// -// for (int i = 0; i < runs; i++) { -// boolean rmv = rand.nextBoolean(); -// -// int j = rand.nextInt(fifos.length); -// -// MockEntry e = entry(fifos, j); -// -// if (rmv) -// fifos[j] = new MockEntry(Integer.toString(j)); -// -// p.onEntryAccessed(rmv, e); -// } -// -// info(p); -// -// int curSize = p.getCurrentSize(); -// -// assert curSize <= max : "curSize <= max [curSize=" + curSize + ", max=" + max + ']'; -// } -// finally { -// stopAllGrids(); -// } -// } -// -// /** -// * @throws Exception If failed. -// */ -// public void testAllowEmptyEntries() throws Exception { -// try { -// startGrid(); -// -// MockEntry e1 = new MockEntry("1"); -// -// e1.setValue("val"); -// -// MockEntry e2 = new MockEntry("2"); -// -// MockEntry e3 = new MockEntry("3"); -// -// e3.setValue("val"); -// -// MockEntry e4 = new MockEntry("4"); -// -// MockEntry e5 = new MockEntry("5"); -// -// e5.setValue("val"); -// -// CacheFifoEvictionPolicy<String, String> p = policy(); -// -// p.setMaxSize(10); -// -// p.onEntryAccessed(false, e1); -// -// assertFalse(e1.isEvicted()); -// -// p.onEntryAccessed(false, e2); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e2.isEvicted()); -// -// p.onEntryAccessed(false, e3); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e3.isEvicted()); -// -// p.onEntryAccessed(false, e4); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e3.isEvicted()); -// assertFalse(e4.isEvicted()); -// -// p.onEntryAccessed(false, e5); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e3.isEvicted()); -// assertFalse(e5.isEvicted()); -// } -// finally { -// stopAllGrids(); -// } -// } -// -// /** -// * @throws Exception If failed. -// */ -// public void testPut() throws Exception { -// mode = LOCAL; -// syncCommit = true; -// plcMax = 100; -// -// Ignite ignite = startGrid(); -// -// try { -// GridCache<Integer, Integer> cache = ignite.cache(null); -// -// int cnt = 500; -// -// int min = Integer.MAX_VALUE; -// -// int minIdx = 0; -// -// for (int i = 0; i < cnt; i++) { -// cache.put(i, i); -// -// int cacheSize = cache.size(); -// -// if (i > plcMax && cacheSize < min) { -// min = cacheSize; -// minIdx = i; -// } -// } -// -// assert min >= plcMax : "Min cache size is too small: " + min; -// -// info("Min cache size [min=" + min + ", idx=" + minIdx + ']'); -// info("Current cache size " + cache.size()); -// info("Current cache key size " + cache.size()); -// info("Current cache entry set size " + cache.entrySet().size()); -// -// min = Integer.MAX_VALUE; -// -// minIdx = 0; -// -// // Touch. -// for (int i = cnt; --i > cnt - plcMax;) { -// cache.get(i); -// -// int cacheSize = cache.size(); -// -// if (cacheSize < min) { -// min = cacheSize; -// minIdx = i; -// } -// } -// -// info("----"); -// info("Min cache size [min=" + min + ", idx=" + minIdx + ']'); -// info("Current cache size " + cache.size()); -// info("Current cache key size " + cache.size()); -// info("Current cache entry set size " + cache.entrySet().size()); -// -// assert min >= plcMax : "Min cache size is too small: " + min; -// } -// finally { -// stopAllGrids(); -// } -// } + /** + * @throws Exception If failed. + */ + public void testAllowEmptyEntries() throws Exception { + try { + startGrid(); + + MockEntry e1 = new MockEntry("1"); + + MockEntry e2 = new MockEntry("2"); + + MockEntry e3 = new MockEntry("3"); + + MockEntry e4 = new MockEntry("4"); + + MockEntry e5 = new MockEntry("5"); + + CacheFifoEvictionPolicy<String, String> p = policy(); + + p.setMaxSize(10); + + p.onEntryAccessed(false, e1); + + assertFalse(e1.isEvicted()); + + p.onEntryAccessed(false, e2); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + + p.onEntryAccessed(false, e3); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + + p.onEntryAccessed(false, e4); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + + p.onEntryAccessed(false, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e5.isEvicted()); + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + public void testPut() throws Exception { + mode = LOCAL; + syncCommit = true; + plcMax = 100; + + Ignite ignite = startGrid(); + + try { + IgniteCache<Object, Object> cache = ignite.jcache(null); + + int cnt = 500; + + int min = Integer.MAX_VALUE; + + int minIdx = 0; + + for (int i = 0; i < cnt; i++) { + cache.put(i, i); + + int cacheSize = cache.size(); + + if (i > plcMax && cacheSize < min) { + min = cacheSize; + minIdx = i; + } + } + + assert min >= plcMax : "Min cache size is too small: " + min; + + info("Min cache size [min=" + min + ", idx=" + minIdx + ']'); + info("Current cache size " + cache.size()); + info("Current cache key size " + cache.size()); + + min = Integer.MAX_VALUE; + + minIdx = 0; + + // Touch. + for (int i = cnt; --i > cnt - plcMax;) { + cache.get(i); + + int cacheSize = cache.size(); + + if (cacheSize < min) { + min = cacheSize; + minIdx = i; + } + } + + info("----"); + info("Min cache size [min=" + min + ", idx=" + minIdx + ']'); + info("Current cache size " + cache.size()); + info("Current cache key size " + cache.size()); + + assert min >= plcMax : "Min cache size is too small: " + min; + } + finally { + stopAllGrids(); + } + } /** {@inheritDoc} */ @Override protected CacheFifoEvictionPolicy<String, String> createPolicy(int plcMax) { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruEvictionPolicySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruEvictionPolicySelfTest.java index 93a7e40..175ef81 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruEvictionPolicySelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruEvictionPolicySelfTest.java @@ -23,6 +23,8 @@ import org.apache.ignite.cache.eviction.*; import org.apache.ignite.cache.eviction.lru.*; import org.apache.ignite.internal.processors.cache.eviction.*; +import java.util.*; + /** * LRU Eviction test. */ @@ -33,316 +35,300 @@ public class GridCacheLruEvictionPolicySelfTest extends * @throws Exception If failed. */ public void testPolicy() throws Exception { - assert false : "ignite-96"; - -// startGrid(); -// -// try { -// MockEntry e1 = new MockEntry("1", "1"); -// MockEntry e2 = new MockEntry("2", "2"); -// MockEntry e3 = new MockEntry("3", "3"); -// MockEntry e4 = new MockEntry("4", "4"); -// MockEntry e5 = new MockEntry("5", "5"); -// -// CacheLruEvictionPolicy<String, String> p = policy(); -// -// p.setMaxSize(3); -// -// p.onEntryAccessed(false, e1); -// -// check(p.queue(), e1); -// -// p.onEntryAccessed(false, e2); -// -// check(p.queue(), e1, e2); -// -// p.onEntryAccessed(false, e3); -// -// check(p.queue(), e1, e2, e3); -// -// assert !e1.isEvicted(); -// assert !e2.isEvicted(); -// assert !e3.isEvicted(); -// -// assertEquals(3, p.getCurrentSize()); -// -// p.onEntryAccessed(false, e4); -// -// check(p.queue(), e2, e3, e4); -// -// assertEquals(3, p.getCurrentSize()); -// -// assert e1.isEvicted(); -// assert !e2.isEvicted(); -// assert !e3.isEvicted(); -// assert !e4.isEvicted(); -// -// p.onEntryAccessed(false, e5); -// -// check(p.queue(), e3, e4, e5); -// -// assertEquals(3, p.getCurrentSize()); -// -// assert e2.isEvicted(); -// assert !e3.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); -// -// check(p.queue(), e4, e5, e1); -// -// assertEquals(3, p.getCurrentSize()); -// -// assert e3.isEvicted(); -// assert !e1.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(false, e5); -// -// assertEquals(3, p.getCurrentSize()); -// -// check(p.queue(), e4, e1, e5); -// -// assert !e1.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(false, e1); -// -// assertEquals(3, p.getCurrentSize()); -// -// check(p.queue(), e4, e5, e1); -// -// assert !e1.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(false, e5); -// -// assertEquals(3, p.getCurrentSize()); -// -// check(p.queue(), e4, e1, e5); -// -// assert !e1.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(true, e1); -// -// assertEquals(2, p.getCurrentSize()); -// -// assert !e1.isEvicted(); -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(true, e4); -// -// assertEquals(1, p.getCurrentSize()); -// -// assert !e4.isEvicted(); -// assert !e5.isEvicted(); -// -// p.onEntryAccessed(true, e5); -// -// assertEquals(0, p.getCurrentSize()); -// -// assert !e5.isEvicted(); -// -// info(p); -// } -// finally { -// stopGrid(); -// } + startGrid(); + + try { + MockEntry e1 = new MockEntry("1", "1"); + MockEntry e2 = new MockEntry("2", "2"); + MockEntry e3 = new MockEntry("3", "3"); + MockEntry e4 = new MockEntry("4", "4"); + MockEntry e5 = new MockEntry("5", "5"); + + CacheLruEvictionPolicy<String, String> p = policy(); + + p.setMaxSize(3); + + p.onEntryAccessed(false, e1); + + check(p.queue(), e1); + + p.onEntryAccessed(false, e2); + + check(p.queue(), e1, e2); + + p.onEntryAccessed(false, e3); + + check(p.queue(), e1, e2, e3); + + assert !e1.isEvicted(); + assert !e2.isEvicted(); + assert !e3.isEvicted(); + + assertEquals(3, p.getCurrentSize()); + + p.onEntryAccessed(false, e4); + + check(p.queue(), e2, e3, e4); + + assertEquals(3, p.getCurrentSize()); + + assert e1.isEvicted(); + assert !e2.isEvicted(); + assert !e3.isEvicted(); + assert !e4.isEvicted(); + + p.onEntryAccessed(false, e5); + + check(p.queue(), e3, e4, e5); + + assertEquals(3, p.getCurrentSize()); + + assert e2.isEvicted(); + assert !e3.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); + + check(p.queue(), e4, e5, e1); + + assertEquals(3, p.getCurrentSize()); + + assert e3.isEvicted(); + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e5); + + assertEquals(3, p.getCurrentSize()); + + check(p.queue(), e4, e1, e5); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e1); + + assertEquals(3, p.getCurrentSize()); + + check(p.queue(), e4, e5, e1); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e5); + + assertEquals(3, p.getCurrentSize()); + + check(p.queue(), e4, e1, e5); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(true, e1); + + assertEquals(2, p.getCurrentSize()); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(true, e4); + + assertEquals(1, p.getCurrentSize()); + + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(true, e5); + + assertEquals(0, p.getCurrentSize()); + + assert !e5.isEvicted(); + + info(p); + } + finally { + stopGrid(); + } } /** * @throws Exception If failed. */ public void testMemory() throws Exception { - assert false : "ignite-96"; - -// startGrid(); -// -// try { -// CacheLruEvictionPolicy<String, String> p = policy(); -// -// int max = 10; -// -// p.setMaxSize(max); -// -// int cnt = 11; -// -// for (int i = 0; i < cnt; i++) -// p.onEntryAccessed(false, new MockEntry(Integer.toString(i), Integer.toString(i))); -// -// info(p); -// -// assertEquals(max, p.getCurrentSize()); -// } -// finally { -// stopGrid(); -// } + startGrid(); + + try { + CacheLruEvictionPolicy<String, String> p = policy(); + + int max = 10; + + p.setMaxSize(max); + + int cnt = 11; + + for (int i = 0; i < cnt; i++) + p.onEntryAccessed(false, new MockEntry(Integer.toString(i), Integer.toString(i))); + + info(p); + + assertEquals(max, p.getCurrentSize()); + } + finally { + stopGrid(); + } } /** * @throws Exception If failed. */ public void testMiddleAccess() throws Exception { - assert false : "ignite-96"; - -// startGrid(); -// -// try { -// CacheLruEvictionPolicy<String, String> p = policy(); -// -// int max = 8; -// -// p.setMaxSize(max); -// -// MockEntry entry1 = new MockEntry("1", "1"); -// MockEntry entry2 = new MockEntry("2", "2"); -// MockEntry entry3 = new MockEntry("3", "3"); -// -// p.onEntryAccessed(false, entry1); -// p.onEntryAccessed(false, entry2); -// p.onEntryAccessed(false, entry3); -// -// MockEntry[] freqUsed = new MockEntry[] { -// new MockEntry("4", "4"), -// new MockEntry("5", "5"), -// new MockEntry("6", "6"), -// new MockEntry("7", "7"), -// new MockEntry("8", "7") -// }; -// -// for (MockEntry e : freqUsed) -// p.onEntryAccessed(false, e); -// -// for (MockEntry e : freqUsed) -// assert !e.isEvicted(); -// -// int cnt = 1001; -// -// for (int i = 0; i < cnt; i++) -// p.onEntryAccessed(false, entry(freqUsed, i % freqUsed.length)); -// -// info(p); -// -// assertEquals(max, p.getCurrentSize()); -// } -// finally { -// stopGrid(); -// } + startGrid(); + + try { + CacheLruEvictionPolicy<String, String> p = policy(); + + int max = 8; + + p.setMaxSize(max); + + MockEntry entry1 = new MockEntry("1", "1"); + MockEntry entry2 = new MockEntry("2", "2"); + MockEntry entry3 = new MockEntry("3", "3"); + + p.onEntryAccessed(false, entry1); + p.onEntryAccessed(false, entry2); + p.onEntryAccessed(false, entry3); + + MockEntry[] freqUsed = new MockEntry[] { + new MockEntry("4", "4"), + new MockEntry("5", "5"), + new MockEntry("6", "6"), + new MockEntry("7", "7"), + new MockEntry("8", "7") + }; + + for (MockEntry e : freqUsed) + p.onEntryAccessed(false, e); + + for (MockEntry e : freqUsed) + assert !e.isEvicted(); + + int cnt = 1001; + + for (int i = 0; i < cnt; i++) + p.onEntryAccessed(false, entry(freqUsed, i % freqUsed.length)); + + info(p); + + assertEquals(max, p.getCurrentSize()); + } + finally { + stopGrid(); + } } /** * @throws Exception If failed. */ public void testRandom() throws Exception { - assert false : "ignite-96"; - -// startGrid(); -// -// try { -// CacheLruEvictionPolicy<String, String> p = policy(); -// -// int max = 10; -// -// p.setMaxSize(max); -// -// Random rand = new Random(); -// -// int keys = 31; -// -// MockEntry[] lrus = new MockEntry[keys]; -// -// for (int i = 0; i < lrus.length; i++) -// lrus[i] = new MockEntry(Integer.toString(i)); -// -// int runs = 500000; -// -// for (int i = 0; i < runs; i++) { -// boolean rmv = rand.nextBoolean(); -// -// int j = rand.nextInt(lrus.length); -// -// MockEntry e = entry(lrus, j); -// -// if (rmv) -// lrus[j] = new MockEntry(Integer.toString(j)); -// -// p.onEntryAccessed(rmv, e); -// } -// -// info(p); -// -// assert p.getCurrentSize() <= max; -// } -// finally { -// stopGrid(); -// } + startGrid(); + + try { + CacheLruEvictionPolicy<String, String> p = policy(); + + int max = 10; + + p.setMaxSize(max); + + Random rand = new Random(); + + int keys = 31; + + MockEntry[] lrus = new MockEntry[keys]; + + for (int i = 0; i < lrus.length; i++) + lrus[i] = new MockEntry(Integer.toString(i)); + + int runs = 500000; + + for (int i = 0; i < runs; i++) { + boolean rmv = rand.nextBoolean(); + + int j = rand.nextInt(lrus.length); + + MockEntry e = entry(lrus, j); + + if (rmv) + lrus[j] = new MockEntry(Integer.toString(j)); + + p.onEntryAccessed(rmv, e); + } + + info(p); + + assert p.getCurrentSize() <= max; + } + finally { + stopGrid(); + } } /** * @throws Exception If failed. */ public void testAllowEmptyEntries() throws Exception { - assert false : "ignite-96"; - -// try { -// startGrid(); -// -// MockEntry e1 = new MockEntry("1"); -// -// e1.setValue("val"); -// -// MockEntry e2 = new MockEntry("2"); -// -// MockEntry e3 = new MockEntry("3"); -// -// e3.setValue("val"); -// -// MockEntry e4 = new MockEntry("4"); -// -// MockEntry e5 = new MockEntry("5"); -// -// e5.setValue("val"); -// -// CacheLruEvictionPolicy<String, String> p = policy(); -// -// p.setMaxSize(10); -// -// p.onEntryAccessed(false, e1); -// -// assertFalse(e1.isEvicted()); -// -// p.onEntryAccessed(false, e2); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e2.isEvicted()); -// -// p.onEntryAccessed(false, e3); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e3.isEvicted()); -// -// p.onEntryAccessed(false, e4); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e3.isEvicted()); -// assertFalse(e4.isEvicted()); -// -// p.onEntryAccessed(false, e5); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e3.isEvicted()); -// assertFalse(e5.isEvicted()); -// } -// finally { -// stopAllGrids(); -// } + try { + startGrid(); + + MockEntry e1 = new MockEntry("1"); + + MockEntry e2 = new MockEntry("2"); + + MockEntry e3 = new MockEntry("3"); + + MockEntry e4 = new MockEntry("4"); + + MockEntry e5 = new MockEntry("5"); + + CacheLruEvictionPolicy<String, String> p = policy(); + + p.setMaxSize(10); + + p.onEntryAccessed(false, e1); + + assertFalse(e1.isEvicted()); + + p.onEntryAccessed(false, e2); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + + p.onEntryAccessed(false, e3); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + + p.onEntryAccessed(false, e4); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + + p.onEntryAccessed(false, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e5.isEvicted()); + } + finally { + stopAllGrids(); + } } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/db9d607e/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/GridCacheRandomEvictionPolicySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/GridCacheRandomEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/GridCacheRandomEvictionPolicySelfTest.java index 140204e..d77158a 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/GridCacheRandomEvictionPolicySelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/GridCacheRandomEvictionPolicySelfTest.java @@ -105,62 +105,54 @@ public class GridCacheRandomEvictionPolicySelfTest extends * @throws Exception If failed. */ public void testAllowEmptyEntries() throws Exception { - assert false : "ignite-96"; - -// try { -// startGrid(); -// -// GridCache<String, String> c = cache(); -// -// MockEntry e1 = new MockEntry("1", c); -// -// e1.setValue("val"); -// -// MockEntry e2 = new MockEntry("2", c); -// -// MockEntry e3 = new MockEntry("3", c); -// -// e3.setValue("val"); -// -// MockEntry e4 = new MockEntry("4", c); -// -// MockEntry e5 = new MockEntry("5", c); -// -// e5.setValue("val"); -// -// CacheRandomEvictionPolicy<String, String> p = policy(); -// -// p.setMaxSize(10); -// -// p.onEntryAccessed(false, e1); -// -// assertFalse(e1.isEvicted()); -// -// p.onEntryAccessed(false, e2); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e2.isEvicted()); -// -// p.onEntryAccessed(false, e3); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e3.isEvicted()); -// -// p.onEntryAccessed(false, e4); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e3.isEvicted()); -// assertFalse(e4.isEvicted()); -// -// p.onEntryAccessed(false, e5); -// -// assertFalse(e1.isEvicted()); -// assertFalse(e3.isEvicted()); -// assertFalse(e5.isEvicted()); -// } -// finally { -// stopAllGrids(); -// } + try { + startGrid(); + + IgniteCache<String, String> c = jcache(); + + MockEntry e1 = new MockEntry("1", c); + + MockEntry e2 = new MockEntry("2", c); + + MockEntry e3 = new MockEntry("3", c); + + MockEntry e4 = new MockEntry("4", c); + + MockEntry e5 = new MockEntry("5", c); + + CacheRandomEvictionPolicy<String, String> p = policy(); + + p.setMaxSize(10); + + p.onEntryAccessed(false, e1); + + assertFalse(e1.isEvicted()); + + p.onEntryAccessed(false, e2); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + + p.onEntryAccessed(false, e3); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + + p.onEntryAccessed(false, e4); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + + p.onEntryAccessed(false, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e5.isEvicted()); + } + finally { + stopAllGrids(); + } } /**