[NO ISSUE][HYR][MISC] += InvokeUtil.tryWithCleanups variant that passes exception to cleanups
Ext-ref: MB-64393
Change-Id: Iffecd13861ca430377e0f6a29a8b84cf8d33ca57
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/19127
Reviewed-by: Michael Blow <mblow@apache.org>
Tested-by: Michael Blow <mblow@apache.org>
diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/InvokeUtil.java b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/InvokeUtil.java
index ad5e919..e7970d3 100644
--- a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/InvokeUtil.java
+++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/InvokeUtil.java
@@ -35,6 +35,7 @@
import org.apache.hyracks.util.InterruptibleSupplier;
import org.apache.hyracks.util.Span;
import org.apache.hyracks.util.ThrowingAction;
+import org.apache.hyracks.util.ThrowingConsumer;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -251,6 +252,44 @@
}
}
+ // catching Throwable, instanceofs, false-positive unreachable code
+ public static void tryWithCleanups(ThrowingAction action, ThrowingConsumer<Throwable>... cleanups)
+ throws Exception {
+ Throwable savedT = null;
+ boolean suppressedInterrupted = false;
+ try {
+ action.run();
+ } catch (Throwable t) {
+ savedT = t;
+ } finally {
+ for (ThrowingConsumer cleanup : cleanups) {
+ try {
+ cleanup.process(savedT);
+ } catch (Throwable t) {
+ if (savedT != null) {
+ savedT.addSuppressed(t);
+ suppressedInterrupted = suppressedInterrupted || t instanceof InterruptedException;
+ } else {
+ savedT = t;
+ }
+ }
+ }
+ }
+ if (savedT == null) {
+ return;
+ }
+ if (suppressedInterrupted) {
+ Thread.currentThread().interrupt();
+ }
+ if (savedT instanceof Error) {
+ throw (Error) savedT;
+ } else if (savedT instanceof Exception) {
+ throw (Exception) savedT;
+ } else {
+ throw HyracksDataException.create(savedT);
+ }
+ }
+
@SuppressWarnings({ "squid:S1181", "squid:S1193", "ConstantConditions" }) // catching Throwable, instanceofs
public static void tryIoWithCleanups(IOThrowingAction action, IOThrowingAction... cleanups) throws IOException {
Throwable savedT = null;