From 576ed87253974c58927f0f82bff6b3303cbe664f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20G=C3=B6kdemir?= Date: Thu, 24 Sep 2026 15:34:41 +0300 Subject: [PATCH] fix: preserve causality for future HLC updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Efe Gökdemir --- .../src/main/java/org/apache/bifromq/basehlc/HLC.java | 2 +- .../test/java/org/apache/bifromq/basehlc/HLCTest.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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); + } }