当前位置:网站首页>A lightweight tracking method based on unique identifier of timestamp
A lightweight tracking method based on unique identifier of timestamp
2022-07-18 15:38:00 【Micro Stone】
Unique identifiers in programs are very useful for tracing . When these id When a high-resolution timestamp is included , They will be more useful .
The unique identifier not only records the time of the event , And it is the only event that can help track through the system .
This unique timestamp is different according to the implementation , The cost will be relatively high .
Next, we discuss a lightweight method , It can generate a unique 、 Monotonically increasing nanosecond resolution timestamp .

Purpose of unique identifier
A unique identifier can be used to associate with a piece of information , So that the information can be explicitly quoted later . It can be an event 、 request 、 Order ID Or customers ID.
Their businesses can be used as databases or keys / The primary key in the value store , So that the information can be retrieved and identified later .
One of the challenges of generating these identifiers is to avoid creating duplicates without increasing costs .
We can record every identifier created in the database , But when we want to add more identifiers , This will use O(n) Storage .
You can generate a random identifier , For example, it is unlikely to repeat UUID, however , This will create a larger id( Don't look at just a string , When the equivalent is large , It's very huge ), Otherwise, it does not contain any information . for example ,UUID It might look like
**
d85686f5-7a53-4682-9177-0b64037af336**
this UUID Can be stored as 16 Bytes , But it is usually stored as occupancy 40 An object of bytes of memory .
Use 256 Bits reduce the risk of duplicate identifiers , But it will double the memory .
Timestamp as a unique identifier
Using timestamp has two benefits . You don't need to store too much information , Because the clock is driver . You only need to check two threads at different times , The disadvantage is that it is lost on restart , for example , The clock time should be long enough , Still won't get a duplicate timestamp .
Such identifiers are also easier to read , And provide additional information useful for tracking . A unique identifier based on timestamp may be similar to **
2021-12-20T23:30:51.8453925**
This timestamp can be stored in LocalDateTime In the object , Can be stored as 8 Byte length .
MappedUniqueTimeProvider Code
This is a GitHub The provision of the MappedUniqueTimeProvider Of Streamlining edition
/** * Timestamps are unique across threads/processes on a single machine. */
public enum MappedUniqueTimeProvider implements TimeProvider {
INSTANCE;
private final Bytes bytes;
private TimeProvider provider = SystemTimeProvider.INSTANCE;
MappedUniqueTimeProvider() {
String user = System.getProperty("user.name", "unknown");
MappedFile file = MappedFile.mappedFile(OS.TMP + "/.time-stamp." + user + ".dat", OS.pageSize(), 0);
bytes = file.acquireBytesForWrite(mumtp, 0);
}
@Override
public long currentTimeNanos() throws IllegalStateException {
long time = provider.currentTimeNanos(), time5 = time >>> 5;
long time0 = bytes.readVolatileLong(LAST_TIME), timeNanos5 = time0 >>> 5;
if (time5 > timeNanos5 && bytes.compareAndSwapLong(LAST_TIME, time0, time))
return time;
while (true) {
time0 = bytes.readVolatileLong(LAST_TIME);
long next = (time0 + 0x20) & ~0x1f;
if (bytes.compareAndSwapLong(LAST_TIME, time0, next))
return next;
Jvm.nanoPause();
}
}
}
The following techniques have been used to ensure the uniqueness and efficiency of timestamp
Memory sharing
TimeProvider Use shared memory to ensure that nanosecond resolution time is unique . Memory mapped files are accessed in a thread safe manner , To ensure that the timestamp is monotonically increasing .Chronicle Bytes There is a library that supports thread safe access to memory mapped files .
Read the value in the memory mapped file and try to update it in the loop .CAS or compare-and-swap Operation is atomic , And check that the previous value has not been changed by another thread . Of course , This is to operate on a thread on the same service .
Store a nanosecond timestamp
We use the original long To store time stamps can improve efficiency , But this is more difficult to use , We support print And parsing is called
NanoTimestampLongConverter Long timestamp of , We also parse these timestamps and implicitly render them as text to make them easier to print 、 Debug and create unit tests .
public class Event extends SelfDescribingMarshallable {
@LongConversion(NanoTimestampLongConverter.class) long time;
}
Event e = new Event();
e.time = CLOCK.currentTimeNanos();
String str = e.toString();
Event e2 = Marshallable.fromString(str);
System.out.println(e2);
Prints
!net.openhft.chronicle.wire.Event {
time: 2021-12-20T23:30:51.8453925
}
Because nanosecond timestamp is a high-resolution format , It will only last until 2262 Year as a signed long integer or 2554 year , Before the value overflows , It can be assumed that it is an unsigned long integer .
We have used the extra position in the timestamp for other purposes , For example, storage host identifier or source ID. For this reason , We also make sure that the timestamp is for 32 ns The multiple of is unique , If we want , You can lower 5 Bits are used for other purposes .
effect
Under normal operation , It takes less than... To get a unique nanosecond timestamp on the server 50 ns. Under heavy multithreading load , It may take hundreds of nanoseconds .
MappedUniqueTimeProvider The application can last for more than 3000 ten thousand / Second generation .
Restartability
As long as time does not go backwards , This strategy can lose all States , But it can still ensure the uniqueness only from the clock . If the clock does go back an hour , Then the state will ensure that there is no repetition , however , Before the clock catches up , The timestamp will not match the clock .
Conclusion
There can be a lightweight 、 Unique identifier generator to save nanosecond timestamp .
边栏推荐
- Hybridclr -- epoch-making unity native C # hot update technology
- ONNX模型tensor shapes inference和Flops统计工具
- Vs2019 inline assembly development
- Lscale theme emlog background management panel theme source code
- Mipi CSI, DSI, UFS, c-phy, d-phy, m-phy concept understanding
- The application of Lora and lorawan wireless technology in the Internet of things
- Calculate the Euclidean distance between the row vectors of two matrices
- 高性能定時器
- Vulnhub-dc9 learning notes
- Minuterie haute performance
猜你喜欢

Overcome the challenges of digital transformation by developing a vision

Vs2019 inline assembly development

Can SQL also do AI? you 're right! Mlops meetup V3 review openmlbd+sqlflow+byzer

RMAN detailed tutorial (II) -- backup, inspection, maintenance, recovery
Win10 timed running program

Differences between get requests and post requests and usage examples

全链路压测 :测试要做的准备工作

第三篇章:运行时数据区——独立空间

Mipi CSI, DSI, UFS, c-phy, d-phy, m-phy concept understanding

Free SSL certificate application and deployment practice
随机推荐
[daily training] 515 Find the maximum value in each tree row
Detailed explanation of the decision table method of common test case design methods
Lscale theme emlog background management panel theme source code
Domestic postman tool, easy to use!
Symbolic naming analysis in reinforcement learning
2022 robocom world robot developer competition - undergraduate group (provincial competition) T4, T5
通过制定愿景克服数字化转型挑战
CV2. Setmousecallback() displays the pixel value and position of the mouse click image
Func () {} () is a strange way of writing. I don't know what category it belongs to
High performance timer
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) - B, C, D
86 touch switch / desk fan / air conditioner / smart home / home appliances, etc., low power consumption, high anti-interference, 3-key, 3-way, 3-way touch ic-vk3603 esop8, stable performance and adju
[daily training] 558 Intersection of quadtree
Overcome the challenges of digital transformation by developing a vision
R语言---颜色选择和设置
Andersen global enters Rwanda
免费SSL证书申请及部署实践
Can SQL also do AI? you 're right! Mlops meetup V3 review openmlbd+sqlflow+byzer
Talking about brain computer interface
Optimization of AI model structure based on nvidiagpu