Skip to content

Commit d6d339a

Browse files
committed
feat(topic): add Topic.latest() for a single-read value + timestamp
Checking whether a published value is stale today means two calls: long stamp = topic.latestPublishNanos(); T v = topic.latestValueOr(null); long age = System.nanoTime() - stamp; Both accessors are individually correct, but a publish can land between them, leaving the caller holding a value from one publish and a timestamp from another. Reading the timestamp first makes that safe in one direction -- the age can only be over-reported, never under-reported -- but over-reporting still discards a fresh value as stale, by up to a full publish interval. How often that happens depends on how long the two reads take. Normally they are nanoseconds apart and a publish landing between them is vanishingly rare. It stops being rare when the reader is preempted between the two calls by a GC pause or the scheduler, since the window becomes milliseconds. This library configures a small young gen, so young GCs are part of the deployment rather than a hypothetical. `latest()` returns the value and its timestamp from one snapshot read, so the pair provably comes from a single publish and the window does not exist: Optional<Topic.Latest<T>> snap = topic.latest(); T v = snap.map(Topic.Latest::value).orElse(default); long age = snap.map(Topic.Latest::ageNanos).orElse(Long.MAX_VALUE); Additive only. No existing signature or behaviour changes, and callers that never check an age can keep using `latestValueOr` -- same single volatile read. `Topic.Latest` becomes public with `value()`, `publishNanos()` and `ageNanos()`. `ageNanos()` is the age of *this* value computed from its own stamp, which also removes the pre-publish `0` sentinel that the two-call form has to guard against by hand. `latestPublishNanos()` still returns 0 before the first publish, so the guard stays necessary for anyone composing it by hand. It is documented as such in the javadoc, the CHANGELOG and the topics guide. The concurrency test's reader now uses `latest()`, which is the stronger property: no straddle is representable, so the "timestamp first" ordering rule has nothing left to guard. That test previously carried a cross-check between `latest()` being empty and `latestPublishNanos()` being nonzero; it is gone, because a publish can legitimately land between those two reads and a nonzero stamp there says nothing about consistency. Suite: 61 tests, 0 failures.
1 parent 54d881e commit d6d339a

4 files changed

Lines changed: 157 additions & 44 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4242
**Read the timestamp before the value** when you use the two together as a
4343
staleness check. Each accessor now returns a self-consistent pair, but two separate
4444
calls can still straddle a publish, so the ordering rule remains — it is now
45-
documented on the public accessors and in the topics guide:
45+
documented on the public accessors and in the topics guide. Prefer the new
46+
`Topic.latest()` when you need both: it returns the value and its timestamp from one
47+
snapshot read, so the two provably come from the same publish and the ordering rule
48+
does not apply.
4649

4750
```java
4851
long stamp = topic.latestPublishNanos(); // first

‎src/main/java/com/aaravlabs/synapse/Topic.java‎

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,50 @@ public final class Topic<T> {
4646
this.boxedType = box(type);
4747
}
4848

49-
/** One publish's value and the {@link System#nanoTime()} at which it was recorded. */
50-
private static final class Latest<T> {
51-
final T value;
52-
final long publishNanos;
49+
/**
50+
* One publish's value and the {@link System#nanoTime()} at which it was recorded.
51+
*
52+
* <p>Immutable. Returned by {@link Topic#latest()} so a caller can read the value and
53+
* its timestamp without risking a publish landing between two calls.
54+
*
55+
* @param <T> the message type carried by this topic
56+
*/
57+
public static final class Latest<T> {
58+
59+
private final T value;
60+
private final long publishNanos;
5361

5462
Latest(T value, long publishNanos) {
5563
this.value = value;
5664
this.publishNanos = publishNanos;
5765
}
66+
67+
/** @return the published value */
68+
public T value() {
69+
return value;
70+
}
71+
72+
/**
73+
* @return the {@link System#nanoTime()} at which {@link #value()} was recorded, on
74+
* the monotonic clock (arbitrary origin — only differences are meaningful)
75+
*/
76+
public long publishNanos() {
77+
return publishNanos;
78+
}
79+
80+
/**
81+
* @return nanos elapsed since {@link #publishNanos()}, on the monotonic clock.
82+
* Unlike {@code System.nanoTime() - publishNanos()}, this is always the age
83+
* of <i>this</i> value and needs no {@code 0} sentinel handling.
84+
*/
85+
public long ageNanos() {
86+
return System.nanoTime() - publishNanos;
87+
}
88+
89+
@Override
90+
public String toString() {
91+
return "Latest[" + value + " @" + publishNanos + "]";
92+
}
5893
}
5994

6095
/**
@@ -74,9 +109,9 @@ public Class<T> type() {
74109
/**
75110
* The most recently published value, or empty if nothing has been published yet.
76111
*
77-
* <p><b>Reading a value together with its age:</b> each accessor reads a
78-
* self-consistent pair, but two separate calls can still straddle a publish. Sample
79-
* the timestamp <i>first</i>, which can only over-report the value's age:
112+
* <p><b>Reading a value together with its age:</b> prefer {@link #latest()}, which
113+
* returns both from a single snapshot read. Composing them from two accessors works
114+
* only if you sample the timestamp <i>first</i>, which can only over-report the age:
80115
*
81116
* <pre>{@code
82117
* long stamp = topic.latestPublishNanos(); // first; 0 means nothing published yet
@@ -88,7 +123,7 @@ public Class<T> type() {
88123
* {@link #latestPublishNanos()} is {@code 0}, and subtracting it would yield the raw
89124
* {@link System#nanoTime()} reading — seconds to days — rather than an age.
90125
*
91-
* Reading the value first is the unsafe order: a publish landing between the two
126+
* <p>Reading the value first is the unsafe order: a publish landing between the two
92127
* calls pairs the older value with the newer timestamp, so an age check on that
93128
* pair passes even though the value is stale.
94129
*
@@ -110,7 +145,7 @@ public Optional<T> latestValue() {
110145
*/
111146
public long latestPublishNanos() {
112147
Latest<T> snap = latest;
113-
return snap == null ? 0L : snap.publishNanos;
148+
return snap == null ? 0L : snap.publishNanos();
114149
}
115150

116151
/**
@@ -165,7 +200,39 @@ boolean acceptsType(Class<?> other) {
165200
*/
166201
public T latestValueOr(T defaultValue) {
167202
Latest<T> snap = latest;
168-
return snap == null ? defaultValue : snap.value;
203+
return snap == null ? defaultValue : snap.value();
204+
}
205+
206+
/**
207+
* The most recent value and the {@link System#nanoTime()} at which it was published,
208+
* as one consistent pair.
209+
*
210+
* <p><b>Prefer this over {@link #latestValue()} plus {@link #latestPublishNanos()}
211+
* when you are checking whether a value is stale.</b> The two accessors each read the
212+
* snapshot correctly, but a caller making two calls can straddle a publish between
213+
* them and end up holding a value from one publish and a timestamp from another:
214+
*
215+
* <pre>{@code
216+
* long stamp = topic.latestPublishNanos(); // publish A
217+
* ...publish B lands...
218+
* T v = topic.latestValueOr(null); // publish B's value, with A's stamp
219+
* long age = System.nanoTime() - stamp; // over-reports by a full publish interval
220+
* }</pre>
221+
*
222+
* <p>Reading the timestamp first makes that safe in one direction — the age can only
223+
* be over-reported, never under-reported — but over-reporting still costs you: a
224+
* fresh value gets rejected as stale. How often that happens depends on how long the
225+
* two reads take. Normally they are nanoseconds apart and a publish landing between
226+
* them is vanishingly rare. It stops being rare when the reader is preempted between
227+
* the two calls by a GC pause or the scheduler, since the window becomes
228+
* milliseconds. This accessor removes the window entirely.
229+
*
230+
* <p>Returns empty before the first publish.
231+
*
232+
* @return an {@link Optional} holding the latest value and its publish timestamp
233+
*/
234+
public Optional<Latest<T>> latest() {
235+
return Optional.ofNullable(latest);
169236
}
170237

171238
/** Treat primitive {@code double.class} and wrapper {@code Double.class} as the same type. */

‎src/test/java/com/aaravlabs/synapse/TopicLockFreeTest.java‎

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ void concurrentPublishAndReadNeverLosesOrTearsTheLatestValue() throws Exception
7878
// not evidence that the code is correct. What this test does establish is that no
7979
// reader ever observes a value outside the published range, and that the final
8080
// state is some publisher's last value.
81+
// Timestamp and value from ONE snapshot read, so there is no window for a
82+
// publish to land in between. This is the property the two-call accessors cannot
83+
// offer, and the reason latest() exists.
8184
final int publishers = 4;
8285
final int perPublisher = 25_000;
8386
final int ids = publishers * perPublisher;
@@ -106,32 +109,37 @@ void concurrentPublishAndReadNeverLosesOrTearsTheLatestValue() throws Exception
106109
Thread reader = new Thread(() -> {
107110
try {
108111
while (readersRunning.get()) {
109-
// Timestamp FIRST, then value. On this order a publish landing
110-
// between the two reads can only make the reported age too large,
111-
// never too small, so a staleness check can never pass a stale value.
112-
long ts = topic.latestPublishNanos();
113-
Integer v = topic.latestValueOr(null);
114-
if (v == null) {
115-
if (ts != 0L) {
116-
failure.compareAndSet(null, new AssertionError(
117-
"timestamp recorded with no value: " + ts));
112+
// ONE snapshot read, so the value and the timestamp provably come
113+
// from the same publish. There is no window for a publish to land in,
114+
// so this cannot over-report age the way two calls can.
115+
Topic.Latest<Integer> snap = topic.latest().orElse(null);
116+
if (snap == null) {
117+
// Nothing published yet. There is deliberately no cross-check
118+
// against latestPublishNanos() here: a publish can land between
119+
// the two calls, so a nonzero stamp here would be that publish,
120+
// not an inconsistency.
121+
} else {
122+
Integer v = snap.value();
123+
if (v < 0 || v >= ids) {
124+
failure.compareAndSet(null,
125+
new AssertionError("torn/garbage latest value: " + v));
118126
return;
119127
}
120-
} else if (v < 0 || v >= ids) {
121-
failure.compareAndSet(null,
122-
new AssertionError("torn/garbage latest value: " + v));
123-
return;
124-
} else {
125-
// Only a timestamp AFTER the window ends is a tear: the reader
126-
// would be holding an older value beside a newer timestamp. A
127-
// timestamp before the window is the safe, expected straddle --
128-
// the reader sampled the timestamp, a publish landed, then it read
129-
// the newer value.
128+
// The pair is self-consistent by construction, so the only way
129+
// to fail is a stamp that predates the value it came with --
130+
// which the publish-window check bounds.
130131
long hi = windowHi.get(v);
131-
if (hi != Long.MIN_VALUE && ts > hi) {
132+
if (hi != Long.MIN_VALUE && snap.publishNanos() > hi) {
132133
failure.compareAndSet(null, new AssertionError(
133-
"value " + v + " is older than the timestamp " + ts
134-
+ " the reader sampled; its publish ended at " + hi));
134+
"value " + v + " paired with stamp " + snap.publishNanos()
135+
+ " later than its publish end " + hi));
136+
return;
137+
}
138+
// ageNanos must agree with the stamp it was taken from.
139+
long age = snap.ageNanos();
140+
if (age <= 0 || age > System.nanoTime() - snap.publishNanos() + 1) {
141+
failure.compareAndSet(null, new AssertionError(
142+
"implausible age " + age + " for stamp " + snap.publishNanos()));
135143
return;
136144
}
137145
}
@@ -240,6 +248,44 @@ void acceptsValueClassTreatsPrimitivesAndWrappersAsEquivalent() {
240248
assertFalse(i.acceptsValueClass(double.class), "Double must not match Integer topic");
241249
}
242250

251+
@Test
252+
void latestReturnsValueAndTimestampFromOneConsistentPublish() throws Exception {
253+
// latest() exists so a staleness check reads one snapshot instead of two. It must
254+
// be empty before the first publish, agree with both single-field accessors, and
255+
// never need a 0 sentinel.
256+
Topic<String> t = orchestrator.getOrCreateTopic("snap", String.class);
257+
assertFalse(t.latest().isPresent(), "latest() must be empty before the first publish");
258+
259+
orchestrator.publish("snap", "a");
260+
Topic.Latest<String> first = t.latest().orElseThrow();
261+
assertEquals("a", first.value());
262+
assertEquals(first.publishNanos(), t.latestPublishNanos(),
263+
"latest() and latestPublishNanos() must describe the same publish");
264+
assertEquals("a", t.latestValueOr(null));
265+
assertTrue(first.publishNanos() != 0L,
266+
"a published stamp must not be the pre-publish 0 sentinel");
267+
268+
// ageNanos is the age of THIS value, computed from its own stamp. Taking a fresh
269+
// clock reading afterwards must give an equal or LARGER delta, never a smaller
270+
// one -- a smaller delta would mean the age came from somewhere other than this
271+
// stamp, which is exactly the straddle latest() exists to prevent.
272+
long age = first.ageNanos();
273+
assertTrue(age >= 0 && age < 1_000_000_000L, "ageNanos out of range: " + age);
274+
assertTrue(age <= System.nanoTime() - first.publishNanos(),
275+
"ageNanos " + age + " exceeds a delta measured later from the same stamp");
276+
277+
// A later publish replaces both halves together; there is no observable state in
278+
// which one half is from this publish and the other from the previous one.
279+
Thread.sleep(2);
280+
orchestrator.publish("snap", "b");
281+
Topic.Latest<String> second = t.latest().orElseThrow();
282+
assertEquals("b", second.value());
283+
assertTrue(second.publishNanos() > first.publishNanos(),
284+
"a later publish must carry a later stamp");
285+
assertTrue(second.ageNanos() < first.ageNanos(),
286+
"the newer value must report a younger age");
287+
}
288+
243289
@Test
244290
void acceptsTypeIsEquivalentToTheOldBoxedAssignableFromCheck() {
245291
// acceptsType() replaced `boxed(t.type()).isAssignableFrom(boxed(type))` at

‎website/src/content/docs/concepts/topics.mdx‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,16 @@ double fl = latest.orElse(0.0);
5252

5353
Topic<Double> flTopic = orchestrator.findTopic("drive/power/fl", Double.class).orElseThrow();
5454

55-
// Read the timestamp first, then the value. Each accessor is internally consistent,
56-
// but two separate calls can still straddle a publish, so this order is the only one
57-
// that cannot pair an older value with a newer timestamp and report a fresh age for a
58-
// value that is already stale.
59-
//
60-
// latestPublishNanos() is 0 until the topic's first publish, so treat 0 as "never
61-
// published" rather than subtracting it -- that would hand you the raw nanoTime
62-
// reading, which is seconds to days rather than an age.
63-
long stamp = flTopic.latestPublishNanos();
64-
long nanosSinceUpdate = stamp == 0L ? Long.MAX_VALUE : System.nanoTime() - stamp;
65-
double fl2 = flTopic.latestValueOr(0.0);
55+
// One snapshot read gives the value and its timestamp together, so the two can never
56+
// come from different publishes. ageNanos() is the age of this exact value, and needs no
57+
// special handling before the first publish -- latest() is simply empty then.
58+
Optional<Topic.Latest<Double>> flSnapshot = flTopic.latest();
59+
double fl2 = flSnapshot.map(Topic.Latest::value).orElse(0.0);
60+
long nanosSinceUpdate = flSnapshot.map(Topic.Latest::ageNanos).orElse(Long.MAX_VALUE);
6661
```
6762

63+
If you only want the value and never check its age, keep using `latestValueOr(0.0)` — it is the same single volatile read and reads better at a glance.
64+
6865
Fetching the latest value is the right tool inside periodic loops; subscribing is the right tool when the *event* matters, not the current state. See [Subscriptions](/docs/concepts/subscriptions) for the tradeoff.
6966

7067
## Next steps

0 commit comments

Comments
 (0)