BufferCache: use new interface in buffer cache

Use newly introduced buffer cache interface in buffer cache
implementation.

Signed-off-by: Jonas Pfefferle <peppe...@apache.org>


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

Branch: refs/heads/master
Commit: fa9c6af1acd47934c54c5ad47014c376066e68a7
Parents: efd44e7
Author: Jonas Pfefferle <peppe...@apache.org>
Authored: Tue Apr 3 15:50:59 2018 +0200
Committer: Jonas Pfefferle <peppe...@apache.org>
Committed: Tue Apr 3 15:59:11 2018 +0200

----------------------------------------------------------------------
 .../org/apache/crail/core/CoreDataStore.java    |  4 +-
 .../org/apache/crail/memory/BufferCache.java    | 58 ++++++++++----------
 .../apache/crail/memory/MappedBufferCache.java  | 30 +++++-----
 .../crail/storage/nvmf/NvmfBufferCache.java     |  2 +-
 .../nvmf/client/NvmfStorageEndpoint.java        |  6 +-
 5 files changed, 48 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-crail/blob/fa9c6af1/client/src/main/java/org/apache/crail/core/CoreDataStore.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/crail/core/CoreDataStore.java 
b/client/src/main/java/org/apache/crail/core/CoreDataStore.java
index 59c7da9..8b5e54f 100644
--- a/client/src/main/java/org/apache/crail/core/CoreDataStore.java
+++ b/client/src/main/java/org/apache/crail/core/CoreDataStore.java
@@ -479,11 +479,11 @@ public class CoreDataStore extends CrailStore {
        }
 
        public CrailBuffer allocateBuffer() throws IOException {
-               return this.bufferCache.getBuffer();
+               return this.bufferCache.allocateBuffer();
        }
 
        public void freeBuffer(CrailBuffer buffer) throws IOException {
-               this.bufferCache.putBuffer(buffer);
+               this.bufferCache.freeBuffer(buffer);
        }
 
        public int getFsId() {

http://git-wip-us.apache.org/repos/asf/incubator-crail/blob/fa9c6af1/client/src/main/java/org/apache/crail/memory/BufferCache.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/crail/memory/BufferCache.java 
b/client/src/main/java/org/apache/crail/memory/BufferCache.java
index 59f3dc9..108d75f 100644
--- a/client/src/main/java/org/apache/crail/memory/BufferCache.java
+++ b/client/src/main/java/org/apache/crail/memory/BufferCache.java
@@ -30,32 +30,34 @@ import org.apache.crail.conf.CrailConstants;
 import org.apache.crail.utils.CrailUtils;
 import org.slf4j.Logger;
 
-public abstract class BufferCache implements 
CrailStatistics.StatisticsProvider {
+public abstract class BufferCache implements 
CrailStatistics.StatisticsProvider, CrailBufferCache {
        private static final Logger LOG = CrailUtils.getLogger();
        private LinkedBlockingQueue<CrailBuffer> cache;
-       
+
        private AtomicLong cacheGet;
        private AtomicLong cachePut;
        private AtomicLong cacheMisses;
        private AtomicLong cacheOut;
        private AtomicLong cacheMax;
-       
+
        private AtomicLong cacheMissesMap;
-       private AtomicLong cacheMissesHeap;             
-       
-       public BufferCache() throws IOException{
+       private AtomicLong cacheMissesHeap;
+
+       public abstract CrailBuffer allocateRegion() throws IOException;
+
+       public BufferCache() throws IOException {
                this.cache = new LinkedBlockingQueue<CrailBuffer>();
-               
+
                this.cacheGet = new AtomicLong(0);
                this.cachePut = new AtomicLong(0);
                this.cacheMisses = new AtomicLong(0);
                this.cacheOut = new AtomicLong(0);
                this.cacheMax = new AtomicLong(0);
-               
+
                this.cacheMissesMap = new AtomicLong(0);
-               this.cacheMissesHeap = new AtomicLong(0);                       
+               this.cacheMissesHeap = new AtomicLong(0);
        }
-       
+
        @Override
        public String providerName() {
                return "cache/buffer";
@@ -65,7 +67,7 @@ public abstract class BufferCache implements 
CrailStatistics.StatisticsProvider
        public String printStatistics() {
                return "cacheGet " + cacheGet.get() + ", cachePut " + 
cachePut.get() + ", cacheMiss " + cacheMisses.get() + ", cacheSize " + 
cache.size() +  ", cacheMax " + cacheMax.get() + ", mapMiss " + 
cacheMissesMap.get() + ", mapHeap " + cacheMissesHeap.get();
        }
-       
+
        public void resetStatistics(){
                this.cacheGet.set(0);
                this.cachePut.set(0);
@@ -74,24 +76,24 @@ public abstract class BufferCache implements 
CrailStatistics.StatisticsProvider
                this.cacheMax.set(0);
                this.cacheMissesMap.set(0);
                this.cacheMissesHeap.set(0);
-       }       
-       
+       }
+
        public void mergeStatistics(StatisticsProvider provider){
-               
+
        }
-       
-       public CrailBuffer getBuffer() throws IOException {
+
+       public CrailBuffer allocateBuffer() throws IOException {
                cacheGet.incrementAndGet();
                cacheOut.incrementAndGet();
                cacheMax.updateAndGet(x -> Math.max(x, cacheOut.get()));
-               
+
                CrailBuffer buffer = cache.poll();
                if (buffer == null){
                        synchronized(this){
                                buffer = cache.poll();
                                if (buffer == null){
                                        cacheMisses.incrementAndGet();
-                                       buffer = allocateBuffer();
+                                       buffer = allocateRegion();
                                        if (buffer == null){
                                                buffer = 
OffHeapBuffer.wrap(ByteBuffer.allocateDirect(CrailConstants.BUFFER_SIZE));
                                                
cacheMissesHeap.incrementAndGet();
@@ -100,29 +102,27 @@ public abstract class BufferCache implements 
CrailStatistics.StatisticsProvider
                                        }
                                }
                        }
-               } 
-               
+               }
+
                buffer.clear();
                return buffer;
        }
-       
-       public void putBuffer(CrailBuffer buffer) throws IOException{
+
+       public void freeBuffer(CrailBuffer buffer) throws IOException{
                if (buffer != null){
                        cachePut.incrementAndGet();
                        cacheOut.decrementAndGet();
                        putBufferInternal(buffer);
                }
        }
-       
+
        public void putBufferInternal(CrailBuffer buffer) throws IOException{
                cache.add(buffer);
-       }       
-       
+       }
+
        public void close(){
                cache.clear();
        }
-       
-       public abstract CrailBuffer allocateBuffer() throws IOException;
 
        @SuppressWarnings("unchecked")
        public static BufferCache createInstance(String name) throws Exception {
@@ -134,7 +134,7 @@ public abstract class BufferCache implements 
CrailStatistics.StatisticsProvider
                } else {
                        throw new Exception("Cannot instantiate storage client 
of type " + name);
                }
-               
-       }               
+
+       }
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-crail/blob/fa9c6af1/client/src/main/java/org/apache/crail/memory/MappedBufferCache.java
----------------------------------------------------------------------
diff --git 
a/client/src/main/java/org/apache/crail/memory/MappedBufferCache.java 
b/client/src/main/java/org/apache/crail/memory/MappedBufferCache.java
index 567e300..9e37ca2 100644
--- a/client/src/main/java/org/apache/crail/memory/MappedBufferCache.java
+++ b/client/src/main/java/org/apache/crail/memory/MappedBufferCache.java
@@ -33,23 +33,23 @@ import org.slf4j.Logger;
 
 public class MappedBufferCache extends BufferCache {
        private static final Logger LOG = CrailUtils.getLogger();
-       
+
        private String id;
        private String directory;
        private File dir;
        private long allocationCount;
        private long bufferCount;
-       private long currentRegion;     
-       
+       private long currentRegion;
+
        public MappedBufferCache() throws IOException {
                super();
-               
+
                this.allocationCount = CrailConstants.CACHE_LIMIT / 
CrailConstants.REGION_SIZE;
                long _bufferSize = (long) CrailConstants.BUFFER_SIZE;
                this.bufferCount = CrailConstants.REGION_SIZE / _bufferSize;
                this.currentRegion = 0;
                LOG.info("buffer cache, allocationCount " + allocationCount + 
", bufferCount " + bufferCount);
-               
+
                if (allocationCount > 0){
                        id = "" + System.currentTimeMillis();
                        directory = CrailUtils.getCacheDirectory(id);
@@ -62,11 +62,11 @@ public class MappedBufferCache extends BufferCache {
                        }
                }
        }
-       
+
        @Override
        public void close() {
                super.close();
-               
+
                if (allocationCount > 0 && dir.exists()){
                        for (File child : dir.listFiles()) {
                                child.delete();
@@ -75,16 +75,12 @@ public class MappedBufferCache extends BufferCache {
                }
                LOG.info("mapped client cache closed");
        }
-       
-       public CrailBuffer allocateBuffer() throws IOException{
-               return allocateRegion();
-       }
 
-       private CrailBuffer allocateRegion() throws IOException {
+       public CrailBuffer allocateRegion() throws IOException {
                if (currentRegion >= allocationCount){
                        return null;
                }
-               
+
                String path = directory + "/" + currentRegion++;
                RandomAccessFile randomFile = new RandomAccessFile(path, "rw");
                randomFile.setLength(CrailConstants.REGION_SIZE);
@@ -96,23 +92,23 @@ public class MappedBufferCache extends BufferCache {
                channel.close();
 
                CrailBuffer firstBuffer = slice(mappedBuffer, 0);
-               
+
                for (int j = 1; j < bufferCount; j++) {
                        int position = j * CrailConstants.BUFFER_SIZE;
                        CrailBuffer sliceBuffer = slice(mappedBuffer, position);
                        this.putBufferInternal(sliceBuffer);
                }
                mappedBuffer.clear();
-               
+
                return firstBuffer;
        }
-       
+
        private CrailBuffer slice(CrailBuffer mappedBuffer, int position){
                int limit = position + CrailConstants.BUFFER_SIZE;
                mappedBuffer.clear();
                mappedBuffer.position(position);
                mappedBuffer.limit(limit);
-               CrailBuffer buffer = mappedBuffer.slice();                      
+               CrailBuffer buffer = mappedBuffer.slice();
                return buffer;
        }
 }

http://git-wip-us.apache.org/repos/asf/incubator-crail/blob/fa9c6af1/storage-nvmf/src/main/java/org/apache/crail/storage/nvmf/NvmfBufferCache.java
----------------------------------------------------------------------
diff --git 
a/storage-nvmf/src/main/java/org/apache/crail/storage/nvmf/NvmfBufferCache.java 
b/storage-nvmf/src/main/java/org/apache/crail/storage/nvmf/NvmfBufferCache.java
index f7b19d9..19142ec 100644
--- 
a/storage-nvmf/src/main/java/org/apache/crail/storage/nvmf/NvmfBufferCache.java
+++ 
b/storage-nvmf/src/main/java/org/apache/crail/storage/nvmf/NvmfBufferCache.java
@@ -52,7 +52,7 @@ public class NvmfBufferCache extends BufferCache {
        }
 
        @Override
-       public CrailBuffer allocateBuffer() throws IOException {
+       public CrailBuffer allocateRegion() throws IOException {
                ByteBuffer buffer = 
endpointGroup.allocateBuffer(CrailConstants.BUFFER_SIZE, ALIGNMENT);
                bufferPool.add(buffer);
                return OffHeapBuffer.wrap(buffer);

http://git-wip-us.apache.org/repos/asf/incubator-crail/blob/fa9c6af1/storage-nvmf/src/main/java/org/apache/crail/storage/nvmf/client/NvmfStorageEndpoint.java
----------------------------------------------------------------------
diff --git 
a/storage-nvmf/src/main/java/org/apache/crail/storage/nvmf/client/NvmfStorageEndpoint.java
 
b/storage-nvmf/src/main/java/org/apache/crail/storage/nvmf/client/NvmfStorageEndpoint.java
index 0e04edf..b4b1054 100644
--- 
a/storage-nvmf/src/main/java/org/apache/crail/storage/nvmf/client/NvmfStorageEndpoint.java
+++ 
b/storage-nvmf/src/main/java/org/apache/crail/storage/nvmf/client/NvmfStorageEndpoint.java
@@ -41,7 +41,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.concurrent.*;
 
-public class NvmfStorageEndpoint implements StorageEndpoint { 
+public class NvmfStorageEndpoint implements StorageEndpoint {
        private static final Logger LOG = CrailUtils.getLogger();
 
        private final InetSocketAddress inetSocketAddress;
@@ -150,7 +150,7 @@ public class NvmfStorageEndpoint implements StorageEndpoint 
{
 //                     LOG.info("unaligned");
                        long alignedLength = 
NvmfStorageUtils.alignLength(sectorSize, remoteOffset, length);
 
-                       CrailBuffer stagingBuffer = cache.getBuffer();
+                       CrailBuffer stagingBuffer = cache.allocateBuffer();
                        stagingBuffer.limit((int)alignedLength);
                        try {
                                switch(op) {
@@ -213,7 +213,7 @@ public class NvmfStorageEndpoint implements StorageEndpoint 
{
        }
 
        void putBuffer(CrailBuffer buffer) throws IOException {
-               cache.putBuffer(buffer);
+               cache.freeBuffer(buffer);
        }
 
        public void close() throws IOException, InterruptedException {

Reply via email to