org.eclipse.ditto.internal.utils.cacheloaders.AskWithRetry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ditto-internal-utils-cache-loaders Show documentation
Show all versions of ditto-internal-utils-cache-loaders Show documentation
Eclipse Ditto is a framework for creating and managing digital twins in the IoT.
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.cacheloaders;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BiFunction;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.exceptions.AskException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.internal.utils.cacheloaders.config.AskWithRetryConfig;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Scheduler;
import org.apache.pekko.pattern.AskTimeoutException;
import org.apache.pekko.pattern.Patterns;
import scala.compat.java8.FutureConverters;
/**
* Helper/Pattern class providing an "ask with retry" pattern based on a provided {@link AskWithRetryConfig}.
*/
public final class AskWithRetry {
private static final DittoRuntimeException DUMMY_DRE = new DittoRuntimeException("dummy",
HttpStatus.INTERNAL_SERVER_ERROR, DittoHeaders.empty(), null, null, null, null) {
@Override
public DittoRuntimeException setDittoHeaders(final DittoHeaders dittoHeaders) {
return this;
}
};
private static final ThreadSafeDittoLogger LOGGER = DittoLoggerFactory.getThreadSafeLogger(AskWithRetry.class);
private AskWithRetry() {
throw new AssertionError();
}
/**
* Dispatcher name to use for AskWithRetry.
*/
public static final String ASK_WITH_RETRY_DISPATCHER = "ask-with-retry-dispatcher";
/**
* Performs the "ask with retry" pattern by asking the passed in {@code actorToAsk} the passed in {@code message},
* mapping a successful response with the provided {@code responseMapper} and retrying the operation on Exceptions
* which are not {@link DittoRuntimeException}s based on the given {@code config}.
*
* @param actorToAsk the actor to ask the message.
* @param message the message to ask.
* @param config the "ask with retry" configuration to apply, e.g. whether to do retries at all,
* with which timeouts, with how many retries and delays, etc.
* @param actorSystem the actorSystem for looking up the scheduler and dispatcher to use.
* @param responseMapper a function converting the response of the asked message.
* @param the type of the message to ask.
* @param the type of the answer.
* @return a CompletionStage which is completed by applying the passed in {@code responseMapper} function on the
* response of the asked message or which is completed exceptionally with the Exception.
*/
public static CompletionStage askWithRetry(final ActorRef actorToAsk,
final M message,
final AskWithRetryConfig config,
final ActorSystem actorSystem,
final Function