pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [pulsar] sijie commented on a change in pull request #4435: Introduce batch message container framework and support key based batching container
Date Wed, 05 Jun 2019 06:42:14 GMT
sijie commented on a change in pull request #4435: Introduce batch message container framework
and support key based batching container
URL: https://github.com/apache/pulsar/pull/4435#discussion_r290595116
 
 

 ##########
 File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/BatchMessageKeyBasedContainer.java
 ##########
 @@ -0,0 +1,204 @@
+/**
+ * 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.client.impl;
+
+import com.google.common.collect.ComparisonChain;
+import com.google.common.collect.Lists;
+import io.netty.buffer.ByteBuf;
+import io.netty.util.ReferenceCountUtil;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.common.allocator.PulsarByteBufAllocator;
+import org.apache.pulsar.common.api.ByteBufPair;
+import org.apache.pulsar.common.api.Commands;
+import org.apache.pulsar.common.api.proto.PulsarApi;
+import org.apache.pulsar.shaded.com.google.protobuf.v241.ByteString;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Key based batch message container
+ *
+ * incoming single messages:
+ * (k1, v1), (k2, v1), (k3, v1), (k1, v2), (k2, v2), (k3, v2), (k1, v3), (k2, v3), (k3, v3)
+ *
+ * batched into multiple batch messages:
+ * [(k1, v1), (k1, v2), (k1, v3)], [(k2, v1), (k2, v2), (k2, v3)], [(k3, v1), (k3, v2), (k3,
v3)]
+ */
+class BatchMessageKeyBasedContainer extends AbstractBatchMessageContainer {
+
+    private ConcurrentHashMap<String, KeyBasedPart> batches = new ConcurrentHashMap<>();
+
+    @Override
+    public void add(MessageImpl<?> msg, SendCallback callback) {
+        if (log.isDebugEnabled()) {
+            log.debug("[{}] [{}] add message to batch, num messages in batch so far {}",
topicName, producerName,
+                    numMessagesInBatch);
+        }
+        numMessagesInBatch++;
+        currentBatchSizeBytes += msg.getDataBuffer().readableBytes();
+        String key = peekKey(msg);
+        KeyBasedPart part = batches.get(key);
+        if (part == null) {
+            part = new KeyBasedPart();
+            part.addMsg(msg, callback);
+            batches.putIfAbsent(key, part);
+        } else {
+            part.addMsg(msg, callback);
+        }
+    }
+
+    @Override
+    public void clear() {
+        numMessagesInBatch = 0;
+        currentBatchSizeBytes = 0;
+        batches = new ConcurrentHashMap<>();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return batches.isEmpty();
+    }
+
+    @Override
+    public void handleException(Exception ex) {
+        try {
+            // Need to protect ourselves from any exception being thrown in the future handler
from the application
+            batches.forEach((k, v) -> v.firstCallback.sendComplete(ex));
+        } catch (Throwable t) {
+            log.warn("[{}] [{}] Got exception while completing the callback", topicName,
producerName, t);
+        }
+        batches.forEach((k, v) -> ReferenceCountUtil.safeRelease(v.batchedMessageMetadataAndPayload));
+        clear();
+    }
+
+    private ProducerImpl.OpSendMsg createOpSendMsg(KeyBasedPart keyBasedPart) throws IOException
{
+        ByteBuf encryptedPayload = producer.encryptMessage(keyBasedPart.messageMetadata,
keyBasedPart.getCompressedBatchMetadataAndPayload());
+        final int numMessagesInBatch = keyBasedPart.messages.size();
+        long currentBatchSizeBytes = 0;
+        for (MessageImpl<?> message : keyBasedPart.messages) {
+            currentBatchSizeBytes += message.getDataBuffer().readableBytes();
+        }
+        keyBasedPart.messageMetadata.setNumMessagesInBatch(numMessagesInBatch);
+        ByteBufPair cmd = producer.sendMessage(producer.producerId, keyBasedPart.sequenceId,
numMessagesInBatch,
+                keyBasedPart.messageMetadata.build(), encryptedPayload);
+
+        ProducerImpl.OpSendMsg op = ProducerImpl.OpSendMsg.create(keyBasedPart.messages,
cmd, keyBasedPart.sequenceId, keyBasedPart.firstCallback);
+
+        if (encryptedPayload.readableBytes() > ClientCnx.getMaxMessageSize()) {
+            cmd.release();
+            if (op != null) {
+                op.callback.sendComplete(new PulsarClientException.InvalidMessageException(
+                        "Message size is bigger than " + ClientCnx.getMaxMessageSize() +
" bytes"));
+                op.recycle();
+            }
+            return null;
+        }
+        op.setNumMessagesInBatch(numMessagesInBatch);
+        op.setBatchSizeByte(currentBatchSizeBytes);
+        return op;
+    }
+
+    @Override
+    public List<ProducerImpl.OpSendMsg> createOpSendMsgs() throws IOException {
+        List<ProducerImpl.OpSendMsg> result = new ArrayList<>();
+        List<KeyBasedPart> list = new ArrayList<>(batches.values());
+        list.sort(((o1, o2) -> ComparisonChain.start()
+                .compare(o1.sequenceId, o2.sequenceId)
+                .result()));
+        for (KeyBasedPart keyBasedPart : list) {
+            ProducerImpl.OpSendMsg op = createOpSendMsg(keyBasedPart);
+            if (op != null) {
+                result.add(op);
+            }
+        }
+        return result;
+    }
+
+    private String peekKey(MessageImpl<?> msg) {
+        if (msg.hasOrderingKey()) {
+            return Base64.getEncoder().encodeToString(msg.getOrderingKey());
+        }
+        return msg.getKey();
+    }
+
+    private class KeyBasedPart {
 
 Review comment:
   can we make the class `static`?
   
   Also is `KeyedBatch` a better name?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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

Mime
View raw message