You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by eo...@apache.org on 2017/10/18 07:17:43 UTC

[bookkeeper] branch master updated: Recycle buffers used in EntryLogger#addEntry

This is an automated email from the ASF dual-hosted git repository.

eolivelli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new d31dcbf  Recycle buffers used in EntryLogger#addEntry
d31dcbf is described below

commit d31dcbf6ba3a737d66ec3958f8443b007d4e5fad
Author: Matteo Merli <mm...@yahoo-inc.com>
AuthorDate: Wed Oct 18 09:17:38 2017 +0200

    Recycle buffers used in EntryLogger#addEntry
    
    To avoid creating a buffer and letting it go out of scope and be GC'd
    each time.
    
    This change was originally made by merlimat in the yahoo-4.3 branch.
    
    Author: Matteo Merli <mm...@yahoo-inc.com>
    
    Reviewers: Enrico Olivelli <eo...@gmail.com>, Sijie Guo <si...@apache.org>, Ivan Kelly <iv...@apache.org>
    
    This closes #639 from ivankelly/recycle-buf
---
 .../org/apache/bookkeeper/bookie/EntryLogger.java  | 39 +++++++++++++++++++---
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryLogger.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryLogger.java
index 4085f78..61aa91b 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryLogger.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryLogger.java
@@ -24,6 +24,9 @@ package org.apache.bookkeeper.bookie;
 import static com.google.common.base.Charsets.UTF_8;
 import static org.apache.bookkeeper.util.BookKeeperConstants.MAX_LOG_SIZE_LIMIT;
 
+import io.netty.util.Recycler;
+import io.netty.util.Recycler.Handle;
+
 import com.google.common.collect.MapMaker;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
@@ -771,6 +774,31 @@ public class EntryLogger {
         return addEntry(ledger, entry, true);
     }
 
+    private static final class RecyclableByteBuffer {
+        private static final Recycler<RecyclableByteBuffer> RECYCLER = new  Recycler<RecyclableByteBuffer>() {
+            @Override
+            protected RecyclableByteBuffer newObject(Handle<RecyclableByteBuffer> handle) {
+                return new RecyclableByteBuffer(handle);
+            }
+        };
+
+        private final ByteBuffer buffer;
+        private final Handle<RecyclableByteBuffer> handle;
+        public RecyclableByteBuffer(Handle<RecyclableByteBuffer> handle) {
+            this.buffer = ByteBuffer.allocate(4);
+            this.handle = handle;
+        }
+
+        public static RecyclableByteBuffer get() {
+            return RECYCLER.get();
+        }
+
+        public void recycle() {
+            buffer.rewind();
+            handle.recycle(this);
+        }
+    }
+
     synchronized long addEntry(long ledger, ByteBuffer entry, boolean rollLog) throws IOException {
         int entrySize = entry.remaining() + 4;
         boolean reachEntryLogLimit =
@@ -788,10 +816,13 @@ public class EntryLogger {
             }
         }
 
-        ByteBuffer buff = ByteBuffer.allocate(4);
-        buff.putInt(entry.remaining());
-        buff.flip();
-        logChannel.write(buff);
+        // Get a buffer from recyclable pool to store the size
+        RecyclableByteBuffer recyclableBuffer = RecyclableByteBuffer.get();
+        recyclableBuffer.buffer.putInt(entry.remaining());
+        recyclableBuffer.buffer.flip();
+        logChannel.write(recyclableBuffer.buffer);
+        recyclableBuffer.recycle();
+
         long pos = logChannel.position();
         logChannel.write(entry);
         logChannel.registerWrittenEntry(ledger, entrySize);

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