From 689e77066dd3e2f9ad0a11dd6f70302102d46b29 Mon Sep 17 00:00:00 2001 From: rushsky518 Date: Wed, 12 Feb 2020 15:59:21 +0800 Subject: [PATCH] [ISSUE #1751]Fix bug in MessageClientIDSetter (#1758) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * pid 4bytes * pid is 4 bytes * #1751 fix bug and add a unit test * #1751 short pid * 保持原来 * #1751 remove repeat code --- .../common/message/MessageClientIDSetter.java | 14 ++++++----- .../message/MessageClientIDSetterTest.java | 23 ++++++++++++++----- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java b/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java index df115563..9d6ec95a 100644 --- a/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java +++ b/common/src/main/java/org/apache/rocketmq/common/message/MessageClientIDSetter.java @@ -39,11 +39,8 @@ public class MessageClientIDSetter { } LEN = ip.length + 2 + 4 + 4 + 2; ByteBuffer tempBuffer = ByteBuffer.allocate(ip.length + 2 + 4); - tempBuffer.position(0); tempBuffer.put(ip); - tempBuffer.position(ip.length); - tempBuffer.putInt(UtilAll.getPid()); - tempBuffer.position(ip.length + 2); + tempBuffer.putShort((short) UtilAll.getPid()); tempBuffer.putInt(MessageClientIDSetter.class.getClassLoader().hashCode()); FIX_STRING = UtilAll.bytes2string(tempBuffer.array()); setStartTime(System.currentTimeMillis()); @@ -107,6 +104,13 @@ public class MessageClientIDSetter { return result; } + public static short getPidFromID(String msgID) { + byte[] bytes = UtilAll.string2bytes(msgID); + ByteBuffer wrap = ByteBuffer.wrap(bytes); + wrap.position(bytes.length - 2 - 4 - 4 - 2); + return wrap.getShort(); + } + public static String createUniqID() { StringBuilder sb = new StringBuilder(LEN * 2); sb.append(FIX_STRING); @@ -120,7 +124,6 @@ public class MessageClientIDSetter { if (current >= nextStartTime) { setStartTime(current); } - buffer.position(0); buffer.putInt((int) (System.currentTimeMillis() - startTime)); buffer.putShort((short) COUNTER.getAndIncrement()); return buffer.array(); @@ -145,4 +148,3 @@ public class MessageClientIDSetter { return fakeIP; } } - diff --git a/common/src/test/java/org/apache/rocketmq/common/message/MessageClientIDSetterTest.java b/common/src/test/java/org/apache/rocketmq/common/message/MessageClientIDSetterTest.java index 1ec6d93c..55aa5058 100644 --- a/common/src/test/java/org/apache/rocketmq/common/message/MessageClientIDSetterTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/message/MessageClientIDSetterTest.java @@ -17,6 +17,7 @@ package org.apache.rocketmq.common.message; +import org.apache.rocketmq.common.UtilAll; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -25,12 +26,22 @@ public class MessageClientIDSetterTest { @Test public void testGetIPStrFromID() { - String ipv4HostMsgId = "C0A803CA00002A9F0000000000031367"; - String ipv6HostMsgId = "24084004018081003FAA1DDE2B3F898A00002A9F0000000000000CA0"; - String v4Ip = "192.168.3.202"; - String v6Ip = "2408:4004:0180:8100:3faa:1dde:2b3f:898a"; - assertThat(MessageClientIDSetter.getIPStrFromID(ipv4HostMsgId)).isEqualTo(v4Ip); - assertThat(MessageClientIDSetter.getIPStrFromID(ipv6HostMsgId)).isEqualTo(v6Ip); + byte[] ip = UtilAll.getIP(); + String ipStr = (4 == ip.length) ? UtilAll.ipToIPv4Str(ip) : UtilAll.ipToIPv6Str(ip); + + String uniqID = MessageClientIDSetter.createUniqID(); + String ipStrFromID = MessageClientIDSetter.getIPStrFromID(uniqID); + + assertThat(ipStr).isEqualTo(ipStrFromID); } + @Test + public void testGetPidFromID() { + int pid = UtilAll.getPid(); + + String uniqID = MessageClientIDSetter.createUniqID(); + short pidFromID = MessageClientIDSetter.getPidFromID(uniqID); + + assertThat(pid).isEqualTo(pidFromID); + } } -- GitLab