-
Notifications
You must be signed in to change notification settings - Fork 12k
[ISSUE #10589] Rename DecodeMessageIdCommond to DecodeMessageIdCommand #10590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yx9o
wants to merge
3
commits into
apache:develop
Choose a base branch
from
yx9o:dev-10589
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tools/src/main/java/org/apache/rocketmq/tools/command/message/DecodeMessageIdCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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.rocketmq.tools.command.message; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import org.apache.commons.cli.CommandLine; | ||
| import org.apache.commons.cli.Option; | ||
| import org.apache.commons.cli.Options; | ||
| import org.apache.rocketmq.common.UtilAll; | ||
| import org.apache.rocketmq.common.message.MessageClientIDSetter; | ||
| import org.apache.rocketmq.common.message.MessageDecoder; | ||
| import org.apache.rocketmq.common.message.MessageId; | ||
| import org.apache.rocketmq.common.utils.NetworkUtil; | ||
| import org.apache.rocketmq.remoting.RPCHook; | ||
| import org.apache.rocketmq.tools.command.SubCommand; | ||
| import org.apache.rocketmq.tools.command.SubCommandException; | ||
|
|
||
| public class DecodeMessageIdCommand implements SubCommand { | ||
| private static final String MESSAGE_ID_TYPE_UNIQUE = "unique"; | ||
| private static final String MESSAGE_ID_TYPE_OFFSET = "offset"; | ||
|
|
||
| @Override | ||
| public String commandName() { | ||
| return "decodeMessageId"; | ||
| } | ||
|
|
||
| @Override | ||
| public String commandDesc() { | ||
| return "Decode unique or offset message ID."; | ||
| } | ||
|
|
||
| @Override | ||
| public Options buildCommandlineOptions(Options options) { | ||
| Option opt = new Option("i", "messageId", true, "message ID"); | ||
| opt.setRequired(true); | ||
| options.addOption(opt); | ||
|
|
||
| opt = new Option("t", "messageIdType", true, "message ID type, support unique or offset. Default: unique"); | ||
| opt.setRequired(false); | ||
| options.addOption(opt); | ||
| return options; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(final CommandLine commandLine, final Options options, | ||
| RPCHook rpcHook) throws SubCommandException { | ||
| String messageId = commandLine.getOptionValue('i').trim(); | ||
| String messageIdType = commandLine.hasOption('t') ? commandLine.getOptionValue('t').trim() : MESSAGE_ID_TYPE_UNIQUE; | ||
|
|
||
| try { | ||
| if (MESSAGE_ID_TYPE_UNIQUE.equalsIgnoreCase(messageIdType)) { | ||
| System.out.printf("IP: %s%n", MessageClientIDSetter.getIPStrFromID(messageId)); | ||
| System.out.printf("Date: %s%n", UtilAll.formatDate(MessageClientIDSetter.getNearlyTimeFromID(messageId), UtilAll.YYYY_MM_DD_HH_MM_SS_SSS)); | ||
| } else if (MESSAGE_ID_TYPE_OFFSET.equalsIgnoreCase(messageIdType)) { | ||
| MessageId decodedMessageId = MessageDecoder.decodeMessageId(messageId); | ||
| System.out.printf("StoreHost: %s%n", formatStoreHost(decodedMessageId)); | ||
| System.out.printf("CommitLogOffset: %d%n", decodedMessageId.getOffset()); | ||
| } else { | ||
| throw new SubCommandException("Unsupported messageIdType: " + messageIdType); | ||
| } | ||
| } catch (SubCommandException e) { | ||
| throw e; | ||
| } catch (Exception e) { | ||
| throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e); | ||
| } | ||
| } | ||
|
|
||
| private String formatStoreHost(MessageId messageId) { | ||
| InetSocketAddress address = (InetSocketAddress) messageId.getAddress(); | ||
| return NetworkUtil.normalizeHostAddress(address.getAddress()) + ":" + address.getPort(); | ||
| } | ||
| } | ||
65 changes: 0 additions & 65 deletions
65
tools/src/main/java/org/apache/rocketmq/tools/command/message/DecodeMessageIdCommond.java
This file was deleted.
Oops, something went wrong.
112 changes: 112 additions & 0 deletions
112
...s/src/test/java/org/apache/rocketmq/tools/command/message/DecodeMessageIdCommandTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * 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.rocketmq.tools.command.message; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.PrintStream; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.nio.charset.StandardCharsets; | ||
| import org.apache.commons.cli.CommandLine; | ||
| import org.apache.commons.cli.DefaultParser; | ||
| import org.apache.commons.cli.Options; | ||
| import org.apache.rocketmq.common.UtilAll; | ||
| import org.apache.rocketmq.common.message.MessageClientIDSetter; | ||
| import org.apache.rocketmq.common.message.MessageDecoder; | ||
| import org.apache.rocketmq.common.message.MessageId; | ||
| import org.apache.rocketmq.common.utils.NetworkUtil; | ||
| import org.apache.rocketmq.srvutil.ServerUtil; | ||
| import org.apache.rocketmq.tools.command.SubCommandException; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| public class DecodeMessageIdCommandTest { | ||
| @Test | ||
| public void testExecuteDecodeUniqueMessageIdByDefault() throws Exception { | ||
| String uniqueMessageId = MessageClientIDSetter.createUniqID(); | ||
| Thread.sleep(5L); | ||
| String output = execute("-i", uniqueMessageId); | ||
|
|
||
| Assert.assertEquals(String.format("IP: %s%nDate: %s%n", | ||
| MessageClientIDSetter.getIPStrFromID(uniqueMessageId), | ||
| UtilAll.formatDate(MessageClientIDSetter.getNearlyTimeFromID(uniqueMessageId), UtilAll.YYYY_MM_DD_HH_MM_SS_SSS)), | ||
| output); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExecuteDecodeDocumentedOffsetMessageId() throws SubCommandException { | ||
| String output = execute("-i", "0A42333A00002A9F000000000134F1F5", "-t", "offset"); | ||
|
|
||
| Assert.assertEquals(String.format("StoreHost: 10.66.51.58:10911%nCommitLogOffset: 20247029%n"), output); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExecuteDecodeIPv4OffsetMessageId() throws Exception { | ||
| long commitLogOffset = 123456L; | ||
| String offsetMessageId = MessageDecoder.createMessageId(new InetSocketAddress("127.0.0.1", 10911), commitLogOffset); | ||
| MessageId decodedMessageId = MessageDecoder.decodeMessageId(offsetMessageId); | ||
|
|
||
| String output = execute("-i", offsetMessageId, "-t", "offset"); | ||
|
|
||
| Assert.assertEquals(String.format("StoreHost: %s%nCommitLogOffset: %d%n", | ||
| formatStoreHost(decodedMessageId), commitLogOffset), output); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExecuteDecodeIPv6OffsetMessageId() throws Exception { | ||
| long commitLogOffset = 654321L; | ||
| String offsetMessageId = MessageDecoder.createMessageId( | ||
| new InetSocketAddress(InetAddress.getByName("1050:0000:0000:0000:0005:0600:300c:326b"), 10912), commitLogOffset); | ||
| Assert.assertEquals(56, offsetMessageId.length()); | ||
|
|
||
| String output = execute("-i", offsetMessageId, "-t", "offset"); | ||
|
|
||
| Assert.assertEquals(String.format("StoreHost: [1050:0:0:0:5:600:300c:326b]:10912%nCommitLogOffset: 654321%n"), output); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExecuteWithUnsupportedMessageIdType() throws SubCommandException { | ||
| try { | ||
| execute("-i", MessageClientIDSetter.createUniqID(), "-t", "unknown"); | ||
| Assert.fail("Should fail when messageIdType is unsupported"); | ||
| } catch (SubCommandException e) { | ||
| Assert.assertTrue(e.getMessage().contains("Unsupported messageIdType: unknown")); | ||
| } | ||
| } | ||
|
|
||
| private String execute(String... subargs) throws SubCommandException { | ||
| DecodeMessageIdCommand cmd = new DecodeMessageIdCommand(); | ||
| Options options = ServerUtil.buildCommandlineOptions(new Options()); | ||
| CommandLine commandLine = ServerUtil.parseCmdLine("mqadmin " + cmd.commandName(), subargs, | ||
| cmd.buildCommandlineOptions(options), new DefaultParser()); | ||
| PrintStream out = System.out; | ||
| ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
| System.setOut(new PrintStream(bos)); | ||
| try { | ||
| cmd.execute(commandLine, options, null); | ||
| return new String(bos.toByteArray(), StandardCharsets.UTF_8); | ||
| } finally { | ||
| System.setOut(out); | ||
| } | ||
| } | ||
|
|
||
| private String formatStoreHost(MessageId messageId) { | ||
| InetSocketAddress address = (InetSocketAddress) messageId.getAddress(); | ||
| return NetworkUtil.normalizeHostAddress(address.getAddress()) + ":" + address.getPort(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This command does not seem to be registered or referenced anywhere. Would it be better to remove it instead of renaming it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Since this command has a clear purpose but is currently not registered, I prefer keeping it and registering it in MQAdminStartup instead of removing it.