Skip to content

Commit

Permalink
(fix)[db] Fix create database and create table data race (#44600)
Browse files Browse the repository at this point in the history
There may be a race condition between CREATE DATABASE and CREATE TABLE.
When CREATE DATABASE is being executed, the edit log may get stuck,
while fullNameToDb in getDbNullable can still return the database. As a
result, the CREATE TABLE process continues, which may ultimately lead to
a strange order in the edit log of BDBJE, where CREATE TABLE appears
before CREATE DATABASE.
  • Loading branch information
deardeng authored Nov 27, 2024
1 parent f3e9d70 commit 5e54970
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class Database extends MetaObject implements Writable, DatabaseIf<Table>,
private final Map<Long, Table> idToTable;
private ConcurrentMap<String, Table> nameToTable;
// table name lower case -> table name
private final Map<String, String> lowerCaseToTableName;
private final ConcurrentMap<String, String> lowerCaseToTableName;

// user define function
@SerializedName(value = "name2Function")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,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();
Expand Down

0 comments on commit 5e54970

Please sign in to comment.