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/4e51fe77
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/4e51fe77
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/4e51fe77

Branch: refs/heads/4.x-HBase-1.1
Commit: 4e51fe7726b896b5c05e43aee825cfb1aded480d
Parents: fd2b064
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:43:34 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/4e51fe77/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/4e51fe77/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;
         

Reply via email to