Reduce boilerplate code to execute index queries with retry

To execute an index query with retry one currently needs to do:

  @Inject RetryHelper retryHelper;
  @Inject Provider<InternalChangeQuery> internalChangeQueryProvider;

  List<ChangeData> changes;
  try {
    changes = retryHelper
      .indexQuery(actionName, () -> internalChangeQueryProvider.get().byProject(project))
      .call();
  } catch (Exception e) {
    Throwables.throwIfUnchecked(e);
    throw new StorageException(e);
  }

The new code removes the need to

* get the InternalQuery injected
* catch and wrap exceptions

  @Inject RetryHelper retryHelper;

  List<ChangeData> changes = retryHelper
      .changeIndexQuery(actionName, q -> q.byProject(project))
      .call();

To make this work we add RetryableIndexQueryAction as a new subclass of
RetryableAction that defines an IndexQueryAction that provides the
InternalQuery and that handles the exceptions on call. This follows the
example of RetryableChangeAction that defines a ChangeAction that
provides a BatchUpdate.Factory and that handles the exceptions on call.

In result we can remove a bunch of executeIndexQuery methods that were
spread around the code base.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I9b577a827bf83d9ed5f11d1eac32a37e78063186
This commit is contained in:
Edwin Kempin
2019-12-11 10:47:44 +01:00
parent 02917c564e
commit 708b38359f
8 changed files with 176 additions and 90 deletions

View File

@@ -43,7 +43,6 @@ import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_RE
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableList;
@@ -167,7 +166,6 @@ import com.google.gerrit.server.update.Context;
import com.google.gerrit.server.update.RepoContext;
import com.google.gerrit.server.update.RepoOnlyOp;
import com.google.gerrit.server.update.RetryHelper;
import com.google.gerrit.server.update.RetryableAction.Action;
import com.google.gerrit.server.update.UpdateException;
import com.google.gerrit.server.util.LabelVote;
import com.google.gerrit.server.util.MagicBranch;
@@ -3315,9 +3313,11 @@ class ReceiveCommits {
for (String changeId : c.getFooterLines(FooterConstants.CHANGE_ID)) {
if (byKey == null) {
byKey =
executeIndexQuery(
"queryOpenChangesByKeyByBranch",
() -> openChangesByKeyByBranch(branch));
retryHelper
.changeIndexQuery(
"queryOpenChangesByKeyByBranch",
q -> openChangesByKeyByBranch(q, branch))
.call();
}
ChangeNotes onto = byKey.get(Change.key(changeId.trim()));
@@ -3397,20 +3397,12 @@ class ReceiveCommits {
}
}
private <T> T executeIndexQuery(String actionName, Action<T> action) {
try (TraceTimer traceTimer = newTimer("executeIndexQuery")) {
return retryHelper.indexQuery(actionName, action).call();
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new StorageException(e);
}
}
private Map<Change.Key, ChangeNotes> openChangesByKeyByBranch(BranchNameKey branch) {
private Map<Change.Key, ChangeNotes> openChangesByKeyByBranch(
InternalChangeQuery internalChangeQuery, BranchNameKey branch) {
try (TraceTimer traceTimer =
newTimer("openChangesByKeyByBranch", Metadata.builder().branchName(branch.branch()))) {
Map<Change.Key, ChangeNotes> r = new HashMap<>();
for (ChangeData cd : queryProvider.get().byBranchOpen(branch)) {
for (ChangeData cd : internalChangeQuery.byBranchOpen(branch)) {
try {
r.put(cd.change().getKey(), cd.notes());
} catch (NoSuchChangeException e) {