Skip to content

Commit b6d08fe

Browse files
committed
fix: [gav] race in GraphAnalyticalView.awaitReady() exposed under load
1 parent 70465d5 commit b6d08fe

2 files changed

Lines changed: 62 additions & 7 deletions

File tree

‎engine/src/main/java/com/arcadedb/graph/olap/GraphAnalyticalView.java‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,25 +373,35 @@ public synchronized void buildAsync() {
373373
public boolean awaitReady(final long timeout, final TimeUnit unit) {
374374
final long deadlineNanos = System.nanoTime() + unit.toNanos(timeout);
375375
try {
376-
// Loop to handle follow-up rebuilds triggered by asyncRebuildNeeded.
377-
// Each rebuild creates a new latch, so we re-read the field after each wait.
378-
while (status != Status.READY || asyncRebuildNeeded) {
379-
if (status == Status.STALE)
376+
while (true) {
377+
// Snapshot the three fields under the monitor: onRelevantCommit() resets
378+
// asyncRebuildNeeded=false and flips status=BUILDING in separate volatile
379+
// writes, so an unsynchronized reader can observe status=READY together
380+
// with asyncRebuildNeeded=false and return early on a stale snapshot.
381+
final Status currentStatus;
382+
final boolean rebuildPending;
383+
final CountDownLatch latch;
384+
synchronized (this) {
385+
currentStatus = status;
386+
rebuildPending = asyncRebuildNeeded;
387+
latch = readyLatch;
388+
}
389+
if (currentStatus == Status.READY && !rebuildPending)
390+
return true;
391+
if (currentStatus == Status.STALE)
380392
return false;
381393
final long remainingNanos = deadlineNanos - System.nanoTime();
382394
if (remainingNanos <= 0)
383395
return false;
384-
final CountDownLatch latch = readyLatch;
385396
if (latch == null)
386-
return status == Status.READY && !asyncRebuildNeeded;
397+
return false;
387398
if (!latch.await(remainingNanos, TimeUnit.NANOSECONDS))
388399
return false;
389400
}
390401
} catch (final InterruptedException e) {
391402
Thread.currentThread().interrupt();
392403
return false;
393404
}
394-
return true;
395405
}
396406

397407
/**

‎engine/src/test/java/com/arcadedb/graph/olap/GraphAnalyticalViewTest.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4085,6 +4085,51 @@ void asynchronousUpdateModeStatusTransitions() throws Exception {
40854085
gav.drop();
40864086
}
40874087

4088+
/**
4089+
* Regression stress test for the awaitReady race: between rebuilds, the worker resets
4090+
* asyncRebuildNeeded=false a moment before flipping status to BUILDING. A reader doing
4091+
* the two volatile reads non-atomically can see status=READY together with
4092+
* asyncRebuildNeeded=false and return early on a stale snapshot. Many iterations of
4093+
* rapid-fire commits widen the window enough to fail consistently.
4094+
*/
4095+
@Test
4096+
void asynchronousUpdateModeAwaitReadyConvergesAfterRapidCommits() throws Exception {
4097+
database.getSchema().createVertexType("Person");
4098+
database.getSchema().createEdgeType("FOLLOWS");
4099+
4100+
database.begin();
4101+
final MutableVertex alice = database.newVertex("Person").set("name", "Alice").save();
4102+
database.commit();
4103+
4104+
final GraphAnalyticalView gav = GraphAnalyticalView.builder(database)
4105+
.withName("async-stress")
4106+
.withVertexTypes("Person")
4107+
.withEdgeTypes("FOLLOWS")
4108+
.withUpdateMode(GraphAnalyticalView.UpdateMode.ASYNCHRONOUS)
4109+
.build();
4110+
4111+
final int iterations = 50;
4112+
final int commitsPerIteration = 5;
4113+
int expectedNodeCount = 1; // Alice
4114+
int expectedEdgeCount = 0;
4115+
for (int it = 0; it < iterations; it++) {
4116+
for (int i = 0; i < commitsPerIteration; i++) {
4117+
database.begin();
4118+
final MutableVertex v = database.newVertex("Person").set("name", "P" + it + "_" + i).save();
4119+
alice.newEdge("FOLLOWS", v);
4120+
database.commit();
4121+
expectedNodeCount++;
4122+
expectedEdgeCount++;
4123+
}
4124+
4125+
assertThat(gav.awaitReady(10, TimeUnit.SECONDS)).as("iteration %d", it).isTrue();
4126+
assertThat(gav.getNodeCount()).as("nodeCount after iteration %d", it).isEqualTo(expectedNodeCount);
4127+
assertThat(gav.getEdgeCount()).as("edgeCount after iteration %d", it).isEqualTo(expectedEdgeCount);
4128+
}
4129+
4130+
gav.drop();
4131+
}
4132+
40884133
// ---- GAVExpandInto tests ----
40894134

40904135
@Test

0 commit comments

Comments
 (0)