From db4a6205ebf045a866bfad4c4751a3372c34af24 Mon Sep 17 00:00:00 2001 From: Andy Seaborne Date: Mon, 29 Jan 2018 21:37:10 +0000 Subject: [PATCH] Fix to releasing old tasks --- .../apache/jena/fuseki/async/AsyncPool.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/async/AsyncPool.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/async/AsyncPool.java index 92c810b6be1..2cca42ec1cf 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/async/AsyncPool.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/async/AsyncPool.java @@ -26,10 +26,12 @@ import org.apache.jena.fuseki.Fuseki ; import org.apache.jena.fuseki.server.DataService ; -/** The set of currently active async tasks */ +/** The set of currently active and recently completed tasks. */ public class AsyncPool { + // Max concurrent tasks. private static int nMaxThreads = 4 ; + // Number of finished tasks kept. private static int MAX_FINISHED = 20 ; // See Executors.newCachedThreadPool and Executors.newFixedThreadPool @@ -38,12 +40,14 @@ public class AsyncPool new LinkedBlockingQueue()) ; private final Object mutex = new Object() ; private long counter = 0 ; - private Map runningTasks = new LinkedHashMap<>() ; + private Map runningTasks = new LinkedHashMap<>() ; private Map finishedTasks = new LinkedHashMap<>() ; + // Finite FIFO of finished tasks. + private LinkedList finishedTasksList = new LinkedList<>(); private static AsyncPool instance = new AsyncPool() ; - public static AsyncPool get() - { return instance ; } + + public static AsyncPool get() { return instance ; } private AsyncPool() { } @@ -78,9 +82,13 @@ public void finished(AsyncTask task) { synchronized(mutex) { String id = task.getTaskId() ; runningTasks.remove(id) ; - while ( finishedTasks.size() >= MAX_FINISHED ) - finishedTasks.remove(task.getTaskId()) ; + // Reduce old tasks list + while ( finishedTasksList.size() >= MAX_FINISHED ) { + AsyncTask oldTask = finishedTasksList.removeFirst(); + finishedTasks.remove(oldTask.getTaskId()); + } finishedTasks.put(id, task) ; + finishedTasksList.add(task); } } @@ -90,7 +98,10 @@ public AsyncTask getRunningTask(String taskId) { } } - /** Get for any state */ + /** + * Get a task - whether running or finished. Return null for no such task and also + * retired tasks. Only a limited number of old, finished tasks are remembered. + */ public AsyncTask getTask(String taskId) { synchronized(mutex) { AsyncTask task = runningTasks.get(taskId) ;