Hybrid Logical Clock Updates
Hybrid Logical Clock (HLC) Updates
Hybrid Logical Clocks combine physical time with a logical counter. Each HLC timestamp is (H, L):
His the physical time componentLis a logical counter for ordering within the sameH
You are given a starting HLC and a sequence of events. Each event includes the current physical time Now. For a receive event, a remote HLC is also provided. Compute the local HLC after each event.
HLC Update Rules
Let the current local clock be (H, L) and Now be the physical time.
Local or Send event:
- If
Now > H:H = Now,L = 0 - Else:
L = L + 1
Receive event with remote (Hr, Lr):
H' = max(H, Hr, Now)- If
H' == HandH' == Hr:L = max(L, Lr) + 1 - Else if
H' == H:L = L + 1 - Else if
H' == Hr:L = Lr + 1 - Else (
H' == Now):L = 0 - Set
H = H'
Types
type Event struct {
Kind string // "local", "send", "recv"
Now int
RemoteH int
RemoteL int
}
type HLC struct {
H int
L int
}
Function signature
func HLCUpdates(start HLC, events []Event) []HLC
Example
start = (H=10, L=0)
Events:
1) local, Now=10
2) recv, Now=12, remote=(11,3)
Result:
1) (10,1)
2) (12,0)
Constraints
0 <= len(events) <= 1000000 <= Now, RemoteH, RemoteL <= 1_000_000
Run tests to see results
No issues detected