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

com.sap.hana.datalake.files.utils.http.ExponentialBackoffRetryStrategy Maven / Gradle / Ivy

Go to download

An implementation of org.apache.hadoop.fs.FileSystem targeting SAP HANA Data Lake Files.

There is a newer version: 3.0.27
Show newest version
// © 2023 SAP SE or an SAP affiliate company. All rights reserved.
package com.sap.hana.datalake.files.utils.http;

import com.sap.hana.datalake.files.classification.InterfaceAudience;
import org.apache.http.HttpResponse;
import org.apache.http.client.ServiceUnavailableRetryStrategy;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.IntPredicate;

@InterfaceAudience.Private
public class ExponentialBackoffRetryStrategy implements ServiceUnavailableRetryStrategy {

  private static final Logger LOG = LoggerFactory.getLogger(ExponentialBackoffRetryStrategy.class);

  private final int maxRetries;
  private final long maxIntervalMs;
  private final IntPredicate statusCodeCheck;

  private long nextRetryIntervalMs;

  public ExponentialBackoffRetryStrategy(final int maxRetries, final long minIntervalMs, final long maxIntervalMs,
                                         final IntPredicate statusCodeCheck) {
    this.maxRetries = maxRetries;
    this.maxIntervalMs = maxIntervalMs;
    this.nextRetryIntervalMs = minIntervalMs;
    this.statusCodeCheck = statusCodeCheck;
  }

  @Override
  public boolean retryRequest(final HttpResponse response, final int executionCount, final HttpContext context) {
    final boolean willBeRetried = executionCount <= this.maxRetries && this.statusCodeCheck.test(response.getStatusLine().getStatusCode());

    if (LOG.isDebugEnabled() && (executionCount > 1 || willBeRetried)) {
      LOG.debug("Request failed with status [{}], its been retried [{}] times and {}", response.getStatusLine().toString(), executionCount, willBeRetried ? "is to be retried again" : "won't be retried anymore");
    }

    return willBeRetried;
  }

  @Override
  public long getRetryInterval() {
    final long millisToSleep = Math.min(this.nextRetryIntervalMs, this.maxIntervalMs);
    this.nextRetryIntervalMs *= 2L;
    return millisToSleep;
  }

}

// © 2023 SAP SE or an SAP affiliate company. All rights reserved.




© 2015 - 2025 Weber Informatics LLC | Privacy Policy