Skip to content

Commit c2b44d9

Browse files
authored
fix direct load trace id generator (#400)
1 parent f28419c commit c2b44d9

File tree

3 files changed

+62
-46
lines changed

3 files changed

+62
-46
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class ObDirectLoadConnection {
6464

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

@@ -259,7 +259,7 @@ ObDirectLoadStatement buildStatement(ObDirectLoadStatement.Builder builder)
259259
ObDirectLoadStatement stmt = null;
260260
try {
261261
final ObDirectLoadTraceId traceId = builder.getTraceId() != null ? builder.getTraceId()
262-
: ObDirectLoadTraceId.generateTraceId();
262+
: ObDirectLoadTraceIdGenerator.generate();
263263
stmt = createStatement(traceId);
264264
stmt.init(builder);
265265
} catch (Exception e) {

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
import com.alipay.oceanbase.rpc.util.ObByteBuf;
2421
import com.alipay.oceanbase.rpc.util.Serialization;
2522

@@ -79,50 +76,9 @@ public int getEncodedSize() {
7976
}
8077

8178
public static final ObDirectLoadTraceId DEFAULT_TRACE_ID;
82-
public static TraceIdGenerator traceIdGenerator;
8379

8480
static {
8581
DEFAULT_TRACE_ID = new ObDirectLoadTraceId(0, 0);
86-
traceIdGenerator = new TraceIdGenerator();
87-
}
88-
89-
public static ObDirectLoadTraceId generateTraceId() {
90-
return traceIdGenerator.generate();
9182
}
9283

93-
public static class TraceIdGenerator {
94-
95-
private final ObDirectLoadLogger logger = ObDirectLoadLogger.getLogger();
96-
97-
private final long uniqueId;
98-
private AtomicLong sequence;
99-
100-
public TraceIdGenerator() {
101-
long ip = 0;
102-
try {
103-
ip = ipToLong(InetAddress.getLocalHost().getHostAddress());
104-
} catch (Exception e) {
105-
logger.warn("get local host address failed", e);
106-
}
107-
long port = (long) (Math.random() % 65536) << 32;
108-
long isUserRequest = (1l << (32 + 16));
109-
long reserved = 0;
110-
uniqueId = ip | port | isUserRequest | reserved;
111-
sequence = new AtomicLong(0);
112-
}
113-
114-
private static long ipToLong(String strIp) {
115-
String[] ip = strIp.split("\\.");
116-
return (Long.parseLong(ip[0]) << 24) + (Long.parseLong(ip[1]) << 16)
117-
+ (Long.parseLong(ip[2]) << 8) + (Long.parseLong(ip[3]));
118-
}
119-
120-
public ObDirectLoadTraceId generate() {
121-
long newSequence = System.currentTimeMillis() * 1000 + sequence.incrementAndGet()
122-
% 1000;
123-
return new ObDirectLoadTraceId(uniqueId, newSequence);
124-
}
125-
126-
};
127-
12884
}
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)