diff options
author | Luca Milanesio <luca.milanesio@gmail.com> | 2022-04-08 09:06:05 +0100 |
---|---|---|
committer | Luca Milanesio <luca.milanesio@gmail.com> | 2022-04-08 09:50:54 +0100 |
commit | 462bb1ed2f483734115c78bc638a3f8ba0dc558e (patch) | |
tree | f529b882cb4f197d0ba8044b3eb4f21effa0b04f | |
parent | a5f95d6c12b03b55fa0aad95a30018da17cb46cd (diff) | |
parent | 23db8a546b800b2c91629d80b6f5117ff635238a (diff) |
Merge branch 'stable-2.16' into stable-3.0
* stable-2.16:
Cache change /meta ref SHA1 for each REST API request
Release-Notes: skip
Change-Id: I00882cba94ae51998ad0b34ab17b0fa4e2f4ecf5
6 files changed, 90 insertions, 11 deletions
diff --git a/java/com/google/gerrit/httpd/restapi/RestApiServlet.java b/java/com/google/gerrit/httpd/restapi/RestApiServlet.java index c45a1189fb..bb05fa85ac 100644 --- a/java/com/google/gerrit/httpd/restapi/RestApiServlet.java +++ b/java/com/google/gerrit/httpd/restapi/RestApiServlet.java @@ -293,7 +293,7 @@ public class RestApiServlet extends HttpServlet { ViewData viewData = null; try (TraceContext traceContext = enableTracing(req, res)) { - try (PerThreadCache ignored = PerThreadCache.create()) { + try (PerThreadCache ignored = PerThreadCache.create(req)) { logger.atFinest().log( "Received REST request: %s %s (parameters: %s)", req.getMethod(), req.getRequestURI(), getParameterNames(req)); diff --git a/java/com/google/gerrit/server/cache/PerThreadCache.java b/java/com/google/gerrit/server/cache/PerThreadCache.java index b4f79d10ab..609fce7b25 100644 --- a/java/com/google/gerrit/server/cache/PerThreadCache.java +++ b/java/com/google/gerrit/server/cache/PerThreadCache.java @@ -21,7 +21,9 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; import com.google.gerrit.common.Nullable; import java.util.Map; +import java.util.Optional; import java.util.function.Supplier; +import javax.servlet.http.HttpServletRequest; /** * Caches object instances for a request as {@link ThreadLocal} in the serving thread. @@ -58,6 +60,12 @@ public class PerThreadCache implements AutoCloseable { private static final int PER_THREAD_CACHE_SIZE = 25; /** + * Optional HTTP request associated with the per-thread cache, should the thread be associated + * with the incoming HTTP thread pool. + */ + private final Optional<HttpServletRequest> httpRequest; + + /** * Unique key for key-value mappings stored in PerThreadCache. The key is based on the value's * class and a list of identifiers that in combination uniquely set the object apart form others * of the same class. @@ -102,9 +110,9 @@ public class PerThreadCache implements AutoCloseable { } } - public static PerThreadCache create() { + public static PerThreadCache create(@Nullable HttpServletRequest httpRequest) { checkState(CACHE.get() == null, "called create() twice on the same request"); - PerThreadCache cache = new PerThreadCache(); + PerThreadCache cache = new PerThreadCache(httpRequest); CACHE.set(cache); return cache; } @@ -121,7 +129,9 @@ public class PerThreadCache implements AutoCloseable { private final Map<Key<?>, Object> cache = Maps.newHashMapWithExpectedSize(PER_THREAD_CACHE_SIZE); - private PerThreadCache() {} + private PerThreadCache(@Nullable HttpServletRequest req) { + httpRequest = Optional.ofNullable(req); + } /** * Returns an instance of {@code T} that was either loaded from the cache or obtained from the @@ -139,6 +149,19 @@ public class PerThreadCache implements AutoCloseable { return value; } + /** Returns the optional HTTP request associated with the local thread cache. */ + public Optional<HttpServletRequest> getHttpRequest() { + return httpRequest; + } + + /** Returns true if there is an HTTP request associated and is a GET or HEAD */ + public boolean hasReadonlyRequest() { + return httpRequest + .map(HttpServletRequest::getMethod) + .filter(m -> m.equalsIgnoreCase("GET") || m.equalsIgnoreCase("HEAD")) + .isPresent(); + } + @Override public void close() { CACHE.remove(); diff --git a/java/com/google/gerrit/server/git/RepoRefCache.java b/java/com/google/gerrit/server/git/RepoRefCache.java index 6b2493a9f1..9086da7fb5 100644 --- a/java/com/google/gerrit/server/git/RepoRefCache.java +++ b/java/com/google/gerrit/server/git/RepoRefCache.java @@ -14,6 +14,7 @@ package com.google.gerrit.server.git; +import com.google.gerrit.server.cache.PerThreadCache; import java.io.IOException; import java.util.Collections; import java.util.HashMap; @@ -29,6 +30,17 @@ public class RepoRefCache implements RefCache { private final RefDatabase refdb; private final Map<String, Optional<ObjectId>> ids; + public static Optional<RefCache> getOptional(Repository repo) { + PerThreadCache cache = PerThreadCache.get(); + if (cache != null && cache.hasReadonlyRequest()) { + return Optional.of( + cache.get( + PerThreadCache.Key.create(RepoRefCache.class, repo), () -> new RepoRefCache(repo))); + } + + return Optional.empty(); + } + public RepoRefCache(Repository repo) { this.refdb = repo.getRefDatabase(); this.ids = new HashMap<>(); diff --git a/java/com/google/gerrit/server/notedb/ChangeNotes.java b/java/com/google/gerrit/server/notedb/ChangeNotes.java index a4021cb28c..22ac95a4d5 100644 --- a/java/com/google/gerrit/server/notedb/ChangeNotes.java +++ b/java/com/google/gerrit/server/notedb/ChangeNotes.java @@ -52,6 +52,7 @@ import com.google.gerrit.server.ReviewerByEmailSet; import com.google.gerrit.server.ReviewerSet; import com.google.gerrit.server.ReviewerStatusUpdate; import com.google.gerrit.server.git.RefCache; +import com.google.gerrit.server.git.RepoRefCache; import com.google.gerrit.server.project.NoSuchChangeException; import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.server.query.change.ChangeData; @@ -533,6 +534,10 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> { @Override protected ObjectId readRef(Repository repo) throws IOException { - return refs != null ? refs.get(getRefName()).orElse(null) : super.readRef(repo); + Optional<RefCache> refsCache = + Optional.ofNullable(refs).map(Optional::of).orElse(RepoRefCache.getOptional(repo)); + return refsCache.isPresent() + ? refsCache.get().get(getRefName()).orElse(null) + : super.readRef(repo); } } diff --git a/javatests/com/google/gerrit/server/cache/BUILD b/javatests/com/google/gerrit/server/cache/BUILD index c255e619aa..bb6ea30739 100644 --- a/javatests/com/google/gerrit/server/cache/BUILD +++ b/javatests/com/google/gerrit/server/cache/BUILD @@ -6,7 +6,10 @@ junit_tests( deps = [ "//java/com/google/gerrit/server", "//java/com/google/gerrit/testing:gerrit-test-util", + "//javatests/com/google/gerrit/util/http/testutil", "//lib:junit", "//lib/truth", + "//lib/truth:truth-java8-extension", + "@servlet-api//jar", ], ) diff --git a/javatests/com/google/gerrit/server/cache/PerThreadCacheTest.java b/javatests/com/google/gerrit/server/cache/PerThreadCacheTest.java index 6a42577d1e..65d9979f56 100644 --- a/javatests/com/google/gerrit/server/cache/PerThreadCacheTest.java +++ b/javatests/com/google/gerrit/server/cache/PerThreadCacheTest.java @@ -15,9 +15,13 @@ package com.google.gerrit.server.cache; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth8.assertThat; import com.google.gerrit.testing.GerritBaseTests; +import com.google.gerrit.util.http.testutil.FakeHttpServletRequest; import java.util.function.Supplier; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; import org.junit.Test; public class PerThreadCacheTest extends GerritBaseTests { @@ -39,7 +43,7 @@ public class PerThreadCacheTest extends GerritBaseTests { @Test public void endToEndCache() { - try (PerThreadCache ignored = PerThreadCache.create()) { + try (PerThreadCache ignored = PerThreadCache.create(null)) { PerThreadCache cache = PerThreadCache.get(); PerThreadCache.Key<String> key1 = PerThreadCache.Key.create(String.class); @@ -57,7 +61,7 @@ public class PerThreadCacheTest extends GerritBaseTests { @Test public void cleanUp() { PerThreadCache.Key<String> key = PerThreadCache.Key.create(String.class); - try (PerThreadCache ignored = PerThreadCache.create()) { + try (PerThreadCache ignored = PerThreadCache.create(null)) { PerThreadCache cache = PerThreadCache.get(); String value1 = cache.get(key, () -> "value1"); assertThat(value1).isEqualTo("value1"); @@ -65,7 +69,7 @@ public class PerThreadCacheTest extends GerritBaseTests { // Create a second cache and assert that it is not connected to the first one. // This ensures that the cleanup is actually working. - try (PerThreadCache ignored = PerThreadCache.create()) { + try (PerThreadCache ignored = PerThreadCache.create(null)) { PerThreadCache cache = PerThreadCache.get(); String value1 = cache.get(key, () -> "value2"); assertThat(value1).isEqualTo("value2"); @@ -74,16 +78,48 @@ public class PerThreadCacheTest extends GerritBaseTests { @Test public void doubleInstantiationFails() { - try (PerThreadCache ignored = PerThreadCache.create()) { + try (PerThreadCache ignored = PerThreadCache.create(null)) { exception.expect(IllegalStateException.class); exception.expectMessage("called create() twice on the same request"); - PerThreadCache.create(); + PerThreadCache.create(null); + } + } + + @Test + public void isAssociatedWithHttpReadonlyRequest() { + HttpServletRequest getRequest = new FakeHttpServletRequest(); + try (PerThreadCache cache = PerThreadCache.create(getRequest)) { + assertThat(cache.getHttpRequest()).hasValue(getRequest); + assertThat(cache.hasReadonlyRequest()).isTrue(); + } + } + + @Test + public void isAssociatedWithHttpWriteRequest() { + HttpServletRequest putRequest = + new HttpServletRequestWrapper(new FakeHttpServletRequest()) { + @Override + public String getMethod() { + return "PUT"; + } + }; + try (PerThreadCache cache = PerThreadCache.create(putRequest)) { + assertThat(cache.getHttpRequest()).hasValue(putRequest); + assertThat(cache.hasReadonlyRequest()).isFalse(); + } + } + + @Test + public void isNotAssociatedWithHttpRequest() { + try (PerThreadCache cache = PerThreadCache.create(null)) { + assertThat(cache.getHttpRequest()).isEmpty(); + assertThat(cache.hasReadonlyRequest()).isFalse(); } } @Test public void enforceMaxSize() { - try (PerThreadCache cache = PerThreadCache.create()) { + try (PerThreadCache cache = PerThreadCache.create(null)) { // Fill the cache for (int i = 0; i < 50; i++) { PerThreadCache.Key<String> key = PerThreadCache.Key.create(String.class, i); |