Skip to content

Commit 0659e49

Browse files
committed
fix direct load trace id generator (#400)
1 parent 55be188 commit 0659e49

File tree

4 files changed

+62
-46
lines changed

4 files changed

+62
-46
lines changed

src/main/java/com/alipay/oceanbase/rpc/direct_load/ObDirectLoadConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class ObDirectLoadConnection {
6363

6464
ObDirectLoadConnection(ObDirectLoadConnectionFactory connectionFactory) {
6565
this.connectionFactory = connectionFactory;
66-
this.traceId = ObDirectLoadTraceId.generateTraceId();
66+
this.traceId = ObDirectLoadTraceIdGenerator.generate();
6767
this.logger = ObDirectLoadLogger.getLogger(this.traceId);
6868
}
6969

src/main/java/com/alipay/oceanbase/rpc/direct_load/ObDirectLoadStatement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class ObDirectLoadStatement {
5757

5858
ObDirectLoadStatement(ObDirectLoadConnection connection) {
5959
this.connection = connection;
60-
this.traceId = ObDirectLoadTraceId.generateTraceId();
60+
this.traceId = ObDirectLoadTraceIdGenerator.generate();
6161
this.logger = ObDirectLoadLogger.getLogger(this.traceId);
6262
}
6363

src/main/java/com/alipay/oceanbase/rpc/direct_load/ObDirectLoadTraceId.java

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
package com.alipay.oceanbase.rpc.direct_load;
1919

20-
import java.net.InetAddress;
21-
import java.util.concurrent.atomic.AtomicLong;
22-
2320
public class ObDirectLoadTraceId {
2421

2522
private final long uniqueId;
@@ -43,50 +40,9 @@ public long getSequence() {
4340
}
4441

4542
public static final ObDirectLoadTraceId DEFAULT_TRACE_ID;
46-
public static TraceIdGenerator traceIdGenerator;
4743

4844
static {
4945
DEFAULT_TRACE_ID = new ObDirectLoadTraceId(0, 0);
50-
traceIdGenerator = new TraceIdGenerator();
51-
}
52-
53-
public static ObDirectLoadTraceId generateTraceId() {
54-
return traceIdGenerator.generate();
5546
}
5647

57-
public static class TraceIdGenerator {
58-
59-
private final ObDirectLoadLogger logger = ObDirectLoadLogger.getLogger();
60-
61-
private final long uniqueId;
62-
private AtomicLong sequence;
63-
64-
public TraceIdGenerator() {
65-
long ip = 0;
66-
try {
67-
ip = ipToLong(InetAddress.getLocalHost().getHostAddress());
68-
} catch (Exception e) {
69-
logger.warn("get local host address failed", e);
70-
}
71-
long port = (long) (Math.random() % 65536) << 32;
72-
long isUserRequest = (1l << (32 + 16));
73-
long reserved = 0;
74-
uniqueId = ip | port | isUserRequest | reserved;
75-
sequence = new AtomicLong(0);
76-
}
77-
78-
private static long ipToLong(String strIp) {
79-
String[] ip = strIp.split("\\.");
80-
return (Long.parseLong(ip[0]) << 24) + (Long.parseLong(ip[1]) << 16)
81-
+ (Long.parseLong(ip[2]) << 8) + (Long.parseLong(ip[3]));
82-
}
83-
84-
public ObDirectLoadTraceId generate() {
85-
long newSequence = System.currentTimeMillis() * 1000 + sequence.incrementAndGet()
86-
% 1000;
87-
return new ObDirectLoadTraceId(uniqueId, newSequence);
88-
}
89-
90-
};
91-
9248
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*-
2+
* #%L
3+
* com.oceanbase:obkv-table-client
4+
* %%
5+
* Copyright (C) 2021 - 2025 OceanBase
6+
* %%
7+
* OBKV Table Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.rpc.direct_load;
19+
20+
import java.net.InetAddress;
21+
import java.util.concurrent.atomic.AtomicLong;
22+
23+
public class ObDirectLoadTraceIdGenerator {
24+
25+
private final ObDirectLoadLogger logger = ObDirectLoadLogger.getLogger();
26+
private static ObDirectLoadTraceIdGenerator instance = new ObDirectLoadTraceIdGenerator();
27+
28+
private final long uniqueId;
29+
private AtomicLong sequence;
30+
31+
public ObDirectLoadTraceIdGenerator() {
32+
long ip = 0;
33+
try {
34+
ip = ipToLong(InetAddress.getLocalHost().getHostAddress());
35+
} catch (Exception e) {
36+
logger.warn("get local host address failed", e);
37+
}
38+
long port = (long) (Math.random() % 65536) << 32;
39+
long isUserRequest = (1l << (32 + 16));
40+
long reserved = 0;
41+
uniqueId = ip | port | isUserRequest | reserved;
42+
sequence = new AtomicLong(0);
43+
}
44+
45+
private static long ipToLong(String strIp) {
46+
String[] ip = strIp.split("\\.");
47+
return (Long.parseLong(ip[0]) << 24) + (Long.parseLong(ip[1]) << 16)
48+
+ (Long.parseLong(ip[2]) << 8) + (Long.parseLong(ip[3]));
49+
}
50+
51+
public ObDirectLoadTraceId generateNextId() {
52+
long newSequence = System.currentTimeMillis() * 1000 + sequence.incrementAndGet() % 1000;
53+
return new ObDirectLoadTraceId(uniqueId, newSequence);
54+
}
55+
56+
public static ObDirectLoadTraceId generate() {
57+
return instance.generateNextId();
58+
}
59+
60+
}

0 commit comments

Comments
 (0)