yuqi1129 commented on code in PR #15070: URL: https://github.com/apache/gravitino/pull/15070#discussion_r4625032782
########## core/src/main/java/org/apache/gravitino/glossary/GlossaryManager.java: ########## @@ -0,0 +1,189 @@ +/* + * 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.glossary; + +import com.google.common.base.Preconditions; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.List; +import java.util.UUID; +import javax.annotation.Nullable; +import org.apache.gravitino.Entity.EntityType; +import org.apache.gravitino.EntityAlreadyExistsException; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.exceptions.NoSuchTermException; +import org.apache.gravitino.exceptions.TermAlreadyExistsException; +import org.apache.gravitino.exceptions.TermVersionConflictException; +import org.apache.gravitino.meta.GlossaryTermEntity; +import org.apache.gravitino.storage.IdGenerator; +import org.apache.gravitino.storage.relational.service.GlossaryTermMetaService; +import org.apache.gravitino.storage.relational.service.OwnerMetaService; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; + +/** Manages live glossary definitions. Callers supply the metalake scope for every operation. */ +public final class GlossaryManager { + private final GlossaryTermMetaService terms = GlossaryTermMetaService.getInstance(); + private final IdGenerator ids; + private final boolean authorizationEnabled; + private final TermChangeHandler handler; + + /** Creates a manager that applies effective changes immediately. */ + public GlossaryManager(IdGenerator ids, boolean authorizationEnabled) { + this(ids, authorizationEnabled, (before, after, persist) -> persist.run()); + } + + GlossaryManager(IdGenerator ids, boolean authorizationEnabled, TermChangeHandler handler) { + this.ids = ids; + this.authorizationEnabled = authorizationEnabled; + this.handler = handler; + } + + /** Lists term definitions in the given metalake. */ + public List<GlossaryTermEntity> listTerms(String metalake) { + return terms.list(NamespaceUtil.ofGlossaryTerm(metalake)); + } + + /** Loads a definition by normalized name. */ + public GlossaryTermEntity getTerm(String metalake, String name) { + NameIdentifier ident = NameIdentifier.of(NamespaceUtil.ofGlossaryTerm(metalake), name); + try { + return terms.get(ident); + } catch (NoSuchEntityException e) { + throw new NoSuchTermException(e, "Term %s does not exist", ident); + } + } + + /** Creates one live definition and assigns its creator as owner when authorization is enabled. */ + public GlossaryTermEntity createTerm(String metalake, TermSpec spec) { + Namespace ns = NamespaceUtil.ofGlossaryTerm(metalake); + GlossaryTermEntity term = TermEdits.create(ids.nextId(), ns, spec); + return terms.withHierarchyLock( + ns, + () -> { + terms.checkTermNames(term); + handler.handle( + null, + term, + () -> { + try { + terms.insert(term, false); + } catch (EntityAlreadyExistsException e) { + throw new TermAlreadyExistsException( + e, "Term name or source reference already exists"); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + if (authorizationEnabled) { + OwnerMetaService.getInstance() Review Comment: I traced the two caches separately. RelationalEntityStore.listEntitiesByRelation delegates directly to the backend; insertRelation evicts endpoint entities, and glossary terms and users are outside the built-in entity cache allowlist. OwnerMetaService reads the live owner by term ID. The new H2 regression in 9776b93b1f reads Alice as owner, drops the term, checks the retired ID has no live owner, then recreates the same name as Bob and checks his ownership. All 25 targeted manager/owner cases passed. The authorizer notification is a separate integration concern. OwnerManager and MetadataIdConverter both use MetadataObjectUtil, which does not yet map GLOSSARY_TERM. Routing creation through OwnerManager today would fail, and notifying from inside the term transaction would precede its outer commit. I documented post-commit invalidation and cross-node drop/recreate verification under T6, where glossary authorization is introduced. I’m leaving this thread open for agreement on that boundary. ########## core/src/main/java/org/apache/gravitino/glossary/GlossaryManager.java: ########## @@ -0,0 +1,189 @@ +/* + * 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.glossary; + +import com.google.common.base.Preconditions; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.List; +import java.util.UUID; +import javax.annotation.Nullable; +import org.apache.gravitino.Entity.EntityType; +import org.apache.gravitino.EntityAlreadyExistsException; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.exceptions.NoSuchTermException; +import org.apache.gravitino.exceptions.TermAlreadyExistsException; +import org.apache.gravitino.exceptions.TermVersionConflictException; +import org.apache.gravitino.meta.GlossaryTermEntity; +import org.apache.gravitino.storage.IdGenerator; +import org.apache.gravitino.storage.relational.service.GlossaryTermMetaService; +import org.apache.gravitino.storage.relational.service.OwnerMetaService; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; + +/** Manages live glossary definitions. Callers supply the metalake scope for every operation. */ +public final class GlossaryManager { + private final GlossaryTermMetaService terms = GlossaryTermMetaService.getInstance(); + private final IdGenerator ids; + private final boolean authorizationEnabled; + private final TermChangeHandler handler; + + /** Creates a manager that applies effective changes immediately. */ + public GlossaryManager(IdGenerator ids, boolean authorizationEnabled) { + this(ids, authorizationEnabled, (before, after, persist) -> persist.run()); + } + + GlossaryManager(IdGenerator ids, boolean authorizationEnabled, TermChangeHandler handler) { + this.ids = ids; + this.authorizationEnabled = authorizationEnabled; + this.handler = handler; + } + + /** Lists term definitions in the given metalake. */ + public List<GlossaryTermEntity> listTerms(String metalake) { + return terms.list(NamespaceUtil.ofGlossaryTerm(metalake)); + } + + /** Loads a definition by normalized name. */ + public GlossaryTermEntity getTerm(String metalake, String name) { + NameIdentifier ident = NameIdentifier.of(NamespaceUtil.ofGlossaryTerm(metalake), name); + try { + return terms.get(ident); + } catch (NoSuchEntityException e) { + throw new NoSuchTermException(e, "Term %s does not exist", ident); + } + } + + /** Creates one live definition and assigns its creator as owner when authorization is enabled. */ + public GlossaryTermEntity createTerm(String metalake, TermSpec spec) { + Namespace ns = NamespaceUtil.ofGlossaryTerm(metalake); + GlossaryTermEntity term = TermEdits.create(ids.nextId(), ns, spec); + return terms.withHierarchyLock( + ns, + () -> { + terms.checkTermNames(term); + handler.handle( + null, + term, + () -> { + try { + terms.insert(term, false); + } catch (EntityAlreadyExistsException e) { + throw new TermAlreadyExistsException( + e, "Term name or source reference already exists"); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + if (authorizationEnabled) { + OwnerMetaService.getInstance() + .setOwner( + term.nameIdentifier(), + EntityType.GLOSSARY_TERM, + NameIdentifierUtil.ofUser(metalake, term.auditInfo().creator()), Review Comment: Yes. T4.1 requires the creator to become owner. With authorization enabled, a creator without a registered metalake user cannot own the term, including a service admin in that situation. We keep the write atomic. In 9776b93b1f the createTerm contract makes this explicit, and the new backend test verifies failure, an empty term list, and successful creation of the same name and source reference after registration. The targeted H2 suites passed. ########## core/src/main/java/org/apache/gravitino/glossary/TermEdits.java: ########## @@ -0,0 +1,179 @@ +/* + * 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.glossary; + +import com.google.common.base.Preconditions; +import java.net.URI; +import java.time.Instant; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; +import org.apache.gravitino.MetadataObjects; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.meta.AuditInfo; +import org.apache.gravitino.meta.GlossaryTermEntity; +import org.apache.gravitino.meta.GlossaryTermNames; +import org.apache.gravitino.utils.PrincipalUtils; + +/** Computes the final definition for a whole request before any rows are written. */ +final class TermEdits { + private TermEdits() {} + + static GlossaryTermEntity create(long id, Namespace namespace, TermSpec spec) { + Preconditions.checkArgument(spec != null, "Term spec is required"); + Preconditions.checkArgument( + spec.domains().length == 0 && spec.broader().length == 0 && spec.related().length == 0, + "Domain and relation changes are not supported yet"); + String[] synonyms = spec.synonyms(); + Preconditions.checkArgument(synonyms.length <= 100, "At most 100 synonyms are allowed"); + Map<String, String> distinct = synonymsByKey(synonyms); + return GlossaryTermEntity.builder() + .withId(id) + .withNamespace(namespace) + .withName(termName(spec.name())) + .withDefinition(spec.definition()) + .withSynonyms(distinct.values().toArray(new String[0])) + .withStatus(spec.status().orElse(GlossaryTerm.Status.APPROVED)) + .withLinks(validateLinks(spec.links())) + .withProperties(spec.properties()) + .withSourceRef(spec.sourceRef().orElse(null)) + .withAuditInfo( + AuditInfo.builder() + .withCreator(PrincipalUtils.getCurrentUserName()) + .withCreateTime(Instant.now()) + .build()) + .build(); + } + + static boolean needsHierarchyLock(TermChange[] changes) { + return Arrays.stream(changes) + .anyMatch( + c -> + c instanceof TermChange.RenameTerm + || c instanceof TermChange.AddSynonym + || c instanceof TermChange.RemoveSynonym); + } + + static GlossaryTermEntity apply(GlossaryTermEntity current, TermChange[] changes) { + String name = current.name(); + String definition = current.definition(); + GlossaryTerm.Status status = current.status(); + TermLink[] links = current.links(); + String sourceRef = current.sourceRef(); + Map<String, String> properties = new LinkedHashMap<>(current.properties()); + Map<String, String> originalSynonyms = synonymsByKey(current.synonyms()); + Map<String, String> synonyms = new LinkedHashMap<>(originalSynonyms); + for (TermChange change : changes) { + if (change instanceof TermChange.RenameTerm) { + name = termName(((TermChange.RenameTerm) change).getNewName()); + } else if (change instanceof TermChange.UpdateDefinition) { + definition = ((TermChange.UpdateDefinition) change).getNewDefinition(); + GlossaryTermEntity.validateDefinition(definition); + } else if (change instanceof TermChange.SetStatus) { + status = ((TermChange.SetStatus) change).getNewStatus(); + } else if (change instanceof TermChange.UpdateLinks) { + links = validateLinks(((TermChange.UpdateLinks) change).getLinks()); + } else if (change instanceof TermChange.SetSourceRef) { + sourceRef = ((TermChange.SetSourceRef) change).getSourceRef(); + if (sourceRef != null) { + GlossaryTermNames.validate(sourceRef); + } + } else if (change instanceof TermChange.AddSynonym) { + addSynonym(synonyms, ((TermChange.AddSynonym) change).getSynonym()); + } else if (change instanceof TermChange.RemoveSynonym) { + String synonym = ((TermChange.RemoveSynonym) change).getSynonym(); + GlossaryTermNames.validate(GlossaryTermNames.display(synonym)); + synonyms.remove(GlossaryTermNames.key(synonym)); + } else if (change instanceof TermChange.SetProperty) { + TermChange.SetProperty property = (TermChange.SetProperty) change; + properties.put(property.getProperty(), property.getValue()); + } else if (change instanceof TermChange.RemoveProperty) { + properties.remove(((TermChange.RemoveProperty) change).getProperty()); + } else { + throw new IllegalArgumentException("Unsupported glossary change: " + change); + } + } + if (name.equals(current.name()) + && Objects.equals(definition, current.definition()) + && status == current.status() + && Arrays.equals(links, current.links()) + && Objects.equals(sourceRef, current.sourceRef()) + && properties.equals(current.properties()) + && originalSynonyms.keySet().equals(synonyms.keySet())) { + return current; + } + // Removing and re-adding the same key preserves its spelling even if another field changed. + originalSynonyms.forEach(synonyms::replace); Review Comment: Documented this on both TermChange.addSynonym and removeSynonym in 9776b93b1f. Membership is compared by normalized key. To change only the stored display spelling, commit the removal and then add the new spelling in a separate alter. The existing mixed-edit regression passed again with the manager suite. ########## core/src/main/java/org/apache/gravitino/glossary/GlossaryManager.java: ########## @@ -0,0 +1,189 @@ +/* + * 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.glossary; + +import com.google.common.base.Preconditions; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.List; +import java.util.UUID; +import javax.annotation.Nullable; +import org.apache.gravitino.Entity.EntityType; +import org.apache.gravitino.EntityAlreadyExistsException; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.exceptions.NoSuchTermException; +import org.apache.gravitino.exceptions.TermAlreadyExistsException; +import org.apache.gravitino.exceptions.TermVersionConflictException; +import org.apache.gravitino.meta.GlossaryTermEntity; +import org.apache.gravitino.storage.IdGenerator; +import org.apache.gravitino.storage.relational.service.GlossaryTermMetaService; +import org.apache.gravitino.storage.relational.service.OwnerMetaService; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.NamespaceUtil; + +/** Manages live glossary definitions. Callers supply the metalake scope for every operation. */ +public final class GlossaryManager { + private final GlossaryTermMetaService terms = GlossaryTermMetaService.getInstance(); + private final IdGenerator ids; + private final boolean authorizationEnabled; + private final TermChangeHandler handler; + + /** Creates a manager that applies effective changes immediately. */ + public GlossaryManager(IdGenerator ids, boolean authorizationEnabled) { + this(ids, authorizationEnabled, (before, after, persist) -> persist.run()); + } + + GlossaryManager(IdGenerator ids, boolean authorizationEnabled, TermChangeHandler handler) { + this.ids = ids; + this.authorizationEnabled = authorizationEnabled; + this.handler = handler; + } + + /** Lists term definitions in the given metalake. */ + public List<GlossaryTermEntity> listTerms(String metalake) { + return terms.list(NamespaceUtil.ofGlossaryTerm(metalake)); + } + + /** Loads a definition by normalized name. */ + public GlossaryTermEntity getTerm(String metalake, String name) { + NameIdentifier ident = NameIdentifier.of(NamespaceUtil.ofGlossaryTerm(metalake), name); + try { + return terms.get(ident); + } catch (NoSuchEntityException e) { + throw new NoSuchTermException(e, "Term %s does not exist", ident); + } + } + + /** Creates one live definition and assigns its creator as owner when authorization is enabled. */ + public GlossaryTermEntity createTerm(String metalake, TermSpec spec) { + Namespace ns = NamespaceUtil.ofGlossaryTerm(metalake); + GlossaryTermEntity term = TermEdits.create(ids.nextId(), ns, spec); + return terms.withHierarchyLock( + ns, + () -> { + terms.checkTermNames(term); + handler.handle( + null, + term, + () -> { + try { + terms.insert(term, false); + } catch (EntityAlreadyExistsException e) { + throw new TermAlreadyExistsException( + e, "Term name or source reference already exists"); + } catch (IOException e) { + throw new UncheckedIOException(e); Review Comment: I kept UncheckedIOException. DomainManager and TagManager wrap checked storage I/O failures in RuntimeException; UncheckedIOException is a RuntimeException that preserves the IOException as its typed cause. Switching to the generic wrapper would not change the HTTP classification. A genuine unexpected storage failure is a server error. Duplicate names/source references and revision conflicts already have separate typed domain exceptions; the glossary REST mapping is a later task. -- 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]
