Check for existence of change's target SHA1 for reindexing

When the target branch of a change was advancing rapidly,
the reindexing retries applied by the ForwardedIndexChangeHandler
were continuing potentially forever as they were waiting to
see the target branch's expected SHA1 for the event.

Whilst it is vital to wait for a target branch SHA1 to appear before
reindexing a change on a remote site, it isn't essential to have
that SHA1 being *exactly* the tip of the target branch, as that
moment can be easily missed.

Example:
- Create two changes C1 and C2 in parallel and push them with
  %submit option
- The changes C1 and C2 will trigger the rapid advance of their
  target SHA1
- The other nodes may reindex C1 but not C2 because the target branch
  has advanced more than expected

Checking simply the existence of the target SHA1 in the repository
is enough to satisfy the conditions for the change being reindexed.

This is a cherry-pick from multi-site afb24239.
The change has been adapted to high-availability: use the same checking
logic described above also against the change meta ref.

Change-Id: Ie45c2f1e209952ae7f2a91dc6b65c0c153f48376
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/index/ChangeCheckerImpl.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/index/ChangeCheckerImpl.java
index 1a2ebf7..a8ba722 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/index/ChangeCheckerImpl.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/index/ChangeCheckerImpl.java
@@ -31,6 +31,7 @@
 import java.sql.Timestamp;
 import java.util.Objects;
 import java.util.Optional;
+import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
 
@@ -107,9 +108,9 @@
           return (computedChangeTs.get() > indexEvent.eventCreatedOn)
               || (computedChangeTs.get() == indexEvent.eventCreatedOn)
                   && (Objects.isNull(indexEvent.targetSha)
-                      || Objects.equals(getBranchTargetSha(), indexEvent.targetSha))
-                  && (Objects.isNull(indexEvent.metaSha)
-                      || Objects.equals(getMetaSha(repo), indexEvent.metaSha));
+                      || repositoryHas(repo, indexEvent.targetSha))
+                  && (Objects.isNull(indexEvent.targetSha)
+                      || repositoryHas(repo, indexEvent.metaSha));
         }
       }
       return true;
@@ -161,6 +162,16 @@
     }
   }
 
+  private boolean repositoryHas(Repository repo, String sha1ToCheck) {
+    try {
+      return repo.parseCommit(ObjectId.fromString(sha1ToCheck)) != null;
+    } catch (IOException e) {
+      log.atWarning().withCause(e).log(
+          "Unable to find SHA1 %s for change %s", sha1ToCheck, changeId);
+      return false;
+    }
+  }
+
   private Optional<Long> computeLastChangeTs() {
     return getChangeNotes().map(this::getTsFromChangeAndDraftComments);
   }