dongjoon-hyun commented on code in PR #12: URL: https://github.com/apache/spark-kubernetes-operator/pull/12#discussion_r1640158931
########## spark-operator/src/main/java/org/apache/spark/k8s/operator/client/RetryInterceptor.java: ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.spark.k8s.operator.client; + +import static io.fabric8.kubernetes.client.utils.Utils.closeQuietly; + +import java.io.IOException; +import java.util.Optional; +import java.util.concurrent.TimeUnit; + +import lombok.Builder; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; +import org.apache.commons.lang3.StringUtils; + +/** Intercepts HTTP requests and add custom retry on 429 and 5xx to overcome server instability */ +@Slf4j +@RequiredArgsConstructor +@Builder +public class RetryInterceptor implements Interceptor { + private static final String RETRY_AFTER_HEADER_NAME = "Retry-After"; + + private final Long maxAttemptCount; + private final Long maxRetryAfterInSecs; + private final Long defaultRetryAfterInSecs; + + @Override + public Response intercept(Chain chain) throws IOException { + Request request = chain.request(); + Response response = chain.proceed(request); + int tryCount = 0; + while (!response.isSuccessful() + && (response.code() == 429 || response.code() >= 500) + && tryCount < maxAttemptCount) { + // only retry on consecutive 429 and 5xx failure responses + if (log.isWarnEnabled()) { + log.warn( + "Request is not successful. attempt={} response-code={} response-headers={}", + tryCount, + response.code(), + response.headers()); + } + Optional<Long> retryAfter = getRetryAfter(response); + if (retryAfter.isPresent()) { + try { + TimeUnit.SECONDS.sleep(retryAfter.get()); + } catch (InterruptedException e) { + if (log.isErrorEnabled()) { + log.error("Aborting retry.", e); + } + } + } + tryCount++; + + ResponseBody responseBody = response.body(); + if (responseBody != null) { + closeQuietly(responseBody); + } + // retry the request for 429 and 5xx + response = chain.proceed(request); + } + return response; + } + + private Optional<Long> getRetryAfter(Response response) { + String retryAfter = response.header(RETRY_AFTER_HEADER_NAME); + if (StringUtils.isNotEmpty(retryAfter)) { + try { + return Optional.of(Math.min(Long.parseLong(retryAfter), maxRetryAfterInSecs)); + } catch (Exception e) { Review Comment: Please use a specific exception instead of `Exception`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org