Traceid生成规则优化
This commit is contained in:
@@ -116,7 +116,6 @@
|
||||
默认置入的system.property.的有:
|
||||
System.setProperty("redkale.convert.pool.size", "128");
|
||||
System.setProperty("redkale.convert.writer.buffer.defsize", "4096");
|
||||
System.setProperty("redkale.trace.enable", "false");
|
||||
|
||||
<properties>节点下也可包含非<property>节点.
|
||||
非<property>其节点可以通过@Resource(name="properties.xxxxxx")进行注入, 被注解的字段类型只能是AnyValue、AnyValue[]
|
||||
|
||||
@@ -310,10 +310,7 @@ public final class Application {
|
||||
if (System.getProperty("redkale.convert.writer.buffer.defsize") == null) {
|
||||
System.setProperty("redkale.convert.writer.buffer.defsize", "4096");
|
||||
}
|
||||
if (System.getProperty("redkale.trace.enable") == null) {
|
||||
System.setProperty("redkale.trace.enable", "false");
|
||||
}
|
||||
|
||||
|
||||
System.setProperty("redkale.version", Redkale.getDotedVersion());
|
||||
int nid = config.getIntValue("nodeid", 0);
|
||||
this.nodeid = nid;
|
||||
|
||||
@@ -18,9 +18,16 @@ public class Traces {
|
||||
|
||||
private static final boolean enable = Boolean.getBoolean("redkale.trace.enable");
|
||||
|
||||
private static final String PROCESS_ID = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
|
||||
private static final ThreadLocal<IdSequence> THREAD_SEQUENCE = ThreadLocal.withInitial(IdSequence::new);
|
||||
|
||||
private static final ThreadLocal<String> localTrace = new ThreadLocal<>();
|
||||
|
||||
private static final Supplier<String> tidSupplier = () -> UUID.randomUUID().toString().replace("-", "");
|
||||
//借用Skywalking的GlobalIdGenerator生成ID的规则
|
||||
private static final Supplier<String> tidSupplier = () -> PROCESS_ID
|
||||
+ "." + Thread.currentThread().getId()
|
||||
+ "." + THREAD_SEQUENCE.get().nextSeq();
|
||||
|
||||
public static boolean enable() {
|
||||
return enable;
|
||||
@@ -46,4 +53,41 @@ public class Traces {
|
||||
return requestTraceid;
|
||||
}
|
||||
|
||||
private static class IdSequence {
|
||||
|
||||
private long lastTimestamp;
|
||||
|
||||
private short threadSeq;
|
||||
|
||||
private long lastShiftTimestamp;
|
||||
|
||||
private int lastShiftValue;
|
||||
|
||||
public IdSequence() {
|
||||
this.lastTimestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long nextSeq() {
|
||||
long rs = timestamp() * 10000;
|
||||
if (threadSeq == 10000) {
|
||||
threadSeq = 0;
|
||||
}
|
||||
return rs + threadSeq++;
|
||||
}
|
||||
|
||||
private long timestamp() {
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
if (currentTimeMillis < lastTimestamp) {
|
||||
// Just for considering time-shift-back by Ops or OS. @hanahmily 's suggestion.
|
||||
if (lastShiftTimestamp != currentTimeMillis) {
|
||||
lastShiftValue++;
|
||||
lastShiftTimestamp = currentTimeMillis;
|
||||
}
|
||||
return lastShiftValue;
|
||||
} else {
|
||||
lastTimestamp = currentTimeMillis;
|
||||
return lastTimestamp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user