Skip to content

Commit 3501017

Browse files
authored
Merge pull request #1727 from Caltech-IPAC/FIREFLY-1683-thread-leak
FIREFLY-1683: prevent excessive thread use
2 parents 76e81c9 + 6f8c122 commit 3501017

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

src/firefly/java/edu/caltech/ipac/firefly/server/ServerContext.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -855,11 +855,12 @@ public void contextInitialized(ServletContextEvent servletContextEvent) {
855855
ServletContext cntx = servletContextEvent.getServletContext();
856856
ServerContext.init(cntx.getContextPath(), cntx.getServletContextName(), cntx.getRealPath(WEBAPP_CONFIG_LOC));
857857
VersionUtil.initVersion(cntx); // can be called multiple times, only inits on the first call
858-
SCHEDULE_TASK_EXEC.scheduleAtFixedRate(
859-
() -> DbMonitor.cleanup(false),
860-
DbMonitor.CLEANUP_INTVL,
861-
DbMonitor.CLEANUP_INTVL,
862-
TimeUnit.MILLISECONDS);
858+
// we're using DuckDB exclusively for embedded database. No need for cleanup.
859+
// SCHEDULE_TASK_EXEC.scheduleAtFixedRate(
860+
// () -> DbMonitor.cleanup(false),
861+
// DbMonitor.CLEANUP_INTVL,
862+
// DbMonitor.CLEANUP_INTVL,
863+
// TimeUnit.SECONDS);
863864
} catch (Throwable e) {
864865
e.printStackTrace();
865866
}
@@ -868,7 +869,7 @@ public void contextInitialized(ServletContextEvent servletContextEvent) {
868869
public void contextDestroyed(ServletContextEvent servletContextEvent) {
869870
try {
870871
System.out.println("contextDestroyed...");
871-
DbMonitor.cleanup(true, false);
872+
// DbMonitor.cleanup(true, false);
872873
((EhcacheProvider)CacheManager.getCacheProvider()).shutdown();
873874
try {
874875
SHORT_TASK_EXEC.shutdownNow();

src/firefly/java/edu/caltech/ipac/firefly/server/db/DbAdapter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,14 @@ class DbStats {
274274
int rowCnt = -1;
275275
int totalRows = -1;
276276
long memory = -1;
277+
final long created = System.currentTimeMillis();
277278

278279
public int tblCnt() { return tblCnt; }
279280
public int colCnt() { return colCnt;}
280281
public int rowCnt() { return rowCnt; }
281282
public int totalRows() { return totalRows;}
282283
public long memory() { return memory;}
284+
public long created() { return created;}
283285
}
284286

285287
/**
@@ -348,8 +350,13 @@ public ReentrantLock getLock() {
348350
public void setCompact(boolean compact) { isCompact = compact;}
349351
public boolean isCompact() { return isCompact; }
350352

351-
public void updateStats() { this.dbStats = dbAdapter.getDbStats(); }
352353
public DbStats getDbStats() { return dbStats == null ? new DbStats() : dbStats; }
354+
355+
public void updateStats() {
356+
if (dbStats == null || dbStats.created < lastAccessed) {
357+
this.dbStats = dbAdapter.getDbStats();
358+
}
359+
}
353360
}
354361

355362
/**

src/firefly/java/edu/caltech/ipac/firefly/server/db/DbMonitor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
import java.util.Collections;
1313
import java.util.List;
1414
import java.util.concurrent.ConcurrentHashMap;
15+
import java.util.concurrent.ExecutorService;
16+
import java.util.concurrent.Executors;
1517
import java.util.concurrent.Future;
1618
import java.util.concurrent.TimeUnit;
1719

1820
import static edu.caltech.ipac.firefly.core.Util.Try;
19-
import static edu.caltech.ipac.firefly.server.ServerContext.SHORT_TASK_EXEC;
2021

2122
/**
2223
* Date: 5/3/24
@@ -71,6 +72,7 @@ private static long maxMemory() {
7172
private static final ConcurrentHashMap<String, DbAdapter.EmbeddedDbInstance> dbInstances = new ConcurrentHashMap<>();
7273
private static final DbAdapter.EmbeddedDbStats dbStats = new DbAdapter.EmbeddedDbStats();
7374
private static final Logger.LoggerImpl LOGGER = Logger.getLogger();
75+
private static final ExecutorService DB_STATS_THREADS = Executors.newFixedThreadPool(5); // up to 5 threads for gathering DB stats
7476

7577
public static ConcurrentHashMap<String, DbAdapter.EmbeddedDbInstance> getDbInstances() {
7678
return dbInstances;
@@ -99,9 +101,9 @@ public static void updateDbStats() {
99101
LOGGER.trace("DbAdapter -> updateDbStats");
100102
Ref<Future<?>> t = new Ref<>();
101103
for (DbAdapter.EmbeddedDbInstance db : dbInstances.values()) {
102-
t.set(SHORT_TASK_EXEC.submit(db::updateStats));
104+
t.set(DB_STATS_THREADS.submit(db::updateStats));
103105
}
104-
Try.it(() -> t.get().get(5, TimeUnit.SECONDS)); // run all in parallel, but wait for up to 5 seconds
106+
Try.it(() -> t.get().get(10, TimeUnit.SECONDS)); // run all in parallel, but wait for up to 5 seconds
105107
}
106108

107109
//====================================================================

0 commit comments

Comments
 (0)