Make H2 database cache size configurable
H2 database by default uses up to the half of the amount of memory available for the virtual machine (Runtime.getRuntime().maxMemory()) as described in [1]. In some cases this could contribute to an out-of-memory error in Gerrit. Introduce a new configuration parameter cache.h2CacheSize to limit the H2 memory cache size. In this change this parameter is applied to all persistent caches. [1] http://www.h2database.com/html/features.html#cache_settings Change-Id: I0217d4114f528275ca4094e0f3414ef23af80677
This commit is contained in:
@@ -58,6 +58,7 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
|
||||
private final DynamicMap<Cache<?, ?>> cacheMap;
|
||||
private final ExecutorService executor;
|
||||
private final ScheduledExecutorService cleanup;
|
||||
private final long h2CacheSize;
|
||||
|
||||
@Inject
|
||||
H2CacheFactory(
|
||||
@@ -68,6 +69,7 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
|
||||
defaultFactory = defaultCacheFactory;
|
||||
config = cfg;
|
||||
cacheDir = getCacheDir(site, cfg.getString("cache", null, "directory"));
|
||||
h2CacheSize = cfg.getLong("cache", null, "h2CacheSize", -1);
|
||||
caches = Lists.newLinkedList();
|
||||
this.cacheMap = cacheMap;
|
||||
|
||||
@@ -220,8 +222,14 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
|
||||
TypeLiteral<K> keyType,
|
||||
long maxSize,
|
||||
Long expireAfterWrite) {
|
||||
String url = "jdbc:h2:" + cacheDir.resolve(name).toUri();
|
||||
return new SqlStore<>(url, keyType, maxSize,
|
||||
StringBuilder url = new StringBuilder();
|
||||
url.append("jdbc:h2:").append(cacheDir.resolve(name).toUri());
|
||||
if (h2CacheSize >= 0) {
|
||||
url.append(";CACHE_SIZE=");
|
||||
// H2 CACHE_SIZE is always given in KB
|
||||
url.append(h2CacheSize / 1024);
|
||||
}
|
||||
return new SqlStore<>(url.toString(), keyType, maxSize,
|
||||
expireAfterWrite == null ? 0 : expireAfterWrite.longValue());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user