Repository: cassandra
Updated Branches:
  refs/heads/trunk f34e829a2 -> d641053ab


Permit use of custom allocator for memtables

patch by Jay Patel; reviewed by benedict for CASSANDRA-7883


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/d641053a
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/d641053a
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/d641053a

Branch: refs/heads/trunk
Commit: d641053ab83d37d089155fef69fd53c9e9bc7535
Parents: f34e829
Author: Benedict Elliott Smith <bened...@apache.org>
Authored: Mon Sep 8 10:52:35 2014 +0700
Committer: Benedict Elliott Smith <bened...@apache.org>
Committed: Mon Sep 8 10:52:35 2014 +0700

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 -
 .../cassandra/utils/memory/NativeAllocator.java | 29 +++++---------------
 2 files changed, 7 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/d641053a/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index a9be239..837f770 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -18,7 +18,6 @@
  * Use unsafe mutations for most unit tests (CASSANDRA-6969)
  * Fix race condition during calculation of pending ranges (CASSANDRA-7390)
 
-
 2.1.1
  * (cqlsh): Support for query paging (CASSANDRA-7514)
  * (cqlsh): Show progress of COPY operations (CASSANDRA-7789)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/d641053a/src/java/org/apache/cassandra/utils/memory/NativeAllocator.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/memory/NativeAllocator.java 
b/src/java/org/apache/cassandra/utils/memory/NativeAllocator.java
index 1b5dcf2..ccb1104 100644
--- a/src/java/org/apache/cassandra/utils/memory/NativeAllocator.java
+++ b/src/java/org/apache/cassandra/utils/memory/NativeAllocator.java
@@ -17,14 +17,13 @@
  */
 package org.apache.cassandra.utils.memory;
 
-import java.lang.reflect.Field;
-
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.cassandra.config.CFMetaData;
+import org.apache.cassandra.config.DatabaseDescriptor;
 import org.apache.cassandra.db.Cell;
 import org.apache.cassandra.db.CounterCell;
 import org.apache.cassandra.db.DecoratedKey;
@@ -35,10 +34,10 @@ import org.apache.cassandra.db.NativeCounterCell;
 import org.apache.cassandra.db.NativeDecoratedKey;
 import org.apache.cassandra.db.NativeDeletedCell;
 import org.apache.cassandra.db.NativeExpiringCell;
+import org.apache.cassandra.io.util.IAllocator;
 import org.apache.cassandra.utils.concurrent.OpOrder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import sun.misc.Unsafe;
 
 public class NativeAllocator extends MemtableAllocator
 {
@@ -47,6 +46,8 @@ public class NativeAllocator extends MemtableAllocator
     private final static int REGION_SIZE = 1024 * 1024;
     private final static int MAX_CLONED_SIZE = 128 * 1024; // bigger than this 
don't go in the region
 
+    private static final IAllocator allocator = 
DatabaseDescriptor.getoffHeapMemoryAllocator();
+    
     // globally stash any Regions we allocate but are beaten to using, and use 
these up before allocating any more
     private static final ConcurrentLinkedQueue<Region> RACE_ALLOCATED = new 
ConcurrentLinkedQueue<>();
 
@@ -104,7 +105,7 @@ public class NativeAllocator extends MemtableAllocator
         if (size > MAX_CLONED_SIZE)
         {
             unslabbed.addAndGet(size);
-            Region region = new Region(unsafe.allocateMemory(size), size);
+            Region region = new Region(allocator.allocate(size), size);
             regions.add(region);
 
             long peer;
@@ -130,7 +131,7 @@ public class NativeAllocator extends MemtableAllocator
     public void setDiscarded()
     {
         for (Region region : regions)
-            unsafe.freeMemory(region.peer);
+            allocator.free(region.peer);
         super.setDiscarded();
     }
 
@@ -150,7 +151,7 @@ public class NativeAllocator extends MemtableAllocator
             // against other allocators to CAS in a Region, and if we fail we 
stash the region for re-use
             region = RACE_ALLOCATED.poll();
             if (region == null)
-                region = new Region(unsafe.allocateMemory(REGION_SIZE), 
REGION_SIZE);
+                region = new Region(allocator.allocate(REGION_SIZE), 
REGION_SIZE);
             if (currentRegion.compareAndSet(null, region))
             {
                 regions.add(region);
@@ -239,20 +240,4 @@ public class NativeAllocator extends MemtableAllocator
         }
     }
 
-
-    static final Unsafe unsafe;
-
-    static
-    {
-        try
-        {
-            Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
-            field.setAccessible(true);
-            unsafe = (sun.misc.Unsafe) field.get(null);
-        }
-        catch (Exception e)
-        {
-            throw new AssertionError(e);
-        }
-    }
 }

Reply via email to