This is an automated email from the ASF dual-hosted git repository.
ShannonDing pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git
The following commit(s) were added to refs/heads/develop by this push:
new c83aceb87f [ISSUE #11556] fix(tools): continue printMsgByQueue after
an unmatched batch (#11557)
c83aceb87f is described below
commit c83aceb87f3f841a4ffafcd9cdf9983e76a76f21
Author: fangm0137 <[email protected]>
AuthorDate: Mon Nov 23 18:54:39 2026 +0800
[ISSUE #11556] fix(tools): continue printMsgByQueue after an unmatched
batch (#11557)
* fix(tools): continue printMsgByQueue after unmatched batches
* test(tools): cover tag scanning across pull batches
* test(tools): cover print message consumer creation
---
.../message/PrintMessageByQueueCommand.java | 13 ++-
.../message/PrintMessageByQueueCommandTest.java | 102 +++++++++++++++++++++
2 files changed, 113 insertions(+), 2 deletions(-)
diff --git
a/tools/src/main/java/org/apache/rocketmq/tools/command/message/PrintMessageByQueueCommand.java
b/tools/src/main/java/org/apache/rocketmq/tools/command/message/PrintMessageByQueueCommand.java
index 9933189ddd..ea237648e6 100644
---
a/tools/src/main/java/org/apache/rocketmq/tools/command/message/PrintMessageByQueueCommand.java
+++
b/tools/src/main/java/org/apache/rocketmq/tools/command/message/PrintMessageByQueueCommand.java
@@ -156,9 +156,13 @@ public class PrintMessageByQueueCommand implements
SubCommand {
return options;
}
+ DefaultMQPullConsumer createConsumer(RPCHook rpcHook) {
+ return new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP, rpcHook);
+ }
+
@Override
public void execute(CommandLine commandLine, Options options, RPCHook
rpcHook) throws SubCommandException {
- DefaultMQPullConsumer consumer = new
DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP, rpcHook);
+ DefaultMQPullConsumer consumer = createConsumer(rpcHook);
try {
String charsetName =
@@ -198,17 +202,22 @@ public class PrintMessageByQueueCommand implements
SubCommand {
for (long offset = minOffset; offset < maxOffset; ) {
try {
PullResult pullResult = consumer.pull(mq, subExpression,
offset, 32);
- offset = pullResult.getNextBeginOffset();
+ long nextOffset = pullResult.getNextBeginOffset();
switch (pullResult.getPullStatus()) {
case FOUND:
calculateByTag(pullResult.getMsgFoundList(),
tagCalmap, calByTag);
printMessage(pullResult.getMsgFoundList(),
charsetName, printMsg, printBody);
break;
case NO_MATCHED_MSG:
+ if (nextOffset <= offset) {
+ break READQ;
+ }
+ break;
case NO_NEW_MSG:
case OFFSET_ILLEGAL:
break READQ;
}
+ offset = nextOffset;
} catch (Exception e) {
e.printStackTrace();
break;
diff --git
a/tools/src/test/java/org/apache/rocketmq/tools/command/message/PrintMessageByQueueCommandTest.java
b/tools/src/test/java/org/apache/rocketmq/tools/command/message/PrintMessageByQueueCommandTest.java
index ac5f8a29a0..80035ca5f5 100644
---
a/tools/src/test/java/org/apache/rocketmq/tools/command/message/PrintMessageByQueueCommandTest.java
+++
b/tools/src/test/java/org/apache/rocketmq/tools/command/message/PrintMessageByQueueCommandTest.java
@@ -16,16 +16,35 @@
*/
package org.apache.rocketmq.tools.command.message;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.Options;
+import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
+import org.apache.rocketmq.client.consumer.PullResult;
+import org.apache.rocketmq.client.consumer.PullStatus;
+import org.apache.rocketmq.common.MixAll;
+import org.apache.rocketmq.common.message.MessageExt;
+import org.apache.rocketmq.common.message.MessageQueue;
+import org.apache.rocketmq.remoting.RPCHook;
import org.junit.Assert;
import org.junit.Test;
public class PrintMessageByQueueCommandTest {
+ @Test
+ public void testCreateConsumerUsesToolsGroup() {
+ DefaultMQPullConsumer consumer = new
PrintMessageByQueueCommand().createConsumer(null);
+
+ Assert.assertEquals(MixAll.TOOLS_CONSUMER_GROUP,
consumer.getConsumerGroup());
+ }
+
@Test
public void testCompareToIsAntisymmetricBeyondIntegerMaxValue() {
PrintMessageByQueueCommand.TagCountBean zero =
newTagCountBean("tag-zero", 0L);
@@ -74,6 +93,89 @@ public class PrintMessageByQueueCommandTest {
Assert.assertTrue(max.compareTo(overInt) < 0);
}
+ @Test
+ public void testTagScanContinuesAfterUnmatchedBatch() throws Exception {
+ MessageExt matched = new MessageExt();
+ matched.setTags("TagA");
+ ScriptedConsumer consumer = new ScriptedConsumer(
+ new PullResult(PullStatus.NO_MATCHED_MSG, 32, 0, 33,
Collections.emptyList()),
+ new PullResult(PullStatus.FOUND, 33, 0, 33,
Collections.singletonList(matched)));
+
+ String output = runTagScan(consumer);
+
+ Assert.assertEquals(Arrays.asList(0L, 32L), consumer.requestedOffsets);
+ Assert.assertTrue(output.replaceAll("\\s+", " ").contains("Tag: TagA
Count: 1"));
+ }
+
+ @Test
+ public void testTagScanStopsWhenUnmatchedBatchDoesNotAdvance() throws
Exception {
+ ScriptedConsumer consumer = new ScriptedConsumer(
+ new PullResult(PullStatus.NO_MATCHED_MSG, 0, 0, 33,
Collections.emptyList()));
+
+ runTagScan(consumer);
+
+ Assert.assertEquals(Collections.singletonList(0L),
consumer.requestedOffsets);
+ }
+
+ private static String runTagScan(ScriptedConsumer consumer) throws
Exception {
+ PrintMessageByQueueCommand command = new PrintMessageByQueueCommand() {
+ @Override
+ DefaultMQPullConsumer createConsumer(RPCHook rpcHook) {
+ return consumer;
+ }
+ };
+ Options options = command.buildCommandlineOptions(new Options());
+ CommandLine arguments = new DefaultParser().parse(options,
+ new String[] {"-t", "TestTopic", "-a", "broker-a", "-i", "0",
"-s", "TagA", "-f", "true"});
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ PrintStream original = System.out;
+ try (PrintStream captured = new PrintStream(output, true, "UTF-8")) {
+ System.setOut(captured);
+ command.execute(arguments, options, null);
+ } finally {
+ System.setOut(original);
+ }
+ return output.toString("UTF-8");
+ }
+
+ private static class ScriptedConsumer extends DefaultMQPullConsumer {
+ private final PullResult[] results;
+ private final List<Long> requestedOffsets = new ArrayList<>();
+
+ ScriptedConsumer(PullResult... results) {
+ this.results = results;
+ }
+
+ @Override
+ public void start() {
+ // The responses are scripted, so no client connection is needed.
+ }
+
+ @Override
+ public void shutdown() {
+ // No client connection was opened.
+ }
+
+ @Override
+ public long minOffset(MessageQueue queue) {
+ return 0;
+ }
+
+ @Override
+ public long maxOffset(MessageQueue queue) {
+ return 33;
+ }
+
+ @Override
+ public PullResult pull(MessageQueue queue, String expression, long
offset, int maxNums) {
+ Assert.assertEquals("TagA", expression);
+ int resultIndex = requestedOffsets.size();
+ requestedOffsets.add(offset);
+ Assert.assertTrue("Unexpected pull at offset " + offset,
resultIndex < results.length);
+ return results[resultIndex];
+ }
+ }
+
private static PrintMessageByQueueCommand.TagCountBean
newTagCountBean(String tag, long count) {
return new PrintMessageByQueueCommand.TagCountBean(tag, new
AtomicLong(count));
}