jerryshao commented on code in PR #15070: URL: https://github.com/apache/gravitino/pull/15070#discussion_r4624009217
########## 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: Wrapping IOException in UncheckedIOException will likely show up as a generic 500 at the REST layer. Consider rethrowing it as the RuntimeException type the other managers use for storage failures. ########## 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: Calling OwnerMetaService.setOwner directly skips what OwnerManager/JDBCBackend do: clear the entity-store relation cache and call gravitinoAuthorizer.handleMetadataOwnerChange. The authorizer can then miss the new owner, and the cascade soft-delete in dropTerm can leave a stale cached owner for a later term with the same name. Route ownership through the store/OwnerManager path, or add the same cache and authorizer notifications here and on drop. ########## 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: If the creator is not a registered user in the metalake (for example a service admin), setOwner throws and the whole create rolls back. Is failing the create intended, or should a missing owner be skipped the way other create flows handle it? ########## 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: Restoring the original spelling means a single request can never fix a synonym's capitalization (remove foo, add Foo still stores foo); it only works as two separate requests. Is that the intended contract? If so, please document it on RemoveSynonym/AddSynonym. -- 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]
