You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/12/13 17:35:26 UTC

[GitHub] srkukarni closed pull request #3156: Share the batch metadata object across all messages in batch

srkukarni closed pull request #3156: Share the batch metadata object across all messages in batch
URL: https://github.com/apache/pulsar/pull/3156
 
 
   

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/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/MessageParser.java b/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/MessageParser.java
index 1280837314..d89494874a 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/MessageParser.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/MessageParser.java
@@ -23,6 +23,7 @@
 import static org.apache.pulsar.common.api.Commands.readChecksum;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.util.ReferenceCountUtil;
 
 import java.io.IOException;
 
@@ -53,6 +54,7 @@ public static void parseMessage(TopicName topicName, long ledgerId, long entryId
         MessageMetadata msgMetadata = null;
         ByteBuf payload = headersAndPayload;
         ByteBuf uncompressedPayload = null;
+        ReferenceCountedObject<MessageMetadata> refCntMsgMetadata = null;
 
         try {
             if (!verifyChecksum(topicName, headersAndPayload, ledgerId, entryId)) {
@@ -80,20 +82,20 @@ public static void parseMessage(TopicName topicName, long ledgerId, long entryId
             }
 
             final int numMessages = msgMetadata.getNumMessagesInBatch();
+            refCntMsgMetadata = new ReferenceCountedObject<>(msgMetadata, (x) -> x.recycle());
 
             if (numMessages == 1 && !msgMetadata.hasNumMessagesInBatch()) {
-
-                processor.process(RawMessageImpl.get(msgMetadata, null, uncompressedPayload, ledgerId, entryId, 0));
+                processor.process(
+                        RawMessageImpl.get(refCntMsgMetadata, null, uncompressedPayload, ledgerId, entryId, 0));
             } else {
                 // handle batch message enqueuing; uncompressed payload has all messages in batch
-                receiveIndividualMessagesFromBatch(msgMetadata, uncompressedPayload, ledgerId, entryId, processor);
-            }
-        } finally {
-            if (uncompressedPayload != null) {
-                uncompressedPayload.release();
+                receiveIndividualMessagesFromBatch(refCntMsgMetadata, uncompressedPayload, ledgerId, entryId, processor);
             }
 
-            msgMetadata.recycle();
+
+        } finally {
+            ReferenceCountUtil.safeRelease(uncompressedPayload);
+            ReferenceCountUtil.safeRelease(refCntMsgMetadata);
         }
     }
 
@@ -134,9 +136,9 @@ public static ByteBuf uncompressPayloadIfNeeded(TopicName topic, MessageMetadata
         }
     }
 
-    private static void receiveIndividualMessagesFromBatch(MessageMetadata msgMetadata,
+    private static void receiveIndividualMessagesFromBatch(ReferenceCountedObject<MessageMetadata> msgMetadata,
             ByteBuf uncompressedPayload, long ledgerId, long entryId, MessageProcessor processor) {
-        int batchSize = msgMetadata.getNumMessagesInBatch();
+        int batchSize = msgMetadata.get().getNumMessagesInBatch();
 
         try {
             for (int i = 0; i < batchSize; ++i) {
diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/RawMessageImpl.java b/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/RawMessageImpl.java
index 6915124251..282945f08a 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/RawMessageImpl.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/RawMessageImpl.java
@@ -35,7 +35,7 @@
 
     private final RawMessageIdImpl messageId = new RawMessageIdImpl();
 
-    private MessageMetadata msgMetadata;
+    private ReferenceCountedObject<MessageMetadata> msgMetadata;
     private PulsarApi.SingleMessageMetadata.Builder singleMessageMetadata;
     private ByteBuf payload;
 
@@ -54,6 +54,9 @@ private RawMessageImpl(Handle<RawMessageImpl> handle) {
 
     @Override
     public void release() {
+        msgMetadata.release();
+        msgMetadata = null;
+
         if (singleMessageMetadata != null) {
             singleMessageMetadata.recycle();
             singleMessageMetadata = null;
@@ -63,12 +66,13 @@ public void release() {
         handle.recycle(this);
     }
 
-    public static RawMessage get(MessageMetadata msgMetadata,
+    public static RawMessage get(ReferenceCountedObject<MessageMetadata> msgMetadata,
             PulsarApi.SingleMessageMetadata.Builder singleMessageMetadata,
             ByteBuf payload,
             long ledgerId, long entryId, long batchIndex) {
         RawMessageImpl msg = RECYCLER.get();
         msg.msgMetadata = msgMetadata;
+        msg.msgMetadata.retain();
         msg.singleMessageMetadata = singleMessageMetadata;
         msg.messageId.ledgerId = ledgerId;
         msg.messageId.entryId = entryId;
@@ -82,8 +86,8 @@ public static RawMessage get(MessageMetadata msgMetadata,
         if (singleMessageMetadata != null && singleMessageMetadata.getPropertiesCount() > 0) {
             return singleMessageMetadata.getPropertiesList().stream()
                     .collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue));
-        } else if (msgMetadata.getPropertiesCount() > 0) {
-            return msgMetadata.getPropertiesList().stream()
+        } else if (msgMetadata.get().getPropertiesCount() > 0) {
+            return msgMetadata.get().getPropertiesList().stream()
                     .collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue));
         } else {
             return Collections.emptyMap();
@@ -102,15 +106,15 @@ public RawMessageId getMessageId() {
 
     @Override
     public long getPublishTime() {
-        return msgMetadata.getPublishTime();
+        return msgMetadata.get().getPublishTime();
     }
 
     @Override
     public long getEventTime() {
         if (singleMessageMetadata != null && singleMessageMetadata.hasEventTime()) {
             return singleMessageMetadata.getEventTime();
-        } else if (msgMetadata.hasEventTime()) {
-            return msgMetadata.getEventTime();
+        } else if (msgMetadata.get().hasEventTime()) {
+            return msgMetadata.get().getEventTime();
         } else {
             return 0;
         }
@@ -118,20 +122,20 @@ public long getEventTime() {
 
     @Override
     public long getSequenceId() {
-        return msgMetadata.getSequenceId() + messageId.batchIndex;
+        return msgMetadata.get().getSequenceId() + messageId.batchIndex;
     }
 
     @Override
     public String getProducerName() {
-        return msgMetadata.getProducerName();
+        return msgMetadata.get().getProducerName();
     }
 
     @Override
     public Optional<String> getKey() {
         if (singleMessageMetadata != null && singleMessageMetadata.hasPartitionKey()) {
             return Optional.of(singleMessageMetadata.getPartitionKey());
-        } else if (msgMetadata.hasPartitionKey()){
-            return Optional.of(msgMetadata.getPartitionKey());
+        } else if (msgMetadata.get().hasPartitionKey()){
+            return Optional.of(msgMetadata.get().getPartitionKey());
         } else {
             return Optional.empty();
         }
diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/ReferenceCountedObject.java b/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/ReferenceCountedObject.java
new file mode 100644
index 0000000000..7c58013338
--- /dev/null
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/api/raw/ReferenceCountedObject.java
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.common.api.raw;
+
+import io.netty.util.AbstractReferenceCounted;
+import io.netty.util.ReferenceCounted;
+
+import java.util.function.Consumer;
+
+public class ReferenceCountedObject<T> extends AbstractReferenceCounted {
+
+    private final T object;
+    private final Consumer<T> destructor;
+
+    public ReferenceCountedObject(T object, Consumer<T> destructor) {
+        this.object = object;
+        this.destructor = destructor;
+    }
+
+    public T get() {
+        return object;
+    }
+
+    @Override
+    public ReferenceCounted touch(Object hint) {
+        return this;
+    }
+
+    @Override
+    protected void deallocate() {
+        destructor.accept(object);
+    }
+}


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services