diff --git a/base-hlc/src/main/java/org/apache/bifromq/basehlc/HLC.java b/base-hlc/src/main/java/org/apache/bifromq/basehlc/HLC.java index 82d29a1ff..b90d7621d 100644 --- a/base-hlc/src/main/java/org/apache/bifromq/basehlc/HLC.java +++ b/base-hlc/src/main/java/org/apache/bifromq/basehlc/HLC.java @@ -118,7 +118,7 @@ public long update(long otherHLC) { c = Math.max(c, otherC) + 1; } else if (updateL == l) { c++; - } else if (otherL == l) { + } else if (updateL == otherL) { c = otherC + 1; } else { c = 0; diff --git a/base-hlc/src/test/java/org/apache/bifromq/basehlc/HLCTest.java b/base-hlc/src/test/java/org/apache/bifromq/basehlc/HLCTest.java index 7acdfbb84..15c85bad3 100644 --- a/base-hlc/src/test/java/org/apache/bifromq/basehlc/HLCTest.java +++ b/base-hlc/src/test/java/org/apache/bifromq/basehlc/HLCTest.java @@ -47,4 +47,13 @@ public void update() { t1 = t; } } + + @Test + public void updateWithFutureTimestampPreservesCausality() { + long observed = ((System.currentTimeMillis() + 1000) << 16) | 5; + + long updated = HLC.INST.update(observed); + + assertTrue(updated > observed); + } }