yuqi1129 commented on code in PR #14992: URL: https://github.com/apache/gravitino/pull/14992#discussion_r4594827458
########## core/src/main/java/org/apache/gravitino/storage/relational/service/GlossaryTermMetaService.java: ########## @@ -0,0 +1,340 @@ +/* + * 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.storage.relational.service; + +import com.fasterxml.jackson.core.type.TypeReference; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; +import org.apache.gravitino.Entity; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.glossary.GlossaryTerm.Status; +import org.apache.gravitino.glossary.TermLink; +import org.apache.gravitino.json.JsonUtils; +import org.apache.gravitino.meta.AuditInfo; +import org.apache.gravitino.meta.GlossaryTermEntity; +import org.apache.gravitino.meta.GlossaryTermNames; +import org.apache.gravitino.meta.NamespacedEntityId; +import org.apache.gravitino.storage.relational.mapper.GlossaryTermMetaMapper; +import org.apache.gravitino.storage.relational.mapper.MetalakeMetaMapper; +import org.apache.gravitino.storage.relational.po.GlossaryTermPO; +import org.apache.gravitino.storage.relational.po.GlossaryTermSynonymPO; +import org.apache.gravitino.storage.relational.po.MetalakePO; +import org.apache.gravitino.storage.relational.utils.ExceptionUtils; +import org.apache.gravitino.storage.relational.utils.SessionUtils; + +/** + * Transactional persistence of glossary definitions and their synonyms. + * + * <p>Operations join an existing SessionUtils transaction or commit their own. Missing definitions + * throw NoSuchEntityException, except delete (false) and batchGet (omitted). Duplicate names, + * source references and reserved IDs are translated to EntityAlreadyExistsException. Other database + * failures propagate through the existing store exception converter. + * + * <p>Insert starts at revision 1; update and overwrite increment the stored revision once. Callers + * must detect semantic no-ops before invoking a write. Relation validation, authorization and the + * name-versus-synonym checks belong to the glossary manager, not this persistence service. + */ +public final class GlossaryTermMetaService { + private static final GlossaryTermMetaService INSTANCE = new GlossaryTermMetaService(); + + private GlossaryTermMetaService() {} + /** Returns the shared service. */ + public static GlossaryTermMetaService getInstance() { + return INSTANCE; + } + + /** Inserts a definition. Overwrite retains the stored identity and increments its revision. */ + public void insert(GlossaryTermEntity entity, boolean overwritten) throws IOException { + try { + transaction( + entity.namespace(), + mapper -> { + long metalakeId = metalake(entity.namespace()).getMetalakeId(); + GlossaryTermPO old = mapper.get(metalakeId, GlossaryTermNames.key(entity.name()), true); + GlossaryTermPO row = toPO(entity, metalakeId); + if (overwritten && old != null) { + row.setTermId(old.getTermId()); + row.setCurrentVersion(Math.addExact(old.getCurrentVersion(), 1)); + requireUpdated(mapper.update(row, old.getCurrentVersion()), entity.nameIdentifier()); + mapper.clearSynonyms(row.getTermId()); + } else { + row.setCurrentVersion(1L); + mapper.insert(row); + } + writeSynonyms(mapper, row, entity.synonyms()); + return null; + }); + } catch (RuntimeException e) { + ExceptionUtils.checkSQLException( + e, Entity.EntityType.GLOSSARY_TERM, entity.nameIdentifier().toString()); + throw e; + } + } + + /** Loads a definition and its synonyms in one protected transaction. */ + public GlossaryTermEntity get(NameIdentifier ident) { + return transaction( + ident.namespace(), + mapper -> fromPO(require(mapper, ident, false), mapper, ident.namespace())); + } + + /** Resolves term and metalake identities from the same protected row snapshot. */ + public NamespacedEntityId getIds(NameIdentifier ident) { + return transaction( + ident.namespace(), + mapper -> { + GlossaryTermPO row = require(mapper, ident, false); + return new NamespacedEntityId(row.getTermId(), row.getMetalakeId()); + }); + } + + /** Lists definitions belonging to the requested metalake. */ + public List<GlossaryTermEntity> list(Namespace ns) { + return transaction( + ns, + mapper -> { + long metalakeId = metalake(ns).getMetalakeId(); + List<GlossaryTermPO> rows = mapper.list(metalakeId); + Map<Long, List<String>> synonyms = new LinkedHashMap<>(); + for (GlossaryTermSynonymPO synonym : mapper.listSynonyms(metalakeId)) { + synonyms + .computeIfAbsent(synonym.getTermId(), ignored -> new ArrayList<>()) + .add(synonym.getSynonym()); + } + List<GlossaryTermEntity> result = new ArrayList<>(); + for (GlossaryTermPO row : rows) { + result.add(fromPO(row, synonyms.getOrDefault(row.getTermId(), List.of()), ns)); + } + return result; + }); + } + + /** + * Loads requested terms, omitting missing names. + * + * <p>Like the existing user/role fallback, this accepts identifiers from different metalakes and + * performs a protected read per identifier. Use list for a complete metalake: it batches synonyms + * and does not issue a query per returned term. + */ + public List<GlossaryTermEntity> batchGet(List<NameIdentifier> identifiers) { + List<GlossaryTermEntity> result = new ArrayList<>(); + for (NameIdentifier ident : identifiers) { + try { + result.add(get(ident)); + } catch (NoSuchEntityException ignored) { + /* Missing entries are omitted. */ + } + } + return result; + } + + /** Updates one definition atomically with its synonyms, preserving its identity and namespace. */ + public <E extends Entity & HasIdentifier> GlossaryTermEntity update( + NameIdentifier ident, Function<E, E> updater) throws IOException { + try { + return transaction( + ident.namespace(), + mapper -> { + GlossaryTermPO old = require(mapper, ident, true); + GlossaryTermEntity updated = + (GlossaryTermEntity) updater.apply((E) fromPO(old, mapper, ident.namespace())); + if (!Objects.equals(old.getTermId(), updated.id()) + || !ident.namespace().equals(updated.namespace())) + throw new IllegalArgumentException( + "A glossary update must retain identity and namespace"); + GlossaryTermPO row = toPO(updated, old.getMetalakeId()); + row.setCurrentVersion(Math.addExact(old.getCurrentVersion(), 1)); + requireUpdated(mapper.update(row, old.getCurrentVersion()), ident); + mapper.clearSynonyms(row.getTermId()); + writeSynonyms(mapper, row, updated.synonyms()); + return fromPO(row, mapper, ident.namespace()); + }); + } catch (RuntimeException e) { + ExceptionUtils.checkSQLException(e, Entity.EntityType.GLOSSARY_TERM, ident.toString()); + throw e; + } + } + + /** Soft-deletes a definition and all its synonyms; a missing definition returns false. */ + public boolean delete(NameIdentifier ident) { + try { + return transaction( + ident.namespace(), + mapper -> { + GlossaryTermPO row = require(mapper, ident, true); + long deletedAt = Math.max(1, System.currentTimeMillis()); + requireUpdated( + mapper.delete(row.getTermId(), row.getCurrentVersion(), deletedAt), ident); + mapper.deleteSynonyms(row.getTermId(), deletedAt); + return true; + }); + } catch (NoSuchEntityException e) { + return false; + } + } + + /** Soft-deletes definitions and synonyms inside the metalake deletion transaction. */ + public void dropMetalake(long metalakeId) { + long deletedAt = Math.max(1, System.currentTimeMillis()); + SessionUtils.doWithoutCommit( + GlossaryTermMetaMapper.class, + mapper -> { + mapper.dropMetalake(metalakeId, deletedAt); + mapper.dropMetalakeSynonyms(metalakeId, deletedAt); + }); + } + + /** Purges at most limit expired definitions and their deleted synonyms atomically. */ + public int collect(long cutoff, int limit) { + return SessionUtils.doWithCommitAndFetchResult( + GlossaryTermMetaMapper.class, + mapper -> { + int count = 0; + for (Long id : mapper.expired(cutoff, limit)) { + mapper.purgeSynonyms(id); + count += mapper.purge(id); + } + return count; + }); + } + + private <T> T transaction(Namespace ns, Function<GlossaryTermMetaMapper, T> work) { + return SessionUtils.doWithCommitAndFetchResult( + GlossaryTermMetaMapper.class, + mapper -> { + MetalakePO observed = metalake(ns); Review Comment: Fixed in 83953f09ef (including e6b6b94fe9). The shared transaction entry now validates the complete reserved glossary namespace before looking up or locking a metalake. Added testWrongNamespaceCannotAccessTerm covering malformed depths and wrong reserved components through delete/get/list/getIds/batchGet/update; it verifies that the canonical term ID, revision and synonyms remain intact and the update callback is never invoked. The regression passes on H2, MySQL and PostgreSQL. ########## core/src/main/java/org/apache/gravitino/meta/GlossaryTermNames.java: ########## @@ -0,0 +1,47 @@ +/* + * 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.meta; + +import java.util.Locale; +import java.util.Objects; +import java.util.regex.Pattern; + +/** Shared normalization for persisted glossary names and synonyms. */ +public final class GlossaryTermNames { + private static final Pattern SPACE = Pattern.compile("\\s+", Pattern.UNICODE_CHARACTER_CLASS); + + private GlossaryTermNames() {} + /** Removes surrounding Unicode whitespace while preserving display spelling. */ + public static String display(String value) { + return SPACE.matcher(Objects.requireNonNull(value, "name")).replaceAll(" ").trim().isEmpty() + ? "" + : value.replaceAll("(?U)^\\s+|\\s+$", ""); Review Comment: Fixed in e6b6b94fe9. display() now scans inward by code point from both ends, preserving internal spelling and matching Unicode White_Space (including NEL and non-breaking spaces). Added exhaustive Unicode boundary coverage and a million-space internal-run regression. All 4 TestGlossaryTermEntity tests pass. A separate old-code mutation exceeds the 5-second timeout on the same input; the fixed implementation finishes in about 0.07 seconds. ########## core/src/main/java/org/apache/gravitino/storage/relational/service/GlossaryTermMetaService.java: ########## @@ -0,0 +1,340 @@ +/* + * 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.storage.relational.service; + +import com.fasterxml.jackson.core.type.TypeReference; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; +import org.apache.gravitino.Entity; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.glossary.GlossaryTerm.Status; +import org.apache.gravitino.glossary.TermLink; +import org.apache.gravitino.json.JsonUtils; +import org.apache.gravitino.meta.AuditInfo; +import org.apache.gravitino.meta.GlossaryTermEntity; +import org.apache.gravitino.meta.GlossaryTermNames; +import org.apache.gravitino.meta.NamespacedEntityId; +import org.apache.gravitino.storage.relational.mapper.GlossaryTermMetaMapper; +import org.apache.gravitino.storage.relational.mapper.MetalakeMetaMapper; +import org.apache.gravitino.storage.relational.po.GlossaryTermPO; +import org.apache.gravitino.storage.relational.po.GlossaryTermSynonymPO; +import org.apache.gravitino.storage.relational.po.MetalakePO; +import org.apache.gravitino.storage.relational.utils.ExceptionUtils; +import org.apache.gravitino.storage.relational.utils.SessionUtils; + +/** + * Transactional persistence of glossary definitions and their synonyms. + * + * <p>Operations join an existing SessionUtils transaction or commit their own. Missing definitions + * throw NoSuchEntityException, except delete (false) and batchGet (omitted). Duplicate names, + * source references and reserved IDs are translated to EntityAlreadyExistsException. Other database + * failures propagate through the existing store exception converter. + * + * <p>Insert starts at revision 1; update and overwrite increment the stored revision once. Callers + * must detect semantic no-ops before invoking a write. Relation validation, authorization and the + * name-versus-synonym checks belong to the glossary manager, not this persistence service. + */ +public final class GlossaryTermMetaService { + private static final GlossaryTermMetaService INSTANCE = new GlossaryTermMetaService(); + + private GlossaryTermMetaService() {} + /** Returns the shared service. */ + public static GlossaryTermMetaService getInstance() { + return INSTANCE; + } + + /** Inserts a definition. Overwrite retains the stored identity and increments its revision. */ + public void insert(GlossaryTermEntity entity, boolean overwritten) throws IOException { + try { + transaction( + entity.namespace(), + mapper -> { + long metalakeId = metalake(entity.namespace()).getMetalakeId(); + GlossaryTermPO old = mapper.get(metalakeId, GlossaryTermNames.key(entity.name()), true); + GlossaryTermPO row = toPO(entity, metalakeId); + if (overwritten && old != null) { + row.setTermId(old.getTermId()); + row.setCurrentVersion(Math.addExact(old.getCurrentVersion(), 1)); + requireUpdated(mapper.update(row, old.getCurrentVersion()), entity.nameIdentifier()); + mapper.clearSynonyms(row.getTermId()); + } else { + row.setCurrentVersion(1L); + mapper.insert(row); + } + writeSynonyms(mapper, row, entity.synonyms()); + return null; + }); + } catch (RuntimeException e) { + ExceptionUtils.checkSQLException( + e, Entity.EntityType.GLOSSARY_TERM, entity.nameIdentifier().toString()); + throw e; + } + } + + /** Loads a definition and its synonyms in one protected transaction. */ + public GlossaryTermEntity get(NameIdentifier ident) { + return transaction( + ident.namespace(), + mapper -> fromPO(require(mapper, ident, false), mapper, ident.namespace())); + } + + /** Resolves term and metalake identities from the same protected row snapshot. */ + public NamespacedEntityId getIds(NameIdentifier ident) { + return transaction( + ident.namespace(), + mapper -> { + GlossaryTermPO row = require(mapper, ident, false); + return new NamespacedEntityId(row.getTermId(), row.getMetalakeId()); + }); + } + + /** Lists definitions belonging to the requested metalake. */ + public List<GlossaryTermEntity> list(Namespace ns) { + return transaction( + ns, + mapper -> { + long metalakeId = metalake(ns).getMetalakeId(); + List<GlossaryTermPO> rows = mapper.list(metalakeId); + Map<Long, List<String>> synonyms = new LinkedHashMap<>(); + for (GlossaryTermSynonymPO synonym : mapper.listSynonyms(metalakeId)) { + synonyms + .computeIfAbsent(synonym.getTermId(), ignored -> new ArrayList<>()) + .add(synonym.getSynonym()); + } + List<GlossaryTermEntity> result = new ArrayList<>(); + for (GlossaryTermPO row : rows) { + result.add(fromPO(row, synonyms.getOrDefault(row.getTermId(), List.of()), ns)); + } + return result; + }); + } + + /** + * Loads requested terms, omitting missing names. + * + * <p>Like the existing user/role fallback, this accepts identifiers from different metalakes and + * performs a protected read per identifier. Use list for a complete metalake: it batches synonyms + * and does not issue a query per returned term. + */ + public List<GlossaryTermEntity> batchGet(List<NameIdentifier> identifiers) { + List<GlossaryTermEntity> result = new ArrayList<>(); + for (NameIdentifier ident : identifiers) { + try { + result.add(get(ident)); + } catch (NoSuchEntityException ignored) { + /* Missing entries are omitted. */ + } + } + return result; + } + + /** Updates one definition atomically with its synonyms, preserving its identity and namespace. */ + public <E extends Entity & HasIdentifier> GlossaryTermEntity update( + NameIdentifier ident, Function<E, E> updater) throws IOException { + try { + return transaction( + ident.namespace(), + mapper -> { + GlossaryTermPO old = require(mapper, ident, true); + GlossaryTermEntity updated = + (GlossaryTermEntity) updater.apply((E) fromPO(old, mapper, ident.namespace())); + if (!Objects.equals(old.getTermId(), updated.id()) + || !ident.namespace().equals(updated.namespace())) + throw new IllegalArgumentException( + "A glossary update must retain identity and namespace"); + GlossaryTermPO row = toPO(updated, old.getMetalakeId()); + row.setCurrentVersion(Math.addExact(old.getCurrentVersion(), 1)); + requireUpdated(mapper.update(row, old.getCurrentVersion()), ident); + mapper.clearSynonyms(row.getTermId()); + writeSynonyms(mapper, row, updated.synonyms()); + return fromPO(row, mapper, ident.namespace()); + }); + } catch (RuntimeException e) { + ExceptionUtils.checkSQLException(e, Entity.EntityType.GLOSSARY_TERM, ident.toString()); + throw e; + } + } + + /** Soft-deletes a definition and all its synonyms; a missing definition returns false. */ + public boolean delete(NameIdentifier ident) { + try { + return transaction( + ident.namespace(), + mapper -> { + GlossaryTermPO row = require(mapper, ident, true); + long deletedAt = Math.max(1, System.currentTimeMillis()); Review Comment: Fixed in 83953f09ef (including e6b6b94fe9) across all six schema/upgrade scripts. H2/MySQL use a nullable generated live-row discriminator and PostgreSQL uses partial unique indexes, so only live rows reserve names/source references while deleted_at remains the real GC timestamp. Deterministic tests cover repeated names, independent source-reference reuse, same-timestamp metalake cleanup, bounded GC/cutoff handling, and preservation of the new live term and synonyms. All 11 glossary storage tests and 13 SQL-script tests pass on each of H2/MySQL/PostgreSQL; a MySQL 5.7.44 probe also passes repeatable DDL, exact-source uniqueness and same-timestamp deletion checks. -- 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]
