nooneuse commented on code in PR #66307:
URL: https://github.com/apache/doris/pull/66307#discussion_r3775259978


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ConstraintCommandUtils.java:
##########
@@ -0,0 +1,200 @@
+// 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 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() {
+    }
+
+    /** Lock all databases referenced by a constraint in a deterministic 
order. */
+    static LockedDatabases lockCurrentDatabases(List<TableNameInfo> 
tableNameInfos)
+            throws DdlException {
+        Map<String, ResolvedDatabase> resolvedByName = new LinkedHashMap<>();
+        for (TableNameInfo tableNameInfo : tableNameInfos) {
+            String databaseKey = databaseKey(tableNameInfo);
+            if (!resolvedByName.containsKey(databaseKey)) {
+                CatalogIf<? extends DatabaseIf<? extends TableIf>> catalog = 
Env.getCurrentEnv()
+                        
.getCatalogMgr().getCatalogOrDdlException(tableNameInfo.getCtl());
+                DatabaseIf<? extends TableIf> database =
+                        catalog.getDbOrDdlException(tableNameInfo.getDb());
+                resolvedByName.put(databaseKey,
+                        new ResolvedDatabase(databaseKey, tableNameInfo, 
catalog, database));
+            }
+        }
+        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 lockedDatabases = new LockedDatabases(resolvedByName, 
lockOrder);
+        try {
+            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) {
+            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.get(tableNameInfo)

Review Comment:
   okay 



-- 
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]

Reply via email to