All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.gcp.RetryUtils Maven / Gradle / Ivy

There is a newer version: 5.0-BETA-1
Show newest version
/*
 * Copyright 2020 Hazelcast Inc.
 *
 * Licensed under the Hazelcast Community License (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at
 *
 * http://hazelcast.com/hazelcast-community-license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.hazelcast.gcp;

import com.hazelcast.core.HazelcastException;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * Static utility class to retry operations.
 */
final class RetryUtils {
    static final long INITIAL_BACKOFF_MS = 1500L;
    static final double BACKOFF_MULTIPLIER = 1.5;

    private static final long MS_IN_SECOND = 1000L;
    private static final long MAX_BACKOFF_MS = 5 * 60 * MS_IN_SECOND;
    private static final ILogger LOGGER = Logger.getLogger(RetryUtils.class);

    private RetryUtils() {
    }

    /**
     * Calls {@code callable.call()} until it does not throw an exception (but no more than {@code retries} times).
     * 

* Note that {@code callable} should be an idempotent operation. *

* If {@code callable} throws an unchecked exception, it is wrapped into {@link HazelcastException}. */ static T retry(Callable callable, int retries, List nonRetryableKeywords) { int retryCount = 0; while (true) { try { return callable.call(); } catch (Exception e) { retryCount++; if (retryCount > retries || containsAnyOf(e, nonRetryableKeywords)) { throw unchecked(e); } long waitIntervalMs = backoffIntervalForRetry(retryCount); LOGGER.warning(String.format("Couldn't connect to the service, [%s] retrying in %s seconds...", retryCount, waitIntervalMs / MS_IN_SECOND)); sleep(waitIntervalMs); } } } static T retry(Callable callable, int retries) { return retry(callable, retries, Collections.emptyList()); } private static boolean containsAnyOf(Exception e, List nonRetryableKeywords) { Throwable currentException = e; while (currentException != null) { String exceptionMessage = currentException.getMessage(); for (String keyword : nonRetryableKeywords) { if (exceptionMessage != null && exceptionMessage.contains(keyword)) { return true; } } currentException = currentException.getCause(); } return false; } private static long backoffIntervalForRetry(int retryCount) { long result = INITIAL_BACKOFF_MS; for (int i = 1; i < retryCount; i++) { result *= BACKOFF_MULTIPLIER; if (result > MAX_BACKOFF_MS) { return MAX_BACKOFF_MS; } } return result; } private static void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new HazelcastException(e); } } private static RuntimeException unchecked(Exception e) { if (e instanceof RuntimeException) { return (RuntimeException) e; } return new HazelcastException(e); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy