Skip to content

Commit

Permalink
[ISSUE apache#1751]Fix bug in MessageClientIDSetter (apache#1758)
Browse files Browse the repository at this point in the history
* pid 4bytes

* pid is 4 bytes

* apache#1751 fix bug and add a unit test

* apache#1751 short pid

* 保持原来

* apache#1751 remove repeat code
  • Loading branch information
rushsky518 committed Feb 12, 2020
1 parent c8894fe commit 6f48059
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
Expand Up @@ -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());
Expand Down Expand Up @@ -107,6 +104,13 @@ public static byte[] getIPFromID(String msgID) {
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);
Expand All @@ -120,7 +124,6 @@ private static byte[] createUniqIDBuffer() {
if (current >= nextStartTime) {
setStartTime(current);
}
buffer.position(0);
buffer.putInt((int) (System.currentTimeMillis() - startTime));
buffer.putShort((short) COUNTER.getAndIncrement());
return buffer.array();
Expand All @@ -145,4 +148,3 @@ public static byte[] createFakeIP() {
return fakeIP;
}
}

Expand Up @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 6f48059

Please sign in to comment.