Allow to use an external ExecutorService for background auto-gc

If set use the external executor, otherwise use JGit's own simple
WorkQueue. Move WorkQueue to an internal package so we can reuse it
without exposing it in the public API.

Change-Id: I060d62ffd6692362a88b4bf13ee07b0dc857abe9
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2017-06-11 01:20:34 +02:00
parent b6f954ad42
commit 18ae9bb57d
5 changed files with 29 additions and 6 deletions

View File

@ -85,6 +85,7 @@ Export-Package: org.eclipse.jgit.annotations;version="4.8.0",
org.eclipse.jgit.treewalk,
org.eclipse.jgit.transport,
org.eclipse.jgit.submodule",
org.eclipse.jgit.lib.internal;version="4.8.0";x-internal:=true,
org.eclipse.jgit.merge;version="4.8.0";
uses:="org.eclipse.jgit.lib,
org.eclipse.jgit.treewalk,

View File

@ -78,7 +78,6 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -107,6 +106,7 @@
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Ref.Storage;
import org.eclipse.jgit.lib.internal.WorkQueue;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.ReflogEntry;
import org.eclipse.jgit.lib.ReflogReader;
@ -151,7 +151,19 @@ public class GC {
private static final int DEFAULT_AUTOLIMIT = 6700;
private static ExecutorService executor = Executors.newFixedThreadPool(1);
private static volatile ExecutorService executor;
/**
* Set the executor for running auto-gc in the background. If no executor is
* set JGit's own WorkQueue will be used.
*
* @param e
* the executor to be used for running auto-gc
* @since 4.8
*/
public static void setExecutor(ExecutorService e) {
executor = e;
}
private final FileRepository repo;
@ -271,7 +283,7 @@ public Collection<PackFile> gc() throws IOException, ParseException {
}
return Collections.emptyList();
};
Future<Collection<PackFile>> result = executor.submit(gcTask);
Future<Collection<PackFile>> result = executor().submit(gcTask);
if (background) {
// TODO(ms): in 5.0 change signature and return the Future
return Collections.emptyList();
@ -283,6 +295,10 @@ public Collection<PackFile> gc() throws IOException, ParseException {
}
}
private ExecutorService executor() {
return (executor != null) ? executor : WorkQueue.getExecutor();
}
private Collection<PackFile> doGc() throws IOException, ParseException {
if (automatic && !needGc()) {
return Collections.emptyList();

View File

@ -46,6 +46,8 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.lib.internal.WorkQueue;
/** ProgressMonitor that batches update events. */
public abstract class BatchingProgressMonitor implements ProgressMonitor {
private long delayStartTime;

View File

@ -55,6 +55,7 @@
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.internal.WorkQueue;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.RawParseUtils;

View File

@ -41,7 +41,7 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.lib;
package org.eclipse.jgit.lib.internal;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
@ -50,7 +50,7 @@
/**
* Simple work queue to run tasks in the background
*/
class WorkQueue {
public class WorkQueue {
private static final ScheduledThreadPoolExecutor executor;
static final Object executorKiller;
@ -94,7 +94,10 @@ protected void finalize() {
};
}
static ScheduledThreadPoolExecutor getExecutor() {
/**
* @return the WorkQueue's executor
*/
public static ScheduledThreadPoolExecutor getExecutor() {
return executor;
}
}