Skip to content

Commit d146ebf

Browse files
authored
[fix](multicatalog) make lastdbofcatalog a session variable (#38117)
bp #37828
1 parent 9dc67a2 commit d146ebf

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4466,7 +4466,7 @@ public void cancelAlterCluster(CancelAlterSystemStmt stmt) throws DdlException {
44664466
this.alter.getClusterHandler().cancel(stmt);
44674467
}
44684468

4469-
// Switch catalog of this sesseion.
4469+
// Switch catalog of this session.
44704470
public void changeCatalog(ConnectContext ctx, String catalogName) throws DdlException {
44714471
CatalogIf catalogIf = catalogMgr.getCatalogNullable(catalogName);
44724472
if (catalogIf == null) {
@@ -4478,11 +4478,11 @@ public void changeCatalog(ConnectContext ctx, String catalogName) throws DdlExce
44784478
if (StringUtils.isNotEmpty(currentDB)) {
44794479
// When dropped the current catalog in current context, the current catalog will be null.
44804480
if (ctx.getCurrentCatalog() != null) {
4481-
catalogMgr.addLastDBOfCatalog(ctx.getCurrentCatalog().getName(), currentDB);
4481+
ctx.addLastDBOfCatalog(ctx.getCurrentCatalog().getName(), currentDB);
44824482
}
44834483
}
44844484
ctx.changeDefaultCatalog(catalogName);
4485-
String lastDb = catalogMgr.getLastDB(catalogName);
4485+
String lastDb = ctx.getLastDBOfCatalog(catalogName);
44864486
if (StringUtils.isNotEmpty(lastDb)) {
44874487
ctx.setDatabase(lastDb);
44884488
}

fe/fe-core/src/main/java/org/apache/doris/datasource/CatalogMgr.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ public class CatalogMgr implements Writable, GsonPostProcessable {
8484
private final Map<Long, CatalogIf> idToCatalog = Maps.newConcurrentMap();
8585
// this map will be regenerated from idToCatalog, so not need to persist.
8686
private final Map<String, CatalogIf> nameToCatalog = Maps.newConcurrentMap();
87-
// record last used database of every catalog
88-
private final Map<String, String> lastDBOfCatalog = Maps.newConcurrentMap();
8987

9088
// Use a separate instance to facilitate access.
9189
// internalDataSource still exists in idToDataSource and nameToDataSource
@@ -119,7 +117,9 @@ private CatalogIf removeCatalog(long catalogId) {
119117
if (catalog != null) {
120118
catalog.onClose();
121119
nameToCatalog.remove(catalog.getName());
122-
lastDBOfCatalog.remove(catalog.getName());
120+
if (ConnectContext.get() != null) {
121+
ConnectContext.get().removeLastDBOfCatalog(catalog.getName());
122+
}
123123
Env.getCurrentEnv().getExtMetaCacheMgr().removeCache(catalog.getId());
124124
if (!Strings.isNullOrEmpty(catalog.getResource())) {
125125
Resource catalogResource = Env.getCurrentEnv().getResourceMgr().getResource(catalog.getResource());
@@ -167,14 +167,6 @@ public CatalogIf getCatalogOrAnalysisException(String name) throws AnalysisExcep
167167
ErrorCode.ERR_UNKNOWN_CATALOG));
168168
}
169169

170-
public void addLastDBOfCatalog(String catalog, String db) {
171-
lastDBOfCatalog.put(catalog, db);
172-
}
173-
174-
public String getLastDB(String catalog) {
175-
return lastDBOfCatalog.get(catalog);
176-
}
177-
178170
public List<Long> getCatalogIds() {
179171
return Lists.newArrayList(idToCatalog.keySet());
180172
}
@@ -274,7 +266,9 @@ public void dropCatalog(DropCatalogStmt stmt) throws UserException {
274266
replayDropCatalog(log);
275267
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_DROP_CATALOG, log);
276268

277-
lastDBOfCatalog.remove(stmt.getCatalogName());
269+
if (ConnectContext.get() != null) {
270+
ConnectContext.get().removeLastDBOfCatalog(stmt.getCatalogName());
271+
}
278272
} finally {
279273
writeUnlock();
280274
}
@@ -297,10 +291,13 @@ public void alterCatalogName(AlterCatalogNameStmt stmt) throws UserException {
297291
replayAlterCatalogName(log);
298292
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_ALTER_CATALOG_NAME, log);
299293

300-
String db = lastDBOfCatalog.get(stmt.getCatalogName());
301-
if (db != null) {
302-
lastDBOfCatalog.remove(stmt.getCatalogName());
303-
lastDBOfCatalog.put(log.getNewCatalogName(), db);
294+
ConnectContext ctx = ConnectContext.get();
295+
if (ctx != null) {
296+
String db = ctx.getLastDBOfCatalog(stmt.getCatalogName());
297+
if (db != null) {
298+
ctx.removeLastDBOfCatalog(stmt.getCatalogName());
299+
ctx.addLastDBOfCatalog(log.getNewCatalogName(), db);
300+
}
304301
}
305302
} finally {
306303
writeUnlock();

fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import com.google.common.base.Strings;
4444
import com.google.common.collect.Lists;
45+
import com.google.common.collect.Maps;
4546
import com.google.common.collect.Sets;
4647
import io.opentelemetry.api.trace.Tracer;
4748
import org.apache.logging.log4j.LogManager;
@@ -50,6 +51,7 @@
5051

5152
import java.io.IOException;
5253
import java.util.List;
54+
import java.util.Map;
5355
import java.util.Optional;
5456
import java.util.Set;
5557

@@ -120,6 +122,9 @@ public class ConnectContext {
120122
protected String defaultCatalog = InternalCatalog.INTERNAL_CATALOG_NAME;
121123
protected boolean isSend;
122124

125+
// record last used database of every catalog
126+
private final Map<String, String> lastDBOfCatalog = Maps.newConcurrentMap();
127+
123128
protected AuditEventBuilder auditEventBuilder = new AuditEventBuilder();
124129

125130
protected String remoteIP;
@@ -197,6 +202,18 @@ public boolean isSend() {
197202
return this.isSend;
198203
}
199204

205+
public void addLastDBOfCatalog(String catalog, String db) {
206+
lastDBOfCatalog.put(catalog, db);
207+
}
208+
209+
public String getLastDBOfCatalog(String catalog) {
210+
return lastDBOfCatalog.get(catalog);
211+
}
212+
213+
public String removeLastDBOfCatalog(String catalog) {
214+
return lastDBOfCatalog.get(catalog);
215+
}
216+
200217
public void setNotEvalNondeterministicFunction(boolean notEvalNondeterministicFunction) {
201218
this.notEvalNondeterministicFunction = notEvalNondeterministicFunction;
202219
}

regression-test/suites/external_table_emr_p2/hive/test_external_catalog_hive.groovy

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,23 @@ suite("test_external_catalog_hive", "p2") {
104104
sql """alter catalog ${catalog_name} rename hms;"""
105105

106106
sql """switch hms;"""
107-
108-
def res3 = sql """select count(*) from test.hive_test limit 10;"""
107+
sql """use test;"""
108+
def res3 = sql """select count(*) from hive_test limit 10;"""
109109
logger.info("recoding select: " + res3.toString())
110110

111+
def user = 'account_user_test'
112+
def pwd = 'C123_567p'
113+
try_sql("DROP USER ${user}")
114+
sql """CREATE USER '${user}' IDENTIFIED BY '${pwd}'"""
115+
sql """GRANT SELECT_PRIV on *.*.* to '${user}'"""
116+
connect(user=user, password="${pwd}", url=context.config.jdbcUrl) {
117+
sql """switch hms;"""
118+
test {
119+
sql "show tables"
120+
exception "errCode = 2, detailMessage = No database selected"
121+
}
122+
}
123+
111124
sql """alter catalog hms rename ${catalog_name};"""
112125
}
113126
}

0 commit comments

Comments
 (0)