net.snowflake.ingest.streaming.internal.StreamingIngestUtils Maven / Gradle / Ivy
package net.snowflake.ingest.streaming.internal;
import static net.snowflake.ingest.utils.Constants.MAX_STREAMING_INGEST_API_CHANNEL_RETRY;
import static net.snowflake.ingest.utils.Constants.RESPONSE_ERR_GENERAL_EXCEPTION_RETRY_REQUEST;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;
import java.util.function.Function;
import net.snowflake.client.jdbc.internal.apache.http.client.methods.CloseableHttpResponse;
import net.snowflake.client.jdbc.internal.apache.http.client.methods.HttpUriRequest;
import net.snowflake.client.jdbc.internal.apache.http.impl.client.CloseableHttpClient;
import net.snowflake.ingest.connection.IngestResponseException;
import net.snowflake.ingest.connection.RequestBuilder;
import net.snowflake.ingest.connection.ServiceResponseHandler;
import net.snowflake.ingest.utils.ErrorCode;
import net.snowflake.ingest.utils.Logging;
import net.snowflake.ingest.utils.SFException;
public class StreamingIngestUtils {
private static class DefaultStatusGetter
implements Function {
public DefaultStatusGetter() {}
public Long apply(T input) {
return input.getStatusCode();
}
}
private static final DefaultStatusGetter defaultStatusGetter = new DefaultStatusGetter();
private static final Logging LOGGER = new Logging(StreamingIngestUtils.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* How many milliseconds of exponential backoff to sleep before retrying the request again:
*
*
* - 0 or 1 failure => no sleep
*
- 2 failures => 1s
*
- 3 failures => 2s
*
- 4 or more failures => 4s
*
*
* @param executionCount How many unsuccessful attempts have been attempted
* @return Sleep time in ms
*/
static long getSleepForRetryMs(int executionCount) {
if (executionCount < 0) {
throw new IllegalArgumentException(
String.format(
"executionCount must be a non-negative integer, passed: %d", executionCount));
} else if (executionCount < 2) {
return 0;
} else {
final int effectiveExecutionCount = Math.min(executionCount, 4);
return (1 << (effectiveExecutionCount - 2)) * 1000L;
}
}
public static void sleepForRetry(int executionCount) {
long sleepForRetryMs = getSleepForRetryMs(executionCount);
if (sleepForRetryMs == 0) {
return;
}
try {
Thread.sleep(sleepForRetryMs);
} catch (InterruptedException e) {
throw new SFException(ErrorCode.INTERNAL_ERROR, e.getMessage());
}
}
static T executeWithRetries(
Class targetClass,
String endpoint,
Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy