This is an automated email from the ASF dual-hosted git repository.

lide pushed a commit to branch branch-1.2-lts
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-1.2-lts by this push:
     new d146ebf0dc6 [fix](multicatalog) make lastdbofcatalog a session 
variable (#38117)
d146ebf0dc6 is described below

commit d146ebf0dc6df9355dcbdfaff85983def51fcac0
Author: Yulei-Yang <yulei.yang0...@gmail.com>
AuthorDate: Tue Jul 23 16:45:10 2024 +0800

    [fix](multicatalog) make lastdbofcatalog a session variable (#38117)
    
    bp #37828
---
 .../main/java/org/apache/doris/catalog/Env.java    |  6 ++---
 .../org/apache/doris/datasource/CatalogMgr.java    | 29 ++++++++++------------
 .../java/org/apache/doris/qe/ConnectContext.java   | 17 +++++++++++++
 .../hive/test_external_catalog_hive.groovy         | 17 +++++++++++--
 4 files changed, 48 insertions(+), 21 deletions(-)

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 d6ce141ca0a..c4f0bed4dd6 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
@@ -4466,7 +4466,7 @@ public class Env {
         this.alter.getClusterHandler().cancel(stmt);
     }
 
-    // Switch catalog of this sesseion.
+    // Switch catalog of this session.
     public void changeCatalog(ConnectContext ctx, String catalogName) throws 
DdlException {
         CatalogIf catalogIf = catalogMgr.getCatalogNullable(catalogName);
         if (catalogIf == null) {
@@ -4478,11 +4478,11 @@ public class Env {
         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);
+                ctx.addLastDBOfCatalog(ctx.getCurrentCatalog().getName(), 
currentDB);
             }
         }
         ctx.changeDefaultCatalog(catalogName);
-        String lastDb = catalogMgr.getLastDB(catalogName);
+        String lastDb = ctx.getLastDBOfCatalog(catalogName);
         if (StringUtils.isNotEmpty(lastDb)) {
             ctx.setDatabase(lastDb);
         }
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 d4e89732ea8..8671f1f9f12 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
@@ -84,8 +84,6 @@ 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
@@ -119,7 +117,9 @@ public class CatalogMgr implements Writable, 
GsonPostProcessable {
         if (catalog != null) {
             catalog.onClose();
             nameToCatalog.remove(catalog.getName());
-            lastDBOfCatalog.remove(catalog.getName());
+            if (ConnectContext.get() != null) {
+                ConnectContext.get().removeLastDBOfCatalog(catalog.getName());
+            }
             
Env.getCurrentEnv().getExtMetaCacheMgr().removeCache(catalog.getId());
             if (!Strings.isNullOrEmpty(catalog.getResource())) {
                 Resource catalogResource = 
Env.getCurrentEnv().getResourceMgr().getResource(catalog.getResource());
@@ -167,14 +167,6 @@ 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());
     }
@@ -274,7 +266,9 @@ public class CatalogMgr implements Writable, 
GsonPostProcessable {
             replayDropCatalog(log);
             
Env.getCurrentEnv().getEditLog().logCatalogLog(OperationType.OP_DROP_CATALOG, 
log);
 
-            lastDBOfCatalog.remove(stmt.getCatalogName());
+            if (ConnectContext.get() != null) {
+                
ConnectContext.get().removeLastDBOfCatalog(stmt.getCatalogName());
+            }
         } finally {
             writeUnlock();
         }
@@ -297,10 +291,13 @@ public class CatalogMgr implements Writable, 
GsonPostProcessable {
             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);
+            ConnectContext ctx = ConnectContext.get();
+            if (ctx != null) {
+                String db = ctx.getLastDBOfCatalog(stmt.getCatalogName());
+                if (db != null) {
+                    ctx.removeLastDBOfCatalog(stmt.getCatalogName());
+                    ctx.addLastDBOfCatalog(log.getNewCatalogName(), db);
+                }
             }
         } finally {
             writeUnlock();
diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java 
b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java
index d709ead10f5..46bc00b0144 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java
@@ -42,6 +42,7 @@ import org.apache.doris.transaction.TransactionStatus;
 
 import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import io.opentelemetry.api.trace.Tracer;
 import org.apache.logging.log4j.LogManager;
@@ -50,6 +51,7 @@ import org.xnio.StreamConnection;
 
 import java.io.IOException;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 
@@ -120,6 +122,9 @@ public class ConnectContext {
     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;
@@ -197,6 +202,18 @@ public class ConnectContext {
         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;
     }
diff --git 
a/regression-test/suites/external_table_emr_p2/hive/test_external_catalog_hive.groovy
 
b/regression-test/suites/external_table_emr_p2/hive/test_external_catalog_hive.groovy
index ca445dbcdad..5f82e585fed 100644
--- 
a/regression-test/suites/external_table_emr_p2/hive/test_external_catalog_hive.groovy
+++ 
b/regression-test/suites/external_table_emr_p2/hive/test_external_catalog_hive.groovy
@@ -104,10 +104,23 @@ suite("test_external_catalog_hive", "p2") {
         sql """alter catalog ${catalog_name} rename hms;"""
 
         sql """switch hms;"""
-
-        def res3 = sql """select count(*) from test.hive_test limit 10;"""
+        sql """use test;"""
+        def res3 = sql """select count(*) from hive_test limit 10;"""
         logger.info("recoding select: " + res3.toString())
 
+        def user = 'account_user_test'
+        def pwd = 'C123_567p'
+        try_sql("DROP USER ${user}")
+        sql """CREATE USER '${user}' IDENTIFIED BY '${pwd}'"""
+        sql """GRANT SELECT_PRIV on *.*.* to '${user}'"""
+        connect(user=user, password="${pwd}", url=context.config.jdbcUrl) {
+            sql """switch hms;"""
+            test {
+                sql "show tables"
+                exception "errCode = 2, detailMessage = No database selected"
+            }
+        }
+
         sql """alter catalog hms rename ${catalog_name};"""
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to