Skip to content

Commit

Permalink
Catch an exception from a callback safely
Browse files Browse the repository at this point in the history
  • Loading branch information
komamitsu committed Dec 10, 2024
1 parent 5db61df commit 782cb03
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ public CommitHandler(
}

/**
* A callback invoked when any exception occurs before committing transactions. This method must
* not throw any exception.
* A callback invoked when any exception occurs before committing transactions.
*
* @param snapshot the failed snapshot.
*/
protected void onFailureBeforeCommit(Snapshot snapshot) {}

private void safelyCallOnFailureBeforeCommit(Snapshot snapshot) {
try {
onFailureBeforeCommit(snapshot);
} catch (Exception e) {
logger.warn("Failed to call the callback. Transaction ID: {}", snapshot.getId(), e);
}
}

private Optional<Future<Void>> invokeBeforePreparationSnapshotHook(Snapshot snapshot)
throws UnknownTransactionStatusException, CommitException {
if (beforePreparationSnapshotHook == null) {
Expand All @@ -69,7 +76,7 @@ private Optional<Future<Void>> invokeBeforePreparationSnapshotHook(Snapshot snap
return Optional.of(
beforePreparationSnapshotHook.handle(tableMetadataManager, snapshot.getReadWriteSets()));
} catch (Exception e) {
onFailureBeforeCommit(snapshot);
safelyCallOnFailureBeforeCommit(snapshot);
abortState(snapshot.getId());
rollbackRecords(snapshot);
throw new CommitException(
Expand All @@ -89,7 +96,7 @@ private void waitBeforePreparationSnapshotHookFuture(
try {
snapshotHookFuture.get();
} catch (Exception e) {
onFailureBeforeCommit(snapshot);
safelyCallOnFailureBeforeCommit(snapshot);
abortState(snapshot.getId());
rollbackRecords(snapshot);
throw new CommitException(
Expand All @@ -104,30 +111,30 @@ public void commit(Snapshot snapshot) throws CommitException, UnknownTransaction
try {
prepare(snapshot);
} catch (PreparationException e) {
onFailureBeforeCommit(snapshot);
safelyCallOnFailureBeforeCommit(snapshot);
abortState(snapshot.getId());
rollbackRecords(snapshot);
if (e instanceof PreparationConflictException) {
throw new CommitConflictException(e.getMessage(), e, e.getTransactionId().orElse(null));
}
throw new CommitException(e.getMessage(), e, e.getTransactionId().orElse(null));
} catch (Exception e) {
onFailureBeforeCommit(snapshot);
safelyCallOnFailureBeforeCommit(snapshot);
throw e;
}

try {
validate(snapshot);
} catch (ValidationException e) {
onFailureBeforeCommit(snapshot);
safelyCallOnFailureBeforeCommit(snapshot);
abortState(snapshot.getId());
rollbackRecords(snapshot);
if (e instanceof ValidationConflictException) {
throw new CommitConflictException(e.getMessage(), e, e.getTransactionId().orElse(null));
}
throw new CommitException(e.getMessage(), e, e.getTransactionId().orElse(null));
} catch (Exception e) {
onFailureBeforeCommit(snapshot);
safelyCallOnFailureBeforeCommit(snapshot);
throw e;
}

Expand Down

0 comments on commit 782cb03

Please sign in to comment.