Merge branch 'stable-3.9'

* stable-3.9:
  Ignore refs/multi-site/version on global-refdb in replication filter
  Fix Java 8 build and avoid Optional.isEmpty()
  Disable the thread-local refs caching
  Remove the websession-flatfile plugin configuration

Change-Id: I14ee88c72b901512fbb6865d3743ff5d78f81596
diff --git a/setup_local_env/configs/gerrit.config b/setup_local_env/configs/gerrit.config
index 8a176f2..c92c60d 100644
--- a/setup_local_env/configs/gerrit.config
+++ b/setup_local_env/configs/gerrit.config
@@ -1,3 +1,5 @@
+[core]
+    usePerRequestRefCache = false
 [gerrit]
     basePath = git
     serverId = 69ec38f0-350e-4d9c-96d4-bc956f2faaac
@@ -39,8 +41,6 @@
     directory = cache
 [plugins]
     allowRemoteAdmin = true
-[plugin "websession-flatfile"]
-    directory = $FAKE_NFS
 [plugin "events-kafka"]
     sendAsync = true
     bootstrapServers = $BROKER_HOST:$BROKER_PORT
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/MultisiteReplicationFetchFilter.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/MultisiteReplicationFetchFilter.java
index 98d1320..3fc57ae 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/MultisiteReplicationFetchFilter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/MultisiteReplicationFetchFilter.java
@@ -14,6 +14,7 @@
 
 package com.googlesource.gerrit.plugins.multisite.validation;
 
+import static com.googlesource.gerrit.plugins.multisite.validation.ProjectVersionRefUpdate.MULTI_SITE_VERSIONING_REF;
 import static com.googlesource.gerrit.plugins.replication.pull.PullReplicationLogger.repLog;
 
 import com.gerritforge.gerrit.globalrefdb.GlobalRefDbLockException;
@@ -64,6 +65,9 @@
           .filter(ref -> !hasBeenRemovedFromGlobalRefDb(projectName, ref))
           .filter(
               ref -> {
+                if (shouldNotBeTrackedAnymoreOnGlobalRefDb(ref)) {
+                  return true;
+                }
                 Optional<ObjectId> localRefOid =
                     getLocalSha1IfEqualsToExistingGlobalRefDb(
                         repository, projectName, refDb, ref, true);
@@ -76,7 +80,7 @@
                             ref,
                             oid.getName()));
 
-                return localRefOid.isEmpty();
+                return !localRefOid.isPresent();
               })
           .collect(Collectors.toSet());
     } catch (IOException ioe) {
@@ -87,6 +91,19 @@
     }
   }
 
+  /*
+   * Since ac43a5f94c773c9db7a73d44035961d69d13fa53 the 'refs/multi-site/version' is
+   * not updated anymore on the global-refdb; however, the values stored already
+   * on the global-refdb could get in the way and prevent replication from happening
+   * as expected.
+   *
+   * Exclude the 'refs/multi-site/version' from local vs. global refdb checking
+   * pretending that the global-refdb for that ref did not exist.
+   */
+  private boolean shouldNotBeTrackedAnymoreOnGlobalRefDb(String ref) {
+    return MULTI_SITE_VERSIONING_REF.equals(ref);
+  }
+
   /* If the ref to fetch has been set to all zeros on the global-refdb, it means
    * that whatever is the situation locally, we do not need to fetch it:
    * - If the remote still has it, fetching it will be useless because the global
@@ -129,7 +146,7 @@
                           .orElse(false))
               .map(Ref::getObjectId);
 
-      if (localRefObjectId.isEmpty() && retryWithRandomSleep) {
+      if (!localRefObjectId.isPresent() && retryWithRandomSleep) {
         randomSleepForMitigatingConditionWhereLocalRefHaveJustBeenChanged(
             projectName, localRefObjectId, ref);
         localRefObjectId =
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultisiteReplicationFetchFilterTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultisiteReplicationFetchFilterTest.java
index 9a7a3b3..e09f6f6 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultisiteReplicationFetchFilterTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultisiteReplicationFetchFilterTest.java
@@ -170,6 +170,24 @@
   }
 
   @Test
+  public void shouldNotFilterOutWhenRefsMultisiteVersionIsPresentInSharedRefDb() throws Exception {
+    String refsMultisiteVersionRef = ProjectVersionRefUpdate.MULTI_SITE_VERSIONING_REF;
+    RevCommit multiSiteVersionRef = newRef(refsMultisiteVersionRef);
+
+    doReturn(Optional.of(multiSiteVersionRef.getId().getName()))
+        .when(sharedRefDatabaseMock)
+        .get(eq(projectName), eq(refsMultisiteVersionRef), eq(String.class));
+
+    Set<String> refsToFetch = Set.of(refsMultisiteVersionRef);
+
+    MultisiteReplicationFetchFilter fetchFilter =
+        new MultisiteReplicationFetchFilter(sharedRefDatabaseMock, gitRepositoryManager, config);
+    Set<String> filteredRefsToFetch = fetchFilter.filter(project, refsToFetch);
+
+    assertThat(filteredRefsToFetch).hasSize(1);
+  }
+
+  @Test
   public void shouldFilterOutWhenRefIsDeletedInTheSharedRefDb() throws Exception {
     String temporaryOutdated = "refs/heads/temporaryOutdated";
     newRef(temporaryOutdated);