Network Time Synchronization
By Soumik Dutta
I was working on some Spring Boot application and one day I noticed a warning “Thread starvation or clock leap detected (housekeeper delta=8m11s787ms)”
2021–03–28 23:16:28.096 WARN 22268 — — [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 — Thread starvation or clock leap detected (housekeeper delta=8m11s787ms).
To know this in detail I had to go back to clock synchronization using NTP.
In Mac go to -> System preference -> Date & Time
time.apple.com is the NTP server here and most of us will have this already set.
To know what has happened during the sync process from the NTP server we need to know the following things
- What is a system clock?
- Synchronizing time with different clocks.
- Different clocks in Java.
What is a system clock?
All modern devices like laptops, phones, smartwatches all have a circuit for keeping track of time. When kept under tension, quartz crystal oscillates at a well-defined frequency that depends on the kind of crystal, how it is cut, and the amount of tension.
Each crystal has two registers, a counter, and a holding register. Each oscillation of crystal decrements the counter by one. When the counter gets to zero, an interrupt is generated and the counter is reloaded from the holding register. In this way, it is possible to program a timer to generate an interrupt 60 times a second. Each interrupt is called one clock tick. This is called a quartz clock. It can have an error of up to 20sec/year this will also vary with temperature.
There is another type of clock that has an error of 1 sec/ 3million years. This is called an atomic clock. Here instead of quartz, cesium-133 atoms are used to measure time. International System of Units (SI) has defined the second as the duration of 9,192,631,770 cycles of the radiation corresponding to the transition between two energy levels of the ground state of the cesium-133 atom.
Now the NTP server will have an atomic clock (in this case time.apple.com) and our system will call this server to adjust its time based on that. The difference between the clocks at any point in time is called clock skew.
Now let us see how this is done
Synchronizing time with different clock
NTP client requests the NTP server at time t1 and this time is also sent in the request. The Server receives the request at time t2 and responds to it at time t3. The response will have time t1,t2,t3.
Now the round trip network delay δ = (t4 — t1) — (t3 — t2)
The estimated server time when the client receives response t3 + δ/2
Estimated clock skew becomes θ = t3 + δ/2 — t4. = (t2-t1+t3-t4)/2
Now the skew time is adjusted by the client in this case our system.
Different clocks in Java.
In Java, we have currentTimeMillis in the System class which is nothing but the system time. Now let’s see an example where this will lead us to erroneous data
Here we record the start time and do something (in this case sleep for 10sec) and then record the endTime and take the difference. In the happy flow, this will perfectly work fine. But if the clock adjustment happens during the 10-sec sleep the elapsed time can be negative.
Java solution to this problem Monotonic clock
The monotonic clock is maintained within the computer system it starts/references from the machine last rebooted. But this time cannot be compared to other machines as it is maintained locally and has no meaning outside.