jerryshao commented on code in PR #10874:
URL: https://github.com/apache/gravitino/pull/10874#discussion_r3166957538
##########
core/src/main/java/org/apache/gravitino/hook/SchemaHookDispatcher.java:
##########
@@ -57,20 +60,62 @@ public NameIdentifier[] listSchemas(Namespace namespace)
throws NoSuchCatalogExc
@Override
public Schema createSchema(NameIdentifier ident, String comment, Map<String,
String> properties)
throws NoSuchCatalogException, SchemaAlreadyExistsException {
+ // Collect missing parent identifiers BEFORE the underlying call; the
catalog itself is
+ // responsible for auto-creating them. We only need to assign ownership
afterwards.
+ List<NameIdentifier> missingParents = findMissingParents(ident);
Schema schema = dispatcher.createSchema(ident, comment, properties);
- // Set the creator as the owner of the schema.
+ String metalake = ident.namespace().level(0);
OwnerDispatcher ownerManager =
GravitinoEnv.getInstance().ownerDispatcher();
if (ownerManager != null) {
+ String creator = PrincipalUtils.getCurrentUserName();
+ for (NameIdentifier parent : missingParents) {
+ if (dispatcher.schemaExists(parent)) {
+ // Parent schemas can be concurrently created by another request
between the pre-check and
+ // this point. Only assign owner when there is no owner to avoid
overwriting ownership.
+ if (canSetOwner(ownerManager, metalake, parent)) {
+ ownerManager.setOwner(
+ metalake,
+ NameIdentifierUtil.toMetadataObject(parent,
Entity.EntityType.SCHEMA),
+ creator,
+ Owner.Type.USER);
+ }
+ }
+ }
ownerManager.setOwner(
- ident.namespace().level(0),
+ metalake,
NameIdentifierUtil.toMetadataObject(ident, Entity.EntityType.SCHEMA),
- PrincipalUtils.getCurrentUserName(),
+ creator,
Owner.Type.USER);
}
return schema;
}
+ /**
+ * Returns ancestor schema identifiers for {@code ident} that do not
currently exist. The catalog
+ * will auto-create them during {@link #createSchema}; we collect them here
only so we can assign
+ * ownership after the fact.
+ */
+ private List<NameIdentifier> findMissingParents(NameIdentifier ident) {
+ String separator = HierarchicalSchemaUtil.schemaSeparator();
+ List<String> ancestorNames =
HierarchicalSchemaUtil.getAncestorNames(ident.name(), separator);
+ List<NameIdentifier> missing = new ArrayList<>();
+ for (String ancestorName : ancestorNames) {
+ NameIdentifier ancestorIdent = NameIdentifier.of(ident.namespace(),
ancestorName);
+ if (!dispatcher.schemaExists(ancestorIdent)) {
Review Comment:
You check the scheam multiple times, this is quite cost, see above.
--
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]