Write cache stats to info log (fine) when value is requested
The cache used in FindOwners is problematic from a system and code
health perspective. It caches state keyed by GitRepositoryManager (a
service object) and keeps a lot of mutable state that can lead to races
or hard to debug issues.
The cache is very short-lived (30s for most instances), so we want to
know if we can just remove it. Unfortunately, the whole plugin doesn't
use the Injector correctly, so we can't just inject a MetricMaker. We
could make the PrologEnvironment expose a MetricMaker, but this seems
like a one-off use case that is better treated with a simpler solution.
Therefore, this commit just logs cache stats at fine whenever a value is
accessed.
Change-Id: Ib1620dbb1b22b2c8ad5991b6ab0b54e40063a3ee
diff --git a/src/main/java/com/googlesource/gerrit/plugins/findowners/Cache.java b/src/main/java/com/googlesource/gerrit/plugins/findowners/Cache.java
index df766e4..0a54970 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/findowners/Cache.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/findowners/Cache.java
@@ -14,7 +14,6 @@
package com.googlesource.gerrit.plugins.findowners;
-
import com.google.common.cache.CacheBuilder;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.exceptions.StorageException;
@@ -31,6 +30,7 @@
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
/** Save OwnersDb in a cache for multiple calls to submit_filter. */
class Cache {
@@ -83,6 +83,7 @@
CacheBuilder.newBuilder()
.maximumSize(maxSize)
.expireAfterWrite(Duration.ofSeconds(maxSeconds))
+ .recordStats()
.build();
} else {
logger.atInfo().log("Cache disabled.");
@@ -168,6 +169,8 @@
try {
logger.atFiner().log(
"Get from cache %s, key=%s, cache size=%d", dbCache, key, dbCache.size());
+ logger.atFine().atMostEvery(30, TimeUnit.SECONDS).log(
+ "FindOwnersCacheStats: " + dbCache.stats());
return dbCache.get(
key,
new Callable<OwnersDb>() {