diff --git a/Documentation/config-gerrit.txt b/Documentation/config-gerrit.txt index 467fc2b23c..586b6def73 100644 --- a/Documentation/config-gerrit.txt +++ b/Documentation/config-gerrit.txt @@ -684,6 +684,7 @@ access. Values can be specified using standard time unit abbreviations ('ms', 'sec', 'min', etc.). + If set to 0, checks occur every time, which may slow down operations. +If set to 'disabled' or 'off', no check will ever be done. Administrators may force the cache to flush with link:cmd-flush-caches.html[gerrit flush-caches]. + diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCacheClock.java b/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCacheClock.java index f86562cf50..6bbe094202 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCacheClock.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/project/ProjectCacheClock.java @@ -14,6 +14,7 @@ package com.google.gerrit.server.project; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.gerrit.server.config.ConfigUtil; import com.google.gerrit.server.config.GerritServerConfig; import com.google.inject.Inject; @@ -34,29 +35,24 @@ public class ProjectCacheClock { @Inject public ProjectCacheClock(@GerritServerConfig Config serverConfig) { - this(TimeUnit.MILLISECONDS.convert( - ConfigUtil.getTimeUnit(serverConfig, - "cache", "projects", "checkFrequency", - 5, TimeUnit.MINUTES), TimeUnit.MINUTES)); + this(checkFrequency(serverConfig)); } public ProjectCacheClock(long checkFrequencyMillis) { - if (10 < checkFrequencyMillis) { + if (checkFrequencyMillis == Long.MAX_VALUE) { + // Start with generation 1 (to avoid magic 0 below). + // Do not begin background thread, disabling the clock. + generation = 1; + } else if (10 < checkFrequencyMillis) { // Start with generation 1 (to avoid magic 0 below). generation = 1; - ThreadFactory factory = new ThreadFactory() { - private final AtomicInteger id = new AtomicInteger(); - - @Override - public Thread newThread(Runnable runnable) { - Thread thread = Executors.defaultThreadFactory().newThread(runnable); - thread.setName(String.format("ProjectCacheClock-%d", id.incrementAndGet())); - thread.setDaemon(true); - thread.setPriority(Thread.MIN_PRIORITY); - return thread; - } - }; - ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, factory); + ScheduledExecutorService executor = Executors.newScheduledThreadPool( + 1, + new ThreadFactoryBuilder() + .setNameFormat("ProjectCacheClock-%d") + .setDaemon(true) + .setPriority(Thread.MIN_PRIORITY) + .build()); executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { @@ -75,4 +71,16 @@ public class ProjectCacheClock { long read() { return generation; } + + private static long checkFrequency(Config serverConfig) { + String freq = serverConfig.getString("cache", "projects", "checkFrequency"); + if (freq != null + && ("disabled".equalsIgnoreCase(freq) || "off".equalsIgnoreCase(freq))) { + return Long.MAX_VALUE; + } + return TimeUnit.MILLISECONDS.convert( + ConfigUtil.getTimeUnit(serverConfig, + "cache", "projects", "checkFrequency", + 5, TimeUnit.MINUTES), TimeUnit.MINUTES); + } }