From 9533bd3e8b094f7bf2bfc199d001f2a19012a971 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 29 Nov 2024 20:50:10 +0800 Subject: [PATCH] branch-2.1: (fix)[db] Fix create database and create table data race #44600 (#44683) Cherry-picked from #44600 Co-authored-by: deardeng --- .../java/org/apache/doris/catalog/Database.java | 2 +- .../apache/doris/datasource/InternalCatalog.java | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 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 df01f6562dee60..50a0367dfec99a 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 @@ -91,7 +91,7 @@ public class Database extends MetaObject implements Writable, DatabaseIf @SerializedName(value = "nameToTable") private Map nameToTable; // table name lower case -> table name - private final Map lowerCaseToTableName; + private final ConcurrentMap lowerCaseToTableName; // user define function @SerializedName(value = "name2Function") 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 aab70f86733964..70a9cffe78d53f 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 @@ -432,8 +432,17 @@ public void createDb(CreateDbStmt stmt) throws DdlException { ErrorReport.reportDdlException(ErrorCode.ERR_DB_CREATE_EXISTS, fullDbName); } } else { - unprotectCreateDb(db); - Env.getCurrentEnv().getEditLog().logCreateDb(db); + if (!db.tryWriteLock(100, TimeUnit.SECONDS)) { + LOG.warn("try lock failed, create database failed {}", fullDbName); + ErrorReport.reportDdlException(ErrorCode.ERR_EXECUTE_TIMEOUT, + "create database " + fullDbName + " time out"); + } + try { + unprotectCreateDb(db); + Env.getCurrentEnv().getEditLog().logCreateDb(db); + } finally { + db.writeUnlock(); + } } } finally { unlock();