You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@asterixdb.apache.org by AsterixDB Code Review <do...@asterix-gerrit.ics.uci.edu> on 2026/10/21 11:05:52 UTC
[M] Change in asterixdb[master]: [ASTERIXDB-3959][HYR] Fix matchingCause depth
From Hussain Altowileb <hu...@gmail.com>:
Hussain Altowileb has uploaded this change for review. ( https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/22135?usp=email )
Change subject: [ASTERIXDB-3959][HYR] Fix matchingCause depth
......................................................................
[ASTERIXDB-3959][HYR] Fix matchingCause depth
ExceptionUtils.matchingCause is documented to find a match anywhere in
the cause hierarchy, but its loop re-read the same cause on its first
step and stopped, so only the exception itself and its first cause were
tested and a match two or more levels down was missed. It now searches
the whole cause chain, and ends on a chain that loops back on itself.
Added test cases covering a match at every depth, no match, and a cyclic
chain.
Ext-ref: MB-74744
Generated-by: Claude Opus 5.5 <no...@anthropic.com>
Change-Id: I756434b8ad95e0024dfc678091ada3e6d29d9973
---
M hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/ExceptionUtils.java
A hyracks-fullstack/hyracks/hyracks-api/src/test/java/org/apache/hyracks/api/util/ExceptionUtilsMatchingCauseTest.java
2 files changed, 71 insertions(+), 8 deletions(-)
git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/35/22135/1
diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/ExceptionUtils.java b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/ExceptionUtils.java
index 17f7827..004be88 100644
--- a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/ExceptionUtils.java
+++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/ExceptionUtils.java
@@ -21,9 +21,12 @@
import java.io.InterruptedIOException;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
+import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -191,17 +194,14 @@
* Determines whether supplied exception contains a matching cause in its hierarchy, or is itself a match
*/
public static boolean matchingCause(Throwable e, Predicate<Throwable> test) {
- Throwable current = e;
- Throwable cause = e.getCause();
- while (cause != null && cause != current) {
- if (test.test(cause)) {
+ // identity, not equals: a cause chain that loops back on itself must still end
+ Set<Throwable> seen = Collections.newSetFromMap(new IdentityHashMap<>());
+ for (Throwable current = e; current != null && seen.add(current); current = current.getCause()) {
+ if (test.test(current)) {
return true;
}
- Throwable nextCause = current.getCause();
- current = cause;
- cause = nextCause;
}
- return test.test(e);
+ return false;
}
/**
diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/test/java/org/apache/hyracks/api/util/ExceptionUtilsMatchingCauseTest.java b/hyracks-fullstack/hyracks/hyracks-api/src/test/java/org/apache/hyracks/api/util/ExceptionUtilsMatchingCauseTest.java
new file mode 100644
index 0000000..ed632a1
--- /dev/null
+++ b/hyracks-fullstack/hyracks/hyracks-api/src/test/java/org/apache/hyracks/api/util/ExceptionUtilsMatchingCauseTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.hyracks.api.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.function.Predicate;
+
+import org.junit.Test;
+
+/**
+ * Covers {@link ExceptionUtils#matchingCause}: the exception itself or any cause in its chain may match.
+ */
+public class ExceptionUtilsMatchingCauseTest {
+
+ private static final Predicate<Throwable> IS_IO = t -> t instanceof IOException;
+
+ /**
+ * A match at any depth is found. The search used to stop after the first cause, so a match two or more levels
+ * down was missed.
+ */
+ @Test
+ public void matchIsFoundAtAnyDepth() {
+ Throwable chain = new IOException("leaf");
+ for (int depth = 0; depth <= 4; depth++) {
+ assertTrue("missed a match at depth " + depth, ExceptionUtils.matchingCause(chain, IS_IO));
+ chain = new RuntimeException("level " + depth, chain);
+ }
+ }
+
+ @Test
+ public void noMatchIsFalse() {
+ Throwable chain = new RuntimeException("a", new IllegalStateException("b", new RuntimeException("c")));
+ assertFalse(ExceptionUtils.matchingCause(chain, IS_IO));
+ assertFalse(ExceptionUtils.matchingCause(new RuntimeException("alone"), IS_IO));
+ }
+
+ @Test
+ public void searchEndsOnACycle() {
+ RuntimeException a = new RuntimeException("a");
+ RuntimeException b = new RuntimeException("b", a);
+ a.initCause(b);
+ assertFalse(ExceptionUtils.matchingCause(a, IS_IO));
+ }
+}
--
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/22135?usp=email
To unsubscribe, or for help writing mail filters, visit https://asterix-gerrit.ics.uci.edu/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Change-Id: I756434b8ad95e0024dfc678091ada3e6d29d9973
Gerrit-Change-Number: 22135
Gerrit-PatchSet: 1
Gerrit-Owner: Hussain Altowileb <hu...@gmail.com>