Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 38 additions & 22 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,24 @@ public String getName() {
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);
Expand Down Expand Up @@ -388,38 +393,49 @@ public boolean isTableExist(String tableName) {
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, 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,7 @@ public void recoverDatabase(RecoverDbStmt recoverStmt) throws DdlException {
}
}
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);
Expand Down Expand Up @@ -837,6 +832,7 @@ public void renameDatabase(AlterDatabaseRename stmt) throws DdlException {
}

Database db = null;
// catalog lock
if (!tryLock(false)) {
throw new DdlException("Failed to acquire catalog lock. Try again");
}
Expand All @@ -850,16 +846,24 @@ public void renameDatabase(AlterDatabaseRename stmt) throws DdlException {
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();
}

Expand Down Expand Up @@ -3198,8 +3202,18 @@ private boolean createOlapTable(Database db, CreateTableStmt stmt) throws UserEx
} 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, tableName);
}
Expand Down