nooneuse commented on code in PR #66307: URL: https://github.com/apache/doris/pull/66307#discussion_r3796030955
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ConstraintCommandUtils.java: ########## @@ -0,0 +1,355 @@ +// 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.doris.nereids.trees.plans.commands; + +import org.apache.doris.catalog.DatabaseIf; +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.TableIf; +import org.apache.doris.catalog.info.TableNameInfo; +import org.apache.doris.common.DdlException; +import org.apache.doris.common.util.MetaLockUtils; +import org.apache.doris.datasource.CatalogIf; +import org.apache.doris.datasource.ExternalCatalog; +import org.apache.doris.datasource.ExternalDatabase; +import org.apache.doris.info.TableNameInfoUtils; +import org.apache.doris.qe.ConnectContext; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashSet; +import java.util.IdentityHashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** Shared locking helpers for constraint DDL commands. */ +final class ConstraintCommandUtils { + private ConstraintCommandUtils() { + } + + static ExternalCatalogSnapshots snapshotExternalCatalogs(List<TableNameInfo> tableNameInfos) + throws DdlException { + Map<Long, ExternalCatalogSnapshot> snapshots = new LinkedHashMap<>(); + for (TableNameInfo tableNameInfo : tableNameInfos) { + CatalogIf<?> catalog = Env.getCurrentEnv().getCatalogMgr() + .getCatalogOrDdlException(tableNameInfo.getCtl()); + if (catalog instanceof ExternalCatalog) { + ExternalCatalog externalCatalog = (ExternalCatalog) catalog; + snapshots.putIfAbsent(externalCatalog.getId(), + new ExternalCatalogSnapshot(tableNameInfo.getCtl(), externalCatalog, + externalCatalog.snapshotConstraintMetadata())); + } + } + return new ExternalCatalogSnapshots(snapshots); + } + + /** Lock external catalog fences and internal databases referenced by a constraint. */ + static LockedDatabases lockCurrentDatabases(List<TableNameInfo> tableNameInfos, + ExternalCatalogSnapshots externalCatalogSnapshots, List<TableIf> analyzedTables) + throws DdlException { + Map<String, TableIf> analyzedExternalTables = new LinkedHashMap<>(); + for (TableIf table : analyzedTables) { + if (table != null + && table.getDatabase().getCatalog() instanceof ExternalCatalog) { + TableNameInfo tableNameInfo = TableNameInfoUtils.fromCatalogDb( + table.getDatabase().getCatalog(), table.getDatabase(), table); + analyzedExternalTables.put(tableKey(tableNameInfo), table); + } + } + LockedExternalCatalogs lockedExternalCatalogs = externalCatalogSnapshots.lock(); + Map<String, ResolvedDatabase> resolvedByName = new LinkedHashMap<>(); + LockedDatabases lockedDatabases = null; + try { + for (TableNameInfo tableNameInfo : tableNameInfos) { + String databaseKey = databaseKey(tableNameInfo); + if (!resolvedByName.containsKey(databaseKey)) { + CatalogIf<? extends DatabaseIf<? extends TableIf>> catalog = Env.getCurrentEnv() + .getCatalogMgr().getCatalogOrDdlException(tableNameInfo.getCtl()); + if (catalog instanceof ExternalCatalog) { + externalCatalogSnapshots.requireSame(tableNameInfo.getCtl(), catalog); + continue; + } + DatabaseIf<? extends TableIf> database = + catalog.getDbOrDdlException(tableNameInfo.getDb()); + resolvedByName.put(databaseKey, + new ResolvedDatabase(databaseKey, tableNameInfo, catalog, database)); + } + } + Map<String, TableIf> resolvedTables = new LinkedHashMap<>(analyzedExternalTables); + for (TableNameInfo tableNameInfo : tableNameInfos) { + ResolvedDatabase resolvedDatabase = resolvedByName.get(databaseKey(tableNameInfo)); + if (resolvedDatabase != null) { + resolvedTables.put(tableKey(tableNameInfo), + resolvedDatabase.database.getTableNullable(tableNameInfo.getTbl())); + } + } + List<ResolvedDatabase> lockOrder = new ArrayList<>(resolvedByName.values()); + lockOrder.sort(Comparator + .comparingLong((ResolvedDatabase resolved) -> resolved.database.getId()) + .thenComparing(resolved -> resolved.databaseKey)); + for (ResolvedDatabase resolved : lockOrder) { + resolved.database.readLock(); + } + lockedDatabases = new LockedDatabases( + resolvedByName, resolvedTables, lockOrder, lockedExternalCatalogs); + for (ResolvedDatabase resolved : lockOrder) { + if (Env.getCurrentEnv().getCatalogMgr().getCatalog( + resolved.tableNameInfo.getCtl()) != resolved.catalog + || resolved.catalog.getDbNullable(resolved.tableNameInfo.getDb()) + != resolved.database) { + throw new DdlException( + "Database changed while altering constraint on " + + resolved.tableNameInfo); + } + } + return lockedDatabases; + } catch (DdlException | RuntimeException e) { + if (lockedDatabases == null) { + lockedExternalCatalogs.close(); + } else { + lockedDatabases.close(); + } + throw e; + } + } + + /** Lock all currently resolved tables in the same deterministic order used by constraint ADD and DROP. */ + static LockedTables lockCurrentTables( + LockedDatabases lockedDatabases, List<TableNameInfo> tableNameInfos) + throws DdlException { + return lockCurrentTables(lockedDatabases, tableNameInfos, true); + } + + private static LockedTables lockCurrentTables( + LockedDatabases lockedDatabases, List<TableNameInfo> tableNameInfos, + boolean requireAllTables) throws DdlException { + Map<String, TableIf> tablesByName = new LinkedHashMap<>(); + Map<TableIf, Boolean> seenTables = new IdentityHashMap<>(); + List<TableIf> lockOrder = new ArrayList<>(); + for (TableNameInfo tableNameInfo : tableNameInfos) { + TableIf table = lockedDatabases.getCurrentTable(tableNameInfo); + if (table == null && requireAllTables) { + throw new DdlException("Table changed while altering constraint on " + tableNameInfo); + } + tablesByName.put(tableKey(tableNameInfo), table); + if (table != null + && !(table.getDatabase() instanceof ExternalDatabase) + && seenTables.put(table, Boolean.TRUE) == null) { + lockOrder.add(table); + } + } + lockOrder.sort(Comparator Review Comment: right. fixed. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
