Repository: phoenix Updated Branches: refs/heads/4.x-HBase-0.98 f8df68997 -> ca664fe10 refs/heads/4.x-HBase-1.1 fd2b064a0 -> 4e51fe772 refs/heads/4.x-HBase-1.2 6f923a419 -> d714afcec refs/heads/master 84dc1d44a -> 764eb8f13
PHOENIX-4225 Using Google cache may lead to lock up on RS side. Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/764eb8f1 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/764eb8f1 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/764eb8f1 Branch: refs/heads/master Commit: 764eb8f13237a89961e259aca366e45b38b76ef1 Parents: 84dc1d4 Author: Sergey Soldatov <s...@apache.org> Authored: Tue Sep 26 14:41:53 2017 -0700 Committer: Sergey Soldatov <s...@apache.org> Committed: Wed Sep 27 10:42:59 2017 -0700 ---------------------------------------------------------------------- .../apache/phoenix/cache/TenantCacheImpl.java | 2 ++ .../apache/phoenix/cache/TenantCacheTest.java | 37 +++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/764eb8f1/phoenix-core/src/main/java/org/apache/phoenix/cache/TenantCacheImpl.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/cache/TenantCacheImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/cache/TenantCacheImpl.java index 3d178f6..fdf0646 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/cache/TenantCacheImpl.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/cache/TenantCacheImpl.java @@ -100,11 +100,13 @@ public class TenantCacheImpl implements TenantCache { @Override public Closeable getServerCache(ImmutableBytesPtr cacheId) { + getServerCaches().cleanUp(); return getServerCaches().getIfPresent(cacheId); } @Override public Closeable addServerCache(ImmutableBytesPtr cacheId, ImmutableBytesWritable cachePtr, byte[] txState, ServerCacheFactory cacheFactory, boolean useProtoForIndexMaintainer) throws SQLException { + getServerCaches().cleanUp(); MemoryChunk chunk = this.getMemoryManager().allocate(cachePtr.getLength() + txState.length); boolean success = false; try { http://git-wip-us.apache.org/repos/asf/phoenix/blob/764eb8f1/phoenix-core/src/test/java/org/apache/phoenix/cache/TenantCacheTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/cache/TenantCacheTest.java b/phoenix-core/src/test/java/org/apache/phoenix/cache/TenantCacheTest.java index 932149c..f4c83b2 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/cache/TenantCacheTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/cache/TenantCacheTest.java @@ -18,6 +18,7 @@ package org.apache.phoenix.cache; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import java.io.Closeable; import java.io.DataInput; @@ -67,7 +68,41 @@ public class TenantCacheTest { cache.cleanUp(); assertEquals(maxBytes, memoryManager.getAvailableMemory()); } - + + + @Test + public void testFreeMemoryOnAccess() throws Exception { + int maxServerCacheTimeToLive = 10; + long maxBytes = 1000; + GlobalMemoryManager memoryManager = new GlobalMemoryManager(maxBytes); + ManualTicker ticker = new ManualTicker(); + TenantCacheImpl cache = new TenantCacheImpl(memoryManager, maxServerCacheTimeToLive, ticker); + ImmutableBytesPtr cacheId1 = new ImmutableBytesPtr(Bytes.toBytes("a")); + ImmutableBytesWritable cachePtr = new ImmutableBytesWritable(Bytes.toBytes("a")); + cache.addServerCache(cacheId1, cachePtr, ByteUtil.EMPTY_BYTE_ARRAY, cacheFactory, true); + assertEquals(maxBytes-1, memoryManager.getAvailableMemory()); + ticker.time += (maxServerCacheTimeToLive + 1) * 1000000; + assertNull(cache.getServerCache(cacheId1)); + assertEquals(maxBytes, memoryManager.getAvailableMemory()); + } + + @Test + public void testExpiredCacheOnAddingNew() throws Exception { + int maxServerCacheTimeToLive = 10; + long maxBytes = 10; + GlobalMemoryManager memoryManager = new GlobalMemoryManager(maxBytes); + ManualTicker ticker = new ManualTicker(); + TenantCacheImpl cache = new TenantCacheImpl(memoryManager, maxServerCacheTimeToLive, ticker); + ImmutableBytesPtr cacheId1 = new ImmutableBytesPtr(Bytes.toBytes("a")); + ImmutableBytesPtr cacheId2 = new ImmutableBytesPtr(Bytes.toBytes("b")); + ImmutableBytesWritable cachePtr = new ImmutableBytesWritable(Bytes.toBytes("12345678")); + cache.addServerCache(cacheId1, cachePtr, ByteUtil.EMPTY_BYTE_ARRAY, cacheFactory, true); + assertEquals(2, memoryManager.getAvailableMemory()); + ticker.time += (maxServerCacheTimeToLive + 1) * 1000000; + cache.addServerCache(cacheId1, cachePtr, ByteUtil.EMPTY_BYTE_ARRAY, cacheFactory, true); + assertEquals(2, memoryManager.getAvailableMemory()); + } + public static class ManualTicker extends Ticker { public long time = 0;