This is an automated email from the ASF dual-hosted git repository. huangli pushed a commit to branch 4.9.2_dev_community in repository https://gitbox.apache.org/repos/asf/rocketmq.git
commit 0bc5c1e8435e04b9a1807543c1e0c01e55029b31 Author: huangli <[email protected]> AuthorDate: Wed Nov 10 20:05:50 2021 +0800 [Issue #3476] Fix last separator of properties string is missing when using batch send. This problem introduced since 4.9.1, may cause tag incorrect. --- store/src/main/java/org/apache/rocketmq/store/CommitLog.java | 12 +++++++++--- .../java/org/apache/rocketmq/store/BatchPutMessageTest.java | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java index 49ad725..c01282a 100644 --- a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java +++ b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java @@ -1706,6 +1706,8 @@ public class CommitLog { short propertiesLen = messagesByteBuff.getShort(); int propertiesPos = messagesByteBuff.position(); messagesByteBuff.position(propertiesPos + propertiesLen); + boolean needAppendLastPropertySeparator = propertiesLen > 0 && batchPropLen > 0 + && messagesByteBuff.get(messagesByteBuff.position() - 1) != MessageDecoder.PROPERTY_SEPARATOR; byte[] topicData; int queueId; @@ -1722,8 +1724,9 @@ public class CommitLog { final int topicLength = topicData.length; - final int msgLen = calMsgLength(messageExtBatch.getSysFlag(), bodyLen, topicLength, - propertiesLen + batchPropLen); + int totalPropLen = needAppendLastPropertySeparator ? propertiesLen + batchPropLen + 1 + : propertiesLen + batchPropLen; + final int msgLen = calMsgLength(messageExtBatch.getSysFlag(), bodyLen, topicLength, totalPropLen); // Exceeds the maximum message if (msgLen > this.maxMessageSize) { @@ -1776,11 +1779,14 @@ public class CommitLog { this.encoderBuffer.put((byte) topicLength); this.encoderBuffer.put(topicData); // 17 PROPERTIES - this.encoderBuffer.putShort((short) (propertiesLen + batchPropLen)); + this.encoderBuffer.putShort((short) totalPropLen); if (propertiesLen > 0) { this.encoderBuffer.put(messagesByteBuff.array(), propertiesPos, propertiesLen); } if (batchPropLen > 0) { + if (needAppendLastPropertySeparator) { + this.encoderBuffer.put((byte) MessageDecoder.PROPERTY_SEPARATOR); + } this.encoderBuffer.put(batchPropData, 0, batchPropLen); } } diff --git a/store/src/test/java/org/apache/rocketmq/store/BatchPutMessageTest.java b/store/src/test/java/org/apache/rocketmq/store/BatchPutMessageTest.java index 2c1fd25..3bc52e3 100644 --- a/store/src/test/java/org/apache/rocketmq/store/BatchPutMessageTest.java +++ b/store/src/test/java/org/apache/rocketmq/store/BatchPutMessageTest.java @@ -105,7 +105,7 @@ public class BatchPutMessageTest { short propertiesLength = (short) propertiesBytes.length; final byte[] topicData = msg.getTopic().getBytes(MessageDecoder.CHARSET_UTF8); final int topicLength = topicData.length; - msgLengthArr[j] = calMsgLength(msg.getBody().length, topicLength, propertiesLength+batchPropLen) + msgLengthArr[j - 1]; + msgLengthArr[j] = calMsgLength(msg.getBody().length, topicLength, propertiesLength+batchPropLen+1) + msgLengthArr[j - 1]; j++; } byte[] batchMessageBody = MessageDecoder.encodeMessages(messages);
