http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/86ed8b82/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 index 041234e..c604d8b 100644 --- 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 @@ -20,6 +20,7 @@ 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.*; import org.apache.ignite.internal.processors.cache.eviction.*; import java.util.*; @@ -50,27 +51,27 @@ public class GridCacheSortedEvictionPolicySelfTest extends p.onEntryAccessed(false, e1); - check(p.set(), e1); + check(p.queue(), e1); + check(MockEntry.ENTRY_SIZE, p.queue(), e1); p.onEntryAccessed(false, e2); - check(p.set(), e1, e2); + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2); p.onEntryAccessed(false, e3); - check(p.set(), e1, e2, e3); + check(p.queue(), e1, e2, e3); + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3); assertFalse(e1.isEvicted()); assertFalse(e2.isEvicted()); assertFalse(e3.isEvicted()); - assertEquals(3, p.getCurrentSize()); + check(MockEntry.ENTRY_SIZE, p.queue(), e1, e2, e3); p.onEntryAccessed(false, e4); - check(p.set(), e2, e3, e4); - - assertEquals(3, p.getCurrentSize()); + check(MockEntry.ENTRY_SIZE, p.queue(), e2, e3, e4); assertTrue(e1.isEvicted()); assertFalse(e2.isEvicted()); @@ -79,9 +80,7 @@ public class GridCacheSortedEvictionPolicySelfTest extends p.onEntryAccessed(false, e5); - check(p.set(), e3, e4, e5); - - assertEquals(3, p.getCurrentSize()); + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); assertTrue(e2.isEvicted()); assertFalse(e3.isEvicted()); @@ -90,9 +89,7 @@ public class GridCacheSortedEvictionPolicySelfTest extends p.onEntryAccessed(false, e1 = new MockEntry("1", "1")); - check(p.set(), e3, e4, e5); - - assertEquals(3, p.getCurrentSize()); + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); assertTrue(e1.isEvicted()); assertFalse(e3.isEvicted()); @@ -101,7 +98,7 @@ public class GridCacheSortedEvictionPolicySelfTest extends p.onEntryAccessed(false, e5); - check(p.set(), e3, e4, e5); + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); assertFalse(e3.isEvicted()); assertFalse(e4.isEvicted()); @@ -109,9 +106,7 @@ public class GridCacheSortedEvictionPolicySelfTest extends p.onEntryAccessed(false, e1); - assertEquals(3, p.getCurrentSize()); - - check(p.set(), e3, e4, e5); + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); assertTrue(e1.isEvicted()); assertFalse(e3.isEvicted()); @@ -120,9 +115,7 @@ public class GridCacheSortedEvictionPolicySelfTest extends p.onEntryAccessed(false, e5); - assertEquals(3, p.getCurrentSize()); - - check(p.set(), e3, e4, e5); + check(MockEntry.ENTRY_SIZE, p.queue(), e3, e4, e5); assertFalse(e3.isEvicted()); assertFalse(e4.isEvicted()); @@ -130,7 +123,7 @@ public class GridCacheSortedEvictionPolicySelfTest extends p.onEntryAccessed(true, e3); - assertEquals(2, p.getCurrentSize()); + check(MockEntry.ENTRY_SIZE, p.queue(), e4, e5); assertFalse(e3.isEvicted()); assertFalse(e4.isEvicted()); @@ -138,14 +131,14 @@ public class GridCacheSortedEvictionPolicySelfTest extends p.onEntryAccessed(true, e4); - assertEquals(1, p.getCurrentSize()); + check(MockEntry.ENTRY_SIZE, p.queue(), e5); assertFalse(e4.isEvicted()); assertFalse(e5.isEvicted()); p.onEntryAccessed(true, e5); - assertEquals(0, p.getCurrentSize()); + check(MockEntry.ENTRY_SIZE, p.queue()); assertFalse(e5.isEvicted()); @@ -176,7 +169,7 @@ public class GridCacheSortedEvictionPolicySelfTest extends info(p); - assertEquals(max, p.getCurrentSize()); + check(max, MockEntry.ENTRY_SIZE); } finally { stopAllGrids(); @@ -222,9 +215,8 @@ public class GridCacheSortedEvictionPolicySelfTest extends info(p); - int curSize = p.getCurrentSize(); - - assertTrue("curSize <= max [curSize=" + curSize + ", max=" + max + ']', curSize <= max); + assertTrue(p.getCurrentSize() <= max); + assertTrue(p.getCurrentMemSize() <= max * MockEntry.ENTRY_SIZE); } finally { stopAllGrids(); @@ -239,13 +231,9 @@ public class GridCacheSortedEvictionPolicySelfTest extends 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(); @@ -256,27 +244,37 @@ public class GridCacheSortedEvictionPolicySelfTest extends 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(); @@ -296,12 +294,16 @@ public class GridCacheSortedEvictionPolicySelfTest extends try { IgniteCache<Object, Object> cache = ignite.cache(null); + GridCacheContext cctx = ((IgniteCacheProxy)cache).context(); + int cnt = 500; int min = Integer.MAX_VALUE; int minIdx = 0; + int entrySize = 0; + for (int i = 0; i < cnt; i++) { cache.put(i, i); @@ -311,10 +313,16 @@ public class GridCacheSortedEvictionPolicySelfTest extends min = cacheSize; minIdx = i; } + + if (entrySize == 0) + entrySize = cctx.toCacheKeyObject(i).valueBytes(cctx.cacheObjectContext()).length + + cctx.toCacheObject(i).valueBytes(cctx.cacheObjectContext()).length; } assertTrue("Min cache size is too small: " + min, min >= plcMax); + check(plcMax, entrySize); + info("Min cache size [min=" + min + ", idx=" + minIdx + ']'); info("Current cache size " + cache.size()); info("Current cache key size " + cache.size()); @@ -341,6 +349,8 @@ public class GridCacheSortedEvictionPolicySelfTest extends info("Current cache key size " + cache.size()); assertTrue("Min cache size is too small: " + min, min >= plcMax); + + check(plcMax, entrySize); } finally { stopAllGrids(); @@ -360,14 +370,21 @@ public class GridCacheSortedEvictionPolicySelfTest extends /** {@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(); + for (EvictableEntry<String, String> 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).set().size() <= plcMax; - } + @Override protected void checkPolicies() { + for (int i = 0; i < gridCnt; i++) { + assert policy(i).queue().size() <= plcMax; + + int size = 0; + for (EvictableEntry<String, String> entry : policy(i).queue()) + size += ((CacheEvictableEntryImpl)entry).size(); + + assertEquals(size, ((SortedEvictionPolicy)policy(i)).getCurrentMemSize()); + } + } }
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/86ed8b82/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedMemoryEvictionPolicySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedMemoryEvictionPolicySelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedMemoryEvictionPolicySelfTest.java new file mode 100644 index 0000000..da898b3 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/sorted/GridCacheSortedMemoryEvictionPolicySelfTest.java @@ -0,0 +1,410 @@ +/* + * 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.*; +import org.apache.ignite.internal.processors.cache.eviction.*; + +import java.util.*; + +import static org.apache.ignite.cache.CacheMode.*; + +/** + * Sorted memory size limited eviction test. + */ +public class GridCacheSortedMemoryEvictionPolicySelfTest 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.setMaxMemSize(6); + + p.onEntryAccessed(false, e1); + + check(p.queue(), 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(p.queue(), e1, e2, 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(); + } + } + + /** + * @throws Exception If failed. + */ + public void testMemory() throws Exception { + try { + startGrid(); + + SortedEvictionPolicy<String, String> p = policy(); + + int max = 10; + + p.setMaxMemSize(20); + + int cnt = 11; + + 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. + */ + public void testRandom() throws Exception { + try { + startGrid(); + + SortedEvictionPolicy<String, String> p = policy(); + + int max = 10; + + p.setMaxMemSize(10); + + 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); + + assertTrue(p.getCurrentSize() <= max); + assertTrue(p.getCurrentMemSize() <= max * MockEntry.ENTRY_SIZE); + } + 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.setMaxMemSize(10); + + 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. + */ + public void testPut() throws Exception { + mode = LOCAL; + syncCommit = true; + plcMax = 100; + + Ignite ignite = startGrid(); + + try { + IgniteCache<Object, Object> cache = ignite.cache(null); + + GridCacheContext cctx = ((IgniteCacheProxy)cache).context(); + + int cnt = 500; + + int min = Integer.MAX_VALUE; + + int minIdx = 0; + + int entrySize = 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; + } + + if (entrySize == 0) + entrySize = cctx.toCacheKeyObject(i).valueBytes(cctx.cacheObjectContext()).length + + cctx.toCacheObject(i).valueBytes(cctx.cacheObjectContext()).length; + } + + assertTrue("Min cache size is too small: " + min, min >= plcMax); + + check(plcMax, entrySize); + + 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); + + check(plcMax, entrySize); + } + finally { + stopAllGrids(); + } + } + + /** @throws Exception If failed. */ + public void testPartitionedNearDisabled() throws Exception { + mode = PARTITIONED; + nearEnabled = false; + plcMax = 0; + plcMaxMemSize = 100; + syncCommit = true; + + gridCnt = 2; + + checkPartitioned(); + } + + /** {@inheritDoc} */ + @Override protected SortedEvictionPolicy<String, String> createPolicy(int plcMax) { + SortedEvictionPolicy<String, String> plc = new SortedEvictionPolicy<>(); + + plc.setMaxMemSize(1000); + + return plc; + } + + /** {@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).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<String, String> entry : policy(i).queue()) + size += ((CacheEvictableEntryImpl)entry).size(); + + assertEquals(size, ((SortedEvictionPolicy)policy(i)).getCurrentMemSize()); + } + else + assert policy(i).queue().size() <= plcMax; + } + + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/86ed8b82/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 8918a29..ef14bbe 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,8 +39,12 @@ public class IgniteCacheEvictionSelfTestSuite extends TestSuite { suite.addTest(new TestSuite(GridCacheFifoEvictionPolicySelfTest.class)); suite.addTest(new TestSuite(GridCacheFifoBatchEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(GridCacheFifoMemoryEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(GridCacheSortedEvictionPolicySelfTest.class)); suite.addTest(new TestSuite(GridCacheSortedBatchEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(GridCacheSortedMemoryEvictionPolicySelfTest.class)); + suite.addTest(new TestSuite(GridCacheLruEvictionPolicySelfTest.class)); suite.addTest(new TestSuite(GridCacheLruNearEvictionPolicySelfTest.class)); suite.addTest(new TestSuite(GridCacheNearOnlyLruNearEvictionPolicySelfTest.class));