jerryshao commented on code in PR #14891:
URL: https://github.com/apache/gravitino/pull/14891#discussion_r4536023676
##########
core/src/main/java/org/apache/gravitino/hook/TableHookDispatcher.java:
##########
@@ -89,24 +90,30 @@ public Table createTable(
SortOrder[] sortOrders,
Index[] indexes)
throws NoSuchSchemaException, TableAlreadyExistsException {
+ OwnerDispatcher ownerManager = ownerDispatcher.get();
+ LongConsumer initialOwnerWriter =
+ ownerManager == null
+ ? null
+ : ownerManager.prepareInitialOwner(
+ ident.namespace().level(0),
+ NameIdentifierUtil.toMetadataObject(ident,
Entity.EntityType.TABLE),
+ PrincipalUtils.getCurrentUserName());
+ Preconditions.checkState(
+ ownerManager == null || initialOwnerWriter != null,
+ "Owner initialization preparation must return a writer");
Table table =
dispatcher.createTable(
ident, columns, comment, properties, partitions, distribution,
sortOrders, indexes);
-
- // Set the creator as the owner of the table.
- OwnerDispatcher ownerManager = ownerDispatcher.get();
- if (ownerManager != null) {
- 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);
+ if (initialOwnerWriter != null) {
+ long id;
+ if (table instanceof EntityCombinedTable) {
+ id = ((EntityCombinedTable) table).id();
+ } else {
+ StringIdentifier identifier =
StringIdentifier.fromProperties(table.properties());
+ Preconditions.checkState(identifier != null, "The created table has no
Gravitino identity");
Review Comment:
If the created table carries no Gravitino ID (for example a catalog that
does not embed it in properties), create succeeds but this throws, leaving a
committed table with no owner and an error returned to the user. Handle this
case without failing the create (log and skip, or use a fallback), and add a
test.
##########
core/src/main/java/org/apache/gravitino/authorization/OwnerManager.java:
##########
@@ -132,6 +136,63 @@ public void setOwner(
}
}
+ /** {@inheritDoc} */
+ @Override
+ public LongConsumer prepareInitialOwner(
+ String metalake, MetadataObject metadataObject, String ownerName) {
+ if (!(store instanceof RelationalEntityStore)
+ || metadataObject.type() != MetadataObject.Type.TABLE) {
+ throw new UnsupportedOperationException(
+ "Initial table ownership requires the built-in relational entity
store");
+ }
+ return expectedId -> initializeOwner(metalake, metadataObject, expectedId,
ownerName);
+ }
+
+ /**
+ * Initializes a table owner only while the exact created entity is live and
unowned.
+ *
+ * <p>Explicit owner transfers continue to use {@link #setOwner}. Plugin
callbacks retain their
+ * existing post-commit, name-based semantics; they are not part of the
database transaction.
+ *
+ * @param metalake the metalake name
+ * @param metadataObject the created table metadata object
+ * @param expectedId the Gravitino ID returned by creation
+ * @param ownerName the creator's user name
+ * @return true if assigned; false if the table was deleted or already owned
+ */
+ private boolean initializeOwner(
+ String metalake, MetadataObject metadataObject, long expectedId, String
ownerName) {
+ RelationUpdate update =
+ RelationUpdate.of(
+ SupportsRelationOperations.Type.OWNER_REL,
+ MetadataObjectUtil.toEntityIdent(metalake, metadataObject),
+ MetadataObjectUtil.toEntityType(metadataObject),
+ new RelationEdgeTarget[] {
+ RelationEdgeTarget.of(
+ AuthorizationUtils.ofUser(metalake, ownerName),
Entity.EntityType.USER, null)
+ },
+ new RelationEdgeTarget[0])
+ .withExpectedSourceId(expectedId);
+ try {
+ List<UserEntity> assigned =
store.relationOperations().updateEntityRelations(update);
+ if (assigned.isEmpty()) {
+ return false;
+ }
+ OwnerImpl newOwner = new OwnerImpl();
+ newOwner.name = assigned.get(0).name();
+ newOwner.type = Owner.Type.USER;
+ AuthorizationUtils.callAuthorizationPluginForMetadataObject(
+ metalake, metadataObject, plugin ->
plugin.onOwnerSet(metadataObject, null, newOwner));
+ notifyOwnerChange(null, metalake, metadataObject);
+ return true;
+ } catch (NoSuchEntityException missing) {
+ throw new NotFoundException(missing, "Initial owner user %s is not
found", ownerName);
Review Comment:
A missing creator user is now raised after the table is already created, so
the client sees a failure while the table exists without an owner. Validate the
owner user in prepareInitialOwner before creating, or decide explicitly how
this post-create failure should be reported.
##########
core/src/main/java/org/apache/gravitino/authorization/OwnerManager.java:
##########
@@ -132,6 +136,63 @@ public void setOwner(
}
}
+ /** {@inheritDoc} */
+ @Override
+ public LongConsumer prepareInitialOwner(
+ String metalake, MetadataObject metadataObject, String ownerName) {
+ if (!(store instanceof RelationalEntityStore)
+ || metadataObject.type() != MetadataObject.Type.TABLE) {
+ throw new UnsupportedOperationException(
Review Comment:
With authorization enabled, every table creation now fails if the store is
not RelationalEntityStore. Is that intended, and are there supported
deployments or other table-like types where this becomes a regression?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]