GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(async): notify unreplayable tasks too when an unreplayable batch … · ArcadeData/arcadedb@19c0371

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • engine/src
    • main/java/com/arcadedb/database/async
      • AsyncResultsetCallback.java
      • DatabaseAsyncCreateRecord.java
      • DatabaseAsyncDeleteRecord.java
      • DatabaseAsyncExecutor.java
      • DatabaseAsyncExecutorImpl.java
      • DatabaseAsyncTask.java
      • DatabaseAsyncUpdateRecord.java
    • test/java/com/arcadedb/database/async
      • Issue7615AsyncCommandBatchCommitConflictTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎engine/src/main/java/com/arcadedb/database/async/AsyncResultsetCallback.java‎

Copy file name to clipboardExpand all lines: engine/src/main/java/com/arcadedb/database/async/AsyncResultsetCallback.java
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
/**
2525
* Callback interface for asynchronous commands. If the command returns an exception, the {@link #onError(Exception)} method is invoked. Otherwise
2626
* {@link #onComplete(ResultSet)} is called.
27+
* <p>
28+
* <b>{@code onComplete} is not a durability signal.</b> It fires as soon as the command applies to the worker's
29+
* still-open shared batch transaction - not once that batch is actually committed (see
30+
* {@link DatabaseAsyncExecutor#setCommitEvery(int)}). If the batch's periodic commit later hits a transient
31+
* conflict, the command is retried by re-executing it (issue #7615) - {@code onComplete} does not fire again for a
32+
* successful retry, but {@code onError} DOES fire, in addition to the {@code onComplete} already received, if every
33+
* retry is exhausted. A command whose text is not safe to execute more than once (e.g. one that calls
34+
* {@code SEQUENCE.next()} or an SQL function with an external side effect) should account for this - the same
35+
* caveat already applies to a transaction body passed to {@link DatabaseAsyncExecutor#transaction(com.arcadedb.database.Database.TransactionScope)}.
2736
*
2837
* @author Luca Garulli ([email protected])
2938
*/
Collapse file

‎engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncCreateRecord.java‎

Copy file name to clipboardExpand all lines: engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncCreateRecord.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public void execute(final DatabaseAsyncExecutorImpl.AsyncThread async, final Dat
6666
}
6767
}
6868

69+
@Override
70+
public void notifyBatchAbandoned(final Throwable cause) {
71+
if (onErrorCallback != null)
72+
onErrorCallback.call(cause);
73+
}
74+
6975
@Override
7076
public String toString() {
7177
return "CreateRecord(" + record + ")";
Collapse file

‎engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncDeleteRecord.java‎

Copy file name to clipboardExpand all lines: engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncDeleteRecord.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public void execute(final DatabaseAsyncExecutorImpl.AsyncThread async, final Dat
7272
}
7373
}
7474

75+
@Override
76+
public void notifyBatchAbandoned(final Throwable cause) {
77+
if (onErrorCallback != null)
78+
onErrorCallback.call(cause);
79+
}
80+
7581
@Override
7682
public String toString() {
7783
return "DeleteRecord(" + record + ")";
Collapse file

‎engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncExecutor.java‎

Copy file name to clipboardExpand all lines: engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncExecutor.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public interface DatabaseAsyncExecutor {
7575

7676
/**
7777
* Schedules the execution of a command by specifying the language and an optional variable array of arguments.
78+
* See {@link AsyncResultsetCallback} for what its {@code onComplete}/{@code onError} actually guarantee - in
79+
* particular, a command may be silently re-executed and reported through both callbacks if a periodic batch
80+
* commit needs to retry it.
7881
*
7982
* @param language The language to use between the supported ones ("sql", "gremlin", "cypher", "graphql", "mongo", etc.)
8083
* @param query The command to be interpreted in the specified language as a string
@@ -85,6 +88,9 @@ public interface DatabaseAsyncExecutor {
8588

8689
/**
8790
* Schedules the execution of a command by specifying the language and arguments in a map.
91+
* See {@link AsyncResultsetCallback} for what its {@code onComplete}/{@code onError} actually guarantee - in
92+
* particular, a command may be silently re-executed and reported through both callbacks if a periodic batch
93+
* commit needs to retry it.
8894
*
8995
* @param language The language to use between the supported ones ("sql", "gremlin", "cypher", "graphql", "mongo", etc.)
9096
* @param query The command to be interpreted in the specified language as a string
Collapse file

‎engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncExecutorImpl.java‎

Copy file name to clipboardExpand all lines: engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncExecutorImpl.java
+29-27Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -270,30 +270,33 @@ public class AsyncThread extends Thread {
270270
// that closes the batch) can still tell every one of them onError instead of only the executor-wide
271271
// callback. Thread-confined: only this worker touches it. Idempotent queries are never buffered -
272272
// they wrote nothing, so there is nothing for a rollback to lose.
273-
private final List<DatabaseAsyncCommand> pendingBatchCommands = new ArrayList<>();
274-
// #7615: true once some OTHER mutating task type (DatabaseAsyncCreateRecord/UpdateRecord/DeleteRecord,
275-
// the graph edge-creation tasks, ...) has run against the shared batch since it was last committed.
276-
// commitBatch() cannot replay those - only pendingBatchCommands is buffered - so replaying the batch
277-
// after a rolled-back commit would silently omit their writes while still reporting success. Gates
278-
// commitBatch()'s retry: set, a CME abandons immediately instead of attempting a replay that would
279-
// fabricate a clean commit over data it actually dropped.
280-
private boolean batchHasUnreplayableWrites = false;
273+
private final List<DatabaseAsyncCommand> pendingBatchCommands = new ArrayList<>();
274+
// #7615: tasks of some OTHER mutating type (DatabaseAsyncCreateRecord/UpdateRecord/DeleteRecord, the
275+
// graph edge-creation tasks, ...) that ran against the shared batch and left it active since it was
276+
// last committed. commitBatch() cannot replay those - only pendingBatchCommands is buffered - so
277+
// replaying the batch after a rolled-back commit would silently omit their writes while still
278+
// reporting success. Non-empty gates commitBatch()'s retry off (a CME abandons immediately instead of
279+
// attempting a replay that would fabricate a clean commit over data it actually dropped), and on
280+
// abandonment each one's own notifyBatchAbandoned(Throwable) is called - a no-op for a task with no
281+
// per-task error callback (the edge tasks), otherwise the same "you were told success, this corrects
282+
// it" notification pendingBatchCommands' own commands get.
283+
private final List<DatabaseAsyncTask> pendingUnreplayableTasks = new ArrayList<>();
281284
// #7615: true only while commitBatch() is replaying pendingBatchCommands after a rolled-back
282285
// commit. DatabaseAsyncCommand checks this to avoid re-invoking its userCallback.onComplete() (already
283286
// fired on the first, now-discarded attempt) and to let a failure during replay propagate to
284287
// commitBatch() instead of being handled - and the whole batch silently rolled back - locally.
285-
private volatile boolean replayingBatch = false;
288+
private volatile boolean replayingBatch = false;
286289
// Monotonic call counter feeding TEST_BEFORE_BATCH_COMMIT_HOOK's 1-based argument. Thread-confined,
287290
// same as pendingBatchCommands - only this worker ever calls commitBatch() on itself.
288-
private int batchCommitAttempt = 0;
291+
private int batchCommitAttempt = 0;
289292

290293
// #7615: single choke point for "the shared batch's non-durable bookkeeping is now moot" - every site
291294
// that commits, rolls back, or otherwise closes the shared batch transaction goes through here instead
292-
// of clearing pendingBatchCommands directly, so batchHasUnreplayableWrites can never drift out of sync
293-
// with it.
295+
// of clearing pendingBatchCommands/pendingUnreplayableTasks directly, so the two can never drift out
296+
// of sync with each other.
294297
private void clearBatchState() {
295298
pendingBatchCommands.clear();
296-
batchHasUnreplayableWrites = false;
299+
pendingUnreplayableTasks.clear();
297300
}
298301

299302
boolean isReplayingBatch() {
@@ -507,8 +510,8 @@ else if (message instanceof final DatabaseAsyncCommand command) {
507510
// Some other mutating task type ran against the shared batch and left it active, i.e. it
508511
// succeeded - DatabaseAsyncCreateRecord/UpdateRecord/DeleteRecord, the graph edge-creation
509512
// tasks, ... commitBatch() only knows how to replay pendingBatchCommands, so it must not
510-
// attempt to on a batch holding a write it cannot reconstruct (see batchHasUnreplayableWrites).
511-
batchHasUnreplayableWrites = true;
513+
// attempt to on a batch holding a write it cannot reconstruct (see pendingUnreplayableTasks).
514+
pendingUnreplayableTasks.add(message);
512515

513516
count++;
514517

@@ -608,7 +611,7 @@ void commitBatch(final boolean currentUseWAL, final WALFile.FlushType currentSyn
608611
lastFailure = e;
609612
if (database.isTransactionActive())
610613
database.rollback();
611-
if (batchHasUnreplayableWrites)
614+
if (!pendingUnreplayableTasks.isEmpty())
612615
// A replay can only reconstruct pendingBatchCommands - committing one over a batch that also
613616
// held a write it cannot reconstruct would silently omit that write while still reporting
614617
// success. No safe retry available: stop here and fall through to the abandon path below,
@@ -634,25 +637,24 @@ void commitBatch(final boolean currentUseWAL, final WALFile.FlushType currentSyn
634637
}
635638

636639
/**
637-
* Calls {@code onError} on every command still buffered in {@link #pendingBatchCommands} - the batch
638-
* is being abandoned, so none of them will be replayed - and clears the buffer. Called wherever the
639-
* worker's shared batch transaction is given up on instead of committed (issue #7615): a
640+
* Calls {@code onError} on every command still buffered in {@link #pendingBatchCommands}, and
641+
* {@link DatabaseAsyncTask#notifyBatchAbandoned} on every task in {@link #pendingUnreplayableTasks} -
642+
* the batch is being abandoned, so none of them will be replayed - then clears both. Called wherever
643+
* the worker's shared batch transaction is given up on instead of committed (issue #7615): a
640644
* {@link #commitBatch} whose retries are exhausted, and the other commit sites that close this same
641645
* shared transaction ({@link #closeTransactionBoundaryIfDurabilityPolicyChanged}, the shutdown commit
642646
* in {@link #runLoop}, and the dangling-batch commit in {@link DatabaseAsyncTransaction#executeTransaction}).
643647
*/
644648
void notifyPendingBatchCommandsAndAbandon(final Throwable cause) {
645-
if (pendingBatchCommands.isEmpty()) {
646-
// #7615: still reset - a batch can be unreplayable (batchHasUnreplayableWrites) with nothing
647-
// buffered at all (e.g. a lone DatabaseAsyncCreateRecord), and that flag must not outlive the
648-
// batch it described or every later batch on this worker would be wrongly denied a replay too.
649-
batchHasUnreplayableWrites = false;
649+
if (pendingBatchCommands.isEmpty() && pendingUnreplayableTasks.isEmpty())
650650
return;
651-
}
652-
final List<DatabaseAsyncCommand> abandoned = new ArrayList<>(pendingBatchCommands);
651+
final List<DatabaseAsyncCommand> abandonedCommands = new ArrayList<>(pendingBatchCommands);
652+
final List<DatabaseAsyncTask> abandonedTasks = new ArrayList<>(pendingUnreplayableTasks);
653653
clearBatchState();
654-
for (final DatabaseAsyncCommand command : abandoned)
654+
for (final DatabaseAsyncCommand command : abandonedCommands)
655655
command.notifyError(cause);
656+
for (final DatabaseAsyncTask task : abandonedTasks)
657+
task.notifyBatchAbandoned(cause);
656658
}
657659

658660
/**
Collapse file

‎engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncTask.java‎

Copy file name to clipboardExpand all lines: engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncTask.java
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,17 @@ default void completed() {
3737
default boolean requiresActiveTx() {
3838
return true;
3939
}
40+
41+
/**
42+
* Called when this task's write was applied to a worker's shared batch transaction that was then
43+
* abandoned instead of committed - a boundary commit conflict {@code AsyncThread#commitBatch} could not
44+
* safely retry because this task's write cannot be replayed (issue #7615). {@code cause} is the failure
45+
* that closed the batch. Default is a no-op, for tasks with no per-task error callback to invoke (e.g.
46+
* the graph edge-creation tasks, whose only callback reports a new edge, not a failure); a task that has
47+
* one (e.g. {@link DatabaseAsyncCreateRecord}) overrides this to call it, so its submitter is not left
48+
* having seen only a success callback for a write that never actually landed.
49+
*/
50+
default void notifyBatchAbandoned(final Throwable cause) {
51+
// DEFAULT IMPLEMENTATION: nothing to notify
52+
}
4053
}
Collapse file

‎engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncUpdateRecord.java‎

Copy file name to clipboardExpand all lines: engine/src/main/java/com/arcadedb/database/async/DatabaseAsyncUpdateRecord.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ public void execute(final DatabaseAsyncExecutorImpl.AsyncThread async, final Dat
9494
}
9595
}
9696

97+
@Override
98+
public void notifyBatchAbandoned(final Throwable cause) {
99+
if (onErrorCallback != null)
100+
onErrorCallback.call(cause);
101+
}
102+
97103
@Override
98104
public String toString() {
99105
return "UpdateRecord(" + record + ")";
Collapse file

‎engine/src/test/java/com/arcadedb/database/async/Issue7615AsyncCommandBatchCommitConflictTest.java‎

Copy file name to clipboardExpand all lines: engine/src/test/java/com/arcadedb/database/async/Issue7615AsyncCommandBatchCommitConflictTest.java
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,13 @@ public void onError(final Exception e) {
153153

154154
assertThat(executorWideErrors.get()).as("the executor-wide callback still fires, unchanged").isGreaterThanOrEqualTo(1);
155155

156-
// The actual fix: every one of the 5 buffered commands is told its own onError once the retries are exhausted,
157-
// instead of the failure being visible only through the executor-wide callback above.
156+
// Every buffered command already got onComplete on its first, now-discarded attempt (the non-durable
157+
// signal from #6470) - that is unchanged by this fix and still fires exactly once per command.
158+
assertThat(perCommandOks.get()).as("onComplete still fires once per command on its first attempt, unchanged").isEqualTo(5);
159+
160+
// The actual fix: every one of the 5 buffered commands is ALSO told its own onError once the retries
161+
// are exhausted, instead of the failure being visible only through the executor-wide callback above -
162+
// in addition to the onComplete above, which is the point (see commitBatch()'s own javadoc).
158163
assertThat(perCommandErrors).hasSize(5);
159164
for (final Exception e : perCommandErrors)
160165
assertThat(e).isInstanceOf(ConcurrentModificationException.class);
@@ -213,8 +218,10 @@ public void onError(final Exception e) {
213218
* commitBatch() can only replay what it buffers in {@code pendingBatchCommands} - plain {@code command()} calls.
214219
* A batch that ALSO ran a {@link DatabaseAsyncExecutor#createRecord} (or updateRecord/deleteRecord) cannot be
215220
* safely replayed: doing so anyway would silently omit that write while still reporting the retry as a clean
216-
* success. This must fall back to the pre-fix behaviour instead - one attempt, no retry, batch abandoned - rather
217-
* than fabricate a commit that never actually included the unreplayable write.
221+
* success. This must fall back to a single attempt instead - no retry, batch abandoned - with both the buffered
222+
* command AND the unreplayable task told via their own error callback ({@code DatabaseAsyncCommand#notifyError}
223+
* and {@code DatabaseAsyncTask#notifyBatchAbandoned} respectively), rather than fabricating a commit that never
224+
* actually included the unreplayable write.
218225
*/
219226
@Test
220227
void mixedBatchWithAnUnreplayableWriteIsNeverSilentlyFabricatedAsASuccess() throws Exception {
@@ -254,6 +261,9 @@ public void onError(final Exception e) {
254261
// No retry attempted for an unreplayable batch: exactly one commit attempt, not up to TX_RETRIES+1.
255262
assertThat(hookCalls.get()).as("a mixed batch must not be retried - there is nothing safe to replay it with").isEqualTo(1);
256263
assertThat(commandErrors.get()).as("the buffered command is still told, unlike before this fix").isEqualTo(1);
264+
// The unreplayable createRecord task is told too, via DatabaseAsyncTask#notifyBatchAbandoned - not just
265+
// the executor-wide onError a caller might not have wired up.
266+
assertThat(createErrors.get()).as("the unreplayable task is told its write never landed too").isEqualTo(1);
257267

258268
// Nothing from this abandoned batch may be durably stored - the guard exists precisely so a partial replay can
259269
// never masquerade as a complete one.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.