ivankelly closed pull request #679: Recycle instances of callback to obtain a 
PerChannelBookie client
URL: https://github.com/apache/bookkeeper/pull/679
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieClient.java 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieClient.java
index c604b1684..ae7f806ab 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieClient.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieClient.java
@@ -60,6 +60,8 @@
 import io.netty.channel.EventLoopGroup;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.util.HashedWheelTimer;
+import io.netty.util.Recycler;
+import io.netty.util.Recycler.Handle;
 import io.netty.util.Timeout;
 import io.netty.util.TimerTask;
 
@@ -253,26 +255,19 @@ public void addEntry(final BookieSocketAddress addr,
                 return;
             }
 
-            // Retain the buffer, since the connection could be obtained after 
the PendingApp might have already
-            // failed
+            // Retain the buffer, since the connection could be obtained after
+            // the PendingApp might have already failed
             toSend.retain();
 
-            client.obtain(new GenericCallback<PerChannelBookieClient>() {
-                @Override
-                public void operationComplete(final int rc, 
PerChannelBookieClient pcbc) {
-                    if (rc != BKException.Code.OK) {
-                        completeAdd(rc, ledgerId, entryId, addr, cb, ctx);
-                    } else {
-                        pcbc.addEntry(ledgerId, masterKey, entryId, toSend, 
cb, ctx, options);
-                    }
-                    toSend.release();
-                }
-            }, ledgerId);
+            client.obtain(ChannelReadyForAddEntryCallback.create(
+                                  this, toSend, ledgerId, entryId, addr,
+                                  ctx, cb, options, masterKey),
+                          ledgerId);
         } finally {
             closeLock.readLock().unlock();
         }
     }
-    
+
     private void completeRead(final int rc,
                               final long ledgerId,
                               final long entryId,
@@ -292,6 +287,69 @@ public void safeRun() {
         }
     }
 
+    private static class ChannelReadyForAddEntryCallback
+        implements GenericCallback<PerChannelBookieClient> {
+        private final Handle<ChannelReadyForAddEntryCallback> recyclerHandle;
+
+        private BookieClient bookieClient;
+        private ByteBuf toSend;
+        private long ledgerId;
+        private long entryId;
+        private BookieSocketAddress addr;
+        private Object ctx;
+        private WriteCallback cb;
+        private int options;
+        private byte[] masterKey;
+
+        static ChannelReadyForAddEntryCallback create(
+                BookieClient bookieClient, ByteBuf toSend, long ledgerId,
+                long entryId, BookieSocketAddress addr, Object ctx,
+                WriteCallback cb, int options, byte[] masterKey) {
+            ChannelReadyForAddEntryCallback callback = RECYCLER.get();
+            callback.bookieClient = bookieClient;
+            callback.toSend = toSend;
+            callback.ledgerId = ledgerId;
+            callback.entryId = entryId;
+            callback.addr = addr;
+            callback.ctx = ctx;
+            callback.cb = cb;
+            callback.options = options;
+            callback.masterKey = masterKey;
+            return callback;
+        }
+
+        @Override
+        public void operationComplete(final int rc,
+                                      PerChannelBookieClient pcbc) {
+            if (rc != BKException.Code.OK) {
+                bookieClient.completeAdd(rc, ledgerId, entryId, addr, cb, ctx);
+            } else {
+                pcbc.addEntry(ledgerId, masterKey, entryId,
+                              toSend, cb, ctx, options);
+            }
+
+            toSend.release();
+            recycle();
+        }
+
+        private ChannelReadyForAddEntryCallback(
+                Handle<ChannelReadyForAddEntryCallback> recyclerHandle) {
+            this.recyclerHandle = recyclerHandle;
+        }
+
+        private static final Recycler<ChannelReadyForAddEntryCallback> RECYCLER
+            = new Recycler<ChannelReadyForAddEntryCallback>() {
+                    protected ChannelReadyForAddEntryCallback newObject(
+                            Recycler.Handle<ChannelReadyForAddEntryCallback> 
recyclerHandle) {
+                        return new 
ChannelReadyForAddEntryCallback(recyclerHandle);
+                    }
+                };
+
+        public void recycle() {
+            recyclerHandle.recycle(this);
+        }
+    }
+
     public void readEntryAndFenceLedger(final BookieSocketAddress addr,
                                         final long ledgerId,
                                         final byte[] masterKey,


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to