geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aba...@apache.org
Subject [geode] branch develop updated: GEODE-1290: AbstractRegionEntry.prepareValueForCache cache the new serialized value for off-heap memory. (#822)
Date Tue, 10 Oct 2017 06:01:46 GMT
This is an automated email from the ASF dual-hosted git repository.

abarve pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new b23abb6  GEODE-1290: AbstractRegionEntry.prepareValueForCache cache the new serialized
value for off-heap memory. (#822)
b23abb6 is described below

commit b23abb68b8206ce13aeeccf10b77e577e4f58aaa
Author: Amey Barve <ameybarve15@gmail.com>
AuthorDate: Tue Oct 10 11:31:44 2017 +0530

    GEODE-1290: AbstractRegionEntry.prepareValueForCache cache the new serialized value for
off-heap memory. (#822)
    
    * GEODE-1290: AbstractRegionEntry.prepareValueForCache cache the new
    serialized value for off-heap memory.
    
    * GEODE-1290: Called event's setCachedSerializedNewValue only once as per
    review request. Added a unit test case.
    
    * GEODE-1290: Called event's setCachedSerializedNewValue only once with
    additional check of event.getCachedSerializedNewValue() == null as per
    review request. Updated unit test case to free OffheapMemory.
    
    * GEODE-1290: Updated unit test case to free OffheapMemory in try-finally
    block.
    
    * GEODE-1290: Updated PR with the changes suggested in review.
    
    * GEODE-1290: Updated PR with the changes suggested in the review and
    updated the test case.
---
 .../geode/internal/cache/AbstractRegionEntry.java  | 23 +++++---
 .../internal/cache/AbstractRegionEntryTest.java    | 64 +++++++++++++++++++++-
 2 files changed, 75 insertions(+), 12 deletions(-)

diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/AbstractRegionEntry.java
b/geode-core/src/main/java/org/apache/geode/internal/cache/AbstractRegionEntry.java
index 4b420b8..01f51d7 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/AbstractRegionEntry.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/AbstractRegionEntry.java
@@ -1327,16 +1327,21 @@ public abstract class AbstractRegionEntry implements RegionEntry,
HashEntry<Obje
         if (isSerialized) {
           if (event != null && event.getCachedSerializedNewValue() != null) {
             data = event.getCachedSerializedNewValue();
-          } else if (val instanceof CachedDeserializable) {
-            data = ((CachedDeserializable) val).getSerializedValue();
-          } else if (val instanceof PdxInstance) {
-            try {
-              data = ((ConvertableToBytes) val).toBytes();
-            } catch (IOException e) {
-              throw new PdxSerializationException("Could not convert " + val + " to bytes",
e);
-            }
           } else {
-            data = EntryEventImpl.serialize(val);
+            if (val instanceof CachedDeserializable) {
+              data = ((CachedDeserializable) val).getSerializedValue();
+            } else if (val instanceof PdxInstance) {
+              try {
+                data = ((ConvertableToBytes) val).toBytes();
+              } catch (IOException e) {
+                throw new PdxSerializationException("Could not convert " + val + " to bytes",
e);
+              }
+            } else {
+              data = EntryEventImpl.serialize(val);
+            }
+            if (event != null) {
+              event.setCachedSerializedNewValue(data);
+            }
           }
         } else {
           data = (byte[]) val;
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/AbstractRegionEntryTest.java
b/geode-core/src/test/java/org/apache/geode/internal/cache/AbstractRegionEntryTest.java
index 63a7ba7..5f01962 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/cache/AbstractRegionEntryTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/AbstractRegionEntryTest.java
@@ -14,15 +14,27 @@
  */
 package org.apache.geode.internal.cache;
 
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
 
 import org.assertj.core.api.Assertions;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
+import org.apache.geode.DataSerializer;
 import org.apache.geode.internal.cache.versions.RegionVersionVector;
 import org.apache.geode.internal.cache.versions.VersionTag;
+import org.apache.geode.internal.offheap.MemoryAllocatorImpl;
+import org.apache.geode.internal.offheap.OffHeapMemoryStats;
+import org.apache.geode.internal.offheap.OutOfOffHeapMemoryListener;
+import org.apache.geode.internal.offheap.SlabImpl;
+import org.apache.geode.internal.offheap.StoredObject;
 import org.apache.geode.internal.offheap.annotations.Unretained;
 import org.apache.geode.internal.util.concurrent.CustomEntryConcurrentHashMap.HashEntry;
 import org.apache.geode.test.junit.categories.UnitTest;
@@ -46,7 +58,39 @@ public class AbstractRegionEntryTest {
   }
 
 
-  public static class TestableRegionEntry extends AbstractRegionEntry {
+
+  @Test
+  public void whenPrepareValueForCacheCalledWithOffHeapEntryHasNewCachedSerializedValue()
+      throws RegionClearedException, IOException, ClassNotFoundException {
+    LocalRegion lr = mock(LocalRegion.class);
+    RegionEntryContext regionEntryContext = mock(RegionEntryContext.class);
+    OutOfOffHeapMemoryListener ooohml = mock(OutOfOffHeapMemoryListener.class);
+    OffHeapMemoryStats stats = mock(OffHeapMemoryStats.class);
+    SlabImpl slab = new SlabImpl(1024); // 1k
+    MemoryAllocatorImpl ma =
+        MemoryAllocatorImpl.createForUnitTest(ooohml, stats, new SlabImpl[] {slab});
+    try {
+      when(regionEntryContext.getOffHeap()).thenReturn(true);
+      String value = "value";
+      AbstractRegionEntry re = new TestableRegionEntry(lr, value);
+      assertEquals(value, re.getValueField());
+      EntryEventImpl entryEvent = new EntryEventImpl();
+      StoredObject valueForCache =
+          (StoredObject) re.prepareValueForCache(regionEntryContext, value, entryEvent, true);
+      final byte[] cachedSerializedNewValue = entryEvent.getCachedSerializedNewValue();
+      assertNotNull(cachedSerializedNewValue);
+      valueForCache.checkDataEquals(cachedSerializedNewValue);
+      DataInputStream dataInputStream =
+          new DataInputStream(new ByteArrayInputStream(cachedSerializedNewValue));
+      Object o = DataSerializer.readObject(dataInputStream);
+      assertEquals(o, value);
+    } finally {
+      MemoryAllocatorImpl.freeOffHeapMemory();
+    }
+  }
+
+  public static class TestableRegionEntry extends AbstractRegionEntry
+      implements OffHeapRegionEntry {
 
     private Object value;
 
@@ -104,5 +148,19 @@ public class AbstractRegionEntryTest {
     @Override
     protected void setEntryHash(int v) {}
 
+    @Override
+    public void release() {
+
+    }
+
+    @Override
+    public long getAddress() {
+      return 0;
+    }
+
+    @Override
+    public boolean setAddress(long expectedAddr, long newAddr) {
+      return false;
+    }
   }
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@geode.apache.org" <commits@geode.apache.org>'].

Mime
View raw message