forked from oceanbase/obkv-table-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObDirectLoadTraceId.java
More file actions
128 lines (102 loc) · 3.84 KB
/
ObDirectLoadTraceId.java
File metadata and controls
128 lines (102 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*-
* #%L
* com.oceanbase:obkv-table-client
* %%
* Copyright (C) 2021 - 2024 OceanBase
* %%
* OBKV Table Client Framework is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* #L%
*/
package com.alipay.oceanbase.rpc.direct_load;
import java.net.InetAddress;
import java.util.concurrent.atomic.AtomicLong;
import com.alipay.oceanbase.rpc.util.ObByteBuf;
import com.alipay.oceanbase.rpc.util.Serialization;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
public class ObDirectLoadTraceId {
private final long uniqueId;
private final long sequence;
public ObDirectLoadTraceId(long uniqueId, long sequence) {
this.uniqueId = uniqueId;
this.sequence = sequence;
}
public long getUniqueId() {
return uniqueId;
}
public long getSequence() {
return sequence;
}
public String toString() {
return String.format("Y%X-%016X", uniqueId, sequence);
}
public byte[] encode() {
int needBytes = (int) getEncodedSize();
ObByteBuf buf = new ObByteBuf(needBytes);
encode(buf);
return buf.bytes;
}
public void encode(ObByteBuf buf) {
Serialization.encodeVi64(buf, uniqueId);
Serialization.encodeVi64(buf, sequence);
}
public static ObDirectLoadTraceId decode(ByteBuf buf) {
long uniqueId = Serialization.decodeVi64(buf);
long sequence = Serialization.decodeVi64(buf);
return new ObDirectLoadTraceId(uniqueId, sequence);
}
public static ObDirectLoadTraceId decode(byte[] bytes) {
ByteBuf buf = Unpooled.wrappedBuffer(bytes);
return decode(buf);
}
public int getEncodedSize() {
int len = 0;
len += Serialization.getNeedBytes(uniqueId);
len += Serialization.getNeedBytes(sequence);
return len;
}
public static final ObDirectLoadTraceId DEFAULT_TRACE_ID;
public static TraceIdGenerator traceIdGenerator;
static {
DEFAULT_TRACE_ID = new ObDirectLoadTraceId(0, 0);
traceIdGenerator = new TraceIdGenerator();
}
public static ObDirectLoadTraceId generateTraceId() {
return traceIdGenerator.generate();
}
public static class TraceIdGenerator {
private final ObDirectLoadLogger logger = ObDirectLoadLogger.getLogger();
private final long uniqueId;
private AtomicLong sequence;
public TraceIdGenerator() {
long ip = 0;
try {
ip = ipToLong(InetAddress.getLocalHost().getHostAddress());
} catch (Exception e) {
logger.warn("get local host address failed", e);
}
long port = (long) (Math.random() % 65536) << 32;
long isUserRequest = (1l << (32 + 16));
long reserved = 0;
uniqueId = ip | port | isUserRequest | reserved;
sequence = new AtomicLong(0);
}
private static long ipToLong(String strIp) {
String[] ip = strIp.split("\\.");
return (Long.parseLong(ip[0]) << 24) + (Long.parseLong(ip[1]) << 16)
+ (Long.parseLong(ip[2]) << 8) + (Long.parseLong(ip[3]));
}
public ObDirectLoadTraceId generate() {
long newSequence = System.currentTimeMillis() * 1000 + sequence.incrementAndGet()
% 1000;
return new ObDirectLoadTraceId(uniqueId, newSequence);
}
};
}