ignite-485 Need to implement SortedEvictionPolicy
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/f6c7df6f Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/f6c7df6f Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/f6c7df6f Branch: refs/heads/ignite-446 Commit: f6c7df6fe2dd04ecd0cd4b800cff8b460e142a8f Parents: fef22b6 Author: agura <ag...@gridgain.com> Authored: Mon Apr 20 19:47:21 2015 +0300 Committer: agura <ag...@gridgain.com> Committed: Mon Apr 20 19:47:21 2015 +0300 ---------------------------------------------------------------------- .../cache/eviction/fifo/FifoEvictionPolicy.java | 2 +- .../eviction/sorted/SortedEvictionPolicy.java | 67 +++- .../sorted/SortedEvictionPolicyMBean.java | 16 + ...dCacheSortedBatchEvictionPolicySelfTest.java | 385 +++++++++++++++++++ .../GridCacheSortedEvictionPolicySelfTest.java | 373 ++++++++++++++++++ .../GridSortedEvictionPolicySelfTest.java | 373 ------------------ .../IgniteCacheEvictionSelfTestSuite.java | 3 +- 7 files changed, 833 insertions(+), 386 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f6c7df6f/modules/core/src/main/java/org/apache/ignite/cache/eviction/fifo/FifoEvictionPolicy.java ---------------------------------------------------------------------- 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 375e955..bf8cf0d 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 @@ -72,7 +72,7 @@ public class FifoEvictionPolicy<K, V> implements EvictionPolicy<K, V>, FifoEvict * Constructs FIFO eviction policy with maximum size and given batch size. Empty entries are allowed. * * @param max Maximum allowed size of cache before entry will start getting evicted. - * @param batchSize Maximum size of batch. + * @param batchSize Batch size. */ public FifoEvictionPolicy(int max, int batchSize) { A.ensure(max > 0, "max > 0"); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f6c7df6f/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicy.java ---------------------------------------------------------------------- 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 0e2b610..0065244 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 @@ -32,18 +32,30 @@ import static java.lang.Math.*; import static org.apache.ignite.configuration.CacheConfiguration.*; /** - * Cache eviction policy which will select the minimum cache entry for eviction if cache - * size exceeds the {@link #getMaxSize()} parameter. Entries comparison based on {@link Comparator} instance if provided. + * Cache eviction policy which will select the minimum cache entry for eviction. + * <p> + * The eviction starts when the cache size becomes {@code batchSize} elements greater than the maximum size. + * {@code batchSize} elements will be evicted in this case. The default {@code batchSize} value is {@code 1}. + * <p> + * Entries comparison based on {@link Comparator} instance if provided. * Default {@code Comparator} behaviour is use cache entries keys for comparison that imposes a requirement for keys * to implement {@link Comparable} interface. + * <p> + * User defined comparator should implement {@link Serializable} interface. */ -public class SortedEvictionPolicy<K, V> implements EvictionPolicy<K, V>, SortedEvictionPolicyMBean, Serializable { +public class SortedEvictionPolicy<K, V> implements EvictionPolicy<K, V>, SortedEvictionPolicyMBean, Externalizable { /** */ private static final long serialVersionUID = 0L; /** Maximum size. */ private volatile int max; + /** Batch size. */ + private volatile int batchSize = 1; + + /** Comparator. */ + private Comparator<Holder<K, V>> comp; + /** Order. */ private final AtomicLong orderCnt = new AtomicLong(); @@ -67,26 +79,30 @@ public class SortedEvictionPolicy<K, V> implements EvictionPolicy<K, V>, SortedE } /** - * Constructs sorted eviction policy with default maximum size and given entry comparator. + * Constructs sorted eviction policy with given maximum size and given entry comparator. * + * @param max Maximum allowed size of cache before entry will start getting evicted. * @param comp Entries comparator. */ - public SortedEvictionPolicy(Comparator<EvictableEntry<K, V>> comp) { - this(DFLT_CACHE_SIZE, comp); + public SortedEvictionPolicy(int max, @Nullable Comparator<EvictableEntry<K, V>> comp) { + this(max, 1, comp); } /** - * Constructs sorted eviction policy with given maximum size and entries comparator. + * Constructs sorted eviction policy with given maximum size, eviction batch size and entries comparator. * * @param max Maximum allowed size of cache before entry will start getting evicted. + * @param batchSize Batch size. * @param comp Entries comparator. */ - public SortedEvictionPolicy(int max, Comparator<EvictableEntry<K, V>> comp) { + public SortedEvictionPolicy(int max, int batchSize, @Nullable Comparator<EvictableEntry<K, V>> comp) { A.ensure(max > 0, "max > 0"); + A.ensure(batchSize > 0, "batchSize > 0"); this.max = max; - this.set = new GridConcurrentSkipListSetEx<>( - comp == null ? new DefaultHolderComparator<K, V>() : new HolderComparator<>(comp)); + this.batchSize = batchSize; + this.comp = comp == null ? new DefaultHolderComparator<K, V>() : new HolderComparator<>(comp); + this.set = new GridConcurrentSkipListSetEx<>(this.comp); } /** @@ -110,6 +126,18 @@ public class SortedEvictionPolicy<K, V> implements EvictionPolicy<K, V>, SortedE } /** {@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 set.sizex(); } @@ -192,9 +220,11 @@ public class SortedEvictionPolicy<K, V> implements EvictionPolicy<K, V>, SortedE private void shrink() { int max = this.max; + int batchSize = this.batchSize; + int startSize = set.sizex(); - if (startSize > max) { + if (startSize >= max + batchSize) { for (int i = max; i < startSize && set.sizex() > max; i++) { Holder<K, V> h = set.pollFirst(); @@ -209,6 +239,21 @@ public class SortedEvictionPolicy<K, V> implements EvictionPolicy<K, V>, SortedE } } + /** {@inheritDoc} */ + @Override public void writeExternal(ObjectOutput out) throws IOException { + out.writeInt(max); + out.writeInt(batchSize); + out.writeObject(comp); + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + max = in.readInt(); + batchSize = in.readInt(); + comp = (Comparator<Holder<K, V>>)in.readObject(); + } + /** * Removes holder from backed set and marks holder as removed. * http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f6c7df6f/modules/core/src/main/java/org/apache/ignite/cache/eviction/sorted/SortedEvictionPolicyMBean.java ---------------------------------------------------------------------- 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 f8b0d23..bc696ff 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 @@ -41,6 +41,22 @@ public interface SortedEvictionPolicyMBean { 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 size. * * @return Current size. http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f6c7df6f/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedBatchEvictionPolicySelfTest.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..3cec217 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedBatchEvictionPolicySelfTest.java @@ -0,0 +1,385 @@ +/* + * 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<SortedEvictionPolicy<String, String>>{ + /** + * @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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<Object, Object> 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<String, String> createPolicy(int plcMax) { + return new SortedEvictionPolicy<>(10, 2, null); + } + + /** {@inheritDoc} */ + @Override protected SortedEvictionPolicy<String, String> 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<String, String> 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(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f6c7df6f/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedEvictionPolicySelfTest.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..041234e --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedEvictionPolicySelfTest.java @@ -0,0 +1,373 @@ +/* + * 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<SortedEvictionPolicy<String, String>> { + /** + * @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<String, String> 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<String, String> p = policy(); + + int max = 10; + + p.setMaxSize(max); + + int cnt = 11; + + for (int i = 0; i < cnt; i++) + p.onEntryAccessed(false, new MockEntry(Integer.toString(i), Integer.toString(i))); + + info(p); + + assertEquals(max, p.getCurrentSize()); + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + public void testRandom() throws Exception { + try { + startGrid(); + + SortedEvictionPolicy<String, String> p = policy(); + + int max = 10; + + p.setMaxSize(max); + + Random rand = new Random(); + + int keys = 31; + + MockEntry[] fifos = new MockEntry[keys]; + + for (int i = 0; i < fifos.length; i++) + fifos[i] = new MockEntry(Integer.toString(i)); + + int runs = 5000000; + + for (int i = 0; i < runs; i++) { + boolean rmv = rand.nextBoolean(); + + int j = rand.nextInt(fifos.length); + + MockEntry e = entry(fifos, j); + + if (rmv) + fifos[j] = new MockEntry(Integer.toString(j)); + + p.onEntryAccessed(rmv, e); + } + + info(p); + + int curSize = p.getCurrentSize(); + + 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<String, String> p = policy(); + + p.setMaxSize(10); + + p.onEntryAccessed(false, e1); + + assertFalse(e1.isEvicted()); + + p.onEntryAccessed(false, e2); + + assertFalse(e1.isEvicted()); + assertFalse(e2.isEvicted()); + + p.onEntryAccessed(false, e3); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + + p.onEntryAccessed(false, e4); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e4.isEvicted()); + + p.onEntryAccessed(false, e5); + + assertFalse(e1.isEvicted()); + assertFalse(e3.isEvicted()); + assertFalse(e5.isEvicted()); + } + finally { + stopAllGrids(); + } + } + + /** + * @throws Exception If failed. + */ + public void testPut() throws Exception { + mode = LOCAL; + syncCommit = true; + plcMax = 100; + + Ignite ignite = startGrid(); + + try { + IgniteCache<Object, Object> cache = ignite.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<String, String> createPolicy(int plcMax) { + return new SortedEvictionPolicy<>(plcMax); + } + + /** {@inheritDoc} */ + @Override protected SortedEvictionPolicy<String, String> createNearPolicy(int nearMax) { + return new SortedEvictionPolicy<>(nearMax); + } + + /** {@inheritDoc} */ + @Override protected void checkNearPolicies(int endNearPlcSize) { + for (int i = 0; i < gridCnt; i++) + for (EvictableEntry<String, String> 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; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f6c7df6f/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridSortedEvictionPolicySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridSortedEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridSortedEvictionPolicySelfTest.java deleted file mode 100644 index 5e255a1..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridSortedEvictionPolicySelfTest.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 GridSortedEvictionPolicySelfTest extends - GridCacheEvictionAbstractTest<SortedEvictionPolicy<String, String>> { - /** - * @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<String, String> 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<String, String> p = policy(); - - int max = 10; - - p.setMaxSize(max); - - int cnt = 11; - - for (int i = 0; i < cnt; i++) - p.onEntryAccessed(false, new MockEntry(Integer.toString(i), Integer.toString(i))); - - info(p); - - assertEquals(max, p.getCurrentSize()); - } - finally { - stopAllGrids(); - } - } - - /** - * @throws Exception If failed. - */ - public void testRandom() throws Exception { - try { - startGrid(); - - SortedEvictionPolicy<String, String> p = policy(); - - int max = 10; - - p.setMaxSize(max); - - Random rand = new Random(); - - int keys = 31; - - MockEntry[] fifos = new MockEntry[keys]; - - for (int i = 0; i < fifos.length; i++) - fifos[i] = new MockEntry(Integer.toString(i)); - - int runs = 5000000; - - for (int i = 0; i < runs; i++) { - boolean rmv = rand.nextBoolean(); - - int j = rand.nextInt(fifos.length); - - MockEntry e = entry(fifos, j); - - if (rmv) - fifos[j] = new MockEntry(Integer.toString(j)); - - p.onEntryAccessed(rmv, e); - } - - info(p); - - int curSize = p.getCurrentSize(); - - 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<String, String> p = policy(); - - p.setMaxSize(10); - - p.onEntryAccessed(false, e1); - - assertFalse(e1.isEvicted()); - - p.onEntryAccessed(false, e2); - - assertFalse(e1.isEvicted()); - assertFalse(e2.isEvicted()); - - p.onEntryAccessed(false, e3); - - assertFalse(e1.isEvicted()); - assertFalse(e3.isEvicted()); - - p.onEntryAccessed(false, e4); - - assertFalse(e1.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e4.isEvicted()); - - p.onEntryAccessed(false, e5); - - assertFalse(e1.isEvicted()); - assertFalse(e3.isEvicted()); - assertFalse(e5.isEvicted()); - } - finally { - stopAllGrids(); - } - } - - /** - * @throws Exception If failed. - */ - public void testPut() throws Exception { - mode = LOCAL; - syncCommit = true; - plcMax = 100; - - Ignite ignite = startGrid(); - - try { - IgniteCache<Object, Object> cache = ignite.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<String, String> createPolicy(int plcMax) { - return new SortedEvictionPolicy<>(plcMax); - } - - /** {@inheritDoc} */ - @Override protected SortedEvictionPolicy<String, String> createNearPolicy(int nearMax) { - return new SortedEvictionPolicy<>(nearMax); - } - - /** {@inheritDoc} */ - @Override protected void checkNearPolicies(int endNearPlcSize) { - for (int i = 0; i < gridCnt; i++) - for (EvictableEntry<String, String> 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; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f6c7df6f/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheEvictionSelfTestSuite.java ---------------------------------------------------------------------- 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 2c0c46d..8918a29 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 @@ -39,7 +39,8 @@ public class IgniteCacheEvictionSelfTestSuite extends TestSuite { suite.addTest(new TestSuite(GridCacheFifoEvictionPolicySelfTest.class)); suite.addTest(new TestSuite(GridCacheFifoBatchEvictionPolicySelfTest.class)); - suite.addTest(new TestSuite(GridSortedEvictionPolicySelfTest.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));