Skip to content

Commit 39675c5

Browse files
author
jeffreykzli
committed
not retry for preflight upload
1 parent de4975c commit 39675c5

File tree

7 files changed

+45
-2
lines changed

7 files changed

+45
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.qcloud</groupId>
66
<artifactId>cos_api</artifactId>
7-
<version>5.6.240.2</version>
7+
<version>5.6.242</version>
88
<packaging>jar</packaging>
99
<name>cos-java-sdk</name>
1010
<description>java sdk for qcloud cos</description>

src/main/java/com/qcloud/cos/COSClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,8 @@ ObjectMetadata uploadObjectInternal(UploadMode uploadMode, UploadObjectRequest u
843843
try {
844844
preflightObj(uploadObjectRequest);
845845
} catch (CosServiceException cse) {
846-
log.warn("fail to do the preflight request due to the service exception, will not do the upload obj request", cse);
846+
String msg = String.format("fail to do the preflight request due to the service exception[statusCode:%s, requestId:%s], will not do the upload obj request", cse.getStatusCode(), cse.getRequestId());
847+
log.warn(msg);
847848
throw cse;
848849
} catch (CosClientException cce) {
849850
log.warn("fail to do the preflight request due to the client exception, will not do the upload obj request", cce);
@@ -5544,6 +5545,7 @@ private void preflightObj(PutObjectRequest putObjectRequest) throws CosClientExc
55445545
if (clientConfig.isCheckPreflightStatus() && preflightBuckets.containsKey(bucketName)) {
55455546
String reqMsg = String.format("will do preflight request for put object[%s] to the bucket[%s]", key, bucketName);
55465547
log.debug(reqMsg);
5548+
putObjectRequest.setHasDonePreflight(true);
55475549
CosServiceRequest serviceRequest = new CosServiceRequest();
55485550
Map<String, String> customHeaders = putObjectRequest.getCustomRequestHeaders();
55495551
if (customHeaders != null) {

src/main/java/com/qcloud/cos/ClientConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public class ClientConfig {
135135

136136
private boolean useConnectionMonitor = false;
137137

138+
private boolean retryAfterPreflight = false;
139+
138140
private long connectionMaxIdleMillis = 60 * 1000;
139141

140142
// 不传入region 用于后续调用List Buckets(获取所有的bucket信息)
@@ -497,4 +499,12 @@ public long getConnectionMaxIdleMillis() {
497499
public void setConnectionMaxIdleMillis(long connectionMaxIdleMillis) {
498500
this.connectionMaxIdleMillis = connectionMaxIdleMillis;
499501
}
502+
503+
public boolean isRetryAfterPreflight() {
504+
return retryAfterPreflight;
505+
}
506+
507+
public void setRetryAfterPreflight(boolean retryAfterPreflight) {
508+
this.retryAfterPreflight = retryAfterPreflight;
509+
}
500510
}

src/main/java/com/qcloud/cos/http/DefaultCosHttpClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public InetAddress[] resolve(String host) throws UnknownHostException {
181181
}
182182
this.maxErrorRetry = clientConfig.getMaxErrorRetry();
183183
this.retryPolicy = ValidationUtils.assertNotNull(clientConfig.getRetryPolicy(), "retry policy");
184+
this.retryPolicy.setRetryAfterPreflight(clientConfig.isRetryAfterPreflight());
184185
this.backoffStrategy = ValidationUtils.assertNotNull(clientConfig.getBackoffStrategy(), "backoff strategy");
185186
this.cosHttpClientTimer = new CosHttpClientTimer();
186187
initHttpClient();

src/main/java/com/qcloud/cos/model/PutObjectRequest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
*/
8181
public class PutObjectRequest extends AbstractPutObjectRequest implements Serializable {
8282
private boolean enableResumableUpload = false;
83+
84+
private boolean hasDonePreflight = false;
8385
/**
8486
* Constructs a new
8587
* {@link PutObjectRequest} object to upload a file to the
@@ -236,4 +238,12 @@ public void setEnableResumableUpload(boolean useResumableUpload) {
236238
public boolean isEnableResumableUpload() {
237239
return enableResumableUpload;
238240
}
241+
242+
public boolean hasDonePreflight() {
243+
return hasDonePreflight;
244+
}
245+
246+
public void setHasDonePreflight(boolean hasDonePreflight) {
247+
this.hasDonePreflight = hasDonePreflight;
248+
}
239249
}

src/main/java/com/qcloud/cos/retry/PredefinedRetryPolicies.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
package com.qcloud.cos.retry;
1919

2020
import java.io.IOException;
21+
import java.util.Objects;
2122

23+
import com.qcloud.cos.exception.CosServiceException;
2224
import com.qcloud.cos.http.CosHttpRequest;
2325
import com.qcloud.cos.internal.CosServiceRequest;
2426

27+
import com.qcloud.cos.model.PutObjectRequest;
2528
import org.apache.http.HttpResponse;
2629
import org.apache.http.client.CircularRedirectException;
2730
import org.slf4j.Logger;
@@ -63,6 +66,14 @@ public <X extends CosServiceRequest> boolean shouldRetry(CosHttpRequest<X> reque
6366
if (request.getParameters().containsKey("preflight")) {
6467
return false;
6568
}
69+
70+
if (!isRetryAfterPreflight() && request.getOriginalRequest() != null && request.getOriginalRequest() instanceof PutObjectRequest && ((PutObjectRequest) request.getOriginalRequest()).hasDonePreflight()) {
71+
if (((CosServiceException) exception).getStatusCode() == 503 && Objects.equals(((CosServiceException) exception).getErrorCode(), "SlowDown")) {
72+
log.info("will not retry for 503 while putting object, because preflight request has been done");
73+
return false;
74+
}
75+
}
76+
6677
return true;
6778
}
6879

src/main/java/com/qcloud/cos/retry/RetryPolicy.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
import org.apache.http.HttpResponse;
2323

2424
public abstract class RetryPolicy {
25+
private boolean retryAfterPreflight = false;
26+
27+
public void setRetryAfterPreflight(boolean retryAfterPreflight) {
28+
this.retryAfterPreflight = retryAfterPreflight;
29+
}
30+
31+
public boolean isRetryAfterPreflight() {
32+
return retryAfterPreflight;
33+
}
2534

2635
public abstract <X extends CosServiceRequest> boolean shouldRetry(CosHttpRequest<X> request,
2736
HttpResponse response,

0 commit comments

Comments
 (0)