未验证 提交 689e7706 编写于 作者: rushsky518's avatar rushsky518 提交者: GitHub

[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
上级 c572b4b2
......@@ -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;
}
}
......@@ -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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册