Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions engine/src/main/java/com/arcadedb/GlobalConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -1328,12 +1328,14 @@ proved who it is, and the handshake timeout (arcadedb.network.socketTimeout) onl
"""
Milliseconds the JVM shutdown hook waits for the server lifecycle lock before giving up and letting \
the JVM exit WITHOUT a graceful stop. It only matters when another thread holds the lifecycle lock \
when the shutdown signal arrives - a stop() in progress, or the tail of start() after the status has \
already turned ONLINE: normally the hook takes the lock immediately and this value is never \
reached, and while the server is still STARTING the hook uses a fixed 2000ms bound instead, which \
this setting does not govern. Giving up leaves databases as a kill would - the next open replays the \
WAL - which is the lesser evil, because a hook that waits forever can make the process unkillable \
(issue #5418). Raise it if a legitimate shutdown of very large databases needs longer than the default.""",
when the shutdown signal arrives - a stop() in progress, a start() that has already opened its \
databases (they open long before the HTTP service, the plugins and HA come up), or the tail of \
start() after the status has already turned ONLINE: normally the hook takes the lock immediately \
and this value is never reached, and while the server is still STARTING with no database open yet \
the hook uses a fixed 2000ms bound instead, which this setting does not govern. Giving up leaves \
databases as a kill would - the next open replays the WAL - which is the lesser evil, because a hook \
that waits forever can make the process unkillable (issue #5418). Raise it if a legitimate shutdown \
of very large databases needs longer than the default.""",
Long.class, 60_000L),

// Metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,14 @@ static Database unwrap(final Database database) {

void updateRecordNoLock(Record record, boolean discardRecordAfter);

void deleteRecordNoLock(Record record);
/**
* Deletes the record without taking the database read lock, dispatching the before/after-delete listeners itself.
*
* @return {@code true} when the record was deleted, {@code false} when a before-delete listener vetoed it. The
* asynchronous delete task relies on this to skip its success callback on a veto without dispatching the listeners
* a second time (issue #7003).
*/
boolean deleteRecordNoLock(Record record);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/**
* Deletes an edge record the way {@code edge.delete()} does - index cleanup, external values, delete events and
Expand Down
14 changes: 8 additions & 6 deletions engine/src/main/java/com/arcadedb/database/LocalDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1500,8 +1500,8 @@ public void deleteEdgeSkippingEndpoint(final Edge edge, final RID skipEndpoint)
}

@Override
public void deleteRecordNoLock(final Record record) {
deleteRecordNoLock(record, null);
public boolean deleteRecordNoLock(final Record record) {
return deleteRecordNoLock(record, null);
}

/**
Expand All @@ -1510,19 +1510,20 @@ public void deleteRecordNoLock(final Record record) {
* {@link #deleteEdgeSkippingEndpoint(Edge, RID)}). Ignored for any other record type,
* and always {@code null} on the ordinary delete path.
*/
private void deleteRecordNoLock(final Record record, final RID skipEdgeEndpoint) {
private boolean deleteRecordNoLock(final Record record, final RID skipEdgeEndpoint) {
if (record.getIdentity() == null)
throw new IllegalArgumentException("Cannot delete a non persistent record");

if (mode == ComponentFile.MODE.READ_ONLY)
throw new DatabaseIsReadOnlyException("Cannot delete record " + record.getIdentity());

// INVOKE EVENT CALLBACKS
// INVOKE EVENT CALLBACKS. This is the ONE place the delete listeners are dispatched from: every caller, the
// asynchronous delete task included, reaches the listeners through here (issue #7003).
if (!events.onBeforeDelete(record))
return;
return false;
if (record instanceof Document document)
if (!((RecordEventsRegistry) document.getType().getEvents()).onBeforeDelete(record))
return;
return false;

boolean success = false;
final boolean implicitTransaction = checkTransactionIsActive(autoTransaction);
Expand Down Expand Up @@ -1634,6 +1635,7 @@ private void deleteRecordNoLock(final Record record, final RID skipEdgeEndpoint)
wrappedDatabaseInstance.rollback();
}
}
return true;
}

/** The INDEX/EXTERNAL cleanup could not read the record, so the delete proceeds without it. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void execute(final DatabaseAsyncExecutorImpl.AsyncThread async, final Dat
if (!callback.onRecord(iterator.next().asDocument()))
break;

if (async.isShutdown())
// isAborting(), not isShutdown(): same silent truncation on a retired worker as DatabaseAsyncScanBucket (#7004)
if (async.isAborting())
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@
package com.arcadedb.database.async;

import com.arcadedb.database.DatabaseInternal;
import com.arcadedb.database.Document;
import com.arcadedb.database.Record;
import com.arcadedb.database.RecordEventsRegistry;
import com.arcadedb.log.LogManager;

import java.util.logging.Level;

/**
* Asynchronous delete of one record.
* <p>
* The before/after-delete listeners are NOT dispatched here: {@code deleteRecordNoLock} owns that dispatch, exactly
* as it does for the synchronous {@code deleteRecord()}. This task used to dispatch them as well, so every listener
* fired twice per asynchronous delete - a counter double-counted, an audit log got two entries, a cascade ran twice
* (issue #7003). A veto is learnt from the delegate's return value instead, so a vetoed delete still skips the
* success callback as it always did.
*/
public class DatabaseAsyncDeleteRecord implements DatabaseAsyncTask {
public final Record record;
public final DeletedRecordCallback onOkCallback;
Expand All @@ -40,21 +47,9 @@ public DatabaseAsyncDeleteRecord(final Record record, final DeletedRecordCallbac
@Override
public void execute(final DatabaseAsyncExecutorImpl.AsyncThread async, final DatabaseInternal database) {
try {
if (record instanceof Document document) {
// INVOKE EVENT CALLBACKS
if (!((RecordEventsRegistry) database.getEvents()).onBeforeDelete(document))
return;
if (!((RecordEventsRegistry) document.getType().getEvents()).onBeforeDelete(document))
return;
}

database.deleteRecordNoLock(record);

if (record instanceof Document document) {
// INVOKE EVENT CALLBACKS
((RecordEventsRegistry) database.getEvents()).onAfterDelete(document);
((RecordEventsRegistry) document.getType().getEvents()).onAfterDelete(document);
}
if (!database.deleteRecordNoLock(record))
// VETOED BY A BEFORE-DELETE LISTENER
return;

if (onOkCallback != null)
onOkCallback.call(record);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,27 @@ else if ("standard".equalsIgnoreCase(cfgQueueImpl))
cfgQueueImpl);
this.queue = new ArrayBlockingQueue<>(queueSize);
}

backPressurePercentage = database.getConfiguration().getValueAsInteger(GlobalConfiguration.ASYNC_BACK_PRESSURE);
}

public boolean isShutdown() {
return shutdown;
}

/**
* Whether a task in flight on this worker must abandon what it is doing: {@link #forceShutdown} is set by
* {@code kill()} and by the close path when a worker is asked to exit at once, and the interrupt is the close
* path's escalation on a worker that outlived its drain budget.
* <p>
* Distinct from {@link #isShutdown()} on purpose (issue #7004). {@link #shutdown} also means "this worker is
* being replaced": a shrinking {@code setParallelLevel()} retires a worker by setting it and lets the worker
* drain its queue, so a scan task already queued on it that bailed on that flag stopped after its first record
* while {@code scanType()} still reported success. A drain is exactly the moment such a task must run to
* completion.
*/
public boolean isAborting() {
return forceShutdown || isInterrupted();
}

@Override
public void run() {
try {
Expand Down Expand Up @@ -575,6 +588,9 @@ public DatabaseAsyncExecutorImpl(final DatabaseInternal database, final ContextC
this.configuration = configuration;
// #4961: a non-positive batch size would make every task fail on count % commitEvery.
this.commitEvery = Math.max(1, database.getConfiguration().getValueAsInteger(GlobalConfiguration.ASYNC_TX_BATCH_SIZE));
// Read once here, NOT in AsyncThread's constructor (issue #7004): the field is executor-wide, and reading it
// per worker meant every setParallelLevel() silently reverted whatever setBackPressure() had been asked for.
this.backPressurePercentage = database.getConfiguration().getValueAsInteger(GlobalConfiguration.ASYNC_BACK_PRESSURE);
createThreads(database.getConfiguration().getValueAsInteger(GlobalConfiguration.ASYNC_WORKER_THREADS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ public DatabaseAsyncScanBucket(final CountDownLatch semaphore, final DocumentCal
public void execute(final DatabaseAsyncExecutorImpl.AsyncThread async, final DatabaseInternal database) {
try {
bucket.scan((rid, view) -> {
if (async.isShutdown())
// isAborting(), not isShutdown() (issue #7004): a worker retired by a shrinking setParallelLevel() carries the
// shutdown flag while it drains its queue, and this scan is part of that drain. Bailing on it truncated the
// scan after its first record while scanType() still reported success to the caller.
if (async.isAborting())
return false;

final Record record = database.getRecordFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@

import com.arcadedb.database.DatabaseInternal;
import com.arcadedb.database.Document;
import com.arcadedb.database.MutableDocument;
import com.arcadedb.database.Record;
import com.arcadedb.database.RecordEventsRegistry;
import com.arcadedb.engine.ComponentFile;
import com.arcadedb.exception.DatabaseIsReadOnlyException;
import com.arcadedb.log.LogManager;

import java.util.logging.Level;

/**
* Asynchronous update of one record. Mirrors the preconditions of the synchronous {@code updateRecord()}: the record
* must be persistent and, when it is a document, it must satisfy its schema. The validation used to be missing here,
* so an asynchronous update persisted a record violating a MANDATORY/NOTNULL constraint with no error anywhere - not
* to the caller, not to the error callback (issue #7002). A violation now takes the same route as any other failure of
* this task: the worker's transaction is rolled back and the error callback is invoked.
*/
public class DatabaseAsyncUpdateRecord implements DatabaseAsyncTask {
public final Record record;
public final UpdatedRecordCallback onOkCallback;
Expand All @@ -40,6 +50,15 @@ public DatabaseAsyncUpdateRecord(final Record record, final UpdatedRecordCallbac
@Override
public void execute(final DatabaseAsyncExecutorImpl.AsyncThread async, final DatabaseInternal database) {
try {
if (record.getIdentity() == null)
throw new IllegalArgumentException("Cannot update the record because it is not persistent");

if (database.getMode() == ComponentFile.MODE.READ_ONLY)
throw new DatabaseIsReadOnlyException("Cannot update a record");

if (record instanceof MutableDocument document)
document.validate();

// INVOKE EVENT CALLBACKS
if (!((RecordEventsRegistry) database.getEvents()).onBeforeUpdate(record))
return;
Expand Down
Loading
Loading