Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE 3585] [Part D] improve performance of createUniqID() #3590

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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