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 Mon, 10 Jun 2019 02:17:45 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_r291863369
 
 

 ##########
 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);
 
 Review comment:
   @codelipenghui if it is not threadsafe, why do we need to use ConcurrentHashMap? Can't
we just use a HashMap?
   
   A good practice is - if you are using ConcurrentMap, it might be worth making it thread-safe.
Otherwise it will be hard to maintain the code, since people will be confused about why a
ConcurrentMap is used here.
   
   

----------------------------------------------------------------
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