Skip to content

Commit

Permalink
[fix](multicatalog) make lastdbofcatalog a session variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Yulei-Yang committed Jul 15, 2024
1 parent ceead84 commit 4442420
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -5381,11 +5381,11 @@ public void changeCatalog(ConnectContext ctx, String catalogName) throws DdlExce
if (StringUtils.isNotEmpty(currentDB)) {
// When dropped the current catalog in current context, the current catalog will be null.
if (ctx.getCurrentCatalog() != null) {
catalogMgr.addLastDBOfCatalog(ctx.getCurrentCatalog().getName(), currentDB);
ConnectContext.get().addLastDBOfCatalog(ctx.getCurrentCatalog().getName(), currentDB);
}
}
ctx.changeDefaultCatalog(catalogName);
String lastDb = catalogMgr.getLastDB(catalogName);
String lastDb = ConnectContext.get().getLastDBOfCatalog(catalogName);
if (StringUtils.isNotEmpty(lastDb)) {
ctx.setDatabase(lastDb);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ public class CatalogMgr implements Writable, GsonPostProcessable {
private final Map<Long, CatalogIf<? extends DatabaseIf<? extends TableIf>>> 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 idToCatalog and nameToCatalog
Expand Down Expand Up @@ -140,7 +138,7 @@ private CatalogIf removeCatalog(long catalogId) {
if (catalog != null) {
catalog.onClose();
nameToCatalog.remove(catalog.getName());
lastDBOfCatalog.remove(catalog.getName());
ConnectContext.get().removeLastDBOfCatalog(catalog.getName());
Env.getCurrentEnv().getExtMetaCacheMgr().removeCache(catalog.getId());
if (!Strings.isNullOrEmpty(catalog.getResource())) {
Resource catalogResource = Env.getCurrentEnv().getResourceMgr().getResource(catalog.getResource());
Expand Down Expand Up @@ -194,14 +192,6 @@ public CatalogIf getCatalogOrAnalysisException(String name) throws AnalysisExcep
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());
}
Expand Down Expand Up @@ -282,7 +272,7 @@ public void dropCatalog(DropCatalogStmt stmt) throws UserException {
replayDropCatalog(log);
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_DROP_CATALOG, log);

lastDBOfCatalog.remove(stmt.getCatalogName());
ConnectContext.get().removeLastDBOfCatalog(stmt.getCatalogName());
Env.getCurrentEnv().getQueryStats().clear(catalog.getId());
} finally {
writeUnlock();
Expand All @@ -306,10 +296,11 @@ public void alterCatalogName(AlterCatalogNameStmt stmt) throws UserException {
replayAlterCatalogName(log);
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_ALTER_CATALOG_NAME, log);

String db = lastDBOfCatalog.get(stmt.getCatalogName());
ConnectContext ctx = ConnectContext.get();
String db = ctx.getLastDBOfCatalog(stmt.getCatalogName());
if (db != null) {
lastDBOfCatalog.remove(stmt.getCatalogName());
lastDBOfCatalog.put(log.getNewCatalogName(), db);
ctx.removeLastDBOfCatalog(stmt.getCatalogName());
ctx.addLastDBOfCatalog(log.getNewCatalogName(), db);
}
} finally {
writeUnlock();
Expand Down
15 changes: 15 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ public enum ConnectType {
protected String defaultCatalog = InternalCatalog.INTERNAL_CATALOG_NAME;
protected boolean isSend;

// record last used database of every catalog
private final Map<String, String> lastDBOfCatalog = Maps.newConcurrentMap();

protected AuditEventBuilder auditEventBuilder = new AuditEventBuilder();

protected String remoteIP;
Expand Down Expand Up @@ -328,6 +331,18 @@ public boolean isSend() {
return this.isSend;
}

public void addLastDBOfCatalog(String catalog, String db) {
lastDBOfCatalog.put(catalog, db);
}

public String getLastDBOfCatalog(String catalog) {
return lastDBOfCatalog.get(catalog);
}

public String removeLastDBOfCatalog(String catalog) {
return lastDBOfCatalog.get(catalog);
}

public void setNotEvalNondeterministicFunction(boolean notEvalNondeterministicFunction) {
this.notEvalNondeterministicFunction = notEvalNondeterministicFunction;
}
Expand Down

0 comments on commit 4442420

Please sign in to comment.