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

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


The following commit(s) were added to refs/heads/branch-1.2 by this push:
     new 64ad89f569 [Cherry-pick to branch-1.2] [#10217] fix(core): preserve 
post-hook exception when rollback fails (#10293) (#10468)
64ad89f569 is described below

commit 64ad89f569f2c3923f40786647bee0c98295f33c
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Mar 18 19:01:44 2026 +0800

    [Cherry-pick to branch-1.2] [#10217] fix(core): preserve post-hook 
exception when rollback fails (#10293) (#10468)
    
    **Cherry-pick Information:**
    - Original commit: 631560312b18b45cd6269aae6442a4ce599b6001
    - Target branch: `branch-1.2`
    - Status: ✅ Clean cherry-pick (no conflicts)
    
    Co-authored-by: pythaac <[email protected]>
    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