This is an automated email from the ASF dual-hosted git repository. morningman pushed a commit to branch branch-1.2-lts in repository https://gitbox.apache.org/repos/asf/doris.git
commit efa4f525397427ab8cc9ac788dca2b9abe22d830 Author: Yulei-Yang <yulei.yang0...@gmail.com> AuthorDate: Thu Dec 8 10:32:30 2022 +0800 [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 --- .../src/main/java/org/apache/doris/catalog/Env.java | 9 +++++++++ .../java/org/apache/doris/datasource/CatalogMgr.java | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java index df08131e42..7c0e261609 100755 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java @@ -4439,7 +4439,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); diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java index e1fbb55062..ecbcdf8790 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java @@ -74,6 +74,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 @@ -103,6 +105,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; @@ -144,6 +147,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()); } @@ -239,6 +250,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(); } @@ -260,6 +273,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(); } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org