bookkeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From eolive...@apache.org
Subject bookkeeper git commit: BOOKKEEPER-1056: Removed PacketHeader serialization/deserialization allocation
Date Thu, 01 Jun 2017 19:22:40 GMT
Repository: bookkeeper
Updated Branches:
  refs/heads/master dd08ce1a6 -> a9fe7d94f


BOOKKEEPER-1056: Removed PacketHeader serialization/deserialization allocation

When parsing the request packet header, use static methods to avoid creating a `PacketHeader`
instance.

Author: Matteo Merli <mmerli@yahoo-inc.com>

Reviewers: Enrico Olivelli <None>, Sijie Guo <None>, Venkateswararao Jujjuri (JV)
<None>

Closes #175 from merlimat/packet-header-parsing


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

Branch: refs/heads/master
Commit: a9fe7d94f1890f697633fdca8abdeff23903025f
Parents: dd08ce1
Author: Matteo Merli <mmerli@yahoo-inc.com>
Authored: Thu Jun 1 21:20:59 2017 +0200
Committer: Enrico Olivelli <eolivelli@localhost.localdomain>
Committed: Thu Jun 1 21:20:59 2017 +0200

----------------------------------------------------------------------
 .../bookkeeper/proto/BookieProtoEncoding.java   | 48 ++++++++++----------
 .../apache/bookkeeper/proto/BookieProtocol.java | 47 +++++++------------
 2 files changed, 40 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/a9fe7d94/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieProtoEncoding.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieProtoEncoding.java
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieProtoEncoding.java
index cd71b26..8de4323 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieProtoEncoding.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieProtoEncoding.java
@@ -101,7 +101,7 @@ public class BookieProtoEncoding {
                 int totalHeaderSize = 4 // for the header
                     + BookieProtocol.MASTER_KEY_LENGTH; // for the master key
                 ByteBuf buf = allocator.buffer(totalHeaderSize);
-                buf.writeInt(new PacketHeader(r.getProtocolVersion(), r.getOpCode(), r.getFlags()).toInt());
+                buf.writeInt(PacketHeader.toInt(r.getProtocolVersion(), r.getOpCode(), r.getFlags()));
                 buf.writeBytes(r.getMasterKey(), 0, BookieProtocol.MASTER_KEY_LENGTH);
                 return DoubleByteBuf.get(buf, ar.getData());
             } else if (r instanceof BookieProtocol.ReadRequest) {
@@ -113,7 +113,7 @@ public class BookieProtoEncoding {
                 }
 
                 ByteBuf buf = allocator.buffer(totalHeaderSize);
-                buf.writeInt(new PacketHeader(r.getProtocolVersion(), r.getOpCode(), r.getFlags()).toInt());
+                buf.writeInt(PacketHeader.toInt(r.getProtocolVersion(), r.getOpCode(), r.getFlags()));
                 buf.writeLong(r.getLedgerId());
                 buf.writeLong(r.getEntryId());
                 if (r.hasMasterKey()) {
@@ -126,9 +126,7 @@ public class BookieProtoEncoding {
                 int totalHeaderSize = 4; // for request type
                 int totalSize = totalHeaderSize + am.getSerializedSize();
                 ByteBuf buf = allocator.buffer(totalSize);
-                buf.writeInt(new PacketHeader(r.getProtocolVersion(),
-                                              r.getOpCode(),
-                                              r.getFlags()).toInt());
+                buf.writeInt(PacketHeader.toInt(r.getProtocolVersion(), r.getOpCode(), r.getFlags()));
                 ByteBufOutputStream bufStream = new ByteBufOutputStream(buf);
                 am.writeTo(bufStream);
                 return buf;
@@ -140,41 +138,43 @@ public class BookieProtoEncoding {
         @Override
         public Object decode(ByteBuf packet)
                 throws Exception {
-            PacketHeader h = PacketHeader.fromInt(packet.readInt());
+            int packetHeader = packet.readInt();
+            byte version = PacketHeader.getVersion(packetHeader);
+            byte opCode = PacketHeader.getOpCode(packetHeader);
+            short flags = PacketHeader.getFlags(packetHeader);
 
             // packet format is different between ADDENTRY and READENTRY
             long ledgerId = -1;
             long entryId = BookieProtocol.INVALID_ENTRY_ID;
 
-            short flags = h.getFlags();
-
             ServerStats.getInstance().incrementPacketsReceived();
 
-            switch (h.getOpCode()) {
+            switch (opCode) {
             case BookieProtocol.ADDENTRY: {
                 byte[] masterKey = readMasterKey(packet);
 
                 // Read ledger and entry id without advancing the reader index
                 ledgerId = packet.getLong(packet.readerIndex());
                 entryId = packet.getLong(packet.readerIndex() + 8);
-                return new BookieProtocol.AddRequest(h.getVersion(), ledgerId, entryId, flags,
masterKey, packet.retain());
+                return new BookieProtocol.AddRequest(version, ledgerId, entryId, flags, masterKey,
packet.retain());
             }
+
             case BookieProtocol.READENTRY:
                 ledgerId = packet.readLong();
                 entryId = packet.readLong();
 
                 if ((flags & BookieProtocol.FLAG_DO_FENCING) == BookieProtocol.FLAG_DO_FENCING
-                    && h.getVersion() >= 2) {
+                    && version >= 2) {
                     byte[] masterKey = readMasterKey(packet);
-                    return new BookieProtocol.ReadRequest(h.getVersion(), ledgerId, entryId,
flags, masterKey);
+                    return new BookieProtocol.ReadRequest(version, ledgerId, entryId, flags,
masterKey);
                 } else {
-                    return new BookieProtocol.ReadRequest(h.getVersion(), ledgerId, entryId,
flags);
+                    return new BookieProtocol.ReadRequest(version, ledgerId, entryId, flags);
                 }
             case BookieProtocol.AUTH:
                 BookkeeperProtocol.AuthMessage.Builder builder
                     = BookkeeperProtocol.AuthMessage.newBuilder();
                 builder.mergeFrom(new ByteBufInputStream(packet), extensionRegistry);
-                return new BookieProtocol.AuthRequest(h.getVersion(), builder.build());
+                return new BookieProtocol.AuthRequest(version, builder.build());
             }
 
             return packet;
@@ -221,8 +221,7 @@ public class BookieProtoEncoding {
             }
             BookieProtocol.Response r = (BookieProtocol.Response)msg;
             ByteBuf buf = allocator.buffer(24);
-            buf.writeInt(new PacketHeader(r.getProtocolVersion(),
-                                          r.getOpCode(), (short)0).toInt());
+            buf.writeInt(PacketHeader.toInt(r.getProtocolVersion(), r.getOpCode(), (short)
0));
 
             ServerStats.getInstance().incrementPacketsSent();
             if (msg instanceof BookieProtocol.ReadResponse) {
@@ -255,16 +254,17 @@ public class BookieProtoEncoding {
                 throws Exception {
             int rc;
             long ledgerId, entryId;
-            final PacketHeader header;
 
-            header = PacketHeader.fromInt(buffer.readInt());
+            int packetHeader = buffer.readInt();
+            byte version = PacketHeader.getVersion(packetHeader);
+            byte opCode = PacketHeader.getOpCode(packetHeader);
 
-            switch (header.getOpCode()) {
+            switch (opCode) {
             case BookieProtocol.ADDENTRY:
                 rc = buffer.readInt();
                 ledgerId = buffer.readLong();
                 entryId = buffer.readLong();
-                return new BookieProtocol.AddResponse(header.getVersion(), rc, ledgerId,
entryId);
+                return new BookieProtocol.AddResponse(version, rc, ledgerId, entryId);
             case BookieProtocol.READENTRY:
                 rc = buffer.readInt();
                 ledgerId = buffer.readLong();
@@ -272,11 +272,9 @@ public class BookieProtoEncoding {
 
                 if (rc == BookieProtocol.EOK) {
                     ByteBuf content = buffer.slice();
-                    return new BookieProtocol.ReadResponse(header.getVersion(), rc,
-                                                           ledgerId, entryId, content.retain());
+                    return new BookieProtocol.ReadResponse(version, rc, ledgerId, entryId,
content.retain());
                 } else {
-                    return new BookieProtocol.ReadResponse(header.getVersion(), rc,
-                                                           ledgerId, entryId);
+                    return new BookieProtocol.ReadResponse(version, rc, ledgerId, entryId);
                 }
             case BookieProtocol.AUTH:
                 ByteBufInputStream bufStream = new ByteBufInputStream(buffer);
@@ -284,7 +282,7 @@ public class BookieProtoEncoding {
                     = BookkeeperProtocol.AuthMessage.newBuilder();
                 builder.mergeFrom(bufStream, extensionRegistry);
                 BookkeeperProtocol.AuthMessage am = builder.build();
-                return new BookieProtocol.AuthResponse(header.getVersion(), am);
+                return new BookieProtocol.AuthResponse(version, am);
             default:
                 return buffer;
             }

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/a9fe7d94/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieProtocol.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieProtocol.java
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieProtocol.java
index f0cfa58..d744d09 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieProtocol.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieProtocol.java
@@ -69,18 +69,8 @@ public interface BookieProtocol {
      * and just had an int representing the opCode as the 
      * first int. This handles that case also. 
      */
-    static class PacketHeader {
-        final byte version;
-        final byte opCode;
-        final short flags;
-
-        public PacketHeader(byte version, byte opCode, short flags) {
-            this.version = version;
-            this.opCode = opCode;
-            this.flags = flags;
-        }
-        
-        int toInt() {
+    final static class PacketHeader {
+        public static int toInt(byte version, byte opCode, short flags) {
             if (version == 0) {
                 return (int)opCode;
             } else {
@@ -90,29 +80,26 @@ public interface BookieProtocol {
             }
         }
 
-        static PacketHeader fromInt(int i) {
-            byte version = (byte)(i >> 24); 
-            byte opCode = 0;
-            short flags = 0;
+        public static byte getVersion(int packetHeader) {
+            return (byte)(packetHeader >> 24);
+        }
+
+        public static byte getOpCode(int packetHeader) {
+            int version = getVersion(packetHeader);
             if (version == 0) {
-                opCode = (byte)i;
+                return (byte) packetHeader;
             } else {
-                opCode = (byte)((i >> 16) & 0xFF);
-                flags = (short)(i & 0xFFFF);
+                return (byte)((packetHeader >> 16) & 0xFF);
             }
-            return new PacketHeader(version, opCode, flags);
-        }
-
-        byte getVersion() {
-            return version;
         }
 
-        byte getOpCode() {
-            return opCode;
-        }
-
-        short getFlags() {
-            return flags;
+        public static short getFlags(int packetHeader) {
+            byte version = (byte)(packetHeader >> 24);
+            if (version == 0) {
+                return 0;
+            } else {
+                return (short)(packetHeader & 0xFFFF);
+            }
         }
     }
 


Mime
View raw message