com.google.gerrit.server.update.RetryableIndexQueryAction Maven / Gradle / Ivy
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.update;
import com.github.rholder.retry.RetryListener;
import com.google.common.base.Throwables;
import com.google.gerrit.exceptions.StorageException;
import com.google.gerrit.index.query.InternalQuery;
import com.google.inject.Provider;
import java.util.function.Consumer;
import java.util.function.Predicate;
/**
 * An action to query an index that is executed with retrying.
 *
 * Instances of this class are created via {@link RetryHelper#accountIndexQuery(String,
 * IndexQueryAction)} and {@link RetryHelper#changeIndexQuery(String, IndexQueryAction)}.
 *
 * 
In contrast to normal {@link RetryableAction.Action}s that are called via {@link
 * RetryableAction} {@link IndexQueryAction}s get a {@link InternalQuery} provided.
 *
 * 
In addition when an index query action is called any exception that is not an unchecked
 * exception gets wrapped into an {@link StorageException}.
 */
public class RetryableIndexQueryAction, T>
    extends RetryableAction {
  @FunctionalInterface
  public interface IndexQueryAction {
    T call(Q internalQuery) throws Exception;
  }
  RetryableIndexQueryAction(
      RetryHelper retryHelper,
      Provider internalQuery,
      String actionName,
      IndexQueryAction indexQuery) {
    super(
        retryHelper,
        ActionType.INDEX_QUERY,
        actionName,
        () -> indexQuery.call(internalQuery.get()));
  }
  @Override
  public RetryableIndexQueryAction retryOn(Predicate exceptionPredicate) {
    super.retryOn(exceptionPredicate);
    return this;
  }
  @Override
  public RetryableIndexQueryAction retryWithTrace(Predicate exceptionPredicate) {
    super.retryWithTrace(exceptionPredicate);
    return this;
  }
  @Override
  public RetryableIndexQueryAction onAutoTrace(Consumer traceIdConsumer) {
    super.onAutoTrace(traceIdConsumer);
    return this;
  }
  @Override
  public RetryableIndexQueryAction listener(RetryListener retryListener) {
    super.listener(retryListener);
    return this;
  }
  @Override
  public RetryableIndexQueryAction defaultTimeoutMultiplier(int multiplier) {
    super.defaultTimeoutMultiplier(multiplier);
    return this;
  }
  @Override
  public T call() {
    try {
      return super.call();
    } catch (Exception e) {
      Throwables.throwIfUnchecked(e);
      throw new StorageException(e);
    }
  }
}