Skip to content

Commit

Permalink
[ISSUE 3585] [Part D] improve performance of createUniqID(), from 2.4…
Browse files Browse the repository at this point in the history
…1% to 0.42% in flame graph of producer side.
  • Loading branch information
areyouok committed Dec 6, 2021
1 parent 4daade3 commit 13d7c22
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
14 changes: 14 additions & 0 deletions common/src/main/java/org/apache/rocketmq/common/UtilAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,20 @@ public static String bytes2string(byte[] src) {
return new String(hexChars);
}

public static void writeInt(char[] buffer, int pos, int value) {
char[] hexArray = HEX_ARRAY;
for (int moveBits = 28; moveBits >= 0; moveBits -= 4) {
buffer[pos++] = hexArray[(value >>> moveBits) & 0x0F];
}
}

public static void writeShort(char[] buffer, int pos, int value) {
char[] hexArray = HEX_ARRAY;
for (int moveBits = 12; moveBits >= 0; moveBits -= 4) {
buffer[pos++] = hexArray[(value >>> moveBits) & 0x0F];
}
}

public static byte[] string2bytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class MessageClientIDSetter {
private static final String TOPIC_KEY_SPLITTER = "#";
private static final int LEN;
private static final String FIX_STRING;
private static final char[] FIX_STRING;
private static final AtomicInteger COUNTER;
private static long startTime;
private static long nextStartTime;
Expand All @@ -42,7 +42,7 @@ public class MessageClientIDSetter {
tempBuffer.put(ip);
tempBuffer.putShort((short) UtilAll.getPid());
tempBuffer.putInt(MessageClientIDSetter.class.getClassLoader().hashCode());
FIX_STRING = UtilAll.bytes2string(tempBuffer.array());
FIX_STRING = UtilAll.bytes2string(tempBuffer.array()).toCharArray();
setStartTime(System.currentTimeMillis());
COUNTER = new AtomicInteger(0);
}
Expand Down Expand Up @@ -112,21 +112,22 @@ public static int getPidFromID(String msgID) {
}

public static String createUniqID() {
StringBuilder sb = new StringBuilder(LEN * 2);
sb.append(FIX_STRING);
sb.append(UtilAll.bytes2string(createUniqIDBuffer()));
return sb.toString();
}

private static byte[] createUniqIDBuffer() {
ByteBuffer buffer = ByteBuffer.allocate(4 + 2);
char[] sb = new char[LEN * 2];
System.arraycopy(FIX_STRING, 0, sb, 0, FIX_STRING.length);
long current = System.currentTimeMillis();
if (current >= nextStartTime) {
setStartTime(current);
}
buffer.putInt((int) (System.currentTimeMillis() - startTime));
buffer.putShort((short) COUNTER.getAndIncrement());
return buffer.array();
int diff = (int)(current - startTime);
if (diff < 0 && diff > -1000_000) {
// may cause by NTP
diff = 0;
}
int pos = FIX_STRING.length;
UtilAll.writeInt(sb, pos, diff);
pos += 8;
UtilAll.writeShort(sb, pos, COUNTER.getAndIncrement());
return new String(sb);
}

public static void setUniqID(final Message msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,30 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.charset.StandardCharsets;

public class MessageClientIDSetterTest {

@Test
public void testGetTimeFromID() {
long t = System.currentTimeMillis();
String uniqID = MessageClientIDSetter.createUniqID();
long t2 = MessageClientIDSetter.getNearlyTimeFromID(uniqID).getTime();
assertThat(t2 - t < 20);
}

@Test
public void testGetCountFromID() {
String uniqID = MessageClientIDSetter.createUniqID();
String uniqID2 = MessageClientIDSetter.createUniqID();
String idHex = uniqID.substring(uniqID.length() - 4);
String idHex2 = uniqID2.substring(uniqID2.length() - 4);
int s1 = Integer.parseInt(idHex, 16);
int s2 = Integer.parseInt(idHex2, 16);
assertThat(s1 == s2 - 1);
}


@Test
public void testGetIPStrFromID() {
byte[] ip = UtilAll.getIP();
Expand Down

0 comments on commit 13d7c22

Please sign in to comment.