Merge changes I50c19da7,I1d58e8ce,Id2bc05da,I758a0766,I57c36e30

* changes:
  FetchOne: remove unused local variable "cause"
  DeleteRefCommand: remove unused field applyObject
  CGitFetchValidator: remove redundant specification of type argument
  Refactor ReplicationQueue to pass around ReferenceUpdatedEvent
  Fix IllegalArgumentException if no replication sources are configured
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d392f0e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.jar
diff --git a/example-setup/Dockerfile b/example-setup/Dockerfile
new file mode 100644
index 0000000..368f26e
--- /dev/null
+++ b/example-setup/Dockerfile
@@ -0,0 +1,24 @@
+FROM gerritcodereview/gerrit:3.5.4-almalinux8
+
+USER root
+
+RUN yum update -y &&\
+    yum install -y gettext
+
+ARG JAVA_OPTS='--add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/java.lang.invoke=ALL-UNNAMED'
+ARG GERRIT_BRANCH=stable-3.5
+ARG LAST_BUILD=lastSuccessfulBuild/artifact/bazel-bin/plugins
+
+RUN  java $JAVA_OPTS -jar /var/gerrit/bin/gerrit.war init --batch --install-all-plugins -d /var/gerrit && \
+    java $JAVA_OPTS -jar /var/gerrit/bin/gerrit.war reindex -d /var/gerrit
+
+RUN git config -f /var/gerrit/etc/secure.config --add auth.bearerToken "theSecretBearerToken"
+
+COPY --chown=gerrit:gerrit pull-replication.jar /var/gerrit/plugins/pull-replication.jar
+COPY --chown=gerrit:gerrit pull-replication.jar /var/gerrit/lib/pull-replication.jar
+COPY --chown=gerrit:gerrit entrypoint.sh /tmp/
+RUN chmod +x /tmp/entrypoint.sh
+COPY --chown=gerrit:gerrit configs/replication.config.template /var/gerrit/etc/
+COPY --chown=gerrit:gerrit configs/gerrit.config.template /var/gerrit/etc/
+
+ENTRYPOINT [ "/tmp/entrypoint.sh" ]
\ No newline at end of file
diff --git a/example-setup/README.md b/example-setup/README.md
new file mode 100644
index 0000000..e52ad45
--- /dev/null
+++ b/example-setup/README.md
@@ -0,0 +1,17 @@
+# What is this for?
+
+This docker compose set up a primary replica using pull-replication to replicate the data
+over git http across the 2 nodes.
+
+To spin up your environment:
+1) copy the pull-replication artefact to test in te example-setup directory:
+
+```bash
+cp $GERRIT_HOME/bazel-bin/plugins/pull-replication/pull-replication.jar .
+```
+
+2) spin up the docker compose
+
+```bash
+docker-compose up
+```
\ No newline at end of file
diff --git a/example-setup/configs/gerrit.config.template b/example-setup/configs/gerrit.config.template
new file mode 100644
index 0000000..8bc8391
--- /dev/null
+++ b/example-setup/configs/gerrit.config.template
@@ -0,0 +1,27 @@
+[gerrit]
+    basePath = git
+    serverId = 69ec38f0-350e-4d9c-96d4-bc956f2faaac
+    canonicalWebUrl = http://localhost:8080
+    instanceId = $INSTANCE_ID
+[container]
+    javaOptions = "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
+    javaOptions = "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
+    replica = $REPLICA
+[index]
+    type = LUCENE
+[auth]
+    type = DEVELOPMENT_BECOME_ANY_ACCOUNT
+[receive]
+    enableSignedPush = false
+[sendemail]
+    smtpServer = localhost
+[sshd]
+    listenAddress = *:29418
+    advertisedAddress = *:29418
+[httpd]
+    listenUrl = http://*:8080/
+    requestLog = true
+[cache]
+    directory = cache
+[plugins]
+    allowRemoteAdmin = true
diff --git a/example-setup/configs/replication.config.template b/example-setup/configs/replication.config.template
new file mode 100644
index 0000000..2939297
--- /dev/null
+++ b/example-setup/configs/replication.config.template
@@ -0,0 +1,25 @@
+[gerrit]
+    autoReload = true
+    replicateOnStartup = false
+[replication]
+    excludeRefs = ^refs/users/\\d\\d/\\d+/edit-\\d+/\\d+$
+    lockErrorMaxRetries = 5
+    maxRetries = 100
+    useCGitClient = false
+    consumeStreamEvents = false
+    syncRefs="ALL REFS ASYNC"
+    maxApiPayloadSize=40000
+[remote "$REMOTE"]
+    url = http://$REMOTE_URL:8080/a/#{name}#.git
+    apiUrl = http://$REMOTE_URL:8080
+    fetch = +refs/*:refs/*
+    mirror = true
+    timeout = 60 # In seconds
+    connectionTimeout = 120000 # In mseconds
+    rescheduleDelay = 15
+    replicationDelay = 1
+    threads = 4
+    createMissingRepositories = true
+    replicateProjectDeletions = true
+    replicateHiddenProjects = true
+    tagopt= --no-tags
\ No newline at end of file
diff --git a/example-setup/docker-compose.yaml b/example-setup/docker-compose.yaml
new file mode 100644
index 0000000..b36f426
--- /dev/null
+++ b/example-setup/docker-compose.yaml
@@ -0,0 +1,24 @@
+version: '3'
+services:
+  gerrit1:
+    build: .
+    environment:
+      - INSTANCE_ID=primary
+      - REPLICA=false
+      - REMOTE=replica-1
+      - REMOTE_URL=gerrit2
+    ports:
+      - "8080:8080"
+      - "29418:29418"
+  gerrit2:
+    build: .
+    environment:
+      - INSTANCE_ID=replica-1
+      - REPLICA=true
+      - REMOTE=primary
+      - REMOTE_URL=gerrit1
+    ports:
+      - "8081:8080"
+      - "29419:29418"
+    depends_on:
+      - gerrit1
\ No newline at end of file
diff --git a/example-setup/entrypoint.sh b/example-setup/entrypoint.sh
new file mode 100644
index 0000000..412c487
--- /dev/null
+++ b/example-setup/entrypoint.sh
@@ -0,0 +1,20 @@
+#!/bin/bash -x
+
+function setup_replication_config {
+
+  echo "Replacing variables for file /var/gerrit/etc/replication.config.template"
+  cat /var/gerrit/etc/replication.config.template | envsubst | sed 's/#{name}#/${name}/g' > /var/gerrit/etc/replication.config
+
+  cat /var/gerrit/etc/replication.config
+}
+
+function setup_gerrit_config {
+  echo "Replacing variables for file /var/gerrit/etc/gerrit.config.template"
+  cat /var/gerrit/etc/gerrit.config.template | envsubst > /var/gerrit/etc/gerrit.config
+}
+
+setup_replication_config
+setup_gerrit_config
+
+echo "Running Gerrit ..."
+exec /var/gerrit/bin/gerrit.sh run
\ No newline at end of file
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchOne.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchOne.java
index d8da65e..eb543a0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchOne.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchOne.java
@@ -328,7 +328,6 @@
     } catch (NotSupportedException e) {
       stateLog.error("Cannot replicate from " + uri, e, getStatesAsArray());
     } catch (TransportException e) {
-      Throwable cause = e.getCause();
       if (e instanceof LockFailureException) {
         lockRetryCount++;
         // The LockFailureException message contains both URI and reason
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/ReplicationQueue.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/ReplicationQueue.java
index 380ac8e..f5b27ee 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/ReplicationQueue.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/ReplicationQueue.java
@@ -156,11 +156,7 @@
           event.getRefName(),
           event.getOldObjectId(),
           event.getNewObjectId());
-      fire(
-          event.getProjectName(),
-          ObjectId.fromString(event.getNewObjectId()),
-          event.getRefName(),
-          event.isDelete());
+      fire(ReferenceUpdatedEvent.from(event));
     }
   }
 
@@ -189,34 +185,39 @@
     return !refsFilter.match(refName);
   }
 
-  private void fire(String projectName, ObjectId objectId, String refName, boolean isDelete) {
+  private void fire(ReferenceUpdatedEvent event) {
     ReplicationState state = new ReplicationState(new GitUpdateProcessing(dispatcher.get()));
-    fire(Project.nameKey(projectName), objectId, refName, isDelete, state);
+    fire(event, state);
     state.markAllFetchTasksScheduled();
   }
 
-  private void fire(
-      Project.NameKey project,
-      ObjectId objectId,
-      String refName,
-      boolean isDelete,
-      ReplicationState state) {
+  private void fire(ReferenceUpdatedEvent event, ReplicationState state) {
     if (!running) {
       stateLog.warn(
           "Replication plugin did not finish startup before event, event replication is postponed",
           state);
-      beforeStartupEventsQueue.add(
-          ReferenceUpdatedEvent.create(project.get(), refName, objectId, isDelete));
+      beforeStartupEventsQueue.add(event);
       return;
     }
     ForkJoinPool fetchCallsPool = null;
     try {
-      fetchCallsPool = new ForkJoinPool(sources.get().getAll().size());
+      List<Source> allSources = sources.get().getAll();
+      int numSources = allSources.size();
+      if (numSources == 0) {
+        repLog.debug("No replication sources configured -> skipping fetch");
+        return;
+      }
+      fetchCallsPool = new ForkJoinPool(numSources);
 
       final Consumer<Source> callFunction =
-          callFunction(project, objectId, refName, isDelete, state);
+          callFunction(
+              Project.nameKey(event.projectName()),
+              event.objectId(),
+              event.refName(),
+              event.isDelete(),
+              state);
       fetchCallsPool
-          .submit(() -> sources.get().getAll().parallelStream().forEach(callFunction))
+          .submit(() -> allSources.parallelStream().forEach(callFunction))
           .get(fetchCallsTimeout, TimeUnit.MILLISECONDS);
     } catch (InterruptedException | ExecutionException | TimeoutException e) {
       stateLog.error(
@@ -491,7 +492,7 @@
       String eventKey = String.format("%s:%s", event.projectName(), event.refName());
       if (!eventsReplayed.contains(eventKey)) {
         repLog.info("Firing pending task {}", event);
-        fire(event.projectName(), event.objectId(), event.refName(), event.isDelete());
+        fire(event);
         eventsReplayed.add(eventKey);
       }
     }
@@ -517,6 +518,14 @@
           projectName, refName, objectId, isDelete);
     }
 
+    static ReferenceUpdatedEvent from(GitReferenceUpdatedListener.Event event) {
+      return ReferenceUpdatedEvent.create(
+          event.getProjectName(),
+          event.getRefName(),
+          ObjectId.fromString(event.getNewObjectId()),
+          event.isDelete());
+    }
+
     public abstract String projectName();
 
     public abstract String refName();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommand.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommand.java
index d86287a..fd62910 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommand.java
@@ -36,7 +36,6 @@
 import com.googlesource.gerrit.plugins.replication.pull.ReplicationState;
 import com.googlesource.gerrit.plugins.replication.pull.Source;
 import com.googlesource.gerrit.plugins.replication.pull.SourcesCollection;
-import com.googlesource.gerrit.plugins.replication.pull.fetch.ApplyObject;
 import com.googlesource.gerrit.plugins.replication.pull.fetch.RefUpdateState;
 import java.io.IOException;
 import java.util.Optional;
@@ -49,7 +48,6 @@
   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
 
   private final PullReplicationStateLogger fetchStateLog;
-  private final ApplyObject applyObject;
   private final DynamicItem<EventDispatcher> eventDispatcher;
   private final ProjectCache projectCache;
   private final SourcesCollection sourcesCollection;
@@ -61,13 +59,11 @@
       PullReplicationStateLogger fetchStateLog,
       ProjectCache projectCache,
       SourcesCollection sourcesCollection,
-      ApplyObject applyObject,
       PermissionBackend permissionBackend,
       DynamicItem<EventDispatcher> eventDispatcher,
       LocalGitRepositoryManagerProvider gitManagerProvider) {
     this.fetchStateLog = fetchStateLog;
     this.projectCache = projectCache;
-    this.applyObject = applyObject;
     this.eventDispatcher = eventDispatcher;
     this.sourcesCollection = sourcesCollection;
     this.permissionBackend = permissionBackend;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/fetch/CGitFetchValidator.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/fetch/CGitFetchValidator.java
index 9a10898..434c0f0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/fetch/CGitFetchValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/fetch/CGitFetchValidator.java
@@ -35,7 +35,7 @@
 
   @Override
   public Void visit(AssistedInjectBinding<? extends FetchFactory> binding) {
-    TypeLiteral<CGitFetch> nativeGitFetchType = new TypeLiteral<CGitFetch>() {};
+    TypeLiteral<CGitFetch> nativeGitFetchType = new TypeLiteral<>() {};
     for (AssistedMethod method : binding.getAssistedMethods()) {
       if (method.getImplementationType().equals(nativeGitFetchType)) {
         String[] command = new String[] {"git", "--version"};
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommandTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommandTest.java
index 2adf72f..efa931f 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommandTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommandTest.java
@@ -102,7 +102,6 @@
             fetchStateLog,
             projectCache,
             sourceCollection,
-            applyObject,
             permissionBackend,
             eventDispatcherDataItem,
             new LocalGitRepositoryManagerProvider(gitManager));