You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by du...@apache.org on 2020/02/12 07:59:30 UTC
[rocketmq] branch develop updated: [ISSUE #1751]Fix bug in
MessageClientIDSetter (#1758)
This is an automated email from the ASF dual-hosted git repository.
duhengforever 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 689e770 [ISSUE #1751]Fix bug in MessageClientIDSetter (#1758)
689e770 is described below
commit 689e77066dd3e2f9ad0a11dd6f70302102d46b29
Author: rushsky518 <ru...@163.com>
AuthorDate: Wed Feb 12 15:59:21 2020 +0800
[ISSUE #1751]Fix bug in MessageClientIDSetter (#1758)
* 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 +++++++------
.../common/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 df11556..9d6ec95 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 1ec6d93..55aa505 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);
+ }
}