Add advanced H2 option handling

Add separate configs for h2CacheSize for the cache tables and
normal persistent H2 dbs. While the caches offer further options
to limit their total size, normal table can grow without bounds,
so different H2 memory cache size seem reasonable.

H2 offers an auto server mode
(http://www.h2database.com/html/features.html#auto_mixed_mode).

Add a flag to enable this mode. This allow to run command line utils
that access the database (e.g.RebuildNoteDb) also for the embedded H2
database.

Add a flag to enable auto-server mode for the H2-based caches as
well.

Change-Id: Id4fe76363bc209064485296d4dd1697e0de0f235
This commit is contained in:
Björn Pedersen
2016-05-20 10:21:07 +02:00
parent ea65e9bff1
commit 64af3ad561
4 changed files with 71 additions and 19 deletions

View File

@@ -539,24 +539,31 @@ Default is unset, no disk cache.
[[cache.h2CacheSize]]cache.h2CacheSize::
+
The size of the in-memory cache for each opened H2 database, in bytes.
The size of the in-memory cache for each opened H2 cache database, in bytes.
+
Some caches of Gerrit are persistent and are backed by an H2 database.
H2 uses memory to cache its database content. The parameter `h2CacheSize`
allows to limit the memory used by H2 and thus prevent out-of-memory
caused by the H2 database using too much memory.
+
Technically the H2 cache size is configured using the CACHE_SIZE parameter in
the H2 JDBC connection URL, as described
link:http://www.h2database.com/html/features.html#cache_settings[here]
See <<database.h2.cachesize,database.h2.cachesize>> for a detailed discussion.
+
Gerrit uses H2 for storing reviewed flags on changes and for persistent
caches. The configured cache size is used for each of these local H2
databases.
+
Default is unset, no cache size limit.
Default is unset, using up to half of the available memory.
H2 will persist this value in the database, so to unset explicitly specify 0.
+
Common unit suffixes of 'k', 'm', or 'g' are supported.
[[cache.h2AutoServer]]cache.h2AutoServer::
+
If set to true, enable H2 autoserver mode for the H2-backed persistent cache
databases.
+
See link:http://www.h2database.com/html/features.html#auto_mixed_mode[here]
for detail.
+
Default is false.
[[cache.name.maxAge]]cache.<name>.maxAge::
+
Maximum age to keep an entry in the cache. Entries are removed from
@@ -1542,6 +1549,42 @@ This class must have default constructor and be available on Gerrit's bootstrap
classpath, e. g. in `$gerrit_site/lib` directory. Example implementation of
SQL monitoring can be found in javamelody-plugin.
[[database.h2]]database.h2::
+
The settings in this section are used for the reviewdb if the
<<database.type,database.type>> is H2.
+
Additionally gerrit uses H2 for storing reviewed flags on changes.
[[database.h2.cacheSize]]database.h2.cacheSize::
+
The size of the H2 internal database cache, in bytes. The H2 internal cache for
persistent H2-backed caches is controlled by
<<cache.h2CacheSize,cache.h2CacheSize>>.
+
H2 uses memory to cache its database content. The parameter `cacheSize`
allows to limit the memory used by H2 and thus prevent out-of-memory
caused by the H2 database using too much memory.
+
Technically the H2 cache size is configured using the CACHE_SIZE parameter in
the H2 JDBC connection URL, as described
link:http://www.h2database.com/html/features.html#cache_settings[here]
+
Default is unset, using up to half of the available memory.
H2 will persist this value in the database, so to unset explicitly specify 0.
+
Common unit suffixes of 'k', 'm', or 'g' are supported.
[[database.h2.autoServer]]database.h2.autoServer::
+
If `true` enable the automatic mixed mode
(see link:http://www.h2database.com/html/features.html#auto_mixed_mode[Automatic Mixed Mode]).
This enables concurrent access to the embedded H2 database from command line
utils (e.g. RebuildNoteDb).
+
Default is `false`.
[[download]]
=== Section download

View File

@@ -60,6 +60,7 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
private final ExecutorService executor;
private final ScheduledExecutorService cleanup;
private final long h2CacheSize;
private final boolean h2AutoServer;
@Inject
H2CacheFactory(
@@ -71,6 +72,7 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
config = cfg;
cacheDir = getCacheDir(site, cfg.getString("cache", null, "directory"));
h2CacheSize = cfg.getLong("cache", null, "h2CacheSize", -1);
h2AutoServer = cfg.getBoolean("cache", null, "h2AutoServer", false);
caches = new LinkedList<>();
this.cacheMap = cacheMap;
@@ -230,6 +232,9 @@ class H2CacheFactory implements PersistentCacheFactory, LifecycleListener {
// H2 CACHE_SIZE is always given in KB
url.append(h2CacheSize / 1024);
}
if (h2AutoServer) {
url.append(";AUTO_SERVER=TRUE");
}
return new SqlStore<>(url.toString(), keyType, maxSize,
expireAfterWrite == null ? 0 : expireAfterWrite.longValue());
}

View File

@@ -40,7 +40,7 @@ class H2 extends BaseDataSourceType {
if (database == null || database.isEmpty()) {
database = "db/ReviewDB";
}
return createUrl(site.resolve(database));
return appendUrlOptions(cfg, createUrl(site.resolve(database)));
}
public static String createUrl(Path path) {
@@ -50,16 +50,20 @@ class H2 extends BaseDataSourceType {
.toString();
}
public static String appendCacheSize(Config cfg, String url) {
long h2CacheSize = cfg.getLong("cache", null, "h2CacheSize", -1);
public static String appendUrlOptions(Config cfg, String url) {
long h2CacheSize = cfg.getLong("database", "h2", "cacheSize", -1);
boolean h2AutoServer = cfg.getBoolean("database", "h2", "autoServer", false);
StringBuilder urlBuilder = new StringBuilder().append(url);
if (h2CacheSize >= 0) {
// H2 CACHE_SIZE is always given in KB
return new StringBuilder()
.append(url)
.append(";CACHE_SIZE=")
.append(h2CacheSize / 1024)
.toString();
urlBuilder.append(";CACHE_SIZE=")
.append(h2CacheSize / 1024);
}
return url;
if (h2AutoServer) {
urlBuilder.append(";AUTO_SERVER=TRUE");
}
return urlBuilder.toString();
}
}

View File

@@ -75,7 +75,7 @@ public class H2AccountPatchReviewStore
@Inject
H2AccountPatchReviewStore(@GerritServerConfig Config cfg,
SitePaths sitePaths) {
this.url = H2.appendCacheSize(cfg, getUrl(sitePaths));
this.url = H2.appendUrlOptions(cfg, getUrl(sitePaths));
}
public static String getUrl(SitePaths sitePaths) {