This is an automated email from the ASF dual-hosted git repository.
jerryshao pushed a commit to branch branch-1.3
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/branch-1.3 by this push:
new 268512ed4f [Cherry-pick to branch-1.3] [#14420] fix(core): Keep a
table create successful when a concurrent drop removes the table (#14787)
(#14805)
268512ed4f is described below
commit 268512ed4f4ca9ce0d4c40ddb39a67c50cdb6d9c
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Nov 10 14:03:58 2026 +0800
[Cherry-pick to branch-1.3] [#14420] fix(core): Keep a table create
successful when a concurrent drop removes the table (#14787) (#14805)
**Cherry-pick Information:**
- Original commit: de323140420dc9445f1a0993d2865f9b7410dc82
- Target branch: `branch-1.3`
- Status: ⚠️ **Has conflicts - manual resolution required**
**Do not merge** until conflict markers are resolved and the
`cherry-pick-conflict` label is removed.
Please review and resolve the conflicts before merging.
---------
Co-authored-by: Qi Yu <[email protected]>
Co-authored-by: yuqi <[email protected]>
---
.../gravitino/authorization/OwnerManager.java | 18 ++++++++
.../apache/gravitino/hook/TableHookDispatcher.java | 21 +++++++---
.../gravitino/authorization/TestOwnerManager.java | 49 ++++++++++++++++++++++
.../gravitino/hook/TestTableHookDispatcher.java | 43 +++++++++++++++++++
4 files changed, 126 insertions(+), 5 deletions(-)
diff --git
a/core/src/main/java/org/apache/gravitino/authorization/OwnerManager.java
b/core/src/main/java/org/apache/gravitino/authorization/OwnerManager.java
index af83b65806..700ada353f 100644
--- a/core/src/main/java/org/apache/gravitino/authorization/OwnerManager.java
+++ b/core/src/main/java/org/apache/gravitino/authorization/OwnerManager.java
@@ -124,6 +124,13 @@ public class OwnerManager implements OwnerDispatcher {
authorizationPlugin.onOwnerSet(metadataObject,
originOwner.orElse(null), newOwner));
notifyOwnerChange(originOwner.orElse(null), metalake, metadataObject);
} catch (NoSuchEntityException nse) {
+ // The object existed when getOwner ran, so it can have been dropped
since then. This
+ // owner-exists check is a heuristic: if the owner is also removed
between the failure and
+ // this check, keep the generic NotFoundException; table creation can
still fail in that race.
+ if (ownerExists(metalake, ownerName, ownerType)) {
+ throw new NoSuchMetadataObjectException(
+ nse, "The metadata object of %s isn't found",
metadataObject.fullName());
+ }
LOG.warn(
"Metadata object {} or owner {} is not found",
metadataObject.fullName(), ownerName, nse);
throw new NotFoundException(
@@ -345,6 +352,17 @@ public class OwnerManager implements OwnerDispatcher {
}
}
+ private boolean ownerExists(String metalake, String ownerName, Owner.Type
ownerType) {
+ try {
+ return ownerType == Owner.Type.USER
+ ? store.exists(AuthorizationUtils.ofUser(metalake, ownerName),
Entity.EntityType.USER)
+ : store.exists(AuthorizationUtils.ofGroup(metalake, ownerName),
Entity.EntityType.GROUP);
+ } catch (IOException ioe) {
+ LOG.warn("Failed to check whether owner {} exists", ownerName, ioe);
+ return false;
+ }
+ }
+
private static class OwnerImpl implements Owner {
private String name;
diff --git
a/core/src/main/java/org/apache/gravitino/hook/TableHookDispatcher.java
b/core/src/main/java/org/apache/gravitino/hook/TableHookDispatcher.java
index 76dc96d2ed..5f472804aa 100644
--- a/core/src/main/java/org/apache/gravitino/hook/TableHookDispatcher.java
+++ b/core/src/main/java/org/apache/gravitino/hook/TableHookDispatcher.java
@@ -28,6 +28,7 @@ import org.apache.gravitino.authorization.AuthorizationUtils;
import org.apache.gravitino.authorization.Owner;
import org.apache.gravitino.authorization.OwnerDispatcher;
import org.apache.gravitino.catalog.TableDispatcher;
+import org.apache.gravitino.exceptions.NoSuchMetadataObjectException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.exceptions.NoSuchTableException;
import org.apache.gravitino.exceptions.TableAlreadyExistsException;
@@ -40,6 +41,8 @@ import
org.apache.gravitino.rel.expressions.transforms.Transform;
import org.apache.gravitino.rel.indexes.Index;
import org.apache.gravitino.utils.NameIdentifierUtil;
import org.apache.gravitino.utils.PrincipalUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* {@code TableHookDispatcher} is a decorator for {@link TableDispatcher} that
not only delegates
@@ -47,6 +50,8 @@ import org.apache.gravitino.utils.PrincipalUtils;
* or after the underlying operations.
*/
public class TableHookDispatcher implements TableDispatcher {
+ private static final Logger LOG =
LoggerFactory.getLogger(TableHookDispatcher.class);
+
private final TableDispatcher dispatcher;
private final Supplier<OwnerDispatcher> ownerDispatcher;
@@ -91,11 +96,17 @@ public class TableHookDispatcher implements TableDispatcher
{
// Set the creator as the owner of the table.
OwnerDispatcher ownerManager = ownerDispatcher.get();
if (ownerManager != null) {
- ownerManager.setOwner(
- ident.namespace().level(0),
- NameIdentifierUtil.toMetadataObject(ident, Entity.EntityType.TABLE),
- PrincipalUtils.getCurrentUserName(),
- Owner.Type.USER);
+ try {
+ ownerManager.setOwner(
+ ident.namespace().level(0),
+ NameIdentifierUtil.toMetadataObject(ident,
Entity.EntityType.TABLE),
+ PrincipalUtils.getCurrentUserName(),
+ Owner.Type.USER);
+ } catch (NoSuchMetadataObjectException e) {
+ // The create has already committed and a concurrent drop removed the
table, so the create
+ // still succeeded. There is no table left to own.
+ LOG.warn("Table {} was dropped before its owner could be set", ident,
e);
+ }
}
return table;
}
diff --git
a/core/src/test/java/org/apache/gravitino/authorization/TestOwnerManager.java
b/core/src/test/java/org/apache/gravitino/authorization/TestOwnerManager.java
index 9bdb976327..a3640ce65d 100644
---
a/core/src/test/java/org/apache/gravitino/authorization/TestOwnerManager.java
+++
b/core/src/test/java/org/apache/gravitino/authorization/TestOwnerManager.java
@@ -56,10 +56,12 @@ import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.MetadataObjects;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.Namespace;
+import org.apache.gravitino.SupportsRelationOperations;
import org.apache.gravitino.catalog.CatalogManager;
import org.apache.gravitino.catalog.CatalogTestUtils;
import org.apache.gravitino.connector.BaseCatalog;
import org.apache.gravitino.connector.authorization.AuthorizationPlugin;
+import org.apache.gravitino.exceptions.NoSuchEntityException;
import org.apache.gravitino.exceptions.NoSuchMetadataObjectException;
import org.apache.gravitino.exceptions.NotFoundException;
import org.apache.gravitino.lock.LockManager;
@@ -290,4 +292,51 @@ public class TestOwnerManager {
GravitinoEnv.getInstance(), "gravitinoAuthorizer",
originalAuthorizer, true);
}
}
+
+ @Test
+ @Order(5)
+ public void testSetOwnerReportsObjectDroppedDuringAssignment() throws
IOException {
+ EntityStore store = storeFailingOwnerInsert();
+ Mockito.when(store.exists(AuthorizationUtils.ofUser(METALAKE, USER),
Entity.EntityType.USER))
+ .thenReturn(true);
+ MetadataObject table = MetadataObjects.parse("c.s.t",
MetadataObject.Type.TABLE);
+
+ Assertions.assertThrows(
+ NoSuchMetadataObjectException.class,
+ () -> new OwnerManager(store).setOwner(METALAKE, table, USER,
Owner.Type.USER));
+ }
+
+ @Test
+ @Order(6)
+ public void testSetOwnerKeepsGenericNotFoundForMissingOwner() throws
IOException {
+ EntityStore store = storeFailingOwnerInsert();
+ Mockito.when(store.exists(AuthorizationUtils.ofUser(METALAKE, USER),
Entity.EntityType.USER))
+ .thenReturn(false);
+ MetadataObject table = MetadataObjects.parse("c.s.t",
MetadataObject.Type.TABLE);
+
+ NotFoundException thrown =
+ Assertions.assertThrows(
+ NotFoundException.class,
+ () -> new OwnerManager(store).setOwner(METALAKE, table, USER,
Owner.Type.USER));
+ Assertions.assertFalse(thrown instanceof NoSuchMetadataObjectException);
+ }
+
+ private static EntityStore storeFailingOwnerInsert() throws IOException {
+ EntityStore store =
+ Mockito.mock(
+ EntityStore.class,
+
Mockito.withSettings().extraInterfaces(SupportsRelationOperations.class));
+ SupportsRelationOperations relations = (SupportsRelationOperations) store;
+ Mockito.when(store.relationOperations()).thenReturn(relations);
+ Mockito.doThrow(new NoSuchEntityException("The entity is gone"))
+ .when(relations)
+ .insertRelation(
+ Mockito.any(),
+ Mockito.any(),
+ Mockito.any(),
+ Mockito.any(),
+ Mockito.any(),
+ Mockito.anyBoolean());
+ return store;
+ }
}
diff --git
a/core/src/test/java/org/apache/gravitino/hook/TestTableHookDispatcher.java
b/core/src/test/java/org/apache/gravitino/hook/TestTableHookDispatcher.java
index 6aff59c0e7..a85f9f3cfc 100644
--- a/core/src/test/java/org/apache/gravitino/hook/TestTableHookDispatcher.java
+++ b/core/src/test/java/org/apache/gravitino/hook/TestTableHookDispatcher.java
@@ -41,6 +41,8 @@ import org.apache.gravitino.catalog.TableNormalizeDispatcher;
import org.apache.gravitino.connector.BaseCatalog;
import org.apache.gravitino.connector.capability.Capability;
import org.apache.gravitino.connector.capability.CapabilityResult;
+import org.apache.gravitino.exceptions.NoSuchMetadataObjectException;
+import org.apache.gravitino.exceptions.NotFoundException;
import org.apache.gravitino.rel.Column;
import org.apache.gravitino.rel.Table;
import org.apache.gravitino.rel.TableChange;
@@ -201,6 +203,47 @@ public class TestTableHookDispatcher {
assertEquals("Set owner failed", thrown.getMessage());
}
+ @Test
+ public void testCreateTableSucceedsWhenTableDroppedBeforeOwnerSet() throws
Exception {
+ OwnerDispatcher ownerDispatcher = Mockito.mock(OwnerDispatcher.class);
+ Mockito.doThrow(new NoSuchMetadataObjectException("The table is gone"))
+ .when(ownerDispatcher)
+ .setOwner(any(), any(), any(), any());
+ TableDispatcher dispatcher = Mockito.mock(TableDispatcher.class);
+ Table createdTable = Mockito.mock(Table.class);
+ Mockito.when(dispatcher.createTable(any(), any(), any(), any(), any(),
any(), any(), any()))
+ .thenReturn(createdTable);
+ TableHookDispatcher hook = new TableHookDispatcher(dispatcher, () ->
ownerDispatcher);
+
+ assertSame(createdTable, createTable(hook));
+ }
+
+ @Test
+ public void testCreateTableThrowsWhenOwnerNotFound() throws Exception {
+ OwnerDispatcher ownerDispatcher = Mockito.mock(OwnerDispatcher.class);
+ Mockito.doThrow(new NotFoundException("The owner is missing"))
+ .when(ownerDispatcher)
+ .setOwner(any(), any(), any(), any());
+ TableDispatcher dispatcher = Mockito.mock(TableDispatcher.class);
+ Mockito.when(dispatcher.createTable(any(), any(), any(), any(), any(),
any(), any(), any()))
+ .thenReturn(Mockito.mock(Table.class));
+ TableHookDispatcher hook = new TableHookDispatcher(dispatcher, () ->
ownerDispatcher);
+
+ assertThrows(NotFoundException.class, () -> createTable(hook));
+ }
+
+ private static Table createTable(TableHookDispatcher hook) {
+ return hook.createTable(
+ NameIdentifier.of(METALAKE, CATALOG, "schema", "table"),
+ new Column[0],
+ "comment",
+ ImmutableMap.of(),
+ new Transform[0],
+ Distributions.NONE,
+ new SortOrder[0],
+ new Index[0]);
+ }
+
@Test
public void testRenameAuthorizationPrivilege() {
TableDispatcher dispatcher = Mockito.mock(TableDispatcher.class);