TASK
Implementation
NTP can adjust the system clock backward. Physical timestamps break. Lamport clocks keep working but lose time correlation. HLC handles it gracefully by keeping its physical component and incrementing the logical counter.
Implement a simulation that demonstrates this:
simulate_clock_backwardsets the HLC's internal notion of "now" backward by a given amount- After the backward jump, HLC.tick() should still produce advancing timestamps
- Report the drift between HLC.pt and actual physical time
Request: {"type": "simulate_backward", "msg_id": 1, "offset_ms": -5000}
Response: {"type": "simulate_backward_ok", "in_reply_to": 1}Then tick and observe HLC advances despite backward clock:
Request: {"type": "hlc_tick", "msg_id": 2}
Response: {"type": "hlc_tick_ok", "in_reply_to": 2, "pt": 1234567, "lc": 1, "drift_ms": 5000}The drift_ms field shows how far HLC.pt is ahead of the (simulated) physical clock.
Sample Test Cases
Simulate backward responds okTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"simulate_backward","msg_id":2,"offset_ms":-5000}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "simulate_backward_ok", "in_reply_to": 2, "msg_id": 1}}
Tick after backward still advancesTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"hlc_tick","msg_id":2}}
{"src":"c1","dest":"n1","body":{"type":"simulate_backward","msg_id":3,"offset_ms":-10000}}
{"src":"c1","dest":"n1","body":{"type":"hlc_tick","msg_id":4}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
When system clock goes backward, HLC keeps its pt and increments lc
Hint 2▾
This means HLC never goes backward, preserving ordering
Hint 3▾
Lamport clocks also handle this, but lose physical time correlation
Hint 4▾
Physical clocks break entirely on backward adjustments
Hint 5▾
Track the drift between HLC.pt and actual physical time
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
NTP adjustmentclock backwardmonotonic guaranteeHLC resilience
main.py
python
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
#!/usr/bin/env python3
import sys
import json
import time
class HLC:
def __init__(self):
self.pt = 0
self.lc = 0
self.clock_offset = 0 # simulated offset in ms
def now_ms(self):
return int(time.time() * 1000) + self.clock_offset
def tick(self):
now = self.now_ms()
if now > self.pt:
self.pt = now
self.lc = 0
else:
self.lc += 1
drift = self.pt - now if self.pt > now else 0
return {"pt": self.pt, "lc": self.lc, "drift_ms": drift}
def simulate_backward(self, offset_ms):
# TODO: Adjust clock_offset to simulate backward jump
pass
def get(self):
return {"pt": self.pt, "lc": self.lc}
class Node:
def __init__(self):
self.node_id = None
self.node_ids = []
self.next_msg_id = 0
self.hlc = HLC()
def send(self, dest, body):
body["msg_id"] = self.next_msg_id