Permit ProjectCacheClock to be completely disabled

Some admins may just want to require all updates to projects to be
made through the web interface, and avoid the small expense of a
background thread ticking off changes. This is not too different
from disabling plugins with plugins.checkFrequency = 0.

Unfortunately we cannot use 0 to disable the checker as it means
always check. Instead define new magic string values of "off" and
"disabled" to represent no clock.

Change-Id: Iccafea1b01c597322802da2441904965e0ada3d2
This commit is contained in:
Shawn Pearce 2013-02-20 11:29:46 -08:00
parent 7609714b2c
commit c825ef1f6a
2 changed files with 27 additions and 18 deletions

View File

@ -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].
+

View File

@ -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);
}
}