This is an automated email from the ASF dual-hosted git repository. mblow pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/asterixdb.git
commit 813c15b3243d6a1f7d42a3ce5db7dee220038d33 Author: Michael Blow <[email protected]> AuthorDate: Mon Nov 25 16:16:32 2024 -0500 [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 <[email protected]> Tested-by: Michael Blow <[email protected]> --- .../org/apache/hyracks/api/util/InvokeUtil.java | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) 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 ad5e91909b..e7970d31be 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.InterruptibleAction; 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 @@ public class InvokeUtil { } } + // 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;
