Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,13 @@ public Options buildCommandlineOptions(Options options) {
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 =
Expand Down Expand Up @@ -198,17 +202,22 @@ public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) t
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -74,6 +93,89 @@ public void testCompareToHandlesLongMaxValueCounts() {
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));
}
Expand Down
Loading