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

yiguolei pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.0 by this push:
     new 5c8a6f23e57 branch-4.0: [improvement](jdbc) Improve JdbcDataSource 
cleanup thread reliability and observability #58484 (#58536)
5c8a6f23e57 is described below

commit 5c8a6f23e5746c2952e841fa256575a40a2f8212
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Dec 2 09:56:26 2025 +0800

    branch-4.0: [improvement](jdbc) Improve JdbcDataSource cleanup thread 
reliability and observability #58484 (#58536)
    
    Cherry-picked from #58484
    
    Co-authored-by: zy-kkk <[email protected]>
---
 .../java/org/apache/doris/jdbc/JdbcDataSource.java | 30 +++++++++++++++++-----
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git 
a/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcDataSource.java
 
b/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcDataSource.java
index 1aea763852f..a254924cbb3 100644
--- 
a/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcDataSource.java
+++ 
b/fe/be-java-extensions/jdbc-scanner/src/main/java/org/apache/doris/jdbc/JdbcDataSource.java
@@ -17,22 +17,29 @@
 
 package org.apache.doris.jdbc;
 
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import com.zaxxer.hikari.HikariDataSource;
 import org.apache.log4j.Logger;
 
+import java.util.Iterator;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 
 public class JdbcDataSource {
     private static final Logger LOG = Logger.getLogger(JdbcDataSource.class);
+    private static final ThreadFactory THREAD_FACTORY = new 
ThreadFactoryBuilder()
+            .setDaemon(true)
+            .setNameFormat("jdbc-datasource-cleanup-%d")
+            .build();
     private static final JdbcDataSource jdbcDataSource = new JdbcDataSource();
     private final Map<String, HikariDataSource> sourcesMap = new 
ConcurrentHashMap<>();
     private final Map<String, Long> lastAccessTimeMap = new 
ConcurrentHashMap<>();
-    private final ScheduledExecutorService executor = 
Executors.newSingleThreadScheduledExecutor();
+    private final ScheduledExecutorService executor = 
Executors.newSingleThreadScheduledExecutor(THREAD_FACTORY);
     private long cleanupInterval = 8 * 60 * 60 * 1000; // 8 hours
     private ScheduledFuture<?> cleanupTask = null;
 
@@ -72,23 +79,34 @@ public class JdbcDataSource {
         cleanupTask = executor.scheduleAtFixedRate(() -> {
             try {
                 long now = System.currentTimeMillis();
-                lastAccessTimeMap.forEach((key, lastAccessTime) -> {
+                int cleanedCount = 0;
+                Iterator<Map.Entry<String, Long>> iterator = 
lastAccessTimeMap.entrySet().iterator();
+                while (iterator.hasNext()) {
+                    Map.Entry<String, Long> entry = iterator.next();
+                    String key = entry.getKey();
+                    long lastAccessTime = entry.getValue();
                     if (now - lastAccessTime > cleanupInterval) {
                         HikariDataSource ds = sourcesMap.remove(key);
                         if (ds != null) {
                             ds.close();
                         }
-                        lastAccessTimeMap.remove(key);
+                        iterator.remove();
+                        cleanedCount++;
                         LOG.info("remove jdbc data source: " + 
key.split("jdbc")[0]);
                     }
-                });
+                }
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("jdbc datasource cleanup task executed, cleaned: 
" + cleanedCount
+                            + ", remaining: " + sourcesMap.size());
+                }
             } catch (Exception e) {
-                LOG.error("failed to cleanup jdbc data source", e);
+                LOG.warn("failed to cleanup jdbc data source", e);
             }
         }, cleanupInterval, cleanupInterval, TimeUnit.MILLISECONDS);
+        LOG.info("jdbc datasource cleanup task started, interval: " + 
cleanupInterval + "ms");
     }
 
-    private void startCleanupTask() {
+    private synchronized void startCleanupTask() {
         if (cleanupTask == null || cleanupTask.isCancelled()) {
             restartCleanupTask();
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to