Merge branch 'stable-3.1' into stable-3.2

* stable-3.1:
  Block Fetch Rest API call when instanceLabel is missing
  Execute refs fetch in batches
  Add native git fetch support
  Make sure replication task clean up is triggered
  Fix issue with replication when replicationDelay is set to zero

Change-Id: I7cf52860e28c251bf7d7c36ef9e125305ccdfbf5
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java
index e9bb620..8363aee 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java
@@ -29,6 +29,7 @@
 import com.google.gerrit.entities.BranchNameKey;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.entities.RefNames;
+import com.google.gerrit.exceptions.StorageException;
 import com.google.gerrit.extensions.config.FactoryModule;
 import com.google.gerrit.extensions.registration.DynamicItem;
 import com.google.gerrit.extensions.restapi.AuthException;
@@ -289,19 +290,19 @@
               new Callable<Boolean>() {
                 @Override
                 public Boolean call() throws NoSuchProjectException, PermissionBackendException {
-                  ProjectState projectState;
+                  Optional<ProjectState> projectState;
                   try {
-                    projectState = projectCache.checkedGet(project);
-                  } catch (IOException e) {
+                    projectState = projectCache.get(project);
+                  } catch (StorageException e) {
                     return false;
                   }
-                  if (projectState == null) {
+                  if (!projectState.isPresent()) {
                     throw new NoSuchProjectException(project);
                   }
-                  if (!projectState.statePermitsRead()) {
+                  if (!projectState.get().statePermitsRead()) {
                     return false;
                   }
-                  if (!shouldReplicate(projectState, userProvider.get())) {
+                  if (!shouldReplicate(projectState.get(), userProvider.get())) {
                     return false;
                   }
                   if (FetchOne.ALL_REFS.equals(ref)) {
@@ -336,16 +337,16 @@
               new Callable<Boolean>() {
                 @Override
                 public Boolean call() throws NoSuchProjectException, PermissionBackendException {
-                  ProjectState projectState;
+                  Optional<ProjectState> projectState;
                   try {
-                    projectState = projectCache.checkedGet(project);
-                  } catch (IOException e) {
+                    projectState = projectCache.get(project);
+                  } catch (StorageException e) {
                     return false;
                   }
-                  if (projectState == null) {
+                  if (!projectState.isPresent()) {
                     throw new NoSuchProjectException(project);
                   }
-                  return shouldReplicate(projectState, userProvider.get());
+                  return shouldReplicate(projectState.get(), userProvider.get());
                 }
               })
           .call();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchAction.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchAction.java
index 6970299..c44bcbc 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchAction.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchAction.java
@@ -84,7 +84,7 @@
         | ExecutionException
         | IllegalStateException
         | TimeoutException e) {
-      throw new RestApiException(e.getMessage(), e);
+      throw RestApiException.wrap(e.getMessage(), e);
     } catch (RemoteConfigurationMissingException e) {
       throw new UnprocessableEntityException(e.getMessage());
     }