This is an automated email from the ASF dual-hosted git repository.

morrysnow pushed a commit to branch branch-3.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-3.1 by this push:
     new 609aad37297 branch-3.1: [fix](database) Fix rename db and create table 
race #55054 (#55991)
609aad37297 is described below

commit 609aad37297cdf982ce1edff30023c7edeadc6a0
Author: deardeng <[email protected]>
AuthorDate: Fri Sep 19 14:14:28 2025 +0800

    branch-3.1: [fix](database) Fix rename db and create table race #55054 
(#55991)
    
    picked from #55054
---
 .../java/org/apache/doris/catalog/Database.java    | 60 ++++++++++++++--------
 .../apache/doris/datasource/InternalCatalog.java   | 44 ++++++++++------
 2 files changed, 67 insertions(+), 37 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java 
b/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java
index 4bed1800162..15b8047c637 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java
@@ -251,19 +251,24 @@ public class Database extends MetaObject implements 
Writable, DatabaseIf<Table>,
         return strs.length == 2 ? strs[1] : strs[0];
     }
 
+    public void setNameWithoutLock(String newName) {
+        // ClusterNamespace.getNameFromFullName should be removed in 3.0
+        this.fullQualifiedName = ClusterNamespace.getNameFromFullName(newName);
+        for (Table table : idToTable.values()) {
+            table.setQualifiedDbName(fullQualifiedName);
+        }
+    }
+
     public void setNameWithLock(String newName) {
         writeLock();
         try {
-            // ClusterNamespace.getNameFromFullName should be removed in 3.0
-            this.fullQualifiedName = 
ClusterNamespace.getNameFromFullName(newName);
-            for (Table table : idToTable.values()) {
-                table.setQualifiedDbName(fullQualifiedName);
-            }
+            setNameWithoutLock(newName);
         } finally {
             writeUnlock();
         }
     }
 
+
     public void setDataQuota(long newQuota) {
         Preconditions.checkArgument(newQuota >= 0L);
         LOG.info("database[{}] set quota from {} to {}", fullQualifiedName, 
dataQuotaBytes, newQuota);
@@ -407,38 +412,49 @@ public class Database extends MetaObject implements 
Writable, DatabaseIf<Table>,
         return nameToTable.containsKey(tableName);
     }
 
-    // return pair <success?, table exist?>
     public Pair<Boolean, Boolean> createTableWithLock(
             Table table, boolean isReplay, boolean setIfNotExist) throws 
DdlException {
+        writeLockOrDdlException();
+        try {
+            return createTableWithoutLock(table, isReplay, setIfNotExist);
+        } finally {
+            writeUnlock();
+        }
+    }
+
+    // return pair <success?, table exist?>
+    // caller must hold db lock
+    public Pair<Boolean, Boolean> createTableWithoutLock(
+            Table table, boolean isReplay, boolean setIfNotExist) throws 
DdlException {
         boolean result = true;
         // if a table is already exists, then edit log won't be executed
         // some caller of this method may need to know this message
         boolean isTableExist = false;
         table.setQualifiedDbName(fullQualifiedName);
-        writeLockOrDdlException();
-        try {
-            String tableName = table.getName();
-            if (Env.isStoredTableNamesLowerCase()) {
-                tableName = tableName.toLowerCase();
-            }
-            if (isTableExist(tableName)) {
-                result = setIfNotExist;
-                isTableExist = true;
-            } else {
+        String tableName = table.getName();
+        if (Env.isStoredTableNamesLowerCase()) {
+            tableName = tableName.toLowerCase();
+        }
+        if (isTableExist(tableName)) {
+            result = setIfNotExist;
+            isTableExist = true;
+        } else {
+            table.writeLock();
+            try {
                 registerTable(table);
                 if (!isReplay) {
                     // Write edit log
                     CreateTableInfo info = new 
CreateTableInfo(fullQualifiedName, id, table);
                     Env.getCurrentEnv().getEditLog().logCreateTable(info);
                 }
-                if (table.getType() == TableType.ELASTICSEARCH) {
-                    
Env.getCurrentEnv().getEsRepository().registerTable((EsTable) table);
-                }
+            } finally {
+                table.writeUnlock();
+            }
+            if (table.getType() == TableType.ELASTICSEARCH) {
+                Env.getCurrentEnv().getEsRepository().registerTable((EsTable) 
table);
             }
-            return Pair.of(result, isTableExist);
-        } finally {
-            writeUnlock();
         }
+        return Pair.of(result, isTableExist);
     }
 
     @Override
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java 
b/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
index 612681f9013..34fec04ff72 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java
@@ -656,12 +656,7 @@ public class InternalCatalog implements 
CatalogIf<Database> {
                 }
             }
             if (!Strings.isNullOrEmpty(newDbName)) {
-                try {
-                    db.writeUnlock();
-                    db.setNameWithLock(newDbName);
-                } finally {
-                    db.writeLock();
-                }
+                db.setNameWithLock(newDbName);
             }
             fullNameToDb.put(db.getFullName(), db);
             idToDb.put(db.getId(), db);
@@ -840,6 +835,7 @@ public class InternalCatalog implements CatalogIf<Database> 
{
         }
 
         Database db = null;
+        // catalog lock
         if (!tryLock(false)) {
             throw new DdlException("Failed to acquire catalog lock. Try 
again");
         }
@@ -853,16 +849,24 @@ public class InternalCatalog implements 
CatalogIf<Database> {
             if (fullNameToDb.get(newFullDbName) != null) {
                 throw new DdlException("Database name[" + newFullDbName + "] 
is already used");
             }
-            // 1. rename db
-            db.setNameWithLock(newFullDbName);
+            // db lock
+            db.writeLock();
+            try {
+                // 1. rename db
+                db.setNameWithoutLock(newFullDbName);
 
-            // 2. add to meta. check again
-            fullNameToDb.remove(fullDbName);
-            fullNameToDb.put(newFullDbName, db);
+                // 2. add to meta. check again
+                fullNameToDb.remove(fullDbName);
+                fullNameToDb.put(newFullDbName, db);
 
-            DatabaseInfo dbInfo = new DatabaseInfo(fullDbName, newFullDbName, 
-1L, QuotaType.NONE);
-            Env.getCurrentEnv().getEditLog().logDatabaseRename(dbInfo);
+                DatabaseInfo dbInfo = new DatabaseInfo(fullDbName, 
newFullDbName, -1L, QuotaType.NONE);
+                Env.getCurrentEnv().getEditLog().logDatabaseRename(dbInfo);
+            } finally {
+                // db lock
+                db.writeUnlock();
+            }
         } finally {
+            // catalog lock
             unlock();
         }
 
@@ -3283,8 +3287,18 @@ public class InternalCatalog implements 
CatalogIf<Database> {
             } else {
                 throw new DdlException("Unsupported partition method: " + 
partitionInfo.getType().name());
             }
-
-            Pair<Boolean, Boolean> result = db.createTableWithLock(olapTable, 
false, stmt.isSetIfNotExists());
+            Pair<Boolean, Boolean> result;
+            db.writeLockOrDdlException();
+            try {
+                // db name not changed
+                if 
(!db.getName().equals(ClusterNamespace.getNameFromFullName(stmt.getDbName()))) {
+                    throw new DdlException("Database name renamed, please 
check the database name");
+                }
+                // register table, write create table edit log
+                result = db.createTableWithoutLock(olapTable, false, 
stmt.isSetIfNotExists());
+            } finally {
+                db.writeUnlock();
+            }
             if (!result.first) {
                 
ErrorReport.reportDdlException(ErrorCode.ERR_TABLE_EXISTS_ERROR, tableShowName);
             }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to