diff options
author | Luca Milanesio <luca.milanesio@gmail.com> | 2022-04-08 10:54:44 +0100 |
---|---|---|
committer | Luca Milanesio <luca.milanesio@gmail.com> | 2022-04-08 10:54:44 +0100 |
commit | a523b96484c1698b2a3b5e83fc78d55564c20aec (patch) | |
tree | 9a36b63ab13540ef99e569864a86805e872facde | |
parent | 1e0fffb542ee59932f940c784323df0618f3aa8a (diff) | |
parent | 4b327bbe8b74e7ee7d66fbf88ed81bb60dd79016 (diff) |
Merge branch 'stable-3.2' into stable-3.3
* stable-3.2:
Cache change /meta ref SHA1 for each REST API request
Release-Notes: skip
Change-Id: I98184b81a00eec5b3b7d9fed44c6701532a3bf6b
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 d427caa0c6..fb1f2353ff 100644 --- a/java/com/google/gerrit/httpd/restapi/RestApiServlet.java +++ b/java/com/google/gerrit/httpd/restapi/RestApiServlet.java @@ -326,7 +326,7 @@ public class RestApiServlet extends HttpServlet { try (TraceContext traceContext = enableTracing(req, res)) { List<IdString> path = splitPath(req); - try (PerThreadCache ignored = PerThreadCache.create()) { + try (PerThreadCache ignored = PerThreadCache.create(req)) { RequestInfo requestInfo = createRequestInfo(traceContext, requestUri(req), path); globals.requestListeners.runEach(l -> l.onRequest(requestInfo)); 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 28f25ec5e4..60954eb9d2 100644 --- a/java/com/google/gerrit/server/notedb/ChangeNotes.java +++ b/java/com/google/gerrit/server/notedb/ChangeNotes.java @@ -56,6 +56,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; @@ -638,6 +639,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 b3b2f5ac09..c708e09f5f 100644 --- a/javatests/com/google/gerrit/server/cache/BUILD +++ b/javatests/com/google/gerrit/server/cache/BUILD @@ -7,8 +7,11 @@ 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 5d420d33f6..c04deb4e8e 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 static com.google.gerrit.testing.GerritJUnit.assertThrows; +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 { @@ -43,7 +47,7 @@ public class PerThreadCacheTest { @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); @@ -61,7 +65,7 @@ public class PerThreadCacheTest { @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"); @@ -69,7 +73,7 @@ public class PerThreadCacheTest { // 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"); @@ -78,16 +82,48 @@ public class PerThreadCacheTest { @Test public void doubleInstantiationFails() { - try (PerThreadCache ignored = PerThreadCache.create()) { + try (PerThreadCache ignored = PerThreadCache.create(null)) { IllegalStateException thrown = - assertThrows(IllegalStateException.class, () -> PerThreadCache.create()); + assertThrows(IllegalStateException.class, () -> PerThreadCache.create(null)); assertThat(thrown).hasMessageThat().contains("called create() twice on the same request"); } } @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); |