[fix](multi-catalog) use last used database for catalog when switch back (#14793)

remember last used database of every catalog and use it when switch back
This commit is contained in:
Yulei-Yang
2022-12-08 10:32:30 +08:00
committed by GitHub
parent 962810b973
commit 27c8147a2b
4 changed files with 47 additions and 1 deletions

View File

@ -4410,7 +4410,16 @@ public class Env {
throw new DdlException(ErrorCode.ERR_UNKNOWN_CATALOG.formatErrorMsg(catalogName),
ErrorCode.ERR_UNKNOWN_CATALOG);
}
String currentDB = ctx.getDatabase();
if (StringUtils.isNotEmpty(currentDB)) {
catalogMgr.addLastDBOfCatalog(ctx.getCurrentCatalog().getName(), currentDB);
}
ctx.changeDefaultCatalog(catalogName);
String lastDb = catalogMgr.getLastDB(catalogName);
if (StringUtils.isNotEmpty(lastDb)) {
ctx.setDatabase(lastDb);
}
if (catalogIf instanceof EsExternalCatalog) {
ctx.setDatabase(SystemInfoService.DEFAULT_CLUSTER + ClusterNamespace.CLUSTER_DELIMITER
+ EsExternalCatalog.DEFAULT_DB);

View File

@ -76,6 +76,8 @@ public class CatalogMgr implements Writable, GsonPostProcessable {
private final Map<Long, CatalogIf> idToCatalog = Maps.newConcurrentMap();
// this map will be regenerated from idToCatalog, so not need to persist.
private final Map<String, CatalogIf> nameToCatalog = Maps.newConcurrentMap();
// record last used database of every catalog
private final Map<String, String> lastDBOfCatalog = Maps.newConcurrentMap();
// Use a separate instance to facilitate access.
// internalDataSource still exists in idToDataSource and nameToDataSource
@ -105,6 +107,7 @@ public class CatalogMgr implements Writable, GsonPostProcessable {
if (catalog != null) {
catalog.onClose();
nameToCatalog.remove(catalog.getName());
lastDBOfCatalog.remove(catalog.getName());
Env.getCurrentEnv().getExtMetaCacheMgr().removeCache(catalog.getName());
}
return catalog;
@ -146,6 +149,14 @@ public class CatalogMgr implements Writable, GsonPostProcessable {
ErrorCode.ERR_UNKNOWN_CATALOG));
}
public void addLastDBOfCatalog(String catalog, String db) {
lastDBOfCatalog.put(catalog, db);
}
public String getLastDB(String catalog) {
return lastDBOfCatalog.get(catalog);
}
public List<Long> getCatalogIds() {
return Lists.newArrayList(idToCatalog.keySet());
}
@ -241,6 +252,8 @@ public class CatalogMgr implements Writable, GsonPostProcessable {
CatalogLog log = CatalogFactory.constructorCatalogLog(catalog.getId(), stmt);
replayDropCatalog(log);
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_DROP_CATALOG, log);
lastDBOfCatalog.remove(stmt.getCatalogName());
} finally {
writeUnlock();
}
@ -262,6 +275,12 @@ public class CatalogMgr implements Writable, GsonPostProcessable {
CatalogLog log = CatalogFactory.constructorCatalogLog(catalog.getId(), stmt);
replayAlterCatalogName(log);
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_ALTER_CATALOG_NAME, log);
String db = lastDBOfCatalog.get(stmt.getCatalogName());
if (db != null) {
lastDBOfCatalog.remove(stmt.getCatalogName());
lastDBOfCatalog.put(log.getNewCatalogName(), db);
}
} finally {
writeUnlock();
}

View File

@ -89,6 +89,25 @@ suite("test_external_catalog_hive", "p2") {
sql """ use tpch_1000_orc; """
q03()
// test remember last used database after switch / rename catalog
sql """switch ${catalog_name};"""
sql """use test;"""
def res2 = sql """select count(*) from hive_test limit 10;"""
logger.info("recoding select: " + res2.toString())
sql """switch internal;"""
sql """alter catalog ${catalog_name} rename hms;"""
sql """switch hms;"""
def res3 = sql """select count(*) from hive_test limit 10;"""
logger.info("recoding select: " + res3.toString())
sql """alter catalog hms rename ${catalog_name};"""
} finally {
// sql """admin set frontend config ("enable_multi_catalog" = "false")"""
}

View File

@ -56,4 +56,3 @@ suite("test_external_hive", "p2") {