yuqi1129 commented on code in PR #15034: URL: https://github.com/apache/gravitino/pull/15034#discussion_r4610761012
########## core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/GlossaryTermRelSQLProvider.java: ########## @@ -0,0 +1,133 @@ +/* + * 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.mapper.provider.base; + +import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType; +import org.apache.ibatis.builder.annotation.ProviderContext; + +/** SQL for glossary edges and the per-metalake hierarchy mutex, including MySQL 5.7. */ +public final class GlossaryTermRelSQLProvider { + private static final String COLUMNS = + "r.metalake_id AS metalakeId, r.from_term_id AS fromTermId, " + + "r.to_term_id AS toTermId, r.rel_type AS relType, r.deleted_at AS deletedAt"; + private static final String KEY = + "metalake_id=#{metalakeId} AND from_term_id=#{fromId} AND to_term_id=#{toId} " + + "AND rel_type=#{type}"; + + private GlossaryTermRelSQLProvider() {} + + /** Creates the mutex row without aborting a concurrent first-use transaction. */ + public static String ensureLock(ProviderContext context) { + switch (JDBCBackendType.fromString(context.getDatabaseId())) { + case MYSQL: + return "INSERT INTO glossary_hierarchy_lock (metalake_id) VALUES (#{metalakeId}) " + + "ON DUPLICATE KEY UPDATE metalake_id=VALUES(metalake_id)"; + case POSTGRESQL: + return "INSERT INTO glossary_hierarchy_lock (metalake_id) VALUES (#{metalakeId}) " + + "ON CONFLICT (metalake_id) DO NOTHING"; + case H2: + return "MERGE INTO glossary_hierarchy_lock (metalake_id) KEY (metalake_id) " + + "VALUES (#{metalakeId})"; + default: + throw new UnsupportedOperationException( + "Unsupported glossary database: " + context.getDatabaseId()); + } + } + + /** Locks the mutex until the caller transaction ends. */ + public static String lockHierarchy() { + return "SELECT metalake_id FROM glossary_hierarchy_lock WHERE metalake_id=#{metalakeId} FOR UPDATE"; + } + + /** Locks one live endpoint after the metalake fence and hierarchy mutex. */ + public static String lockTerm() { + return "SELECT term_id FROM glossary_term_meta WHERE metalake_id=#{metalakeId} " + + "AND term_id=#{termId} AND deleted_at=0 FOR UPDATE"; + } + + /** Inserts an already canonicalized edge. */ + public static String insert() { + return "INSERT INTO glossary_term_rel (metalake_id, from_term_id, to_term_id, rel_type, deleted_at) " + + "VALUES (#{metalakeId}, #{fromId}, #{toId}, #{type}, 0)"; + } + + /** Tests whether a live edge already exists under the hierarchy mutex. */ + public static String exists() { + return "SELECT COUNT(*) FROM glossary_term_rel WHERE " + KEY + " AND deleted_at=0"; Review Comment: Reviewing the actual session path, SqlSessions.getSqlSession() explicitly opens READ_COMMITTED sessions on MySQL as well as H2 and PostgreSQL. The mutex acquisition clears the local MyBatis cache. In 42ad61e9bd, testPeerReplacementBeforeHierarchyLock starts with a plain read, lets a peer commit, then verifies duplicate add returns false. The existing fresh-read test also checks traversal after a peer commit. Both passed on H2. ########## core/src/main/java/org/apache/gravitino/storage/relational/service/GlossaryTermMetaService.java: ########## @@ -202,12 +202,14 @@ public boolean delete(NameIdentifier ident) { try { return transaction( ident.namespace(), + true, (mapper, metalakeId) -> { GlossaryTermPO row = require(mapper, ident, metalakeId); long deletedAt = Math.max(1, System.currentTimeMillis()); requireUpdated( mapper.delete(row.getTermId(), row.getCurrentVersion(), deletedAt), ident); mapper.deleteSynonyms(row.getTermId(), deletedAt); + GlossaryTermRelService.getInstance().removeTerm(metalakeId, row.getTermId()); Review Comment: Added the two-transaction regression in 42ad61e9bd. The reader opens its transaction before a peer removes and re-adds the edge. A future-dated retained tombstone forces term deletion to allocate exactly the next generation. It passed on H2 and runs in the MySQL and PostgreSQL lanes as well. The session uses READ_COMMITTED explicitly. ########## core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/GlossaryTermRelSQLProvider.java: ########## @@ -0,0 +1,133 @@ +/* + * 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.mapper.provider.base; + +import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType; +import org.apache.ibatis.builder.annotation.ProviderContext; + +/** SQL for glossary edges and the per-metalake hierarchy mutex, including MySQL 5.7. */ +public final class GlossaryTermRelSQLProvider { + private static final String COLUMNS = + "r.metalake_id AS metalakeId, r.from_term_id AS fromTermId, " + + "r.to_term_id AS toTermId, r.rel_type AS relType, r.deleted_at AS deletedAt"; + private static final String KEY = + "metalake_id=#{metalakeId} AND from_term_id=#{fromId} AND to_term_id=#{toId} " + + "AND rel_type=#{type}"; + + private GlossaryTermRelSQLProvider() {} + + /** Creates the mutex row without aborting a concurrent first-use transaction. */ + public static String ensureLock(ProviderContext context) { + switch (JDBCBackendType.fromString(context.getDatabaseId())) { + case MYSQL: + return "INSERT INTO glossary_hierarchy_lock (metalake_id) VALUES (#{metalakeId}) " + + "ON DUPLICATE KEY UPDATE metalake_id=VALUES(metalake_id)"; + case POSTGRESQL: + return "INSERT INTO glossary_hierarchy_lock (metalake_id) VALUES (#{metalakeId}) " + + "ON CONFLICT (metalake_id) DO NOTHING"; + case H2: + return "MERGE INTO glossary_hierarchy_lock (metalake_id) KEY (metalake_id) " + + "VALUES (#{metalakeId})"; + default: + throw new UnsupportedOperationException( + "Unsupported glossary database: " + context.getDatabaseId()); + } + } + + /** Locks the mutex until the caller transaction ends. */ + public static String lockHierarchy() { + return "SELECT metalake_id FROM glossary_hierarchy_lock WHERE metalake_id=#{metalakeId} FOR UPDATE"; + } + + /** Locks one live endpoint after the metalake fence and hierarchy mutex. */ + public static String lockTerm() { + return "SELECT term_id FROM glossary_term_meta WHERE metalake_id=#{metalakeId} " + + "AND term_id=#{termId} AND deleted_at=0 FOR UPDATE"; + } + + /** Inserts an already canonicalized edge. */ + public static String insert() { + return "INSERT INTO glossary_term_rel (metalake_id, from_term_id, to_term_id, rel_type, deleted_at) " + + "VALUES (#{metalakeId}, #{fromId}, #{toId}, #{type}, 0)"; + } + + /** Tests whether a live edge already exists under the hierarchy mutex. */ + public static String exists() { + return "SELECT COUNT(*) FROM glossary_term_rel WHERE " + KEY + " AND deleted_at=0"; + } + + /** Retires one live edge. The caller supplies a collision-free tombstone time. */ + public static String remove() { + return "UPDATE glossary_term_rel SET deleted_at=#{deletedAt} WHERE " + + KEY + + " AND deleted_at=0"; + } + + /** Reads the latest tombstone time under the metalake's hierarchy mutex or deletion fence. */ + public static String lastDeletedAt() { + return "SELECT COALESCE(MAX(deleted_at), 0) FROM glossary_term_rel WHERE metalake_id=#{metalakeId}"; Review Comment: The aggregate runs under the hierarchy mutex (or the exclusive parent fence for metalake deletion) in an explicitly READ_COMMITTED session. The mapper also flushes the local query cache. The regression in 42ad61e9bd verifies the final MAX advances past the peer’s committed tombstone even with an earlier read in the deleting transaction; H2 passed. -- 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]
