From e5d5d08d91fc0564b655c6e45afe0760957bff0c Mon Sep 17 00:00:00 2001 From: agura Date: Mon, 18 May 2015 21:01:36 +0300 Subject: [PATCH] ignite-916 Eviction policy should evict cache entries when memory size limit is reached --- .../ignite/cache/eviction/EvictableEntry.java | 7 + .../ignite/cache/eviction/EvictionPolicy.java | 2 + .../cache/eviction/fifo/FifoEvictionPolicy.java | 117 ++- .../eviction/fifo/FifoEvictionPolicyMBean.java | 22 + .../cache/eviction/lru/LruEvictionPolicy.java | 135 ++- .../cache/eviction/lru/LruEvictionPolicyMBean.java | 38 + .../eviction/random/RandomEvictionPolicy.java | 7 +- .../eviction/sorted/SortedEvictionPolicy.java | 141 ++- .../eviction/sorted/SortedEvictionPolicyMBean.java | 22 + .../processors/cache/CacheEvictableEntryImpl.java | 31 + .../cache/GridCacheConcurrentTxMultiNodeTest.java | 8 +- .../GridCacheConfigurationConsistencySelfTest.java | 14 +- .../cache/GridCacheMemoryModeSelfTest.java | 23 +- .../processors/cache/GridCacheOffHeapTest.java | 5 +- .../processors/cache/GridCacheReloadSelfTest.java | 6 +- .../IgniteCacheP2pUnmarshallingNearErrorTest.java | 6 +- .../cache/IgniteCachePeekModesAbstractTest.java | 5 +- .../GridCacheDhtEvictionNearReadersSelfTest.java | 11 +- .../dht/GridCacheDhtEvictionSelfTest.java | 11 +- .../dht/IgniteCacheMultiTxLockSelfTest.java | 6 +- .../near/GridCachePartitionedEvictionSelfTest.java | 11 +- ...achePartitionedMultiThreadedPutGetSelfTest.java | 6 +- .../cache/eviction/EvictionAbstractTest.java | 1057 ++++++++++++++++++++ .../GridCacheBatchEvictUnswapSelfTest.java | 5 +- ...CacheConcurrentEvictionConsistencySelfTest.java | 82 +- .../GridCacheConcurrentEvictionsSelfTest.java | 29 +- .../GridCacheDistributedEvictionsSelfTest.java | 5 +- .../GridCacheEmptyEntriesAbstractSelfTest.java | 11 +- .../eviction/GridCacheEvictionAbstractTest.java | 484 --------- .../eviction/GridCacheEvictionTouchSelfTest.java | 22 +- .../cache/eviction/GridCacheMockEntry.java | 5 + .../eviction/fifo/FifoEvictionPolicySelfTest.java | 262 +++++ .../GridCacheFifoBatchEvictionPolicySelfTest.java | 384 ------- .../fifo/GridCacheFifoEvictionPolicySelfTest.java | 372 ------- .../lru/GridCacheLruEvictionPolicySelfTest.java | 417 -------- .../GridCacheLruNearEvictionPolicySelfTest.java | 136 --- ...CacheNearOnlyLruNearEvictionPolicySelfTest.java | 171 ---- .../eviction/lru/LruEvictionPolicySelfTest.java | 353 +++++++ .../lru/LruNearEvictionPolicySelfTest.java | 140 +++ .../lru/LruNearOnlyNearEvictionPolicySelfTest.java | 175 ++++ .../GridCacheRandomEvictionPolicySelfTest.java | 258 ----- .../random/RandomEvictionPolicySelfTest.java | 357 +++++++ ...GridCacheSortedBatchEvictionPolicySelfTest.java | 385 ------- ...idCacheSortedEvictionPolicyPerformanceTest.java | 135 --- .../GridCacheSortedEvictionPolicySelfTest.java | 373 ------- .../SortedEvictionPolicyPerformanceTest.java | 134 +++ .../sorted/SortedEvictionPolicySelfTest.java | 266 +++++ .../loadtests/GridCacheMultiNodeLoadTest.java | 5 +- .../GridCachePartitionedAtomicLongLoadTest.java | 6 +- .../loadtests/swap/GridSwapEvictAllBenchmark.java | 6 +- .../IgniteCacheEvictionSelfTestSuite.java | 15 +- .../cache/GridIndexingWithNoopSwapSelfTest.java | 6 +- .../IgniteCacheQueryMultiThreadedSelfTest.java | 11 +- .../cache/ttl/CacheTtlAbstractSelfTest.java | 6 +- .../org/apache/ignite/yardstick/IgniteNode.java | 7 +- 55 files changed, 3480 insertions(+), 3234 deletions(-) create mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/EvictionAbstractTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionAbstractTest.java create mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/FifoEvictionPolicySelfTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoBatchEvictionPolicySelfTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoEvictionPolicySelfTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruEvictionPolicySelfTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruNearEvictionPolicySelfTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheNearOnlyLruNearEvictionPolicySelfTest.java create mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruEvictionPolicySelfTest.java create mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruNearEvictionPolicySelfTest.java create mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruNearOnlyNearEvictionPolicySelfTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/GridCacheRandomEvictionPolicySelfTest.java create mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/RandomEvictionPolicySelfTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedBatchEvictionPolicySelfTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedEvictionPolicyPerformanceTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedEvictionPolicySelfTest.java create mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/SortedEvictionPolicyPerformanceTest.java create mode 100644 modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/SortedEvictionPolicySelfTest.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 d87109f..9f1889a 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 @@ -46,6 +46,13 @@ public interface EvictableEntry extends Cache.Entry { public boolean isCached(); /** + * Returns entry size in bytes. + * + * @return entry size in bytes. + */ + public int size(); + + /** * Gets metadata added by eviction policy. * * @return Metadata value or {@code null}. diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/EvictionPolicy.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/EvictionPolicy.java index f409e9b..07c269d 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/EvictionPolicy.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/EvictionPolicy.java @@ -20,6 +20,7 @@ package org.apache.ignite.cache.eviction; import org.apache.ignite.cache.eviction.fifo.*; import org.apache.ignite.cache.eviction.lru.*; import org.apache.ignite.cache.eviction.random.*; +import org.apache.ignite.cache.eviction.sorted.*; /** * Pluggable cache eviction policy. Usually, implementations will internally order @@ -32,6 +33,7 @@ import org.apache.ignite.cache.eviction.random.*; *
  • {@link LruEvictionPolicy}
  • *
  • {@link RandomEvictionPolicy}
  • *
  • {@link FifoEvictionPolicy}
  • + *
  • {@link SortedEvictionPolicy}
  • * *

    * The eviction policy thread-safety is ensured by Ignition. Implementations of this interface should diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicy.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicy.java index bf8cf0d..7222ae4 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicy.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicy.java @@ -18,18 +18,28 @@ package org.apache.ignite.cache.eviction.fifo; import org.apache.ignite.cache.eviction.*; -import org.apache.ignite.configuration.*; import org.apache.ignite.internal.util.typedef.internal.*; + import org.jsr166.*; import org.jsr166.ConcurrentLinkedDeque8.*; import java.io.*; import java.util.*; +import static org.apache.ignite.configuration.CacheConfiguration.*; + /** * Eviction policy based on {@code First In First Out (FIFO)} algorithm and supports batch eviction. *

    - * The eviction starts when the cache size becomes {@code batchSize} elements greater than the maximum size. + * The eviction starts in the following cases: + *

      + *
    • The cache size becomes {@code batchSize} elements greater than the maximum size.
    • + *
    • + * The size of cache entries in bytes becomes greater than the maximum memory size. + * The size of cache entry calculates as sum of key size and value size. + *
    • + *
    + * Note:Batch eviction is enabled only if maximum memory limit isn't set ({@code maxMemSize == 0}). * {@code batchSize} elements will be evicted in this case. The default {@code batchSize} value is {@code 1}. *

    * This implementation is very efficient since it does not create any additional @@ -41,11 +51,17 @@ public class FifoEvictionPolicy implements EvictionPolicy, FifoEvict private static final long serialVersionUID = 0L; /** Maximum size. */ - private volatile int max = CacheConfiguration.DFLT_CACHE_SIZE; + private volatile int max = DFLT_CACHE_SIZE; /** Batch size. */ private volatile int batchSize = 1; + /** Max memory size. */ + private volatile long maxMemSize; + + /** Memory size. */ + private final LongAdder8 memSize = new LongAdder8(); + /** FIFO queue. */ private final ConcurrentLinkedDeque8> queue = new ConcurrentLinkedDeque8<>(); @@ -63,7 +79,7 @@ public class FifoEvictionPolicy implements EvictionPolicy, FifoEvict * @param max Maximum allowed size of cache before entry will start getting evicted. */ public FifoEvictionPolicy(int max) { - A.ensure(max > 0, "max > 0"); + A.ensure(max >= 0, "max >= 0"); this.max = max; } @@ -75,7 +91,7 @@ public class FifoEvictionPolicy implements EvictionPolicy, FifoEvict * @param batchSize Batch size. */ public FifoEvictionPolicy(int max, int batchSize) { - A.ensure(max > 0, "max > 0"); + A.ensure(max >= 0, "max >= 0"); A.ensure(batchSize > 0, "batchSize > 0"); this.max = max; @@ -97,7 +113,7 @@ public class FifoEvictionPolicy implements EvictionPolicy, FifoEvict * @param max Maximum allowed size of cache before entry will start getting evicted. */ @Override public void setMaxSize(int max) { - A.ensure(max > 0, "max > 0"); + A.ensure(max >= 0, "max >= 0"); this.max = max; } @@ -119,6 +135,23 @@ public class FifoEvictionPolicy implements EvictionPolicy, FifoEvict return queue.size(); } + /** {@inheritDoc} */ + @Override public long getMaxMemSize() { + return maxMemSize; + } + + /** {@inheritDoc} */ + @Override public void setMaxMemSize(long maxMemSize) { + A.ensure(maxMemSize >= 0, "maxMemSize >= 0"); + + this.maxMemSize = maxMemSize; + } + + /** {@inheritDoc} */ + @Override public long getCurrentMemSize() { + return memSize.longValue(); + } + /** * Gets read-only view on internal {@code FIFO} queue in proper order. * @@ -141,8 +174,11 @@ public class FifoEvictionPolicy implements EvictionPolicy, FifoEvict else { Node> node = entry.removeMeta(); - if (node != null) + if (node != null) { queue.unlinkx(node); + + memSize.add(-entry.size()); + } } } @@ -173,11 +209,18 @@ public class FifoEvictionPolicy implements EvictionPolicy, FifoEvict return false; } + memSize.add(entry.size()); + return true; } // If node was unlinked by concurrent shrink() call, we must repeat the whole cycle. else if (!entry.removeMeta(node)) return false; + else { + memSize.add(-entry.size()); + + return true; + } } } @@ -189,38 +232,74 @@ public class FifoEvictionPolicy implements EvictionPolicy, FifoEvict * Shrinks FIFO queue to maximum allowed size. */ private void shrink() { + long maxMem = this.maxMemSize; + + if (maxMem > 0) { + long startMemSize = memSize.longValue(); + + if (startMemSize >= maxMem) + for (long i = maxMem; i < startMemSize && memSize.longValue() > maxMem;) { + int size = shrink0(); + + if (size == -1) + break; + + i += size; + } + } + int max = this.max; - int batchSize = this.batchSize; + if (max > 0) { + int startSize = queue.sizex(); + + // Shrink only if queue is full. + if (startSize >= max + (maxMem > 0 ? 1 : this.batchSize)) + for (int i = max; i < startSize && queue.sizex() > max; i++) + if (shrink0() == -1) + break; + } + } - int startSize = queue.sizex(); + /** + * Tries to remove one item from queue. + * + * @return number of bytes that was free. {@code -1} if queue is empty. + */ + private int shrink0() { + EvictableEntry entry = queue.poll(); - // Shrink only if queue is full. - if (startSize >= max + batchSize) { - for (int i = max; i < startSize && queue.sizex() > max; i++) { - EvictableEntry entry = queue.poll(); + if (entry == null) + return -1; - if (entry == null) - break; + int size = 0; - Node> meta = entry.removeMeta(); + Node> meta = entry.removeMeta(); - if (meta != null && !entry.evict()) - touch(entry); - } + if (meta != null) { + size = entry.size(); + + memSize.add(-size); + + if (!entry.evict()) + touch(entry); } + + return size; } /** {@inheritDoc} */ @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(max); out.writeInt(batchSize); + out.writeLong(maxMemSize); } /** {@inheritDoc} */ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { max = in.readInt(); batchSize = in.readInt(); + maxMemSize = in.readLong(); } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicyMBean.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicyMBean.java index 63a413e..c9a09ec 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicyMBean.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicyMBean.java @@ -63,4 +63,26 @@ public interface FifoEvictionPolicyMBean { */ @MXBeanDescription("Current FIFO queue size.") public int getCurrentSize(); + + /** + * Gets maximum allowed cache size in bytes. + * + * @return maximum allowed cache size in bytes. + */ + @MXBeanDescription("Maximum allowed cache size in bytes.") + public long getMaxMemSize(); + + /** + * Sets maximum allowed cache size in bytes. + */ + @MXBeanDescription("Set maximum allowed cache size in bytes.") + public void setMaxMemSize(long maxMemSize); + + /** + * Gets current queue size in bytes. + * + * @return current queue size in bytes. + */ + @MXBeanDescription("Current FIFO queue size in bytes.") + public long getCurrentMemSize(); } diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicy.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicy.java index 309d577..d56f99a 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicy.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicy.java @@ -18,26 +18,48 @@ package org.apache.ignite.cache.eviction.lru; import org.apache.ignite.cache.eviction.*; -import org.apache.ignite.configuration.*; import org.apache.ignite.internal.util.typedef.internal.*; + import org.jsr166.*; import org.jsr166.ConcurrentLinkedDeque8.*; import java.io.*; import java.util.*; +import static org.apache.ignite.configuration.CacheConfiguration.*; + /** - * Eviction policy based on {@code Least Recently Used (LRU)} algorithm. This - * implementation is very efficient since it is lock-free and does not - * create any additional table-like data structures. The {@code LRU} ordering - * information is maintained by attaching ordering metadata to cache entries. + * Eviction policy based on {@code Least Recently Used (LRU)} algorithm and supports batch eviction. + *

    + * The eviction starts in the following cases: + *

      + *
    • The cache size becomes {@code batchSize} elements greater than the maximum size.
    • + *
    • + * The size of cache entries in bytes becomes greater than the maximum memory size. + * The size of cache entry calculates as sum of key size and value size. + *
    • + *
    + * Note:Batch eviction is enabled only if maximum memory limit isn't set ({@code maxMemSize == 0}). + * {@code batchSize} elements will be evicted in this case. The default {@code batchSize} value is {@code 1}. + + * This implementation is very efficient since it is lock-free and does not create any additional table-like + * data structures. The {@code LRU} ordering information is maintained by attaching ordering metadata to cache entries. */ public class LruEvictionPolicy implements EvictionPolicy, LruEvictionPolicyMBean, Externalizable { /** */ private static final long serialVersionUID = 0L; /** Maximum size. */ - private volatile int max = CacheConfiguration.DFLT_CACHE_SIZE; + private volatile int max = DFLT_CACHE_SIZE; + + /** Batch size. */ + private volatile int batchSize = 1; + + /** Max memory size. */ + private volatile long maxMemSize; + + /** Memory size. */ + private final LongAdder8 memSize = new LongAdder8(); /** Queue. */ private final ConcurrentLinkedDeque8> queue = @@ -56,7 +78,7 @@ public class LruEvictionPolicy implements EvictionPolicy, LruEvictio * @param max Maximum allowed size of cache before entry will start getting evicted. */ public LruEvictionPolicy(int max) { - A.ensure(max > 0, "max > 0"); + A.ensure(max >= 0, "max >= 0"); this.max = max; } @@ -76,16 +98,45 @@ public class LruEvictionPolicy implements EvictionPolicy, LruEvictio * @param max Maximum allowed size of cache before entry will start getting evicted. */ @Override public void setMaxSize(int max) { - A.ensure(max > 0, "max > 0"); + A.ensure(max >= 0, "max >= 0"); this.max = max; } /** {@inheritDoc} */ + @Override public int getBatchSize() { + return batchSize; + } + + /** {@inheritDoc} */ + @Override public void setBatchSize(int batchSize) { + A.ensure(batchSize > 0, "batchSize > 0"); + + this.batchSize = batchSize; + } + + /** {@inheritDoc} */ @Override public int getCurrentSize() { return queue.size(); } + /** {@inheritDoc} */ + @Override public long getMaxMemSize() { + return maxMemSize; + } + + /** {@inheritDoc} */ + @Override public void setMaxMemSize(long maxMemSize) { + A.ensure(maxMemSize >= 0, "maxMemSize >= 0"); + + this.maxMemSize = maxMemSize; + } + + /** {@inheritDoc} */ + @Override public long getCurrentMemSize() { + return memSize.longValue(); + } + /** * Gets read-only view on internal {@code FIFO} queue in proper order. * @@ -107,8 +158,11 @@ public class LruEvictionPolicy implements EvictionPolicy, LruEvictio else { Node> node = entry.removeMeta(); - if (node != null) + if (node != null) { queue.unlinkx(node); + + memSize.add(-entry.size()); + } } } @@ -139,11 +193,18 @@ public class LruEvictionPolicy implements EvictionPolicy, LruEvictio return false; } + memSize.add(entry.size()); + return true; } // If node was unlinked by concurrent shrink() call, we must repeat the whole cycle. else if (!entry.removeMeta(node)) return false; + else { + memSize.add(-entry.size()); + + return true; + } } } else if (queue.unlinkx(node)) { @@ -163,31 +224,73 @@ public class LruEvictionPolicy implements EvictionPolicy, LruEvictio * Shrinks queue to maximum allowed size. */ private void shrink() { + long maxMem = this.maxMemSize; + + if (maxMem > 0) { + long startMemSize = memSize.longValue(); + + if (startMemSize >= maxMem) + for (long i = maxMem; i < startMemSize && memSize.longValue() > maxMem;) { + int size = shrink0(); + + if (size == -1) + break; + + i += size; + } + } + int max = this.max; - int startSize = queue.sizex(); + if (max > 0) { + int startSize = queue.sizex(); - for (int i = 0; i < startSize && queue.sizex() > max; i++) { - EvictableEntry entry = queue.poll(); + if (startSize >= max + (maxMem > 0 ? 1 : this.batchSize)) + for (int i = max; i < startSize && queue.sizex() > max; i++) + if (shrink0() == -1) + break; + } + } - if (entry == null) - break; + /** + * Tries to remove one item from queue. + * + * @return number of bytes that was free. {@code -1} if queue is empty. + */ + private int shrink0() { + EvictableEntry entry = queue.poll(); - Node> meta = entry.removeMeta(); + if (entry == null) + return -1; - if (meta != null && !entry.evict()) + int size = 0; + + Node> meta = entry.removeMeta(); + + if (meta != null) { + size = entry.size(); + + memSize.add(-size); + + if (!entry.evict()) touch(entry); } + + return size; } /** {@inheritDoc} */ @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(max); + out.writeInt(batchSize); + out.writeLong(maxMemSize); } /** {@inheritDoc} */ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { max = in.readInt(); + batchSize = in.readInt(); + maxMemSize = in.readLong(); } /** {@inheritDoc} */ diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicyMBean.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicyMBean.java index c243374..69347ee 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicyMBean.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/lru/LruEvictionPolicyMBean.java @@ -41,10 +41,48 @@ public interface LruEvictionPolicyMBean { public void setMaxSize(int max); /** + * Gets batch size. + * + * @return batch size. + */ + @MXBeanDescription("Batch size.") + public int getBatchSize(); + + /** + * Sets batch size. + * + * @param batchSize Batch size. + */ + @MXBeanDescription("Set batch size.") + public void setBatchSize(int batchSize); + + /** * Gets current queue size. * * @return Current queue size. */ @MXBeanDescription("Current queue size.") public int getCurrentSize(); + + /** + * Gets maximum allowed cache size in bytes. + * + * @return maximum allowed cache size in bytes. + */ + @MXBeanDescription("Maximum allowed cache size in bytes.") + public long getMaxMemSize(); + + /** + * Sets maximum allowed cache size in bytes. + */ + @MXBeanDescription("Set maximum allowed cache size in bytes.") + public void setMaxMemSize(long maxMemSize); + + /** + * Gets current queue size in bytes. + * + * @return current queue size in bytes. + */ + @MXBeanDescription("Current queue size in bytes.") + public long getCurrentMemSize(); } diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/random/RandomEvictionPolicy.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/random/RandomEvictionPolicy.java index c88b31d..0d840e3 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/random/RandomEvictionPolicy.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/random/RandomEvictionPolicy.java @@ -19,19 +19,20 @@ 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.*; import java.io.*; +import static org.apache.ignite.configuration.CacheConfiguration.*; + /** * Cache eviction policy which will select random cache entry for eviction if cache * size exceeds the {@link #getMaxSize()} parameter. This implementation is * extremely light weight, lock-free, and does not create any data structures to maintain * any order for eviction. *

    - * Random eviction will provide the best performance over any key set in which every + * Random eviction will provide the best performance over any key queue in which every * key has the same probability of being accessed. */ public class RandomEvictionPolicy implements EvictionPolicy, RandomEvictionPolicyMBean, Externalizable { @@ -39,7 +40,7 @@ public class RandomEvictionPolicy implements EvictionPolicy, RandomE private static final long serialVersionUID = 0L; /** Maximum size. */ - private volatile int max = CacheConfiguration.DFLT_CACHE_SIZE; + private volatile int max = DFLT_CACHE_SIZE; /** * Constructs random eviction policy with all defaults. diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicy.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicy.java index 7965c97..abfc1d0 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicy.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicy.java @@ -34,7 +34,15 @@ import static org.apache.ignite.configuration.CacheConfiguration.*; /** * Cache eviction policy which will select the minimum cache entry for eviction. *

    - * The eviction starts when the cache size becomes {@code batchSize} elements greater than the maximum size. + * The eviction starts in the following cases: + *

      + *
    • The cache size becomes {@code batchSize} elements greater than the maximum size.
    • + *
    • + * The size of cache entries in bytes becomes greater than the maximum memory size. + * The size of cache entry calculates as sum of key size and value size. + *
    • + *
    + * Note:Batch eviction is enabled only if maximum memory limit isn't set ({@code maxMemSize == 0}). * {@code batchSize} elements will be evicted in this case. The default {@code batchSize} value is {@code 1}. *

    * Entries comparison based on {@link Comparator} instance if provided. @@ -48,18 +56,24 @@ public class SortedEvictionPolicy implements EvictionPolicy, SortedE private static final long serialVersionUID = 0L; /** Maximum size. */ - private volatile int max; + private volatile int max = DFLT_CACHE_SIZE; /** Batch size. */ private volatile int batchSize = 1; + /** Max memory size. */ + private volatile long maxMemSize; + + /** Memory size. */ + private final LongAdder8 memSize = new LongAdder8(); + /** Comparator. */ private Comparator> comp; /** Order. */ private final AtomicLong orderCnt = new AtomicLong(); - /** Backed sorted set. */ + /** Backed sorted queue. */ private final GridConcurrentSkipListSetEx set; /** @@ -96,7 +110,7 @@ public class SortedEvictionPolicy implements EvictionPolicy, SortedE * @param comp Entries comparator. */ public SortedEvictionPolicy(int max, int batchSize, @Nullable Comparator> comp) { - A.ensure(max > 0, "max > 0"); + A.ensure(max >= 0, "max >= 0"); A.ensure(batchSize > 0, "batchSize > 0"); this.max = max; @@ -106,6 +120,16 @@ public class SortedEvictionPolicy implements EvictionPolicy, SortedE } /** + * Constructs sorted eviction policy with given maximum size and given entry comparator. + * + * @param comp Entries comparator. + */ + public SortedEvictionPolicy(@Nullable Comparator> comp) { + this.comp = comp == null ? new DefaultHolderComparator() : new HolderComparator<>(comp); + this.set = new GridConcurrentSkipListSetEx<>(this.comp); + } + + /** * Gets maximum allowed size of cache before entry will start getting evicted. * * @return Maximum allowed size of cache before entry will start getting evicted. @@ -120,7 +144,7 @@ public class SortedEvictionPolicy implements EvictionPolicy, SortedE * @param max Maximum allowed size of cache before entry will start getting evicted. */ @Override public void setMaxSize(int max) { - A.ensure(max > 0, "max > 0"); + A.ensure(max >= 0, "max >= 0"); this.max = max; } @@ -142,12 +166,29 @@ public class SortedEvictionPolicy implements EvictionPolicy, SortedE return set.sizex(); } + /** {@inheritDoc} */ + @Override public long getMaxMemSize() { + return maxMemSize; + } + + /** {@inheritDoc} */ + @Override public void setMaxMemSize(long maxMemSize) { + A.ensure(maxMemSize >= 0, "maxMemSize >= 0"); + + this.maxMemSize = maxMemSize; + } + + /** {@inheritDoc} */ + @Override public long getCurrentMemSize() { + return memSize.longValue(); + } + /** - * Gets read-only view of backed set in proper order. + * Gets read-only view of backed queue in proper order. * - * @return Read-only view of backed set. + * @return Read-only view of backed queue. */ - public Collection> set() { + public Collection> queue() { Set> cp = new LinkedHashSet<>(); for (Holder holder : set) @@ -168,19 +209,22 @@ public class SortedEvictionPolicy implements EvictionPolicy, SortedE else { Holder holder = entry.removeMeta(); - if (holder != null) + if (holder != null) { removeHolder(holder); + + memSize.add(-entry.size()); + } } } /** * @param entry Entry to touch. - * @return {@code True} if backed set has been changed by this call. + * @return {@code True} if backed queue has been changed by this call. */ private boolean touch(EvictableEntry entry) { Holder holder = entry.meta(); - // Entry has not been add yet to backed set.. + // Entry has not been add yet to backed queue.. if (holder == null) { while (true) { holder = new Holder<>(entry, orderCnt.incrementAndGet()); @@ -188,7 +232,7 @@ public class SortedEvictionPolicy implements EvictionPolicy, SortedE set.add(holder); if (entry.putMetaIfAbsent(holder) != null) { - // Was concurrently added, need to remove it from set. + // Was concurrently added, need to remove it from queue. removeHolder(holder); // Set has not been changed. @@ -196,17 +240,24 @@ public class SortedEvictionPolicy implements EvictionPolicy, SortedE } else if (holder.order > 0) { if (!entry.isCached()) { - // Was concurrently evicted, need to remove it from set. + // Was concurrently evicted, need to remove it from queue. removeHolder(holder); return false; } + memSize.add(entry.size()); + return true; } // If holder was removed by concurrent shrink() call, we must repeat the whole cycle. else if (!entry.removeMeta(holder)) return false; + else { + memSize.add(-entry.size()); + + return true; + } } } @@ -215,34 +266,71 @@ public class SortedEvictionPolicy implements EvictionPolicy, SortedE } /** - * Shrinks backed set to maximum allowed size. + * Shrinks backed queue to maximum allowed size. */ private void shrink() { - int max = this.max; + long maxMem = this.maxMemSize; + + if (maxMem > 0) { + long startMemSize = memSize.longValue(); - int batchSize = this.batchSize; + if (startMemSize >= maxMem) + for (long i = maxMem; i < startMemSize && memSize.longValue() > maxMem;) { + int size = shrink0(); - int startSize = set.sizex(); + if (size == -1) + break; - if (startSize >= max + batchSize) { - for (int i = max; i < startSize && set.sizex() > max; i++) { - Holder h = set.pollFirst(); + i += size; + } + } - if (h == null) - break; + int max = this.max; - EvictableEntry entry = h.entry; + if (max > 0) { + int startSize = set.sizex(); - if (h.order > 0 && entry.removeMeta(h) && !entry.evict()) - touch(entry); + if (startSize >= max + (maxMem > 0 ? 1 : this.batchSize)) { + for (int i = max; i < startSize && set.sizex() > max; i++) { + if (shrink0() == -1) + break; + } } } } + /** + * Tries to remove one item from queue. + * + * @return number of bytes that was free. {@code -1} if queue is empty. + */ + private int shrink0() { + Holder h = set.pollFirst(); + + if (h == null) + return -1; + + int size = 0; + + EvictableEntry entry = h.entry; + + if (h.order > 0 && entry.removeMeta(h)) { + size = entry.size(); + + memSize.add(-size); + + if (!entry.evict()) + touch(entry); + } + + return size; + } + /** {@inheritDoc} */ @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(max); out.writeInt(batchSize); + out.writeLong(maxMemSize); out.writeObject(comp); } @@ -251,11 +339,12 @@ public class SortedEvictionPolicy implements EvictionPolicy, SortedE @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { max = in.readInt(); batchSize = in.readInt(); + maxMemSize = in.readLong(); comp = (Comparator>)in.readObject(); } /** - * Removes holder from backed set and marks holder as removed. + * Removes holder from backed queue and marks holder as removed. * * @param holder Holder. */ diff --git a/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicyMBean.java b/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicyMBean.java index bc696ff..aada1ce 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicyMBean.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicyMBean.java @@ -63,4 +63,26 @@ public interface SortedEvictionPolicyMBean { */ @MXBeanDescription("Current sorted key set size.") public int getCurrentSize(); + + /** + * Gets maximum allowed cache size in bytes. + * + * @return maximum allowed cache size in bytes. + */ + @MXBeanDescription("Maximum allowed cache size in bytes.") + public long getMaxMemSize(); + + /** + * Sets maximum allowed cache size in bytes. + */ + @MXBeanDescription("Set maximum allowed cache size in bytes.") + public void setMaxMemSize(long maxMemSize); + + /** + * Gets current sorted entries queue size in bytes. + * + * @return current sorted entries queue size in bytes. + */ + @MXBeanDescription("Current sorted entries set size in bytes.") + public long getCurrentMemSize(); } diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheEvictableEntryImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheEvictableEntryImpl.java index 5d6062e..7a3fbee 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheEvictableEntryImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheEvictableEntryImpl.java @@ -23,6 +23,7 @@ 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.*; import java.util.*; @@ -91,6 +92,36 @@ public class CacheEvictableEntryImpl implements EvictableEntry { } /** {@inheritDoc} */ + public int size() { + try { + GridCacheContext cctx = cached.context(); + + KeyCacheObject key = cached.key(); + + byte[] keyBytes = key.valueBytes(cctx.cacheObjectContext()); + + byte[] valBytes = null; + + if (cctx.useOffheapEntry()) + valBytes = cctx.offheap().get(cctx.swap().spaceName(), cached.partition(), key, keyBytes); + else { + CacheObject cacheObj = cached.valueBytes(); + + if (cacheObj != null) + valBytes = cacheObj.valueBytes(cctx.cacheObjectContext()); + } + + return valBytes == null ? keyBytes.length : keyBytes.length + valBytes.length; + } + catch (GridCacheEntryRemovedException e) { + return 0; + } + catch (IgniteCheckedException e) { + throw new IgniteException(e); + } + } + + /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public V getValue() { try { diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentTxMultiNodeTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentTxMultiNodeTest.java index 8a1ae78..bba4ad9 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentTxMultiNodeTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentTxMultiNodeTest.java @@ -26,7 +26,6 @@ import org.apache.ignite.cluster.*; import org.apache.ignite.compute.*; import org.apache.ignite.configuration.*; import org.apache.ignite.internal.*; -import org.apache.ignite.internal.processors.cache.query.*; import org.apache.ignite.internal.processors.cache.distributed.dht.*; import org.apache.ignite.internal.processors.cache.distributed.near.*; import org.apache.ignite.internal.util.*; @@ -38,6 +37,7 @@ import org.apache.ignite.spi.discovery.tcp.ipfinder.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; import org.apache.ignite.testframework.junits.common.*; import org.apache.ignite.transactions.*; + import org.jetbrains.annotations.*; import java.io.*; @@ -110,7 +110,11 @@ public class GridCacheConcurrentTxMultiNodeTest extends GridCommonAbstractTest { CacheConfiguration cc = defaultCacheConfiguration(); cc.setCacheMode(mode); - cc.setEvictionPolicy(new LruEvictionPolicy(1000)); + + LruEvictionPolicy plc = new LruEvictionPolicy(); + plc.setMaxSize(1000); + + cc.setEvictionPolicy(plc); cc.setEvictSynchronized(false); cc.setSwapEnabled(false); cc.setWriteSynchronizationMode(FULL_SYNC); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConfigurationConsistencySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConfigurationConsistencySelfTest.java index 44171a8..445a5e1 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConfigurationConsistencySelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheConfigurationConsistencySelfTest.java @@ -442,7 +442,12 @@ public class GridCacheConfigurationConsistencySelfTest extends GridCommonAbstrac /** {@inheritDoc} */ @Override public Void apply(CacheConfiguration cfg) { cfg.setEvictSynchronized(true); - cfg.setEvictionPolicy(new FifoEvictionPolicy(100)); + + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + + plc.setMaxSize(100); + + cfg.setEvictionPolicy(plc); return null; } }, @@ -450,7 +455,12 @@ public class GridCacheConfigurationConsistencySelfTest extends GridCommonAbstrac /** {@inheritDoc} */ @Override public Void apply(CacheConfiguration cfg) { cfg.setEvictSynchronized(false); - cfg.setEvictionPolicy(new FifoEvictionPolicy(100)); + + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + + plc.setMaxSize(100); + + cfg.setEvictionPolicy(plc); return null; } } diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheMemoryModeSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheMemoryModeSelfTest.java index 08e35ce..b4e523e 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheMemoryModeSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheMemoryModeSelfTest.java @@ -28,6 +28,7 @@ import org.apache.ignite.spi.discovery.tcp.ipfinder.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; import org.apache.ignite.spi.swapspace.file.*; import org.apache.ignite.testframework.junits.common.*; + import org.junit.*; import java.util.*; @@ -81,6 +82,11 @@ public class GridCacheMemoryModeSelfTest extends GridCommonAbstractTest { return cfg; } + /** + * Returns cache configuration. + * + * @return cache configuration. + */ protected CacheConfiguration cacheConfiguration() { CacheConfiguration cacheCfg = defaultCacheConfiguration(); @@ -89,7 +95,16 @@ public class GridCacheMemoryModeSelfTest extends GridCommonAbstractTest { cacheCfg.setSwapEnabled(swapEnabled); cacheCfg.setCacheMode(mode); cacheCfg.setMemoryMode(memoryMode); - cacheCfg.setEvictionPolicy(maxOnheapSize == Integer.MAX_VALUE ? null : new LruEvictionPolicy(maxOnheapSize)); + + LruEvictionPolicy plc = null; + + if (maxOnheapSize != Integer.MAX_VALUE) { + plc = new LruEvictionPolicy(); + plc.setMaxSize(maxOnheapSize); + } + + cacheCfg.setEvictionPolicy(plc); + cacheCfg.setAtomicityMode(atomicity); cacheCfg.setOffHeapMaxMemory(offheapSize); @@ -199,7 +214,8 @@ public class GridCacheMemoryModeSelfTest extends GridCommonAbstractTest { * @param swapEmpty Swap is empty. * @throws Exception If failed. */ - private void doTestPutAndPutAll(int cache, int offheapSwap, boolean offheapEmpty, boolean swapEmpty) throws Exception { + private void doTestPutAndPutAll(int cache, int offheapSwap, boolean offheapEmpty, boolean swapEmpty) + throws Exception { final int all = cache + offheapSwap; // put @@ -231,7 +247,8 @@ public class GridCacheMemoryModeSelfTest extends GridCommonAbstractTest { * @param x Cache modifier. * @throws IgniteCheckedException If failed. */ - void doTest(int cache, int offheapSwap, boolean offheapEmpty, boolean swapEmpty, CIX1> x) throws Exception { + void doTest(int cache, int offheapSwap, boolean offheapEmpty, boolean swapEmpty, + CIX1> x) throws Exception { ipFinder = new TcpDiscoveryVmIpFinder(true); startGrid(); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapTest.java index d69f7fa..b0f07f0 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapTest.java @@ -76,7 +76,10 @@ public class GridCacheOffHeapTest extends GridCommonAbstractTest { cacheCfg.setStartSize(startSize); if (onheap > 0) { - cacheCfg.setEvictionPolicy(new FifoEvictionPolicy(onheap)); + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(onheap); + + cacheCfg.setEvictionPolicy(plc); cacheCfg.setOffHeapMaxMemory(80 * 1024L * 1024L * 1024L); // 10GB } diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheReloadSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheReloadSelfTest.java index e2cdd08..b6bfbc7 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheReloadSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheReloadSelfTest.java @@ -74,7 +74,11 @@ public class GridCacheReloadSelfTest extends GridCommonAbstractTest { CacheConfiguration cacheCfg = defaultCacheConfiguration(); cacheCfg.setName(CACHE_NAME); cacheCfg.setCacheMode(cacheMode); - cacheCfg.setEvictionPolicy(new LruEvictionPolicy(MAX_CACHE_ENTRIES)); + + LruEvictionPolicy plc = new LruEvictionPolicy(); + plc.setMaxSize(MAX_CACHE_ENTRIES); + + cacheCfg.setEvictionPolicy(plc); cacheCfg.setNearConfiguration(nearEnabled ? new NearCacheConfiguration() : null); final CacheStore store = new CacheStoreAdapter() { diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheP2pUnmarshallingNearErrorTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheP2pUnmarshallingNearErrorTest.java index 732d12d..30f7b92 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheP2pUnmarshallingNearErrorTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheP2pUnmarshallingNearErrorTest.java @@ -36,7 +36,11 @@ public class IgniteCacheP2pUnmarshallingNearErrorTest extends IgniteCacheP2pUnma cfg.getCacheConfiguration()[0].setEvictMaxOverflowRatio(0); cfg.getCacheConfiguration()[0].setEvictSynchronized(true); cfg.getCacheConfiguration()[0].setEvictSynchronizedKeyBufferSize(1); - cfg.getCacheConfiguration()[0].setEvictionPolicy(new FifoEvictionPolicy(1)); + + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(1); + + cfg.getCacheConfiguration()[0].setEvictionPolicy(plc); return cfg; } diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCachePeekModesAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCachePeekModesAbstractTest.java index 7cd8414..448f171 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCachePeekModesAbstractTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCachePeekModesAbstractTest.java @@ -93,7 +93,10 @@ public abstract class IgniteCachePeekModesAbstractTest extends IgniteCacheAbstra if (hasNearCache()) ccfg.setNearConfiguration(new NearCacheConfiguration()); - ccfg.setEvictionPolicy(new FifoEvictionPolicy(HEAP_ENTRIES)); + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(HEAP_ENTRIES); + + ccfg.setEvictionPolicy(plc); return ccfg; } diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheDhtEvictionNearReadersSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheDhtEvictionNearReadersSelfTest.java index aff5512..7adeba8 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheDhtEvictionNearReadersSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheDhtEvictionNearReadersSelfTest.java @@ -77,11 +77,18 @@ public class GridCacheDhtEvictionNearReadersSelfTest extends GridCommonAbstractT // Set eviction queue size explicitly. cacheCfg.setEvictSynchronizedKeyBufferSize(1); cacheCfg.setEvictMaxOverflowRatio(0); - cacheCfg.setEvictionPolicy(new FifoEvictionPolicy(10)); + + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(10); + + cacheCfg.setEvictionPolicy(plc); NearCacheConfiguration nearCfg = new NearCacheConfiguration(); - nearCfg.setNearEvictionPolicy(new FifoEvictionPolicy(10)); + FifoEvictionPolicy nearPlc = new FifoEvictionPolicy(); + nearPlc.setMaxSize(10); + + nearCfg.setNearEvictionPolicy(nearPlc); cacheCfg.setNearConfiguration(nearCfg); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheDhtEvictionSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheDhtEvictionSelfTest.java index 97e8657..8eada9c 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheDhtEvictionSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridCacheDhtEvictionSelfTest.java @@ -76,14 +76,21 @@ public class GridCacheDhtEvictionSelfTest extends GridCommonAbstractTest { cacheCfg.setBackups(1); NearCacheConfiguration nearCfg = new NearCacheConfiguration(); - nearCfg.setNearEvictionPolicy(new FifoEvictionPolicy(10000)); + + FifoEvictionPolicy nearPlc = new FifoEvictionPolicy(); + nearPlc.setMaxSize(10000); + + nearCfg.setNearEvictionPolicy(nearPlc); cacheCfg.setNearConfiguration(nearCfg); // Set eviction queue size explicitly. cacheCfg.setEvictMaxOverflowRatio(0); cacheCfg.setEvictSynchronizedKeyBufferSize(1); - cacheCfg.setEvictionPolicy(new FifoEvictionPolicy(10000)); + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(10000); + + cacheCfg.setEvictionPolicy(plc); cfg.setCacheConfiguration(cacheCfg); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/IgniteCacheMultiTxLockSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/IgniteCacheMultiTxLockSelfTest.java index 5983c1b..77fe8cf 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/IgniteCacheMultiTxLockSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/dht/IgniteCacheMultiTxLockSelfTest.java @@ -71,7 +71,11 @@ public class IgniteCacheMultiTxLockSelfTest extends GridCommonAbstractTest { ccfg.setBackups(2); ccfg.setCacheMode(CacheMode.PARTITIONED); ccfg.setStartSize(100000); - ccfg.setEvictionPolicy(new LruEvictionPolicy(100000)); + + LruEvictionPolicy plc = new LruEvictionPolicy(); + plc.setMaxSize(100000); + + ccfg.setEvictionPolicy(plc); ccfg.setEvictSynchronized(true); c.setCacheConfiguration(ccfg); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCachePartitionedEvictionSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCachePartitionedEvictionSelfTest.java index 28ad7a8..c0d36e2 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCachePartitionedEvictionSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCachePartitionedEvictionSelfTest.java @@ -78,8 +78,15 @@ public class GridCachePartitionedEvictionSelfTest extends GridCacheAbstractSelfT cc.setCacheMode(PARTITIONED); cc.setWriteSynchronizationMode(FULL_SYNC); - cc.setEvictionPolicy(new FifoEvictionPolicy(EVICT_CACHE_SIZE)); - cc.getNearConfiguration().setNearEvictionPolicy(new FifoEvictionPolicy(EVICT_CACHE_SIZE)); + + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(EVICT_CACHE_SIZE); + cc.setEvictionPolicy(plc); + + FifoEvictionPolicy nearPlc = new FifoEvictionPolicy(); + nearPlc.setMaxSize(EVICT_CACHE_SIZE); + cc.getNearConfiguration().setNearEvictionPolicy(nearPlc); + cc.setSwapEnabled(false); // We set 1 backup explicitly. diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCachePartitionedMultiThreadedPutGetSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCachePartitionedMultiThreadedPutGetSelfTest.java index 8131d3e..1c45de8 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCachePartitionedMultiThreadedPutGetSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/near/GridCachePartitionedMultiThreadedPutGetSelfTest.java @@ -66,7 +66,11 @@ public class GridCachePartitionedMultiThreadedPutGetSelfTest extends GridCommonA cc.setCacheMode(PARTITIONED); cc.setBackups(1); cc.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC); - cc.setEvictionPolicy(new FifoEvictionPolicy<>(1000)); + + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxMemSize(1000); + + cc.setEvictionPolicy(plc); cc.setSwapEnabled(false); cc.setAtomicityMode(TRANSACTIONAL); cc.setEvictSynchronized(false); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/EvictionAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/EvictionAbstractTest.java new file mode 100644 index 0000000..c2a599d --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/EvictionAbstractTest.java @@ -0,0 +1,1057 @@ +/* + * 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.eviction; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.cache.eviction.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.*; +import org.apache.ignite.internal.processors.cache.distributed.dht.colocated.*; +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.*; +import org.apache.ignite.testframework.junits.common.*; +import org.apache.ignite.transactions.*; + +import org.jetbrains.annotations.*; + +import javax.cache.*; +import java.lang.reflect.*; +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.*; + +import static org.apache.ignite.cache.CacheAtomicityMode.*; +import static org.apache.ignite.cache.CacheMode.*; +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*; +import static org.apache.ignite.events.EventType.*; +import static org.apache.ignite.internal.processors.cache.eviction.EvictionAbstractTest.EvictionPolicyProxy.*; +import static org.apache.ignite.transactions.TransactionConcurrency.*; +import static org.apache.ignite.transactions.TransactionIsolation.*; + +/** + * Base class for eviction tests. + */ +public abstract class EvictionAbstractTest> + extends GridCommonAbstractTest { + /** IP finder. */ + protected static final TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); + + /** Put entry size. */ + protected static final int PUT_ENTRY_SIZE = 10; + + /** Replicated cache. */ + protected CacheMode mode = REPLICATED; + + /** Near enabled flag. */ + protected boolean nearEnabled; + + /** Evict backup sync. */ + protected boolean evictSync; + + /** Evict near sync. */ + protected boolean evictNearSync = true; + + /** Policy max. */ + protected int plcMax = 10; + + /** Policy batch size. */ + protected int plcBatchSize = 1; + + /** Policy max memory size. */ + protected long plcMaxMemSize = 0; + + /** Near policy max. */ + protected int nearMax = 3; + + /** Synchronous commit. */ + protected boolean syncCommit; + + /** */ + protected int gridCnt = 2; + + /** */ + protected EvictionFilter filter; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration c = super.getConfiguration(gridName); + + CacheConfiguration cc = defaultCacheConfiguration(); + + cc.setCacheMode(mode); + cc.setEvictionPolicy(createPolicy(plcMax)); + cc.setEvictSynchronized(evictSync); + cc.setSwapEnabled(false); + cc.setWriteSynchronizationMode(syncCommit ? FULL_SYNC : FULL_ASYNC); + cc.setStartSize(plcMax); + cc.setAtomicityMode(TRANSACTIONAL); + + if (nearEnabled) { + NearCacheConfiguration nearCfg = new NearCacheConfiguration(); + + nearCfg.setNearEvictionPolicy(createNearPolicy(nearMax)); + + cc.setNearConfiguration(nearCfg); + } + else + cc.setNearConfiguration(null); + + if (mode == PARTITIONED) + cc.setBackups(1); + + if (filter != null) + cc.setEvictionFilter(filter); + + c.setCacheConfiguration(cc); + + TcpDiscoverySpi disco = new TcpDiscoverySpi(); + + disco.setIpFinder(ipFinder); + + c.setDiscoverySpi(disco); + + c.setIncludeEventTypes(EVT_TASK_FAILED, EVT_TASK_FINISHED, EVT_JOB_MAPPED); + + c.setIncludeProperties(); + + return c; + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + filter = null; + + super.afterTestsStopped(); + } + + /** + * @throws Exception If failed. + */ + public void testMaxSizePolicy() throws Exception { + plcMax = 3; + plcMaxMemSize = 0; + plcBatchSize = 1; + + doTestPolicy(); + } + + /** + * @throws Exception If failed. + */ + public void testMaxSizePolicyWithBatch() throws Exception { + plcMax = 3; + plcMaxMemSize = 0; + plcBatchSize = 2; + + doTestPolicyWithBatch(); + } + + /** + * @throws Exception If failed. + */ + public void testMaxMemSizePolicy() throws Exception { + plcMax = 0; + plcMaxMemSize = 3 * MockEntry.ENTRY_SIZE; + plcBatchSize = 1; + + doTestPolicy(); + } + + /** + * Batch ignored when {@code maxSize > 0} and {@code maxMemSize > 0}. + * + * @throws Exception If failed. + */ + public void testMaxMemSizePolicyWithBatch() throws Exception { + plcMax = 3; + plcMaxMemSize = 10 * MockEntry.ENTRY_SIZE; + plcBatchSize = 2; + + doTestPolicy(); + } + + /** + * @throws Exception If failed. + */ + public void testMaxSizeMemory() throws Exception { + int max = 10; + + plcMax = max; + plcMaxMemSize = 0; + plcBatchSize = 1; + + doTestMemory(max); + } + + /** + * @throws Exception If failed. + */ + public void testMaxSizeMemoryWithBatch() throws Exception { + int max = 10; + + plcMax = max; + plcMaxMemSize = 0; + plcBatchSize = 2; + + doTestMemory(max); + } + + /** + * @throws Exception If failed. + */ + public void testMaxMemSizeMemory() throws Exception { + int max = 10; + + plcMax = 0; + plcMaxMemSize = max * MockEntry.ENTRY_SIZE; + plcBatchSize = 1; + + doTestMemory(max); + } + + /** + * @throws Exception If failed. + */ + public void testMaxSizeRandom() throws Exception { + plcMax = 10; + plcMaxMemSize = 0; + plcBatchSize = 1; + + doTestRandom(); + } + + /** + * @throws Exception If failed. + */ + public void testMaxSizeRandomWithBatch() throws Exception { + plcMax = 10; + plcMaxMemSize = 0; + plcBatchSize = 2; + + doTestRandom(); + } + + /** + * @throws Exception If failed. + */ + public void testMaxMemSizeRandom() throws Exception { + plcMax = 0; + plcMaxMemSize = 10 * MockEntry.KEY_SIZE; + plcBatchSize = 1; + + doTestRandom(); + } + + /** + * @throws Exception If failed. + */ + public void testMaxSizeAllowEmptyEntries() throws Exception { + plcMax = 10; + plcMaxMemSize = 0; + plcBatchSize = 1; + + doTestAllowEmptyEntries(); + } + + /** + * @throws Exception If failed. + */ + public void testMaxSizeAllowEmptyEntriesWithBatch() throws Exception { + plcMax = 10; + plcMaxMemSize = 0; + plcBatchSize = 2; + + doTestAllowEmptyEntries(); + } + + /** + * @throws Exception If failed. + */ + public void testMaxMemSizeAllowEmptyEntries() throws Exception { + plcMax = 0; + plcMaxMemSize = 10 * MockEntry.KEY_SIZE; + plcBatchSize = 1; + + doTestAllowEmptyEntries(); + } + + /** + * @throws Exception If failed. + */ + public void testMaxSizePut() throws Exception { + plcMax = 100; + plcBatchSize = 1; + plcMaxMemSize = 0; + + doTestPut(plcMax); + } + + /** + * @throws Exception If failed. + */ + public void testMaxSizePutWithBatch() throws Exception { + plcMax = 100; + plcBatchSize = 2; + plcMaxMemSize = 0; + + doTestPut(plcMax); + } + + /** + * @throws Exception If failed. + */ + public void testMaxMemSizePut() throws Exception { + int max = 100; + + plcMax = 0; + plcBatchSize = 2; + plcMaxMemSize = max * PUT_ENTRY_SIZE; + + doTestPut(max); + } + + /** + * Tests policy behaviour. + * + * @throws Exception If failed. + */ + protected abstract void doTestPolicy() throws Exception; + + /** + * Tests policy behaviour with batch enabled. + * + * @throws Exception If failed. + */ + protected abstract void doTestPolicyWithBatch() throws Exception; + + /** + * @throws Exception If failed. + */ + protected void doTestAllowEmptyEntries() 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"); + + EvictionPolicyProxy p = proxy(policy()); + + p.onEntryAccessed(false, e1); + + assertFalse(e1.isEvicted()); + + check(p.queue().size(), MockEntry.KEY_SIZE); + + p.onEntryAccessed(false, e2); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + + check(p.queue().size(), MockEntry.KEY_SIZE); + + p.onEntryAccessed(false, e3); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + + check(p.queue().size(), MockEntry.KEY_SIZE); + + p.onEntryAccessed(false, e4); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + + check(p.queue().size(), MockEntry.KEY_SIZE); + + p.onEntryAccessed(false, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e5.isEvicted()); + + check(p.queue().size(), MockEntry.KEY_SIZE); + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + protected void doTestMemory(int max) throws Exception { + try { + startGrid(); + + EvictionPolicyProxy p = proxy(policy()); + + int cnt = max + plcBatchSize; + + for (int i = 0; i < cnt; i++) + p.onEntryAccessed(false, new MockEntry(Integer.toString(i), Integer.toString(i))); + + info(p); + + check(max, MockEntry.ENTRY_SIZE); + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + protected void doTestRandom() throws Exception { + try { + startGrid(); + + EvictionPolicyProxy p = proxy(policy()); + + int max = 10; + + Random rand = new Random(); + + int keys = 31; + + MockEntry[] entries = new MockEntry[keys]; + + for (int i = 0; i < entries.length; i++) + entries[i] = new MockEntry(Integer.toString(i)); + + int runs = 5000000; + + for (int i = 0; i < runs; i++) { + boolean rmv = rand.nextBoolean(); + + int j = rand.nextInt(entries.length); + + MockEntry e = entry(entries, j); + + if (rmv) + entries[j] = new MockEntry(Integer.toString(j)); + + p.onEntryAccessed(rmv, e); + } + + info(p); + + assertTrue(p.getCurrentSize() <= (plcMaxMemSize > 0 ? max : max + plcBatchSize)); + assertTrue(p.getCurrentMemSize() <= (plcMaxMemSize > 0 ? max : max + plcBatchSize) * MockEntry.KEY_SIZE); + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + protected void doTestPut(int max) throws Exception { + mode = LOCAL; + syncCommit = true; + + try { + Ignite ignite = startGrid(); + + IgniteCache 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 > max && cacheSize < min) { + min = cacheSize; + minIdx = i; + } + } + + assertTrue("Min cache size is too small: " + min, min >= max); + + check(max, PUT_ENTRY_SIZE); + + 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 - max;) { + 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()); + + check(max, PUT_ENTRY_SIZE); + } + finally { + stopAllGrids(); + } + } + + /** + * @param arr Array. + * @param idx Index. + * @return Entry at the index. + */ + protected MockEntry entry(MockEntry[] arr, int idx) { + MockEntry e = arr[idx]; + + if (e.isEvicted()) + e = arr[idx] = new MockEntry(e.getKey()); + + return e; + } + + /** + * @param prefix Prefix. + * @param p Policy. + */ + protected void info(String prefix, EvictionPolicy p) { + info(prefix + ": " + p.toString()); + } + + /** @param p Policy. */ + protected void info(EvictionPolicy p) { + info(p.toString()); + } + + /** + * @param c1 Policy collection. + * @param c2 Expected list. + */ + protected static void check(Collection> c1, MockEntry... c2) { + check(c1, F.asList(c2)); + } + + /** + * @param expSize Expected size. + * @param entrySize Entry size. + */ + protected void check(int expSize, int entrySize) { + EvictionPolicyProxy proxy = proxy(policy()); + + assertEquals(expSize, proxy.getCurrentSize()); + assertEquals(expSize * entrySize, proxy.getCurrentMemSize()); + } + + /** + * @param entrySize Entry size. + * @param c1 Closure 1. + * @param c2 Closure 2. + */ + protected void check(int entrySize, Collection> c1, MockEntry... c2) { + check(c2.length, entrySize); + + check(c1, c2); + } + + /** @return Policy. */ + @SuppressWarnings({"unchecked"}) + protected T policy() { + return (T)grid().cache(null).getConfiguration(CacheConfiguration.class).getEvictionPolicy(); + } + + /** + * @param i Grid index. + * @return Policy. + */ + @SuppressWarnings({"unchecked"}) + protected T policy(int i) { + return (T)grid(i).cache(null).getConfiguration(CacheConfiguration.class).getEvictionPolicy(); + } + + /** + * @param i Grid index. + * @return Policy. + */ + @SuppressWarnings({"unchecked"}) + protected T nearPolicy(int i) { + CacheConfiguration cfg = grid(i).cache(null).getConfiguration(CacheConfiguration.class); + + NearCacheConfiguration nearCfg = cfg.getNearConfiguration(); + + return (T)(nearCfg == null ? null : nearCfg.getNearEvictionPolicy()); + } + + /** + * @param c1 Policy collection. + * @param c2 Expected list. + */ + protected static void check(Collection> c1, List c2) { + assert c1.size() == c2.size() : "Mismatch [actual=" + string(c1) + ", expected=" + string(c2) + ']'; + + assert c1.containsAll(c2) : "Mismatch [actual=" + string(c1) + ", expected=" + string(c2) + ']'; + + int i = 0; + + // Check order. + for (Cache.Entry e : c1) + assertEquals(e, c2.get(i++)); + } + + /** + * @param c Collection. + * @return String. + */ + @SuppressWarnings("unchecked") + protected static String string(Iterable c) { + return "[" + + F.fold( + c, + "", + new C2() { + @Override public String apply(Cache.Entry e, String b) { + return b.isEmpty() ? e.getKey().toString() : b + ", " + e.getKey(); + } + }) + + "]]"; + } + + /** @throws Exception If failed. */ + public void testMaxSizePartitionedNearDisabled() throws Exception { + mode = PARTITIONED; + nearEnabled = false; + plcMax = 10; + syncCommit = true; + + gridCnt = 2; + + checkPartitioned(); + } + + /** @throws Exception If failed. */ + public void testMaxSizePartitionedNearDisabledWithBatch() throws Exception { + mode = PARTITIONED; + nearEnabled = false; + plcMax = 10; + plcBatchSize = 2; + syncCommit = true; + + gridCnt = 2; + + checkPartitioned(); + } + + /** @throws Exception If failed. */ + public void testMaxMemSizePartitionedNearDisabled() throws Exception { + mode = PARTITIONED; + nearEnabled = false; + plcMax = 0; + plcMaxMemSize = 100; + syncCommit = true; + + gridCnt = 2; + + checkPartitioned(); + } + + /** @throws Exception If failed. */ + public void testPartitionedNearEnabled() throws Exception { + mode = PARTITIONED; + nearEnabled = true; + nearMax = 3; + plcMax = 10; + evictNearSync = true; + syncCommit = true; + + gridCnt = 2; + + checkPartitioned(); // Near size is 0 because of backups present. + } + + /** @throws Exception If failed. */ + public void testPartitionedNearDisabledMultiThreaded() throws Exception { + mode = PARTITIONED; + nearEnabled = false; + plcMax = 100; + evictSync = false; + + gridCnt = 2; + + checkPartitionedMultiThreaded(); + } + + /** @throws Exception If failed. */ + public void testPartitionedNearDisabledBackupSyncMultiThreaded() throws Exception { + mode = PARTITIONED; + nearEnabled = false; + plcMax = 100; + evictSync = true; + + gridCnt = 2; + + checkPartitionedMultiThreaded(); + } + + /** @throws Exception If failed. */ + public void testPartitionedNearEnabledMultiThreaded() throws Exception { + mode = PARTITIONED; + nearEnabled = true; + plcMax = 10; + evictSync = false; + + gridCnt = 2; + + checkPartitionedMultiThreaded(); + } + + /** @throws Exception If failed. */ + public void testPartitionedNearEnabledBackupSyncMultiThreaded() throws Exception { + mode = PARTITIONED; + nearEnabled = true; + plcMax = 10; + evictSync = true; + + gridCnt = 2; + + checkPartitionedMultiThreaded(); + } + + /** + * @throws Exception If failed. + */ + protected void checkPartitioned() throws Exception { + + int endSize = nearEnabled ? 0 : plcMax; + + int endPlcSize = nearEnabled ? 0 : plcMax; + + startGridsMultiThreaded(gridCnt); + + try { + Random rand = new Random(); + + int cnt = 500; + + for (int i = 0; i < cnt; i++) { + IgniteCache cache = grid(rand.nextInt(2)).cache(null); + + int key = rand.nextInt(100); + String val = Integer.toString(key); + + cache.put(key, val); + + if (i % 100 == 0) + info("Stored cache object for key [key=" + key + ", idx=" + i + ']'); + } + + if (nearEnabled) { + for (int i = 0; i < gridCnt; i++) + assertEquals(endSize, near(i).nearSize()); + + if (endPlcSize >= 0) + checkNearPolicies(endPlcSize); + } + else { + if (plcMaxMemSize > 0) { + for (int i = 0; i < gridCnt; i++) { + GridDhtColocatedCache cache = colocated(i); + + int memSize = 0; + + for (Cache.Entry entry : cache.entrySet()) + memSize += entry.unwrap(EvictableEntry.class).size(); + + EvictionPolicyProxy plc = proxy(policy(i)); + + assertTrue(plc.getCurrentMemSize() <= memSize); + } + } + + if (plcMax > 0) { + for (int i = 0; i < gridCnt; i++) { + int actual = colocated(i).size(); + + assertTrue("Cache size is greater then policy size [expected=" + endSize + ", actual=" + actual + ']', + actual <= endSize + (plcMaxMemSize > 0 ? 1 : plcBatchSize)); + } + } + + checkPolicies(); + } + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + protected void checkPartitionedMultiThreaded() throws Exception { + try { + startGridsMultiThreaded(gridCnt); + + final Random rand = new Random(); + + final AtomicInteger cntr = new AtomicInteger(); + + multithreaded(new Callable() { + @Nullable @Override public Object call() throws Exception { + int cnt = 100; + + for (int i = 0; i < cnt && !Thread.currentThread().isInterrupted(); i++) { + IgniteEx grid = grid(rand.nextInt(2)); + + IgniteCache cache = grid.cache(null); + + int key = rand.nextInt(1000); + String val = Integer.toString(key); + + try (Transaction tx = grid.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { + String v = cache.get(key); + + assert v == null || v.equals(Integer.toString(key)) : "Invalid value for key [key=" + key + + ", val=" + v + ']'; + + cache.put(key, val); + + tx.commit(); + } + + if (cntr.incrementAndGet() % 100 == 0) + info("Stored cache object for key [key=" + key + ", idx=" + i + ']'); + } + + return null; + } + }, 10); + } + finally { + stopAllGrids(); + } + } + + /** + * @param plcMax Policy max. + * @return Policy. + */ + protected abstract T createPolicy(int plcMax); + + /** + * @param nearMax Near max. + * @return Policy. + */ + protected abstract T createNearPolicy(int nearMax); + + /** + * Performs after-test near policy check. + * + * @param nearMax Near max. + */ + protected void checkNearPolicies(int nearMax) { + for (int i = 0; i < gridCnt; i++) { + + EvictionPolicyProxy proxy = proxy(nearPolicy(i)); + + for (EvictableEntry e : proxy.queue()) + assert !e.isCached() : "Invalid near policy size: " + proxy.queue(); + } + } + + /** + * Performs after-test policy check. + */ + protected void checkPolicies() { + for (int i = 0; i < gridCnt; i++) { + if (plcMaxMemSize > 0) { + int size = 0; + + for (EvictableEntry entry : proxy(policy(i)).queue()) + size += entry.size(); + + assertEquals(size, proxy(policy(i)).getCurrentMemSize()); + } + else + assertTrue(proxy(policy(i)).queue().size() <= plcMax + plcBatchSize); + } + } + + /** + * + */ + @SuppressWarnings({"PublicConstructorInNonPublicClass"}) + protected static class MockEntry extends GridCacheMockEntry { + /** Key size. */ + public static final int KEY_SIZE = 1; + + /** Value size. */ + public static final int VALUE_SIZE = 1; + + /** Entry size. */ + public static final int ENTRY_SIZE = KEY_SIZE + VALUE_SIZE; + + /** */ + private IgniteCache parent; + + /** Entry value. */ + private String val; + + /** @param key Key. */ + public MockEntry(String key) { + super(key); + } + + /** + * @param key Key. + * @param val Value. + */ + public MockEntry(String key, String val) { + super(key); + + this.val = val; + } + + /** + * @param key Key. + * @param parent Parent. + */ + public MockEntry(String key, @Nullable IgniteCache parent) { + super(key); + + this.parent = parent; + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override public T unwrap(Class 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 int size() { + return val == null ? KEY_SIZE : ENTRY_SIZE; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(MockEntry.class, this, super.toString()); + } + } + + /** + * Rvicition policy proxy. + */ + public static class EvictionPolicyProxy implements EvictionPolicy { + /** Policy. */ + private final EvictionPolicy plc; + + /** + * @param plc Policy. + */ + private EvictionPolicyProxy(EvictionPolicy plc) { + this.plc = plc; + } + + /** + * @param plc Policy. + */ + public static EvictionPolicyProxy proxy(EvictionPolicy plc) { + return new EvictionPolicyProxy(plc); + } + + /** + * Get current size. + */ + public int getCurrentSize() { + try { + return (Integer)plc.getClass().getDeclaredMethod("getCurrentSize").invoke(plc); + } + catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + /** + * Current memory size. + */ + public long getCurrentMemSize() { + try { + return (Long)plc.getClass().getDeclaredMethod("getCurrentMemSize").invoke(plc); + } + catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + /** + * Current queue. + */ + public Collection queue() { + try { + return (Collection)plc.getClass().getDeclaredMethod("queue").invoke(plc); + } + catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + /** + * @param rmv Remove. + * @param entry Entry. + */ + @Override public void onEntryAccessed(boolean rmv, EvictableEntry entry) { + try { + plc.getClass() + .getDeclaredMethod("onEntryAccessed", boolean.class, EvictableEntry.class) + .invoke(plc, rmv, entry); + } + catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } + +} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheBatchEvictUnswapSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheBatchEvictUnswapSelfTest.java index 36598fd..da5017a 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheBatchEvictUnswapSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheBatchEvictUnswapSelfTest.java @@ -90,7 +90,10 @@ public class GridCacheBatchEvictUnswapSelfTest extends GridCacheAbstractSelfTest cacheCfg.setWriteThrough(true); cacheCfg.setLoadPreviousValue(true); - cacheCfg.setEvictionPolicy(new FifoEvictionPolicy(EVICT_PLC_SIZE)); + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(EVICT_PLC_SIZE); + + cacheCfg.setEvictionPolicy(plc); cacheCfg.setSwapEnabled(true); cacheCfg.setEvictSynchronized(false); cacheCfg.setNearConfiguration(null); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheConcurrentEvictionConsistencySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheConcurrentEvictionConsistencySelfTest.java index 4daa908..921422e 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheConcurrentEvictionConsistencySelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheConcurrentEvictionConsistencySelfTest.java @@ -21,6 +21,7 @@ import org.apache.ignite.*; import org.apache.ignite.cache.eviction.*; import org.apache.ignite.cache.eviction.fifo.*; import org.apache.ignite.cache.eviction.lru.*; +import org.apache.ignite.cache.eviction.sorted.*; import org.apache.ignite.configuration.*; import org.apache.ignite.internal.*; import org.apache.ignite.internal.util.typedef.internal.*; @@ -100,7 +101,10 @@ public class GridCacheConcurrentEvictionConsistencySelfTest extends GridCommonAb * @throws Exception If failed. */ public void testPolicyConsistencyFifoLocalTwoKeys() throws Exception { - plc = new FifoEvictionPolicy(1); + FifoEvictionPolicy plc = new FifoEvictionPolicy<>(); + plc.setMaxSize(1); + + this.plc = plc; keyCnt = 2; threadCnt = Runtime.getRuntime().availableProcessors() / 2; @@ -112,7 +116,25 @@ public class GridCacheConcurrentEvictionConsistencySelfTest extends GridCommonAb * @throws Exception If failed. */ public void testPolicyConsistencyLruLocalTwoKeys() throws Exception { - plc = new LruEvictionPolicy(1); + LruEvictionPolicy plc = new LruEvictionPolicy<>(); + plc.setMaxSize(1); + + this.plc = plc; + + keyCnt = 2; + threadCnt = Runtime.getRuntime().availableProcessors() / 2; + + checkPolicyConsistency(); + } + + /** + * @throws Exception If failed. + */ + public void testPolicyConsistencySortedLocalTwoKeys() throws Exception { + SortedEvictionPolicy plc = new SortedEvictionPolicy<>(); + plc.setMaxSize(1); + + this.plc = plc; keyCnt = 2; threadCnt = Runtime.getRuntime().availableProcessors() / 2; @@ -124,7 +146,10 @@ public class GridCacheConcurrentEvictionConsistencySelfTest extends GridCommonAb * @throws Exception If failed. */ public void testPolicyConsistencyFifoLocalFewKeys() throws Exception { - plc = new FifoEvictionPolicy(POLICY_QUEUE_SIZE); + FifoEvictionPolicy plc = new FifoEvictionPolicy<>(); + plc.setMaxSize(POLICY_QUEUE_SIZE); + + this.plc = plc; keyCnt = POLICY_QUEUE_SIZE + 5; @@ -135,7 +160,24 @@ public class GridCacheConcurrentEvictionConsistencySelfTest extends GridCommonAb * @throws Exception If failed. */ public void testPolicyConsistencyLruLocalFewKeys() throws Exception { - plc = new LruEvictionPolicy(POLICY_QUEUE_SIZE); + LruEvictionPolicy plc = new LruEvictionPolicy<>(); + plc.setMaxSize(POLICY_QUEUE_SIZE); + + this.plc = plc; + + keyCnt = POLICY_QUEUE_SIZE + 5; + + checkPolicyConsistency(); + } + + /** + * @throws Exception If failed. + */ + public void testPolicyConsistencySortedLocalFewKeys() throws Exception { + SortedEvictionPolicy plc = new SortedEvictionPolicy<>(); + plc.setMaxSize(POLICY_QUEUE_SIZE); + + this.plc = plc; keyCnt = POLICY_QUEUE_SIZE + 5; @@ -146,7 +188,10 @@ public class GridCacheConcurrentEvictionConsistencySelfTest extends GridCommonAb * @throws Exception If failed. */ public void testPolicyConsistencyFifoLocal() throws Exception { - plc = new FifoEvictionPolicy(POLICY_QUEUE_SIZE); + FifoEvictionPolicy plc = new FifoEvictionPolicy<>(); + plc.setMaxSize(POLICY_QUEUE_SIZE); + + this.plc = plc; keyCnt = POLICY_QUEUE_SIZE * 10; @@ -157,7 +202,24 @@ public class GridCacheConcurrentEvictionConsistencySelfTest extends GridCommonAb * @throws Exception If failed. */ public void testPolicyConsistencyLruLocal() throws Exception { - plc = new LruEvictionPolicy(POLICY_QUEUE_SIZE); + LruEvictionPolicy plc = new LruEvictionPolicy<>(); + plc.setMaxSize(POLICY_QUEUE_SIZE); + + this.plc = plc; + + keyCnt = POLICY_QUEUE_SIZE * 10; + + checkPolicyConsistency(); + } + + /** + * @throws Exception If failed. + */ + public void testPolicyConsistencySortedLocal() throws Exception { + SortedEvictionPolicy plc = new SortedEvictionPolicy<>(); + plc.setMaxSize(POLICY_QUEUE_SIZE); + + this.plc = plc; keyCnt = POLICY_QUEUE_SIZE * 10; @@ -177,8 +239,7 @@ public class GridCacheConcurrentEvictionConsistencySelfTest extends GridCommonAb IgniteInternalFuture fut = multithreadedAsync( new Callable() { - @Override - public Object call() throws Exception { + @Override public Object call() throws Exception { final Random rnd = new Random(); for (int i = 0; i < ITERATION_CNT; i++) { @@ -258,6 +319,11 @@ public class GridCacheConcurrentEvictionConsistencySelfTest extends GridCommonAb return plc0.queue(); } + else if (plc instanceof SortedEvictionPolicy) { + SortedEvictionPolicy plc0 = (SortedEvictionPolicy)plc; + + return plc0.queue(); + } assert false : "Unexpected policy type: " + plc.getClass().getName(); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheConcurrentEvictionsSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheConcurrentEvictionsSelfTest.java index 3cadc45..270f991 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheConcurrentEvictionsSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheConcurrentEvictionsSelfTest.java @@ -22,6 +22,7 @@ import org.apache.ignite.cache.*; import org.apache.ignite.cache.eviction.*; import org.apache.ignite.cache.eviction.fifo.*; import org.apache.ignite.cache.eviction.lru.*; +import org.apache.ignite.cache.eviction.sorted.*; import org.apache.ignite.configuration.*; import org.apache.ignite.internal.*; import org.apache.ignite.spi.discovery.tcp.*; @@ -98,7 +99,11 @@ public class GridCacheConcurrentEvictionsSelfTest extends GridCommonAbstractTest */ public void testConcurrentPutsFifoLocal() throws Exception { mode = LOCAL; - plc = new FifoEvictionPolicy(1000); + + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(1000); + + this.plc = plc; warmUpPutsCnt = 100000; iterCnt = 100000; @@ -110,7 +115,27 @@ public class GridCacheConcurrentEvictionsSelfTest extends GridCommonAbstractTest */ public void testConcurrentPutsLruLocal() throws Exception { mode = LOCAL; - plc = new LruEvictionPolicy(1000); + + LruEvictionPolicy plc = new LruEvictionPolicy(); + plc.setMaxSize(1000); + + this.plc = plc; + warmUpPutsCnt = 100000; + iterCnt = 100000; + + checkConcurrentPuts(); + } + + /** + * @throws Exception If failed. + */ + public void testConcurrentPutsSortedLocal() throws Exception { + mode = LOCAL; + + SortedEvictionPolicy plc = new SortedEvictionPolicy(); + plc.setMaxSize(1000); + + this.plc = plc; warmUpPutsCnt = 100000; iterCnt = 100000; 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 396f360..75c4dfe 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 @@ -86,7 +86,10 @@ public class GridCacheDistributedEvictionsSelfTest extends GridCommonAbstractTes cc.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC); // Set only DHT policy, leave default near policy. - cc.setEvictionPolicy(new FifoEvictionPolicy<>(10)); + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(10); + + cc.setEvictionPolicy(plc); cc.setEvictSynchronized(evictSync); cc.setEvictSynchronizedKeyBufferSize(1); 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 768bced..8e17eed 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 @@ -120,14 +120,19 @@ public abstract class GridCacheEmptyEntriesAbstractSelfTest extends GridCommonAb * @throws Exception If failed. */ public void testFifo() throws Exception { - plc = new FifoEvictionPolicy(50); - nearPlc = new FifoEvictionPolicy(50); + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(50); + this.plc = plc; + + FifoEvictionPolicy nearPlc = new FifoEvictionPolicy(); + nearPlc.setMaxSize(50); + this.nearPlc = nearPlc; checkPolicy(); } /** - * Checks policy with and without store set. + * Checks policy with and without store queue. * * @throws Exception If failed. */ 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 deleted file mode 100644 index e0dab7d..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionAbstractTest.java +++ /dev/null @@ -1,484 +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.eviction; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -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.*; -import org.apache.ignite.testframework.junits.common.*; -import org.apache.ignite.transactions.*; -import org.jetbrains.annotations.*; - -import javax.cache.*; -import java.util.*; -import java.util.concurrent.*; -import java.util.concurrent.atomic.*; - -import static org.apache.ignite.cache.CacheAtomicityMode.*; -import static org.apache.ignite.cache.CacheMode.*; -import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*; -import static org.apache.ignite.events.EventType.*; -import static org.apache.ignite.transactions.TransactionConcurrency.*; -import static org.apache.ignite.transactions.TransactionIsolation.*; - -/** - * Base class for eviction tests. - */ -public abstract class GridCacheEvictionAbstractTest> - extends GridCommonAbstractTest { - /** IP finder. */ - protected static final TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); - - /** Replicated cache. */ - protected CacheMode mode = REPLICATED; - - /** Near enabled flag. */ - protected boolean nearEnabled; - - /** Evict backup sync. */ - protected boolean evictSync; - - /** Evict near sync. */ - protected boolean evictNearSync = true; - - /** Policy batch size. */ - protected int plcBatchSize = 0; - - /** Policy max. */ - protected int plcMax = 10; - - /** Near policy max. */ - protected int nearMax = 3; - - /** Synchronous commit. */ - protected boolean syncCommit; - - /** */ - protected int gridCnt = 2; - - /** */ - protected EvictionFilter filter; - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration c = super.getConfiguration(gridName); - - CacheConfiguration cc = defaultCacheConfiguration(); - - cc.setCacheMode(mode); - cc.setEvictionPolicy(createPolicy(plcMax)); - cc.setEvictSynchronized(evictSync); - cc.setSwapEnabled(false); - cc.setWriteSynchronizationMode(syncCommit ? FULL_SYNC : FULL_ASYNC); - cc.setStartSize(plcMax); - cc.setAtomicityMode(TRANSACTIONAL); - - if (nearEnabled) { - NearCacheConfiguration nearCfg = new NearCacheConfiguration(); - - nearCfg.setNearEvictionPolicy(createNearPolicy(nearMax)); - - cc.setNearConfiguration(nearCfg); - } - else - cc.setNearConfiguration(null); - - if (mode == PARTITIONED) - cc.setBackups(1); - - if (filter != null) - cc.setEvictionFilter(filter); - - c.setCacheConfiguration(cc); - - TcpDiscoverySpi disco = new TcpDiscoverySpi(); - - disco.setIpFinder(ipFinder); - - c.setDiscoverySpi(disco); - - c.setIncludeEventTypes(EVT_TASK_FAILED, EVT_TASK_FINISHED, EVT_JOB_MAPPED); - - c.setIncludeProperties(); - - return c; - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - filter = null; - - super.afterTestsStopped(); - } - - /** - * @param arr Array. - * @param idx Index. - * @return Entry at the index. - */ - protected MockEntry entry(MockEntry[] arr, int idx) { - MockEntry e = arr[idx]; - - if (e.isEvicted()) - e = arr[idx] = new MockEntry(e.getKey()); - - return e; - } - - /** - * @param prefix Prefix. - * @param p Policy. - */ - protected void info(String prefix, EvictionPolicy p) { - info(prefix + ": " + p.toString()); - } - - /** @param p Policy. */ - protected void info(EvictionPolicy p) { - info(p.toString()); - } - - /** - * @param c1 Policy collection. - * @param c2 Expected list. - */ - protected void check(Collection> c1, MockEntry... c2) { - check(c1, F.asList(c2)); - } - - /** @return Policy. */ - @SuppressWarnings({"unchecked"}) - protected T policy() { - return (T)grid().cache(null).getConfiguration(CacheConfiguration.class).getEvictionPolicy(); - } - - /** - * @param i Grid index. - * @return Policy. - */ - @SuppressWarnings({"unchecked"}) - protected T policy(int i) { - return (T)grid(i).cache(null).getConfiguration(CacheConfiguration.class).getEvictionPolicy(); - } - - /** - * @param i Grid index. - * @return Policy. - */ - @SuppressWarnings({"unchecked"}) - protected T nearPolicy(int i) { - CacheConfiguration cfg = grid(i).cache(null).getConfiguration(CacheConfiguration.class); - - NearCacheConfiguration nearCfg = cfg.getNearConfiguration(); - - return (T)(nearCfg == null ? null : nearCfg.getNearEvictionPolicy()); - } - - /** - * @param c1 Policy collection. - * @param c2 Expected list. - */ - protected void check(Collection> c1, List c2) { - assert c1.size() == c2.size() : "Mismatch [actual=" + string(c1) + ", expected=" + string(c2) + ']'; - - assert c1.containsAll(c2) : "Mismatch [actual=" + string(c1) + ", expected=" + string(c2) + ']'; - - int i = 0; - - // Check order. - for (Cache.Entry e : c1) - assertEquals(e, c2.get(i++)); - } - - /** - * @param c Collection. - * @return String. - */ - @SuppressWarnings("unchecked") - protected String string(Iterable c) { - return "[" + - F.fold( - c, - "", - new C2() { - @Override public String apply(Cache.Entry e, String b) { - return b.isEmpty() ? e.getKey().toString() : b + ", " + e.getKey(); - } - }) + - "]]"; - } - - /** @throws Exception If failed. */ - public void testPartitionedNearDisabled() throws Exception { - mode = PARTITIONED; - nearEnabled = false; - plcMax = 10; - syncCommit = true; - - gridCnt = 2; - - checkPartitioned(plcMax, plcMax, false); - } - - /** @throws Exception If failed. */ - public void testPartitionedNearEnabled() throws Exception { - mode = PARTITIONED; - nearEnabled = true; - nearMax = 3; - plcMax = 10; - evictNearSync = true; - syncCommit = true; - - gridCnt = 2; - - checkPartitioned(0, 0, true); // Near size is 0 because of backups present. - } - - /** @throws Exception If failed. */ - public void testPartitionedNearDisabledMultiThreaded() throws Exception { - mode = PARTITIONED; - nearEnabled = false; - plcMax = 100; - evictSync = false; - - gridCnt = 2; - - checkPartitionedMultiThreaded(gridCnt); - } - - /** @throws Exception If failed. */ - public void testPartitionedNearDisabledBackupSyncMultiThreaded() throws Exception { - mode = PARTITIONED; - nearEnabled = false; - plcMax = 100; - evictSync = true; - - gridCnt = 2; - - checkPartitionedMultiThreaded(gridCnt); - } - - /** @throws Exception If failed. */ - public void testPartitionedNearEnabledMultiThreaded() throws Exception { - mode = PARTITIONED; - nearEnabled = true; - plcMax = 10; - evictSync = false; - - gridCnt = 2; - - checkPartitionedMultiThreaded(gridCnt); - } - - /** @throws Exception If failed. */ - public void testPartitionedNearEnabledBackupSyncMultiThreaded() throws Exception { - mode = PARTITIONED; - nearEnabled = true; - plcMax = 10; - evictSync = true; - - gridCnt = 2; - - checkPartitionedMultiThreaded(gridCnt); - } - - /** - * @param endSize Final near size. - * @param endPlcSize Final near policy size. - * @throws Exception If failed. - */ - private void checkPartitioned(int endSize, int endPlcSize, boolean near) throws Exception { - startGridsMultiThreaded(gridCnt); - - try { - Random rand = new Random(); - - int cnt = 500; - - for (int i = 0; i < cnt; i++) { - IgniteCache cache = grid(rand.nextInt(2)).cache(null); - - int key = rand.nextInt(100); - String val = Integer.toString(key); - - cache.put(key, val); - - if (i % 100 == 0) - info("Stored cache object for key [key=" + key + ", idx=" + i + ']'); - } - - if (near) { - for (int i = 0; i < gridCnt; i++) - assertEquals(endSize, near(i).nearSize()); - - if (endPlcSize >= 0) - checkNearPolicies(endPlcSize); - } - else { - for (int i = 0; i < gridCnt; i++) { - int actual = colocated(i).size(); - - assertTrue("Cache size is greater then policy size [expected=" + endSize + ", actual=" + actual + ']', - actual <= endSize + plcBatchSize); - } - - checkPolicies(endPlcSize); - } - } - finally { - stopAllGrids(); - } - } - - /** - * @param gridCnt Grid count. - * @throws Exception If failed. - */ - protected void checkPartitionedMultiThreaded(int gridCnt) throws Exception { - try { - startGridsMultiThreaded(gridCnt); - - final Random rand = new Random(); - - final AtomicInteger cntr = new AtomicInteger(); - - multithreaded(new Callable() { - @Nullable @Override public Object call() throws Exception { - int cnt = 100; - - for (int i = 0; i < cnt && !Thread.currentThread().isInterrupted(); i++) { - IgniteEx grid = grid(rand.nextInt(2)); - - IgniteCache cache = grid.cache(null); - - int key = rand.nextInt(1000); - String val = Integer.toString(key); - - try (Transaction tx = grid.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { - String v = cache.get(key); - - assert v == null || v.equals(Integer.toString(key)) : "Invalid value for key [key=" + key + - ", val=" + v + ']'; - - cache.put(key, val); - - tx.commit(); - } - - if (cntr.incrementAndGet() % 100 == 0) - info("Stored cache object for key [key=" + key + ", idx=" + i + ']'); - } - - return null; - } - }, 10); - } - finally { - stopAllGrids(); - } - } - - /** - * @param plcMax Policy max. - * @return Policy. - */ - protected abstract T createPolicy(int plcMax); - - /** - * @param nearMax Near max. - * @return Policy. - */ - protected abstract T createNearPolicy(int nearMax); - - /** - * Performs after-test near policy check. - * - * @param nearMax Near max. - */ - protected abstract void checkNearPolicies(int nearMax); - - /** - * Performs after-test policy check. - * - * @param plcMax Maximum allowed size of ploicy. - */ - protected abstract void checkPolicies(int plcMax); - - /** - * - */ - @SuppressWarnings({"PublicConstructorInNonPublicClass"}) - protected static class MockEntry extends GridCacheMockEntry { - /** */ - private IgniteCache parent; - - /** Entry value. */ - private String val; - - /** @param key Key. */ - public MockEntry(String key) { - super(key); - } - - /** - * @param key Key. - * @param val Value. - */ - public MockEntry(String key, String val) { - super(key); - - this.val = val; - } - - /** - * @param key Key. - * @param parent Parent. - */ - public MockEntry(String key, @Nullable IgniteCache parent) { - super(key); - - this.parent = parent; - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public T unwrap(Class 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()); - } - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionTouchSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionTouchSelfTest.java index 9873ce6..55b8d63 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionTouchSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/GridCacheEvictionTouchSelfTest.java @@ -110,7 +110,10 @@ public class GridCacheEvictionTouchSelfTest extends GridCommonAbstractTest { * @throws Exception If failed. */ public void testPolicyConsistency() throws Exception { - plc = new FifoEvictionPolicy(500); + FifoEvictionPolicy plc = new FifoEvictionPolicy<>(); + plc.setMaxSize(500); + + this.plc = plc; try { Ignite ignite = startGrid(1); @@ -136,7 +139,7 @@ public class GridCacheEvictionTouchSelfTest extends GridCommonAbstractTest { info("Stats [iterCnt=" + i + ", size=" + cache.size() + ']'); } - FifoEvictionPolicy plc0 = (FifoEvictionPolicy) plc; + FifoEvictionPolicy plc0 = (FifoEvictionPolicy)this.plc; if (!plc0.queue().isEmpty()) { for (Cache.Entry e : plc0.queue()) @@ -162,7 +165,10 @@ public class GridCacheEvictionTouchSelfTest extends GridCommonAbstractTest { * @throws Exception If failed. */ public void testEvictSingle() throws Exception { - plc = new FifoEvictionPolicy(500); + FifoEvictionPolicy plc = new FifoEvictionPolicy<>(); + plc.setMaxSize(500); + + this.plc = plc; try { Ignite ignite = startGrid(1); @@ -189,7 +195,10 @@ public class GridCacheEvictionTouchSelfTest extends GridCommonAbstractTest { * @throws Exception If failed. */ public void testEvictAll() throws Exception { - plc = new FifoEvictionPolicy(500); + FifoEvictionPolicy plc = new FifoEvictionPolicy<>(); + plc.setMaxSize(500); + + this.plc = plc; try { Ignite ignite = startGrid(1); @@ -221,7 +230,10 @@ public class GridCacheEvictionTouchSelfTest extends GridCommonAbstractTest { * @throws Exception If failed. */ public void testReload() throws Exception { - plc = new FifoEvictionPolicy(100); + FifoEvictionPolicy plc = new FifoEvictionPolicy<>(); + plc.setMaxSize(100); + + this.plc = plc; try { Ignite ignite = startGrid(1); 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 5898ed9..5d183bf 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 @@ -83,6 +83,11 @@ public class GridCacheMockEntry extends GridMetadataAwareAdapter implement return !evicted; } + /** {@inheritDoc} */ + @Override public int size() { + return 0; + } + /** * * @return Evicted or not. diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/FifoEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/FifoEvictionPolicySelfTest.java new file mode 100644 index 0000000..a908259 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/FifoEvictionPolicySelfTest.java @@ -0,0 +1,262 @@ +/* + * 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.eviction.fifo; + +import org.apache.ignite.cache.eviction.fifo.*; +import org.apache.ignite.internal.processors.cache.eviction.*; + +/** + * FIFO eviction policy tests. + */ +public class FifoEvictionPolicySelfTest extends + EvictionAbstractTest> { + /** {@inheritDoc} */ + @Override protected void doTestPolicy() throws Exception { + 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"); + + FifoEvictionPolicy p = policy(); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1); + + p.onEntryAccessed(false, e2); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2); + + p.onEntryAccessed(false, e3); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3); + + assert !e1.isEvicted(); + assert !e2.isEvicted(); + assert !e3.isEvicted(); + + p.onEntryAccessed(false, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e2, e3, e4); + + assert e1.isEvicted(); + assert !e2.isEvicted(); + assert !e3.isEvicted(); + assert !e4.isEvicted(); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assert e2.isEvicted(); + assert !e3.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e5, e1); + + assert e3.isEvicted(); + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e5, e1); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e5, e1); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e5, e1); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(true, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e5); + + assert !e1.isEvicted(); + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(true, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e5); + + assert !e4.isEvicted(); + assert !e5.isEvicted(); + + p.onEntryAccessed(true, e5); + + check(MockEntry.ENTRY_SIZE, p.queue()); + + assert !e5.isEvicted(); + + info(p); + } + finally { + stopAllGrids(); + } + } + + /** {@inheritDoc} */ + @Override protected void doTestPolicyWithBatch() throws Exception { + 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"); + + FifoEvictionPolicy p = policy(); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1); + + p.onEntryAccessed(false, e2); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2); + + p.onEntryAccessed(false, e3); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3); + + p.onEntryAccessed(false, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3, e4); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + + p.onEntryAccessed(false, e5); + + // Batch evicted. + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertTrue(e1.isEvicted()); + assertTrue(e2.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5, e1); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + assertFalse(e1.isEvicted()); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5, e1); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + assertFalse(e1.isEvicted()); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5, e1); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + assertFalse(e1.isEvicted()); + + p.onEntryAccessed(true, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3); + + assertFalse(e3.isEvicted()); + + p.onEntryAccessed(true, e3); + + check(MockEntry.ENTRY_SIZE, p.queue()); + + assertFalse(e3.isEvicted()); + + info(p); + } + finally { + stopAllGrids(); + } + } + + /** {@inheritDoc} */ + @Override protected FifoEvictionPolicy createPolicy(int plcMax) { + FifoEvictionPolicy plc = new FifoEvictionPolicy<>(); + + plc.setMaxSize(this.plcMax); + plc.setBatchSize(this.plcBatchSize); + plc.setMaxMemSize(this.plcMaxMemSize); + + return plc; + } + + /** {@inheritDoc} */ + @Override protected FifoEvictionPolicy createNearPolicy(int nearMax) { + FifoEvictionPolicy plc = new FifoEvictionPolicy<>(); + + plc.setMaxSize(nearMax); + plc.setBatchSize(plcBatchSize); + + return plc; + } +} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoBatchEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoBatchEvictionPolicySelfTest.java deleted file mode 100644 index f907c36..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoBatchEvictionPolicySelfTest.java +++ /dev/null @@ -1,384 +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.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 batch eviction test. - */ -public class GridCacheFifoBatchEvictionPolicySelfTest extends - GridCacheEvictionAbstractTest> { - /** - * @throws Exception If failed. - */ - public void testPolicy() throws Exception { - 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"); - - FifoEvictionPolicy p = policy(); - - p.setMaxSize(3); - - p.setBatchSize(2); - - 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); - - p.onEntryAccessed(false, e4); - - check(p.queue(), e1, e2, e3, e4); - - assertFalse(e1.isEvicted()); - assertFalse(e2.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - - assertEquals(4, p.getCurrentSize()); - - p.onEntryAccessed(false, e5); - - // Batch evicted. - check(p.queue(), e3, e4, e5); - - assertEquals(3, p.getCurrentSize()); - - assertTrue(e1.isEvicted()); - assertTrue(e2.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); - - check(p.queue(), e3, e4, e5, e1); - - assertEquals(4, p.getCurrentSize()); - - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - assertFalse(e1.isEvicted()); - - p.onEntryAccessed(false, e5); - - check(p.queue(), e3, e4, e5, e1); - - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - assertFalse(e1.isEvicted()); - - p.onEntryAccessed(false, e1); - - assertEquals(4, p.getCurrentSize()); - - check(p.queue(), e3, e4, e5, e1); - - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - assertFalse(e1.isEvicted()); - - p.onEntryAccessed(true, e1); - - assertEquals(3, p.getCurrentSize()); - - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(true, e4); - - assertEquals(2, p.getCurrentSize()); - - assertFalse(e3.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(true, e5); - - assertEquals(1, p.getCurrentSize()); - - assertFalse(e3.isEvicted()); - - p.onEntryAccessed(true, e3); - - assertEquals(0, p.getCurrentSize()); - - assertFalse(e3.isEvicted()); - - info(p); - } - finally { - stopAllGrids(); - } - } - - /** - * @throws Exception If failed. - */ - public void testMemory() throws Exception { - try { - startGrid(); - - FifoEvictionPolicy p = policy(); - - int max = 10; - - int batchSize = 2; - - p.setMaxSize(max); - p.setBatchSize(batchSize); - - int cnt = max + batchSize; - - for (int i = 0; i < cnt; i++) - p.onEntryAccessed(false, new MockEntry(Integer.toString(i), Integer.toString(i))); - - info(p); - - assertEquals(cnt - batchSize, p.getCurrentSize()); - } - finally { - stopAllGrids(); - } - } - - /** - * @throws Exception If failed. - */ - public void testRandom() throws Exception { - try { - startGrid(); - - FifoEvictionPolicy p = policy(); - - int max = 10; - - int batchSize = 2; - - p.setMaxSize(max); - - p.setBatchSize(batchSize); - - 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 + batchSize : - "curSize < max + batchSize [curSize=" + curSize + ", max=" + max + ", batchSize=" + batchSize + ']'; - } - 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"); - - FifoEvictionPolicy p = policy(); - - p.setBatchSize(2); - - 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 = 10; - - Ignite ignite = startGrid(); - - try { - IgniteCache 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; - } - } - - // Batch evicted. - 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()); - - // Batch evicted. - assert min >= plcMax : "Min cache size is too small: " + min; - } - finally { - stopAllGrids(); - } - } - - /** {@inheritDoc} */ - @Override public void testPartitionedNearDisabled() throws Exception { - plcBatchSize = 2; - - super.testPartitionedNearDisabled(); - } - - /** {@inheritDoc} */ - @Override protected FifoEvictionPolicy createPolicy(int plcMax) { - return new FifoEvictionPolicy<>(10, 2); - } - - /** {@inheritDoc} */ - @Override protected FifoEvictionPolicy createNearPolicy(int nearMax) { - return new FifoEvictionPolicy<>(nearMax, 2); - } - - /** {@inheritDoc} */ - @Override protected void checkNearPolicies(int endNearPlcSize) { - for (int i = 0; i < gridCnt; i++) - for (EvictableEntry e : nearPolicy(i).queue()) - assert !e.isCached() : "Invalid near policy size: " + nearPolicy(i).queue(); - } - - /** {@inheritDoc} */ - @Override protected void checkPolicies(int plcMax) { - for (int i = 0; i < gridCnt; i++) - assert policy(i).queue().size() <= plcMax + policy(i).getBatchSize(); - } -} 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 deleted file mode 100644 index 64d3831..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/fifo/GridCacheFifoEvictionPolicySelfTest.java +++ /dev/null @@ -1,372 +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.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. - */ -@SuppressWarnings({"TypeMayBeWeakened"}) -public class GridCacheFifoEvictionPolicySelfTest extends - GridCacheEvictionAbstractTest> { - /** - * @throws Exception If failed. - */ - public void testPolicy() throws Exception { - 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"); - - FifoEvictionPolicy 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(); - - FifoEvictionPolicy 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(); - - FifoEvictionPolicy 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"); - - MockEntry e2 = new MockEntry("2"); - - MockEntry e3 = new MockEntry("3"); - - MockEntry e4 = new MockEntry("4"); - - MockEntry e5 = new MockEntry("5"); - - FifoEvictionPolicy 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 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()); - - 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 FifoEvictionPolicy createPolicy(int plcMax) { - return new FifoEvictionPolicy<>(plcMax); - } - - /** {@inheritDoc} */ - @Override protected FifoEvictionPolicy createNearPolicy(int nearMax) { - return new FifoEvictionPolicy<>(nearMax); - } - - /** {@inheritDoc} */ - @Override protected void checkNearPolicies(int endNearPlcSize) { - for (int i = 0; i < gridCnt; i++) - for (EvictableEntry e : nearPolicy(i).queue()) - assert !e.isCached() : "Invalid near policy size: " + nearPolicy(i).queue(); - } - - /** {@inheritDoc} */ - @Override protected void checkPolicies(int plcMax) { - for (int i = 0; i < gridCnt; i++) - assert policy(i).queue().size() <= plcMax; - } -} 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 deleted file mode 100644 index c623b38..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruEvictionPolicySelfTest.java +++ /dev/null @@ -1,417 +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.eviction.lru; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -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. - */ -@SuppressWarnings( {"TypeMayBeWeakened"}) -public class GridCacheLruEvictionPolicySelfTest extends - GridCacheEvictionAbstractTest> { - /** - * @throws Exception If failed. - */ - public void testPolicy() throws Exception { - 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"); - - LruEvictionPolicy 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 { - startGrid(); - - try { - LruEvictionPolicy 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 { - startGrid(); - - try { - LruEvictionPolicy 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 { - startGrid(); - - try { - LruEvictionPolicy 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 { - 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"); - - LruEvictionPolicy 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 = CacheMode.LOCAL; - syncCommit = true; - plcMax = 100; - - Ignite ignite = startGrid(); - - try { - IgniteCache 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()); - - 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()); - - assert min >= plcMax : "Min cache size is too small: " + min; - } - finally { - stopAllGrids(); - } - } - - /** {@inheritDoc} */ - @Override protected LruEvictionPolicy createPolicy(int plcMax) { - return new LruEvictionPolicy<>(plcMax); - } - - @Override protected LruEvictionPolicy createNearPolicy(int nearMax) { - return new LruEvictionPolicy<>(nearMax); - } - - /** {@inheritDoc} */ - @Override protected void checkNearPolicies(int endNearPlcSize) { - for (int i = 0; i < gridCnt; i++) - for (EvictableEntry e : nearPolicy(i).queue()) - assert !e.isCached() : "Invalid near policy size: " + nearPolicy(i).queue(); - } - - /** {@inheritDoc} */ - @Override protected void checkPolicies(int plcMax) { - for (int i = 0; i < gridCnt; i++) - assert policy(i).queue().size() <= plcMax; - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruNearEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruNearEvictionPolicySelfTest.java deleted file mode 100644 index e0606a7..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheLruNearEvictionPolicySelfTest.java +++ /dev/null @@ -1,136 +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.eviction.lru; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cache.eviction.lru.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.spi.discovery.tcp.*; -import org.apache.ignite.spi.discovery.tcp.ipfinder.*; -import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; -import org.apache.ignite.testframework.junits.common.*; - -import java.util.*; - -import static org.apache.ignite.cache.CacheAtomicityMode.*; -import static org.apache.ignite.cache.CacheMode.*; -import static org.apache.ignite.cache.CacheRebalanceMode.*; -import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*; - -/** - * LRU near eviction tests (GG-8884). - */ -public class GridCacheLruNearEvictionPolicySelfTest extends GridCommonAbstractTest { - /** */ - private static final TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); - - /** Maximum size for near eviction policy. */ - private static final int EVICTION_MAX_SIZE = 10; - - /** Grid count. */ - private static final int GRID_COUNT = 2; - - /** Cache atomicity mode specified by test. */ - private CacheAtomicityMode atomicityMode; - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration c = super.getConfiguration(gridName); - - CacheConfiguration cc = new CacheConfiguration(); - - cc.setAtomicityMode(atomicityMode); - cc.setCacheMode(PARTITIONED); - cc.setWriteSynchronizationMode(PRIMARY_SYNC); - cc.setRebalanceMode(SYNC); - cc.setStartSize(100); - cc.setBackups(0); - - NearCacheConfiguration nearCfg = new NearCacheConfiguration(); - nearCfg.setNearEvictionPolicy(new LruEvictionPolicy(EVICTION_MAX_SIZE)); - cc.setNearConfiguration(nearCfg); - - c.setCacheConfiguration(cc); - - TcpDiscoverySpi disco = new TcpDiscoverySpi(); - - disco.setIpFinder(ipFinder); - - c.setDiscoverySpi(disco); - - return c; - } - - /** - * @throws Exception If failed. - */ - public void testAtomicNearEvictionMaxSize() throws Exception { - atomicityMode = ATOMIC; - - checkNearEvictionMaxSize(); - } - - /** - * @throws Exception If failed. - */ - public void testTransactionalNearEvictionMaxSize() throws Exception { - atomicityMode = TRANSACTIONAL; - - checkNearEvictionMaxSize(); - } - - /** - * @throws Exception If failed. - */ - private void checkNearEvictionMaxSize() throws Exception { - startGridsMultiThreaded(GRID_COUNT); - - try { - Random rand = new Random(0); - - int cnt = 1000; - - info("Inserting " + cnt + " keys to cache."); - - try (IgniteDataStreamer ldr = grid(0).dataStreamer(null)) { - for (int i = 0; i < cnt; i++) - ldr.addData(i, Integer.toString(i)); - } - - for (int i = 0; i < GRID_COUNT; i++) - assertTrue("Near cache size " + near(i).nearSize() + ", but eviction maximum size " + EVICTION_MAX_SIZE, - near(i).nearSize() <= EVICTION_MAX_SIZE); - - info("Getting " + cnt + " keys from cache."); - - for (int i = 0; i < cnt; i++) { - IgniteCache cache = grid(rand.nextInt(GRID_COUNT)).cache(null); - - assertTrue(cache.get(i).equals(Integer.toString(i))); - } - - for (int i = 0; i < GRID_COUNT; i++) - assertTrue("Near cache size " + near(i).nearSize() + ", but eviction maximum size " + EVICTION_MAX_SIZE, - near(i).nearSize() <= EVICTION_MAX_SIZE); - } - finally { - stopAllGrids(); - } - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheNearOnlyLruNearEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheNearOnlyLruNearEvictionPolicySelfTest.java deleted file mode 100644 index 5d4ff85..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/GridCacheNearOnlyLruNearEvictionPolicySelfTest.java +++ /dev/null @@ -1,171 +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.eviction.lru; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cache.eviction.lru.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.spi.discovery.tcp.*; -import org.apache.ignite.spi.discovery.tcp.ipfinder.*; -import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; -import org.apache.ignite.testframework.junits.common.*; - -import static org.apache.ignite.cache.CacheAtomicityMode.*; -import static org.apache.ignite.cache.CacheMode.*; -import static org.apache.ignite.cache.CacheRebalanceMode.*; -import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*; - -/** - * LRU near eviction tests for NEAR_ONLY distribution mode (GG-8884). - */ -public class GridCacheNearOnlyLruNearEvictionPolicySelfTest extends GridCommonAbstractTest { - /** */ - private static final TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); - - /** Grid count. */ - private static final int GRID_COUNT = 2; - - /** Maximum size for near eviction policy. */ - private static final int EVICTION_MAX_SIZE = 10; - - /** Node count. */ - private int cnt; - - /** Caching mode specified by test. */ - private CacheMode cacheMode; - - /** Cache atomicity mode specified by test. */ - private CacheAtomicityMode atomicityMode; - - /** {@inheritDoc} */ - @Override protected void beforeTest() throws Exception { - super.beforeTest(); - - cnt = 0; - } - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration c = super.getConfiguration(gridName); - - if (cnt == 0) - c.setClientMode(true); - - CacheConfiguration cc = new CacheConfiguration(); - - cc.setAtomicityMode(atomicityMode); - cc.setCacheMode(cacheMode); - cc.setWriteSynchronizationMode(PRIMARY_SYNC); - cc.setRebalanceMode(SYNC); - cc.setStartSize(100); - cc.setBackups(0); - - c.setCacheConfiguration(cc); - - TcpDiscoverySpi disco = new TcpDiscoverySpi(); - - disco.setIpFinder(ipFinder); - - c.setDiscoverySpi(disco); - - cnt++; - - return c; - } - - /** - * @throws Exception If failed. - */ - public void testPartitionedAtomicNearEvictionMaxSize() throws Exception { - atomicityMode = ATOMIC; - cacheMode = PARTITIONED; - - checkNearEvictionMaxSize(); - } - - /** - * @throws Exception If failed. - */ - public void testPartitionedTransactionalNearEvictionMaxSize() throws Exception { - atomicityMode = TRANSACTIONAL; - cacheMode = PARTITIONED; - - checkNearEvictionMaxSize(); - } - - /** - * @throws Exception If failed. - */ - public void testReplicatedAtomicNearEvictionMaxSize() throws Exception { - atomicityMode = ATOMIC; - cacheMode = REPLICATED; - - checkNearEvictionMaxSize(); - } - - /** - * @throws Exception If failed. - */ - public void testReplicatedTransactionalNearEvictionMaxSize() throws Exception { - atomicityMode = TRANSACTIONAL; - cacheMode = REPLICATED; - - checkNearEvictionMaxSize(); - } - - /** - * @throws Exception If failed. - */ - private void checkNearEvictionMaxSize() throws Exception { - startGrids(GRID_COUNT); - - try { - NearCacheConfiguration nearCfg = new NearCacheConfiguration(); - nearCfg.setNearEvictionPolicy(new LruEvictionPolicy(EVICTION_MAX_SIZE)); - - grid(0).createNearCache(null, nearCfg); - - int cnt = 1000; - - info("Inserting " + cnt + " keys to cache."); - - try (IgniteDataStreamer ldr = grid(1).dataStreamer(null)) { - for (int i = 0; i < cnt; i++) - ldr.addData(i, Integer.toString(i)); - } - - assertTrue("Near cache size " + near(0).nearSize() + ", but eviction maximum size " + EVICTION_MAX_SIZE, - near(0).nearSize() <= EVICTION_MAX_SIZE); - - info("Getting " + cnt + " keys from cache."); - - for (int i = 0; i < cnt; i++) { - IgniteCache cache = grid(0).cache(null); - - assertTrue(cache.get(i).equals(Integer.toString(i))); - } - - assertTrue("Near cache size " + near(0).nearSize() + ", but eviction maximum size " + EVICTION_MAX_SIZE, - near(0).nearSize() <= EVICTION_MAX_SIZE); - } - finally { - stopAllGrids(); - } - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruEvictionPolicySelfTest.java new file mode 100644 index 0000000..8e1e8cf --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruEvictionPolicySelfTest.java @@ -0,0 +1,353 @@ +/* + * 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.eviction.lru; + +import org.apache.ignite.cache.eviction.*; +import org.apache.ignite.cache.eviction.lru.*; +import org.apache.ignite.internal.processors.cache.*; +import org.apache.ignite.internal.processors.cache.eviction.*; + +/** + * LRU Eviction policy tests. + */ +public class LruEvictionPolicySelfTest extends + EvictionAbstractTest> { + /** + * @throws Exception If failed. + */ + public void testMiddleAccess() throws Exception { + startGrid(); + + try { + LruEvictionPolicy p = policy(); + + int max = 8; + + p.setMaxSize(max * MockEntry.ENTRY_SIZE); + + 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); + + check(max, MockEntry.ENTRY_SIZE); + } + finally { + stopGrid(); + } + } + + /** {@inheritDoc} */ + @Override protected void doTestPolicy() throws Exception { + 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"); + + LruEvictionPolicy p = policy(); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1); + + p.onEntryAccessed(false, e2); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2); + + p.onEntryAccessed(false, e3); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + assertFalse(e3.isEvicted()); + + p.onEntryAccessed(false, e4); + + check(p.queue(), e2, e3, e4); + check(MockEntry.ENTRY_SIZE, p.queue(), e2, e3, e4); + + assertTrue(e1.isEvicted()); + assertFalse(e2.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertTrue(e2.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e5, e1); + + assertTrue(e3.isEvicted()); + assertFalse(e1.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e5); + + assertEquals(3, p.getCurrentSize()); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e1, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e5, e1); + + assertFalse(e1.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e5); + + assertEquals(3, p.getCurrentSize()); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e1, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e5); + + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e5); + + check(MockEntry.ENTRY_SIZE, p.queue()); + + assertFalse(e5.isEvicted()); + + info(p); + } + finally { + stopGrid(); + } + } + + /** {@inheritDoc} */ + @Override protected void doTestPolicyWithBatch() throws Exception { + 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"); + + LruEvictionPolicy p = policy(); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1); + + p.onEntryAccessed(false, e2); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2); + + p.onEntryAccessed(false, e3); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + assertFalse(e3.isEvicted()); + + p.onEntryAccessed(false, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3, e4); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + + p.onEntryAccessed(false, e5); + + // Batch evicted + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertTrue(e1.isEvicted()); + assertTrue(e2.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5, e1); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + assertFalse(e1.isEvicted()); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e1, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e1.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5, e1); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + assertFalse(e1.isEvicted()); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e1, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e1.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3); + + assertFalse(e3.isEvicted()); + + p.onEntryAccessed(true, e3); + + check(MockEntry.ENTRY_SIZE, p.queue()); + + info(p); + } + finally { + stopGrid(); + } + } + + /** {@inheritDoc} */ + @Override protected LruEvictionPolicy createPolicy(int plcMax) { + LruEvictionPolicy plc = new LruEvictionPolicy<>(); + + plc.setMaxSize(this.plcMax); + plc.setBatchSize(this.plcBatchSize); + plc.setMaxMemSize(this.plcMaxMemSize); + + return plc; + } + + /** {@inheritDoc} */ + @Override protected LruEvictionPolicy createNearPolicy(int nearMax) { + LruEvictionPolicy plc = new LruEvictionPolicy<>(); + + plc.setMaxSize(nearMax); + plc.setBatchSize(plcBatchSize); + + return plc; + } + + /** {@inheritDoc} */ + @Override protected void checkNearPolicies(int endNearPlcSize) { + for (int i = 0; i < gridCnt; i++) + for (EvictableEntry e : nearPolicy(i).queue()) + assert !e.isCached() : "Invalid near policy size: " + nearPolicy(i).queue(); + } + + /** {@inheritDoc} */ + @Override protected void checkPolicies() { + for (int i = 0; i < gridCnt; i++) { + if (plcMaxMemSize > 0) { + int size = 0; + + for (EvictableEntry entry : policy(i).queue()) + size += ((CacheEvictableEntryImpl)entry).size(); + + assertEquals(size, ((LruEvictionPolicy)policy(i)).getCurrentMemSize()); + } + else + assertTrue(policy(i).queue().size() <= plcMax + plcBatchSize); + } + } + +} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruNearEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruNearEvictionPolicySelfTest.java new file mode 100644 index 0000000..218b817 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruNearEvictionPolicySelfTest.java @@ -0,0 +1,140 @@ +/* + * 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.eviction.lru; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.cache.eviction.lru.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; +import org.apache.ignite.testframework.junits.common.*; + +import java.util.*; + +import static org.apache.ignite.cache.CacheAtomicityMode.*; +import static org.apache.ignite.cache.CacheMode.*; +import static org.apache.ignite.cache.CacheRebalanceMode.*; +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*; + +/** + * LRU near eviction tests (GG-8884). + */ +public class LruNearEvictionPolicySelfTest extends GridCommonAbstractTest { + /** */ + private static final TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); + + /** Maximum size for near eviction policy. */ + private static final int EVICTION_MAX_SIZE = 10; + + /** Grid count. */ + private static final int GRID_COUNT = 2; + + /** Cache atomicity mode specified by test. */ + private CacheAtomicityMode atomicityMode; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration c = super.getConfiguration(gridName); + + CacheConfiguration cc = new CacheConfiguration(); + + cc.setAtomicityMode(atomicityMode); + cc.setCacheMode(PARTITIONED); + cc.setWriteSynchronizationMode(PRIMARY_SYNC); + cc.setRebalanceMode(SYNC); + cc.setStartSize(100); + cc.setBackups(0); + + NearCacheConfiguration nearCfg = new NearCacheConfiguration(); + + LruEvictionPolicy plc = new LruEvictionPolicy(); + plc.setMaxSize(EVICTION_MAX_SIZE); + + nearCfg.setNearEvictionPolicy(plc); + cc.setNearConfiguration(nearCfg); + + c.setCacheConfiguration(cc); + + TcpDiscoverySpi disco = new TcpDiscoverySpi(); + + disco.setIpFinder(ipFinder); + + c.setDiscoverySpi(disco); + + return c; + } + + /** + * @throws Exception If failed. + */ + public void testAtomicNearEvictionMaxSize() throws Exception { + atomicityMode = ATOMIC; + + checkNearEvictionMaxSize(); + } + + /** + * @throws Exception If failed. + */ + public void testTransactionalNearEvictionMaxSize() throws Exception { + atomicityMode = TRANSACTIONAL; + + checkNearEvictionMaxSize(); + } + + /** + * @throws Exception If failed. + */ + private void checkNearEvictionMaxSize() throws Exception { + startGridsMultiThreaded(GRID_COUNT); + + try { + Random rand = new Random(0); + + int cnt = 1000; + + info("Inserting " + cnt + " keys to cache."); + + try (IgniteDataStreamer ldr = grid(0).dataStreamer(null)) { + for (int i = 0; i < cnt; i++) + ldr.addData(i, Integer.toString(i)); + } + + for (int i = 0; i < GRID_COUNT; i++) + assertTrue("Near cache size " + near(i).nearSize() + ", but eviction maximum size " + EVICTION_MAX_SIZE, + near(i).nearSize() <= EVICTION_MAX_SIZE); + + info("Getting " + cnt + " keys from cache."); + + for (int i = 0; i < cnt; i++) { + IgniteCache cache = grid(rand.nextInt(GRID_COUNT)).cache(null); + + assertTrue(cache.get(i).equals(Integer.toString(i))); + } + + for (int i = 0; i < GRID_COUNT; i++) + assertTrue("Near cache size " + near(i).nearSize() + ", but eviction maximum size " + EVICTION_MAX_SIZE, + near(i).nearSize() <= EVICTION_MAX_SIZE); + } + finally { + stopAllGrids(); + } + } +} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruNearOnlyNearEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruNearOnlyNearEvictionPolicySelfTest.java new file mode 100644 index 0000000..faca01d --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/lru/LruNearOnlyNearEvictionPolicySelfTest.java @@ -0,0 +1,175 @@ +/* + * 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.eviction.lru; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.cache.eviction.lru.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; +import org.apache.ignite.testframework.junits.common.*; + +import static org.apache.ignite.cache.CacheAtomicityMode.*; +import static org.apache.ignite.cache.CacheMode.*; +import static org.apache.ignite.cache.CacheRebalanceMode.*; +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*; + +/** + * LRU near eviction tests for NEAR_ONLY distribution mode (GG-8884). + */ +public class LruNearOnlyNearEvictionPolicySelfTest extends GridCommonAbstractTest { + /** */ + private static final TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); + + /** Grid count. */ + private static final int GRID_COUNT = 2; + + /** Maximum size for near eviction policy. */ + private static final int EVICTION_MAX_SIZE = 10; + + /** Node count. */ + private int cnt; + + /** Caching mode specified by test. */ + private CacheMode cacheMode; + + /** Cache atomicity mode specified by test. */ + private CacheAtomicityMode atomicityMode; + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + super.beforeTest(); + + cnt = 0; + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration c = super.getConfiguration(gridName); + + if (cnt == 0) + c.setClientMode(true); + + CacheConfiguration cc = new CacheConfiguration(); + + cc.setAtomicityMode(atomicityMode); + cc.setCacheMode(cacheMode); + cc.setWriteSynchronizationMode(PRIMARY_SYNC); + cc.setRebalanceMode(SYNC); + cc.setStartSize(100); + cc.setBackups(0); + + c.setCacheConfiguration(cc); + + TcpDiscoverySpi disco = new TcpDiscoverySpi(); + + disco.setIpFinder(ipFinder); + + c.setDiscoverySpi(disco); + + cnt++; + + return c; + } + + /** + * @throws Exception If failed. + */ + public void testPartitionedAtomicNearEvictionMaxSize() throws Exception { + atomicityMode = ATOMIC; + cacheMode = PARTITIONED; + + checkNearEvictionMaxSize(); + } + + /** + * @throws Exception If failed. + */ + public void testPartitionedTransactionalNearEvictionMaxSize() throws Exception { + atomicityMode = TRANSACTIONAL; + cacheMode = PARTITIONED; + + checkNearEvictionMaxSize(); + } + + /** + * @throws Exception If failed. + */ + public void testReplicatedAtomicNearEvictionMaxSize() throws Exception { + atomicityMode = ATOMIC; + cacheMode = REPLICATED; + + checkNearEvictionMaxSize(); + } + + /** + * @throws Exception If failed. + */ + public void testReplicatedTransactionalNearEvictionMaxSize() throws Exception { + atomicityMode = TRANSACTIONAL; + cacheMode = REPLICATED; + + checkNearEvictionMaxSize(); + } + + /** + * @throws Exception If failed. + */ + private void checkNearEvictionMaxSize() throws Exception { + startGrids(GRID_COUNT); + + try { + NearCacheConfiguration nearCfg = new NearCacheConfiguration(); + + LruEvictionPolicy plc = new LruEvictionPolicy(); + plc.setMaxSize(EVICTION_MAX_SIZE); + + nearCfg.setNearEvictionPolicy(plc); + + grid(0).createNearCache(null, nearCfg); + + int cnt = 1000; + + info("Inserting " + cnt + " keys to cache."); + + try (IgniteDataStreamer ldr = grid(1).dataStreamer(null)) { + for (int i = 0; i < cnt; i++) + ldr.addData(i, Integer.toString(i)); + } + + assertTrue("Near cache size " + near(0).nearSize() + ", but eviction maximum size " + EVICTION_MAX_SIZE, + near(0).nearSize() <= EVICTION_MAX_SIZE); + + info("Getting " + cnt + " keys from cache."); + + for (int i = 0; i < cnt; i++) { + IgniteCache cache = grid(0).cache(null); + + assertTrue(cache.get(i).equals(Integer.toString(i))); + } + + assertTrue("Near cache size " + near(0).nearSize() + ", but eviction maximum size " + EVICTION_MAX_SIZE, + near(0).nearSize() <= EVICTION_MAX_SIZE); + } + finally { + stopAllGrids(); + } + } +} 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 deleted file mode 100644 index 7088714..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/GridCacheRandomEvictionPolicySelfTest.java +++ /dev/null @@ -1,258 +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.eviction.random; - -import org.apache.ignite.*; -import org.apache.ignite.cache.eviction.random.*; -import org.apache.ignite.internal.processors.cache.eviction.*; -import org.jetbrains.annotations.*; - -import java.util.*; -import java.util.concurrent.*; - -/** - * Random eviction policy test. - */ -public class GridCacheRandomEvictionPolicySelfTest extends - GridCacheEvictionAbstractTest> { - /** - * @throws Exception If failed. - */ - public void testMemory() throws Exception { - try { - Ignite g = startGrid(0); - - int max = 10; - - policy(0).setMaxSize(max); - - int keys = 31; - - for (int i = 0; i < keys; i++) { - String s = Integer.toString(i); - - g.cache(null).put(s, s); - } - - assert g.cache(null).size() <= max; - } - finally { - stopAllGrids(); - } - } - - /** - * @throws Exception If failed. - */ - public void testRandom() throws Exception { - try { - Ignite g = startGrid(0); - - int max = 10; - - policy(0).setMaxSize(max); - - Random rand = new Random(); - - int keys = 31; - - String[] t = new String[keys]; - - for (int i = 0; i < t.length; i++) - t[i] = Integer.toString(i); - - int runs = 10000; - - for (int i = 0; i < runs; i++) { - boolean rmv = rand.nextBoolean(); - - int j = rand.nextInt(t.length); - - if (rmv) - g.cache(null).remove(t[j]); - else - g.cache(null).put(t[j], t[j]); - - if (i % 1000 == 0) - info("Stats [cntr=" + i + ", total=" + runs + ']'); - } - - assert g.cache(null).size() <= max; - - info(policy(0)); - } - finally { - stopAllGrids(); - } - } - - /** - * @throws Exception If failed. - */ - public void testAllowEmptyEntries() throws Exception { - try { - startGrid(); - - IgniteCache 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); - - RandomEvictionPolicy 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 testRandomMultiThreaded() throws Exception { - try { - final Ignite g = startGrid(0); - - int max = 10; - - policy(0).setMaxSize(max); - - final Random rand = new Random(); - - int keys = 31; - - final String[] t = new String[keys]; - - for (int i = 0; i < t.length; i++) - t[i] = Integer.toString(i); - - multithreaded(new Callable() { - @Nullable @Override public Object call() { - int runs = 3000; - - for (int i = 0; i < runs; i++) { - boolean rmv = rand.nextBoolean(); - - int j = rand.nextInt(t.length); - - if (rmv) - g.cache(null).remove(t[j]); - else - g.cache(null).put(t[j], t[j]); - - if (i != 0 && i % 1000 == 0) - info("Stats [cntr=" + i + ", total=" + runs + ']'); - } - - return null; - } - }, 10); - - assert g.cache(null).size() <= max; - - info(policy(0)); - } - finally { - stopAllGrids(); - } - } - - /** {@inheritDoc} */ - @Override public void testPartitionedNearDisabled() throws Exception { - // No-op. - } - - /** {@inheritDoc} */ - @Override public void testPartitionedNearEnabled() throws Exception { - // No-op. - } - - /** {@inheritDoc} */ - @Override public void testPartitionedNearDisabledMultiThreaded() throws Exception { - // No-op. - } - - /** {@inheritDoc} */ - @Override public void testPartitionedNearDisabledBackupSyncMultiThreaded() throws Exception { - // No-op. - } - - /** {@inheritDoc} */ - @Override public void testPartitionedNearEnabledMultiThreaded() throws Exception { - // No-op. - } - - /** {@inheritDoc} */ - @Override public void testPartitionedNearEnabledBackupSyncMultiThreaded() throws Exception { - // No-op. - } - - /** {@inheritDoc} */ - @Override protected RandomEvictionPolicy createPolicy(int plcMax) { - return new RandomEvictionPolicy<>(plcMax); - } - - /** {@inheritDoc} */ - @Override protected RandomEvictionPolicy createNearPolicy(int nearMax) { - return new RandomEvictionPolicy<>(plcMax); - } - - /** {@inheritDoc} */ - @Override protected void checkNearPolicies(int nearMax) { - // No-op. - } - - /** {@inheritDoc} */ - @Override protected void checkPolicies(int plcMax) { - // No-op. - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/RandomEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/RandomEvictionPolicySelfTest.java new file mode 100644 index 0000000..ef34a13 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/random/RandomEvictionPolicySelfTest.java @@ -0,0 +1,357 @@ +/* + * 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.eviction.random; + +import org.apache.ignite.*; +import org.apache.ignite.cache.eviction.random.*; +import org.apache.ignite.internal.processors.cache.eviction.*; +import org.jetbrains.annotations.*; + +import java.util.*; +import java.util.concurrent.*; + +/** + * Random eviction policy test. + */ +public class RandomEvictionPolicySelfTest extends + EvictionAbstractTest> { + /** + * @throws Exception If failed. + */ + public void testMemory() throws Exception { + try { + Ignite g = startGrid(0); + + int max = 10; + + policy(0).setMaxSize(max); + + int keys = 31; + + for (int i = 0; i < keys; i++) { + String s = Integer.toString(i); + + g.cache(null).put(s, s); + } + + assert g.cache(null).size() <= max; + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + public void testRandom() throws Exception { + try { + Ignite g = startGrid(0); + + int max = 10; + + policy(0).setMaxSize(max); + + Random rand = new Random(); + + int keys = 31; + + String[] t = new String[keys]; + + for (int i = 0; i < t.length; i++) + t[i] = Integer.toString(i); + + int runs = 10000; + + for (int i = 0; i < runs; i++) { + boolean rmv = rand.nextBoolean(); + + int j = rand.nextInt(t.length); + + if (rmv) + g.cache(null).remove(t[j]); + else + g.cache(null).put(t[j], t[j]); + + if (i % 1000 == 0) + info("Stats [cntr=" + i + ", total=" + runs + ']'); + } + + assert g.cache(null).size() <= max; + + info(policy(0)); + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + public void testAllowEmptyEntries() throws Exception { + try { + startGrid(); + + IgniteCache 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); + + RandomEvictionPolicy 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 testRandomMultiThreaded() throws Exception { + try { + final Ignite g = startGrid(0); + + int max = 10; + + policy(0).setMaxSize(max); + + final Random rand = new Random(); + + int keys = 31; + + final String[] t = new String[keys]; + + for (int i = 0; i < t.length; i++) + t[i] = Integer.toString(i); + + multithreaded(new Callable() { + @Nullable @Override public Object call() { + int runs = 3000; + + for (int i = 0; i < runs; i++) { + boolean rmv = rand.nextBoolean(); + + int j = rand.nextInt(t.length); + + if (rmv) + g.cache(null).remove(t[j]); + else + g.cache(null).put(t[j], t[j]); + + if (i != 0 && i % 1000 == 0) + info("Stats [cntr=" + i + ", total=" + runs + ']'); + } + + return null; + } + }, 10); + + assert g.cache(null).size() <= max; + + info(policy(0)); + } + finally { + stopAllGrids(); + } + } + + /** {@inheritDoc} */ + @Override public void testMaxMemSizeAllowEmptyEntries() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxMemSizeMemory() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxMemSizePartitionedNearDisabled() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxMemSizePolicy() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxMemSizePolicyWithBatch() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxMemSizePut() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxMemSizeRandom() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizeAllowEmptyEntries() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizeAllowEmptyEntriesWithBatch() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizeMemory() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizeMemoryWithBatch() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override protected void doTestPolicy() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizePut() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizePutWithBatch() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizeRandom() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizeRandomWithBatch() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizePolicyWithBatch() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizePartitionedNearDisabledWithBatch() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override protected void doTestPolicyWithBatch() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testMaxSizePartitionedNearDisabled() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testPartitionedNearEnabled() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testPartitionedNearDisabledMultiThreaded() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testPartitionedNearDisabledBackupSyncMultiThreaded() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testPartitionedNearEnabledMultiThreaded() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void testPartitionedNearEnabledBackupSyncMultiThreaded() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override protected RandomEvictionPolicy createPolicy(int plcMax) { + RandomEvictionPolicy plc = new RandomEvictionPolicy<>(); + + plc.setMaxSize(plcMax); + + return plc; + } + + /** {@inheritDoc} */ + @Override protected RandomEvictionPolicy createNearPolicy(int nearMax) { + RandomEvictionPolicy plc = new RandomEvictionPolicy<>(); + + plc.setMaxSize(plcMax); + + return plc; + } + + /** {@inheritDoc} */ + @Override protected void checkNearPolicies(int nearMax) { + // No-op. + } + + /** {@inheritDoc} */ + @Override protected void checkPolicies() { + // No-op. + } +} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedBatchEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedBatchEvictionPolicySelfTest.java deleted file mode 100644 index 3cec217..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedBatchEvictionPolicySelfTest.java +++ /dev/null @@ -1,385 +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.eviction.sorted; - -import org.apache.ignite.*; -import org.apache.ignite.cache.eviction.*; -import org.apache.ignite.cache.eviction.sorted.*; -import org.apache.ignite.internal.processors.cache.eviction.*; - -import java.util.*; - -import static org.apache.ignite.cache.CacheMode.*; - -/** - * Sorted batch eviction test. - */ -public class GridCacheSortedBatchEvictionPolicySelfTest extends - GridCacheEvictionAbstractTest>{ - /** - * @throws Exception If failed. - */ - public void testPolicy() throws Exception { - try { - startGrid(); - - GridCacheEvictionAbstractTest.MockEntry e1 = new GridCacheEvictionAbstractTest.MockEntry("1", "1"); - GridCacheEvictionAbstractTest.MockEntry e2 = new GridCacheEvictionAbstractTest.MockEntry("2", "2"); - GridCacheEvictionAbstractTest.MockEntry e3 = new GridCacheEvictionAbstractTest.MockEntry("3", "3"); - GridCacheEvictionAbstractTest.MockEntry e4 = new GridCacheEvictionAbstractTest.MockEntry("4", "4"); - GridCacheEvictionAbstractTest.MockEntry e5 = new GridCacheEvictionAbstractTest.MockEntry("5", "5"); - - SortedEvictionPolicy p = policy(); - - p.setMaxSize(3); - - p.setBatchSize(2); - - p.onEntryAccessed(false, e1); - - check(p.set(), e1); - - p.onEntryAccessed(false, e2); - - check(p.set(), e1, e2); - - p.onEntryAccessed(false, e3); - - check(p.set(), e1, e2, e3); - - p.onEntryAccessed(false, e4); - - check(p.set(), e1, e2, e3, e4); - - assertFalse(e1.isEvicted()); - assertFalse(e2.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - - assertEquals(4, p.getCurrentSize()); - - p.onEntryAccessed(false, e5); - - // Batch evicted. - check(p.set(), e3, e4, e5); - - assertEquals(3, p.getCurrentSize()); - - assertTrue(e1.isEvicted()); - assertTrue(e2.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(false, e1 = new GridCacheEvictionAbstractTest.MockEntry("1", "1")); - - check(p.set(), e1, e3, e4, e5); - - assertEquals(4, p.getCurrentSize()); - - assertFalse(e1.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(false, e5); - - check(p.set(), e1, e3, e4, e5); - - assertFalse(e1.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(false, e1); - - assertEquals(4, p.getCurrentSize()); - - check(p.set(), e1, e3, e4, e5); - - assertFalse(e1.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(true, e1); - - assertEquals(3, p.getCurrentSize()); - - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(true, e4); - - assertEquals(2, p.getCurrentSize()); - - assertFalse(e3.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(true, e5); - - assertEquals(1, p.getCurrentSize()); - - assertFalse(e3.isEvicted()); - - p.onEntryAccessed(true, e3); - - assertEquals(0, p.getCurrentSize()); - - assertFalse(e3.isEvicted()); - - info(p); - } - finally { - stopAllGrids(); - } - } - - /** - * @throws Exception If failed. - */ - public void testMemory() throws Exception { - try { - startGrid(); - - SortedEvictionPolicy p = policy(); - - int max = 10; - - int batchSize = 2; - - p.setMaxSize(max); - p.setBatchSize(batchSize); - - int cnt = max + batchSize; - - for (int i = 0; i < cnt; i++) - p.onEntryAccessed(false, new GridCacheEvictionAbstractTest.MockEntry(Integer.toString(i), Integer.toString(i))); - - info(p); - - assertEquals(cnt - batchSize, p.getCurrentSize()); - } - finally { - stopAllGrids(); - } - } - - /** - * @throws Exception If failed. - */ - public void testRandom() throws Exception { - try { - startGrid(); - - SortedEvictionPolicy p = policy(); - - int max = 10; - - int batchSize = 2; - - p.setMaxSize(max); - - p.setBatchSize(batchSize); - - Random rand = new Random(); - - int keys = 31; - - GridCacheEvictionAbstractTest.MockEntry[] fifos = new GridCacheEvictionAbstractTest.MockEntry[keys]; - - for (int i = 0; i < fifos.length; i++) - fifos[i] = new GridCacheEvictionAbstractTest.MockEntry(Integer.toString(i)); - - int runs = 5000000; - - for (int i = 0; i < runs; i++) { - boolean rmv = rand.nextBoolean(); - - int j = rand.nextInt(fifos.length); - - GridCacheEvictionAbstractTest.MockEntry e = entry(fifos, j); - - if (rmv) - fifos[j] = new GridCacheEvictionAbstractTest.MockEntry(Integer.toString(j)); - - p.onEntryAccessed(rmv, e); - } - - info(p); - - int curSize = p.getCurrentSize(); - - assert curSize < max + batchSize : - "curSize < max + batchSize [curSize=" + curSize + ", max=" + max + ", batchSize=" + batchSize + ']'; - } - finally { - stopAllGrids(); - } - } - - /** - * @throws Exception If failed. - */ - public void testAllowEmptyEntries() throws Exception { - try { - startGrid(); - - GridCacheEvictionAbstractTest.MockEntry e1 = new GridCacheEvictionAbstractTest.MockEntry("1"); - - GridCacheEvictionAbstractTest.MockEntry e2 = new GridCacheEvictionAbstractTest.MockEntry("2"); - - GridCacheEvictionAbstractTest.MockEntry e3 = new GridCacheEvictionAbstractTest.MockEntry("3"); - - GridCacheEvictionAbstractTest.MockEntry e4 = new GridCacheEvictionAbstractTest.MockEntry("4"); - - GridCacheEvictionAbstractTest.MockEntry e5 = new GridCacheEvictionAbstractTest.MockEntry("5"); - - SortedEvictionPolicy p = policy(); - - p.setBatchSize(2); - - 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 = 10; - - Ignite ignite = startGrid(); - - try { - IgniteCache 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; - } - } - - // Batch evicted. - 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()); - - // Batch evicted. - assert min >= plcMax : "Min cache size is too small: " + min; - } - finally { - stopAllGrids(); - } - } - - /** {@inheritDoc} */ - @Override public void testPartitionedNearDisabled() throws Exception { - plcBatchSize = 2; - - super.testPartitionedNearDisabled(); - } - - /** {@inheritDoc} */ - @Override protected SortedEvictionPolicy createPolicy(int plcMax) { - return new SortedEvictionPolicy<>(10, 2, null); - } - - /** {@inheritDoc} */ - @Override protected SortedEvictionPolicy createNearPolicy(int nearMax) { - return new SortedEvictionPolicy<>(nearMax, 2, null); - } - - /** {@inheritDoc} */ - @Override protected void checkNearPolicies(int endNearPlcSize) { - for (int i = 0; i < gridCnt; i++) - for (EvictableEntry e : nearPolicy(i).set()) - assert !e.isCached() : "Invalid near policy size: " + nearPolicy(i).set(); - } - - /** {@inheritDoc} */ - @Override protected void checkPolicies(int plcMax) { - for (int i = 0; i < gridCnt; i++) - assert policy(i).set().size() <= plcMax + policy(i).getBatchSize(); - } - -} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedEvictionPolicyPerformanceTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedEvictionPolicyPerformanceTest.java deleted file mode 100644 index 8d97a1e..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedEvictionPolicyPerformanceTest.java +++ /dev/null @@ -1,135 +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.eviction.sorted; - -import org.apache.ignite.*; -import org.apache.ignite.cache.*; -import org.apache.ignite.cache.eviction.fifo.*; -import org.apache.ignite.cache.eviction.sorted.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.util.typedef.internal.*; -import org.apache.ignite.testframework.junits.common.*; - -import org.jsr166.*; - -import java.util.concurrent.*; -import java.util.concurrent.atomic.*; - -/** - * {@link SortedEvictionPolicy} performance test. - */ -public class GridCacheSortedEvictionPolicyPerformanceTest extends GridCommonAbstractTest { - /** Threads. */ - private static final int THREADS = 8; - - /** Keys. */ - private static final int KEYS = 100_000; - - /** Max size. */ - private static final int MAX_SIZE = 1000; - - /** Put probability. */ - private static final int P_PUT = 50; - - /** Get probability. */ - private static final int P_GET = 30; - - /** Measurement count. */ - private static final int MEASUREMENT_CNT = 100; - - /** Rnd. */ - private static final ThreadLocalRandom8 RND = ThreadLocalRandom8.current(); - - /** Ignite. */ - private static Ignite ignite; - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - ignite = startGrid(); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration cfg = super.getConfiguration(gridName); - - CacheConfiguration ccfg = defaultCacheConfiguration(); - - ccfg.setCacheMode(CacheMode.PARTITIONED); - ccfg.setAtomicityMode(CacheAtomicityMode.ATOMIC); - ccfg.setNearConfiguration(null); - ccfg.setEvictionPolicy(new SortedEvictionPolicy(MAX_SIZE)); -// ccfg.setEvictionPolicy(new FifoEvictionPolicy(MAX_SIZE)); - ccfg.setEvictSynchronized(false); - - cfg.setPeerClassLoadingEnabled(false); - - cfg.setCacheConfiguration(ccfg); - - return cfg; - } - - /** - * Tests throughput. - */ - public void testThroughput() throws Exception { - final LongAdder8 cnt = new LongAdder8(); - final AtomicBoolean finished = new AtomicBoolean(); - - final int pPut = P_PUT; - final int pGet = P_PUT + P_GET; - - final IgniteCache cache = ignite.cache(null); - - multithreadedAsync(new Callable() { - @Override public Object call() throws Exception { - for (;;) { - U.sleep(1000); - - info("Ops/sec: " + cnt.sumThenReset()); - } - } - }, 1); - - multithreaded( - new Callable() { - @Override public Object call() throws Exception { - while (!finished.get()) { - int p = RND.nextInt(100); - - int key = RND.nextInt(KEYS); - - if (p >= 0 && p < pPut) - cache.put(key, 0); - else if (p >= pPut && p < pGet) - cache.get(key); - else - cache.remove(key); - - cnt.increment(); - } - - return null; - } - }, THREADS); - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedEvictionPolicySelfTest.java deleted file mode 100644 index 041234e..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedEvictionPolicySelfTest.java +++ /dev/null @@ -1,373 +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.eviction.sorted; - -import org.apache.ignite.*; -import org.apache.ignite.cache.eviction.*; -import org.apache.ignite.cache.eviction.sorted.*; -import org.apache.ignite.internal.processors.cache.eviction.*; - -import java.util.*; - -import static org.apache.ignite.cache.CacheMode.*; - -/** - * Sorted eviction test. - */ -public class GridCacheSortedEvictionPolicySelfTest extends - GridCacheEvictionAbstractTest> { - /** - * @throws Exception If failed. - */ - public void testPolicy() throws Exception { - 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"); - - SortedEvictionPolicy p = policy(); - - p.setMaxSize(3); - - p.onEntryAccessed(false, e1); - - check(p.set(), e1); - - p.onEntryAccessed(false, e2); - - check(p.set(), e1, e2); - - p.onEntryAccessed(false, e3); - - check(p.set(), e1, e2, e3); - - assertFalse(e1.isEvicted()); - assertFalse(e2.isEvicted()); - assertFalse(e3.isEvicted()); - - assertEquals(3, p.getCurrentSize()); - - p.onEntryAccessed(false, e4); - - check(p.set(), e2, e3, e4); - - assertEquals(3, p.getCurrentSize()); - - assertTrue(e1.isEvicted()); - assertFalse(e2.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - - p.onEntryAccessed(false, e5); - - check(p.set(), e3, e4, e5); - - assertEquals(3, p.getCurrentSize()); - - assertTrue(e2.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); - - check(p.set(), e3, e4, e5); - - assertEquals(3, p.getCurrentSize()); - - assertTrue(e1.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(false, e5); - - check(p.set(), e3, e4, e5); - - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(false, e1); - - assertEquals(3, p.getCurrentSize()); - - check(p.set(), e3, e4, e5); - - assertTrue(e1.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(false, e5); - - assertEquals(3, p.getCurrentSize()); - - check(p.set(), e3, e4, e5); - - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(true, e3); - - assertEquals(2, p.getCurrentSize()); - - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(true, e4); - - assertEquals(1, p.getCurrentSize()); - - assertFalse(e4.isEvicted()); - assertFalse(e5.isEvicted()); - - p.onEntryAccessed(true, e5); - - assertEquals(0, p.getCurrentSize()); - - assertFalse(e5.isEvicted()); - - info(p); - } - finally { - stopAllGrids(); - } - } - - /** - * @throws Exception If failed. - */ - public void testMemory() throws Exception { - try { - startGrid(); - - SortedEvictionPolicy 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(); - - SortedEvictionPolicy 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(); - - assertTrue("curSize <= max [curSize=" + curSize + ", max=" + max + ']', curSize <= max); - } - 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"); - - SortedEvictionPolicy 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 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; - } - } - - assertTrue("Min cache size is too small: " + min, min >= plcMax); - - 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()); - - assertTrue("Min cache size is too small: " + min, min >= plcMax); - } - finally { - stopAllGrids(); - } - } - - /** {@inheritDoc} */ - @Override protected SortedEvictionPolicy createPolicy(int plcMax) { - return new SortedEvictionPolicy<>(plcMax); - } - - /** {@inheritDoc} */ - @Override protected SortedEvictionPolicy createNearPolicy(int nearMax) { - return new SortedEvictionPolicy<>(nearMax); - } - - /** {@inheritDoc} */ - @Override protected void checkNearPolicies(int endNearPlcSize) { - for (int i = 0; i < gridCnt; i++) - for (EvictableEntry e : nearPolicy(i).set()) - assert !e.isCached() : "Invalid near policy size: " + nearPolicy(i).set(); - } - - /** {@inheritDoc} */ - @Override protected void checkPolicies(int plcMax) { - for (int i = 0; i < gridCnt; i++) - assert policy(i).set().size() <= plcMax; - } - -} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/SortedEvictionPolicyPerformanceTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/SortedEvictionPolicyPerformanceTest.java new file mode 100644 index 0000000..a687da9 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/SortedEvictionPolicyPerformanceTest.java @@ -0,0 +1,134 @@ +/* + * 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.eviction.sorted; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.cache.eviction.sorted.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.testframework.junits.common.*; + +import org.jsr166.*; + +import java.util.concurrent.*; +import java.util.concurrent.atomic.*; + +/** + * {@link SortedEvictionPolicy} performance test. + */ +public class SortedEvictionPolicyPerformanceTest extends GridCommonAbstractTest { + /** Threads. */ + private static final int THREADS = 8; + + /** Keys. */ + private static final int KEYS = 100_000; + + /** Max size. */ + private static final int MAX_SIZE = 1000; + + /** Put probability. */ + private static final int P_PUT = 50; + + /** Get probability. */ + private static final int P_GET = 30; + + /** Rnd. */ + private static final ThreadLocalRandom8 RND = ThreadLocalRandom8.current(); + + /** Ignite. */ + private static Ignite ignite; + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + ignite = startGrid(); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + CacheConfiguration ccfg = defaultCacheConfiguration(); + + ccfg.setCacheMode(CacheMode.PARTITIONED); + ccfg.setAtomicityMode(CacheAtomicityMode.ATOMIC); + ccfg.setNearConfiguration(null); + + SortedEvictionPolicy plc = new SortedEvictionPolicy(); + plc.setMaxSize(MAX_SIZE); + + ccfg.setEvictionPolicy(plc); + ccfg.setEvictSynchronized(false); + + cfg.setPeerClassLoadingEnabled(false); + + cfg.setCacheConfiguration(ccfg); + + return cfg; + } + + /** + * Tests throughput. + */ + public void testThroughput() throws Exception { + final LongAdder8 cnt = new LongAdder8(); + final AtomicBoolean finished = new AtomicBoolean(); + + final int pPut = P_PUT; + final int pGet = P_PUT + P_GET; + + final IgniteCache cache = ignite.cache(null); + + multithreadedAsync(new Callable() { + @Override public Object call() throws Exception { + for (;;) { + U.sleep(1000); + + info("Ops/sec: " + cnt.sumThenReset()); + } + } + }, 1); + + multithreaded( + new Callable() { + @Override public Object call() throws Exception { + while (!finished.get()) { + int p = RND.nextInt(100); + + int key = RND.nextInt(KEYS); + + if (p >= 0 && p < pPut) + cache.put(key, 0); + else if (p >= pPut && p < pGet) + cache.get(key); + else + cache.remove(key); + + cnt.increment(); + } + + return null; + } + }, THREADS); + } +} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/SortedEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/SortedEvictionPolicySelfTest.java new file mode 100644 index 0000000..dbbbe5d --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/SortedEvictionPolicySelfTest.java @@ -0,0 +1,266 @@ +/* + * 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.eviction.sorted; + +import org.apache.ignite.cache.eviction.sorted.*; +import org.apache.ignite.internal.processors.cache.eviction.*; + +/** + * Sorted eviction policy tests. + */ +public class SortedEvictionPolicySelfTest extends + EvictionAbstractTest> { + /** {@inheritDoc} */ + @Override protected void doTestPolicy() throws Exception { + 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"); + + SortedEvictionPolicy p = policy(); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1); + + p.onEntryAccessed(false, e2); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2); + + p.onEntryAccessed(false, e3); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + assertFalse(e3.isEvicted()); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3); + + p.onEntryAccessed(false, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e2, e3, e4); + + assertTrue(e1.isEvicted()); + assertFalse(e2.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertTrue(e2.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertTrue(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertTrue(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e3); + + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e5); + + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e5); + + check(MockEntry.ENTRY_SIZE, p.queue()); + + assertFalse(e5.isEvicted()); + + info(p); + } + finally { + stopAllGrids(); + } + } + + /** {@inheritDoc} */ + @Override protected void doTestPolicyWithBatch() throws Exception { + 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"); + + SortedEvictionPolicy p = policy(); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1); + + p.onEntryAccessed(false, e2); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2); + + p.onEntryAccessed(false, e3); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3); + + p.onEntryAccessed(false, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3, e4); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + + p.onEntryAccessed(false, e5); + + // Batch evicted. + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertTrue(e1.isEvicted()); + assertTrue(e2.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e3, e4, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e3, e4, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(false, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e3, e4, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e1); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e4); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e5); + + assertFalse(e3.isEvicted()); + assertFalse(e5.isEvicted()); + + p.onEntryAccessed(true, e5); + + check(MockEntry.ENTRY_SIZE, p.queue(), e3); + + assertFalse(e3.isEvicted()); + + p.onEntryAccessed(true, e3); + + check(MockEntry.ENTRY_SIZE, p.queue()); + + assertFalse(e3.isEvicted()); + + info(p); + } + finally { + stopAllGrids(); + } + } + + /** {@inheritDoc} */ + @Override protected SortedEvictionPolicy createPolicy(int plcMax) { + SortedEvictionPolicy plc = new SortedEvictionPolicy<>(); + + plc.setMaxSize(this.plcMax); + plc.setBatchSize(this.plcBatchSize); + plc.setMaxMemSize(this.plcMaxMemSize); + + return plc; + } + + /** {@inheritDoc} */ + @Override protected SortedEvictionPolicy createNearPolicy(int nearMax) { + SortedEvictionPolicy plc = new SortedEvictionPolicy<>(); + + plc.setMaxSize(nearMax); + plc.setBatchSize(plcBatchSize); + + return plc; + } + +} diff --git a/modules/core/src/test/java/org/apache/ignite/loadtests/GridCacheMultiNodeLoadTest.java b/modules/core/src/test/java/org/apache/ignite/loadtests/GridCacheMultiNodeLoadTest.java index d106f5b..0030ce1 100644 --- a/modules/core/src/test/java/org/apache/ignite/loadtests/GridCacheMultiNodeLoadTest.java +++ b/modules/core/src/test/java/org/apache/ignite/loadtests/GridCacheMultiNodeLoadTest.java @@ -65,7 +65,10 @@ public class GridCacheMultiNodeLoadTest extends GridCommonAbstractTest { cacheCfg.setStartSize(10); cacheCfg.setWriteSynchronizationMode(FULL_SYNC); - cacheCfg.setEvictionPolicy(new LruEvictionPolicy(100000)); + LruEvictionPolicy plc = new LruEvictionPolicy(); + plc.setMaxSize(100000); + + cacheCfg.setEvictionPolicy(plc); cacheCfg.setBackups(1); cacheCfg.setRebalanceMode(SYNC); diff --git a/modules/core/src/test/java/org/apache/ignite/loadtests/datastructures/GridCachePartitionedAtomicLongLoadTest.java b/modules/core/src/test/java/org/apache/ignite/loadtests/datastructures/GridCachePartitionedAtomicLongLoadTest.java index 6960fa1..0d9ec8f 100644 --- a/modules/core/src/test/java/org/apache/ignite/loadtests/datastructures/GridCachePartitionedAtomicLongLoadTest.java +++ b/modules/core/src/test/java/org/apache/ignite/loadtests/datastructures/GridCachePartitionedAtomicLongLoadTest.java @@ -70,7 +70,11 @@ public class GridCachePartitionedAtomicLongLoadTest extends GridCommonAbstractTe cc.setStartSize(200); cc.setRebalanceMode(CacheRebalanceMode.SYNC); cc.setWriteSynchronizationMode(FULL_SYNC); - cc.setEvictionPolicy(new LruEvictionPolicy<>(1000)); + + LruEvictionPolicy plc = new LruEvictionPolicy(); + plc.setMaxSize(1000); + + cc.setEvictionPolicy(plc); cc.setBackups(1); cc.setAffinity(new RendezvousAffinityFunction(true)); cc.setEvictSynchronized(true); diff --git a/modules/core/src/test/java/org/apache/ignite/loadtests/swap/GridSwapEvictAllBenchmark.java b/modules/core/src/test/java/org/apache/ignite/loadtests/swap/GridSwapEvictAllBenchmark.java index 1561b77..62066da 100644 --- a/modules/core/src/test/java/org/apache/ignite/loadtests/swap/GridSwapEvictAllBenchmark.java +++ b/modules/core/src/test/java/org/apache/ignite/loadtests/swap/GridSwapEvictAllBenchmark.java @@ -266,7 +266,11 @@ public class GridSwapEvictAllBenchmark { ccfg.setSwapEnabled(true); ccfg.setEvictSynchronized(false); - ccfg.setEvictionPolicy(new FifoEvictionPolicy(EVICT_PLC_SIZE)); + + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(EVICT_PLC_SIZE); + + ccfg.setEvictionPolicy(plc); if (store != null) { ccfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory(store)); diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheEvictionSelfTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheEvictionSelfTestSuite.java index 0852496..917185e 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheEvictionSelfTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheEvictionSelfTestSuite.java @@ -37,15 +37,14 @@ public class IgniteCacheEvictionSelfTestSuite extends TestSuite { public static TestSuite suite() throws Exception { TestSuite suite = new TestSuite("Ignite Cache Eviction Test Suite"); - suite.addTest(new TestSuite(GridCacheFifoEvictionPolicySelfTest.class)); - suite.addTest(new TestSuite(GridCacheFifoBatchEvictionPolicySelfTest.class)); - suite.addTest(new TestSuite(GridCacheSortedEvictionPolicySelfTest.class)); - suite.addTest(new TestSuite(GridCacheSortedBatchEvictionPolicySelfTest.class)); - suite.addTest(new TestSuite(GridCacheLruEvictionPolicySelfTest.class)); - suite.addTest(new TestSuite(GridCacheLruNearEvictionPolicySelfTest.class)); - suite.addTest(new TestSuite(GridCacheNearOnlyLruNearEvictionPolicySelfTest.class)); suite.addTest(new TestSuite(RandomEvictionPolicyCacheSizeSelfTest.class)); - suite.addTest(new TestSuite(GridCacheRandomEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(FifoEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(SortedEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(LruEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(LruNearEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(LruNearOnlyNearEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(RandomEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(RandomEvictionPolicyCacheSizeSelfTest.class)); suite.addTest(new TestSuite(GridCacheNearEvictionSelfTest.class)); suite.addTest(new TestSuite(GridCacheAtomicNearEvictionSelfTest.class)); suite.addTest(new TestSuite(GridCacheEvictionFilterSelfTest.class)); diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/GridIndexingWithNoopSwapSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/GridIndexingWithNoopSwapSelfTest.java index 2c0962b..5b623da 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/GridIndexingWithNoopSwapSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/GridIndexingWithNoopSwapSelfTest.java @@ -64,7 +64,11 @@ public class GridIndexingWithNoopSwapSelfTest extends GridCommonAbstractTest { cc.setRebalanceMode(SYNC); cc.setSwapEnabled(true); cc.setNearConfiguration(new NearCacheConfiguration()); - cc.setEvictionPolicy(new FifoEvictionPolicy(1000)); + + FifoEvictionPolicy plc = new FifoEvictionPolicy(); + plc.setMaxSize(1000); + + cc.setEvictionPolicy(plc); cc.setBackups(1); cc.setAtomicityMode(TRANSACTIONAL); cc.setIndexedTypes( diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheQueryMultiThreadedSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheQueryMultiThreadedSelfTest.java index 23a97c9..1d6bbc8 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheQueryMultiThreadedSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheQueryMultiThreadedSelfTest.java @@ -104,7 +104,16 @@ public class IgniteCacheQueryMultiThreadedSelfTest extends GridCommonAbstractTes cacheCfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC); cacheCfg.setSwapEnabled(true); cacheCfg.setBackups(1); - cacheCfg.setEvictionPolicy(evictsEnabled() ? new LruEvictionPolicy(100) : null); + + LruEvictionPolicy plc = null; + + if (evictsEnabled()) { + plc = new LruEvictionPolicy(); + plc.setMaxSize(100); + } + + cacheCfg.setEvictionPolicy(plc); + cacheCfg.setSqlOnheapRowCacheSize(128); cacheCfg.setIndexedTypes( Integer.class, Integer.class, diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/ttl/CacheTtlAbstractSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/ttl/CacheTtlAbstractSelfTest.java index 3833576..4553fec 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/ttl/CacheTtlAbstractSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/ttl/CacheTtlAbstractSelfTest.java @@ -67,7 +67,11 @@ public abstract class CacheTtlAbstractSelfTest extends GridCommonAbstractTest { ccfg.setAtomicityMode(atomicityMode()); ccfg.setMemoryMode(memoryMode()); ccfg.setOffHeapMaxMemory(0); - ccfg.setEvictionPolicy(new LruEvictionPolicy(MAX_CACHE_SIZE)); + + LruEvictionPolicy plc = new LruEvictionPolicy(); + plc.setMaxSize(MAX_CACHE_SIZE); + + ccfg.setEvictionPolicy(plc); ccfg.setIndexedTypes(Integer.class, Integer.class); ccfg.setBackups(2); ccfg.setWriteSynchronizationMode(FULL_SYNC); diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/IgniteNode.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/IgniteNode.java index 8638623..2027b80 100644 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/IgniteNode.java +++ b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/IgniteNode.java @@ -104,8 +104,11 @@ public class IgniteNode implements BenchmarkServer { if (args.isOffheapValues()) cc.setMemoryMode(OFFHEAP_VALUES); - else - cc.setEvictionPolicy(new LruEvictionPolicy(50000)); + else { + LruEvictionPolicy plc = new LruEvictionPolicy(); + plc.setMaxSize(50000); + cc.setEvictionPolicy(plc); + } } cc.setReadThrough(args.isStoreEnabled()); -- 1.9.5.msysgit.1