This is an automated email from the ASF dual-hosted git repository.

yuqi4733 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 631560312b [#10217] fix(core): preserve post-hook exception when 
rollback fails (#10293)
631560312b is described below

commit 631560312b18b45cd6269aae6442a4ce599b6001
Author: pythaac <[email protected]>
AuthorDate: Wed Mar 18 15:05:42 2026 +0900

    [#10217] fix(core): preserve post-hook exception when rollback fails 
(#10293)
    
    <!--
    1. Title: [#<issue>] <type>(<scope>): <subject>
       Examples:
         - "[#123] feat(operator): support xxx"
         - "[#233] fix: check null before access result in xxx"
         - "[MINOR] refactor: fix typo in variable name"
         - "[MINOR] docs: fix typo in README"
         - "[#255] test: fix flaky test NameOfTheTest"
       Reference: https://www.conventionalcommits.org/en/v1.0.0/
    2. If the PR is unfinished, please mark this PR as draft.
    -->
    
    ### What changes were proposed in this pull request?
    
    In `createCatalog()`, when post-hook operations fail, Gravitino attempts
    to roll back by calling `dropCatalog()`.
    - Before this change, if the rollback also failed, the rollback
    exception masked the original post-hook exception.
    - After this change, the original post-hook exception is rethrown, and
    the rollback exception is attached as a suppressed exception.
    
    ### Why are the changes needed?
    
    The previous behavior could hide the root cause (post-hook failure),
    making debugging difficult.
    
    Fix: #10217
    
    ### Does this PR introduce _any_ user-facing change?
    
    N/A
    
    ### How was this patch tested?
    
    Added unit tests in `TestCatalogHookDispatcher`:
    
    1. Verify that the original post-hook exception is thrown when rollback
    succeeds.
    2. Verify that the original post-hook exception is still thrown and the
    rollback exception is attached as suppressed when rollback fails.
    
    ---------
    
    Co-authored-by: Qi Yu <[email protected]>
---
 .../gravitino/hook/CatalogHookDispatcher.java      |  15 ++-
 .../gravitino/hook/TestCatalogHookDispatcher.java  | 138 +++++++++++++++++++++
 2 files changed, 149 insertions(+), 4 deletions(-)

diff --git 
a/core/src/main/java/org/apache/gravitino/hook/CatalogHookDispatcher.java 
b/core/src/main/java/org/apache/gravitino/hook/CatalogHookDispatcher.java
index ab643acb8a..6d05df6b0f 100644
--- a/core/src/main/java/org/apache/gravitino/hook/CatalogHookDispatcher.java
+++ b/core/src/main/java/org/apache/gravitino/hook/CatalogHookDispatcher.java
@@ -98,10 +98,17 @@ public class CatalogHookDispatcher implements 
CatalogDispatcher {
         futureGrantManager.grantNewlyCreatedCatalog(
             ident.namespace().level(0), (BaseCatalog) catalog);
       }
-    } catch (Exception e) {
-      LOG.warn("Fail to execute the post hook operations, rollback the catalog 
" + ident, e);
-      dispatcher.dropCatalog(ident, true);
-      throw e;
+    } catch (Exception postHookException) {
+      LOG.warn(
+          "Fail to execute the post hook operations, rollback the catalog " + 
ident,
+          postHookException);
+      try {
+        dispatcher.dropCatalog(ident, true);
+      } catch (Exception rollbackException) {
+        LOG.warn("Fail to rollback the catalog during the post hook", 
rollbackException);
+        postHookException.addSuppressed(rollbackException);
+      }
+      throw postHookException;
     }
 
     return catalog;
diff --git 
a/core/src/test/java/org/apache/gravitino/hook/TestCatalogHookDispatcher.java 
b/core/src/test/java/org/apache/gravitino/hook/TestCatalogHookDispatcher.java
new file mode 100644
index 0000000000..1803646fc2
--- /dev/null
+++ 
b/core/src/test/java/org/apache/gravitino/hook/TestCatalogHookDispatcher.java
@@ -0,0 +1,138 @@
+/*
+ * 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.gravitino.hook;
+
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Arrays;
+import java.util.Collections;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.gravitino.Catalog;
+import org.apache.gravitino.GravitinoEnv;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.authorization.OwnerDispatcher;
+import org.apache.gravitino.catalog.CatalogDispatcher;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+public class TestCatalogHookDispatcher {
+
+  @Test
+  public void testCreateCatalogThrowsPostHookExceptionWhenRollbackSucceeds() 
throws Exception {
+    GravitinoEnv gravitinoEnv = GravitinoEnv.getInstance();
+    Object originalOwnerDispatcher = FieldUtils.readField(gravitinoEnv, 
"ownerDispatcher", true);
+    Object originalFutureGrantManager =
+        FieldUtils.readField(gravitinoEnv, "futureGrantManager", true);
+
+    CatalogDispatcher dispatcher = Mockito.mock(CatalogDispatcher.class);
+    Catalog catalog = Mockito.mock(Catalog.class);
+    NameIdentifier ident = NameIdentifier.of("metalake", "catalog");
+    RuntimeException postHookException = new RuntimeException("post-hook 
failed");
+
+    OwnerDispatcher ownerDispatcher = Mockito.mock(OwnerDispatcher.class);
+    Mockito.doThrow(postHookException)
+        .when(ownerDispatcher)
+        .setOwner(Mockito.anyString(), Mockito.any(), Mockito.anyString(), 
Mockito.any());
+    Mockito.when(
+            dispatcher.createCatalog(
+                Mockito.eq(ident),
+                Mockito.eq(Catalog.Type.RELATIONAL),
+                Mockito.eq("provider"),
+                Mockito.eq("comment"),
+                Mockito.anyMap()))
+        .thenReturn(catalog);
+
+    FieldUtils.writeField(gravitinoEnv, "ownerDispatcher", ownerDispatcher, 
true);
+    FieldUtils.writeField(gravitinoEnv, "futureGrantManager", null, true);
+
+    try {
+      CatalogHookDispatcher hookDispatcher = new 
CatalogHookDispatcher(dispatcher);
+      RuntimeException thrown =
+          assertThrowsExactly(
+              RuntimeException.class,
+              () ->
+                  hookDispatcher.createCatalog(
+                      ident,
+                      Catalog.Type.RELATIONAL,
+                      "provider",
+                      "comment",
+                      Collections.emptyMap()));
+      assertSame(postHookException, thrown);
+
+      Mockito.verify(dispatcher).dropCatalog(ident, true);
+    } finally {
+      FieldUtils.writeField(gravitinoEnv, "ownerDispatcher", 
originalOwnerDispatcher, true);
+      FieldUtils.writeField(gravitinoEnv, "futureGrantManager", 
originalFutureGrantManager, true);
+    }
+  }
+
+  @Test
+  public void testCreateCatalogRollbackExceptionDoesNotMaskPostHookException() 
throws Exception {
+    GravitinoEnv gravitinoEnv = GravitinoEnv.getInstance();
+    Object originalOwnerDispatcher = FieldUtils.readField(gravitinoEnv, 
"ownerDispatcher", true);
+    Object originalFutureGrantManager =
+        FieldUtils.readField(gravitinoEnv, "futureGrantManager", true);
+
+    CatalogDispatcher dispatcher = Mockito.mock(CatalogDispatcher.class);
+    Catalog catalog = Mockito.mock(Catalog.class);
+    NameIdentifier ident = NameIdentifier.of("metalake", "catalog");
+    RuntimeException postHookException = new RuntimeException("post-hook 
failed");
+    RuntimeException rollbackException = new RuntimeException("rollback 
failed");
+
+    OwnerDispatcher ownerDispatcher = Mockito.mock(OwnerDispatcher.class);
+    Mockito.doThrow(postHookException)
+        .when(ownerDispatcher)
+        .setOwner(Mockito.anyString(), Mockito.any(), Mockito.anyString(), 
Mockito.any());
+    Mockito.when(
+            dispatcher.createCatalog(
+                Mockito.eq(ident),
+                Mockito.eq(Catalog.Type.RELATIONAL),
+                Mockito.eq("provider"),
+                Mockito.eq("comment"),
+                Mockito.anyMap()))
+        .thenReturn(catalog);
+    Mockito.doThrow(rollbackException).when(dispatcher).dropCatalog(ident, 
true);
+
+    FieldUtils.writeField(gravitinoEnv, "ownerDispatcher", ownerDispatcher, 
true);
+    FieldUtils.writeField(gravitinoEnv, "futureGrantManager", null, true);
+
+    try {
+      CatalogHookDispatcher hookDispatcher = new 
CatalogHookDispatcher(dispatcher);
+      RuntimeException thrown =
+          assertThrowsExactly(
+              RuntimeException.class,
+              () ->
+                  hookDispatcher.createCatalog(
+                      ident,
+                      Catalog.Type.RELATIONAL,
+                      "provider",
+                      "comment",
+                      Collections.emptyMap()));
+      assertSame(postHookException, thrown);
+      assertTrue(Arrays.stream(thrown.getSuppressed()).anyMatch(t -> t == 
rollbackException));
+
+      Mockito.verify(dispatcher).dropCatalog(ident, true);
+    } finally {
+      FieldUtils.writeField(gravitinoEnv, "ownerDispatcher", 
originalOwnerDispatcher, true);
+      FieldUtils.writeField(gravitinoEnv, "futureGrantManager", 
originalFutureGrantManager, true);
+    }
+  }
+}

Reply via email to