Implement a PerThreadCache

This commit implements a per-request cache as a per-thread cache.

PerThreadCache is intended to cache objects that have a high
instantiation cost, are specific to the current request and
potentially need to be instantiated multiple times while serving a
request.

The implementation uses a ThreadLocal which limits the cache to
the serving thread. Given that fan-outs (like ChangeJson's executor)
run with high parallelization, it seems acceptable to re-do some
instantiation there in lieu of this cache.

This commit uses a the new cache in DefaultPermissionBackend to cache
a ProjectControl instance.

Change-Id: Id5de21ed8941c7e08eb37197b16ce2cadece7aea
This commit is contained in:
Patrick Hiesel
2018-04-18 10:01:48 +02:00
parent 76846bdeba
commit 16c0875d40
5 changed files with 238 additions and 2 deletions

View File

@@ -31,6 +31,7 @@ import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.PeerDaemonUser;
import com.google.gerrit.server.account.CapabilityCollection;
import com.google.gerrit.server.cache.PerThreadCache;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectState;
@@ -108,7 +109,15 @@ public class DefaultPermissionBackend extends PermissionBackend {
try {
ProjectState state = projectCache.checkedGet(project);
if (state != null) {
return projectControlFactory.create(user, state).asForProject().database(db);
PerThreadCache perThreadCache = PerThreadCache.get();
if (perThreadCache == null) {
return projectControlFactory.create(user, state).asForProject().database(db);
}
PerThreadCache.Key<ProjectControl> cacheKey =
PerThreadCache.Key.create(ProjectControl.class, project, user.getCacheKey());
ProjectControl control =
perThreadCache.get(cacheKey, () -> projectControlFactory.create(user, state));
return control.asForProject().database(db);
}
return FailedPermissionBackend.project("not found", new NoSuchProjectException(project));
} catch (IOException e) {