Skip to content

Commit a7667e2

Browse files
committed
Add a default retry strategy to InitiateMultipartUploadRequest.
1 parent b6d565d commit a7667e2

File tree

3 files changed

+115
-9
lines changed

3 files changed

+115
-9
lines changed

src/main/java/com/aliyun/oss/internal/OSSMultipartOperation.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import com.aliyun.oss.model.UploadPartResult;
8787
import com.aliyun.oss.model.Payer;
8888
import com.aliyun.oss.model.VoidResult;
89+
import com.aliyun.oss.model.WebServiceRequest;
8990

9091
/**
9192
* Multipart operation.
@@ -100,6 +101,14 @@ public OSSMultipartOperation(ServiceClient client, CredentialsProvider credsProv
100101
super(client, credsProvider);
101102
}
102103

104+
@Override
105+
protected boolean isRetryablePostRequest(WebServiceRequest request) {
106+
if (request instanceof InitiateMultipartUploadRequest) {
107+
return true;
108+
}
109+
return super.isRetryablePostRequest(request);
110+
}
111+
103112
/**
104113
* Abort multipart upload.
105114
*/

src/main/java/com/aliyun/oss/internal/OSSOperation.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected <T> T doOperation(RequestMessage request, ResponseParser<T> parser, St
114114
request.getHeaders().putAll(originalRequest.getHeaders());
115115
request.getParameters().putAll(originalRequest.getParameters());
116116

117-
ExecutionContext context = createDefaultContext(request.getMethod(), bucketName, key);
117+
ExecutionContext context = createDefaultContext(request.getMethod(), bucketName, key, originalRequest);
118118

119119
if (context.getCredentials().useSecurityToken() && !request.isUseUrlSignature()) {
120120
request.addHeader(OSSHeaders.OSS_SECURITY_TOKEN, context.getCredentials().getSecurityToken());
@@ -163,14 +163,14 @@ private static RequestSigner createSigner(HttpMethod method, String bucketName,
163163
return new OSSRequestSigner(method.toString(), resourcePath, creds, signatureVersion);
164164
}
165165

166-
protected ExecutionContext createDefaultContext(HttpMethod method, String bucketName, String key) {
166+
protected ExecutionContext createDefaultContext(HttpMethod method, String bucketName, String key, WebServiceRequest originalRequest) {
167167
ExecutionContext context = new ExecutionContext();
168168
Credentials credentials = credsProvider.getCredentials();
169169
assertParameterNotNull(credentials, "credentials");
170170
context.setCharset(DEFAULT_CHARSET_NAME);
171171
context.setSigner(createSigner(method, bucketName, key, credentials, client.getClientConfiguration().getSignatureVersion()));
172172
context.addResponseHandler(errorResponseHandler);
173-
if (method == HttpMethod.POST) {
173+
if (method == HttpMethod.POST && !isRetryablePostRequest(originalRequest)) {
174174
context.setRetryStrategy(noRetryStrategy);
175175
}
176176

@@ -182,12 +182,19 @@ protected ExecutionContext createDefaultContext(HttpMethod method, String bucket
182182
return context;
183183
}
184184

185+
protected ExecutionContext createDefaultContext(HttpMethod method, String bucketName, String key) {
186+
return this.createDefaultContext(method, bucketName, key, null);
187+
}
188+
185189
protected ExecutionContext createDefaultContext(HttpMethod method, String bucketName) {
186-
return this.createDefaultContext(method, bucketName, null);
190+
return this.createDefaultContext(method, bucketName, null, null);
187191
}
188192

189193
protected ExecutionContext createDefaultContext(HttpMethod method) {
190-
return this.createDefaultContext(method, null, null);
194+
return this.createDefaultContext(method, null, null, null);
191195
}
192196

197+
protected boolean isRetryablePostRequest(WebServiceRequest request) {
198+
return false;
199+
}
193200
}

src/test/java/com/aliyun/oss/common/NoCreationClassTest.java

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@
1919

2020
package com.aliyun.oss.common;
2121

22+
import com.aliyun.oss.ClientConfiguration;
23+
import com.aliyun.oss.HttpMethod;
24+
import com.aliyun.oss.common.auth.CredentialsProvider;
25+
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
26+
import com.aliyun.oss.common.comm.DefaultServiceClient;
27+
import com.aliyun.oss.common.comm.ExecutionContext;
28+
import com.aliyun.oss.common.comm.NoRetryStrategy;
29+
import com.aliyun.oss.common.comm.ServiceClient;
2230
import com.aliyun.oss.common.utils.AuthUtils;
2331
import com.aliyun.oss.common.utils.VersionInfoUtils;
24-
import com.aliyun.oss.internal.Mimetypes;
25-
import com.aliyun.oss.internal.OSSConstants;
26-
import com.aliyun.oss.internal.RequestParameters;
27-
import com.aliyun.oss.internal.SignParameters;
32+
import com.aliyun.oss.internal.*;
33+
import com.aliyun.oss.model.AbortMultipartUploadRequest;
34+
import com.aliyun.oss.model.InitiateMultipartUploadRequest;
2835
import com.aliyun.oss.model.LocationConstraint;
36+
import com.aliyun.oss.model.WebServiceRequest;
2937
import org.junit.Assert;
3038
import org.junit.Test;
3139

@@ -64,4 +72,86 @@ public void testMimetypesClass() {
6472
}
6573

6674
}
75+
76+
static class TestOSSOperation extends OSSOperation {
77+
public TestOSSOperation(ServiceClient client, CredentialsProvider credsProvider) {
78+
super(client, credsProvider);
79+
}
80+
81+
@Override
82+
protected boolean isRetryablePostRequest(WebServiceRequest request) {
83+
return super.isRetryablePostRequest(request);
84+
}
85+
86+
@Override
87+
public ExecutionContext createDefaultContext(HttpMethod method, String bucketName, String key, WebServiceRequest originalRequest) {
88+
return super.createDefaultContext(method, bucketName, key, originalRequest);
89+
}
90+
}
91+
92+
static class TestOSSMultipartOperation extends OSSMultipartOperation {
93+
public TestOSSMultipartOperation(ServiceClient client, CredentialsProvider credsProvider) {
94+
super(client, credsProvider);
95+
}
96+
97+
@Override
98+
protected boolean isRetryablePostRequest(WebServiceRequest request) {
99+
return super.isRetryablePostRequest(request);
100+
}
101+
102+
@Override
103+
public ExecutionContext createDefaultContext(HttpMethod method, String bucketName, String key, WebServiceRequest originalRequest) {
104+
return super.createDefaultContext(method, bucketName, key, originalRequest);
105+
}
106+
}
107+
108+
@Test
109+
public void testOSSOperationClass() {
110+
ClientConfiguration config = new ClientConfiguration();
111+
ServiceClient client = new DefaultServiceClient(config);
112+
CredentialsProvider cred = new DefaultCredentialProvider("ak", "sk");
113+
TestOSSOperation operation = new TestOSSOperation(client, cred);
114+
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest("bucket", "key");
115+
Assert.assertEquals(operation.isRetryablePostRequest(request), false);
116+
117+
Assert.assertEquals(operation.isRetryablePostRequest(null), false);
118+
119+
ExecutionContext context = null;
120+
context = operation.createDefaultContext(HttpMethod.POST, "bucket", "key", request);
121+
Assert.assertTrue(context.getRetryStrategy() instanceof NoRetryStrategy);
122+
123+
context = operation.createDefaultContext(HttpMethod.GET, "bucket", "key", request);
124+
Assert.assertEquals(context.getRetryStrategy(), null);
125+
Assert.assertFalse(context.getRetryStrategy() instanceof NoRetryStrategy);
126+
}
127+
128+
@Test
129+
public void testTestOSSMultipartOperationClass() {
130+
ClientConfiguration config = new ClientConfiguration();
131+
ServiceClient client = new DefaultServiceClient(config);
132+
CredentialsProvider cred = new DefaultCredentialProvider("ak", "sk");
133+
TestOSSMultipartOperation operation = new TestOSSMultipartOperation(client, cred);
134+
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest("bucket", "key");
135+
Assert.assertEquals(operation.isRetryablePostRequest(request), true);
136+
137+
Assert.assertEquals(operation.isRetryablePostRequest(null), false);
138+
139+
ExecutionContext context = null;
140+
context = operation.createDefaultContext(HttpMethod.POST, "bucket", "key", request);
141+
Assert.assertEquals(context.getRetryStrategy(), null);
142+
Assert.assertFalse(context.getRetryStrategy() instanceof NoRetryStrategy);
143+
144+
context = operation.createDefaultContext(HttpMethod.POST, "bucket", "key", null);
145+
Assert.assertNotNull(context.getRetryStrategy());
146+
Assert.assertTrue(context.getRetryStrategy() instanceof NoRetryStrategy);
147+
148+
context = operation.createDefaultContext(HttpMethod.POST, "bucket", "key",
149+
new AbortMultipartUploadRequest("bucket", "key", "id"));
150+
Assert.assertNotNull(context.getRetryStrategy());
151+
Assert.assertTrue(context.getRetryStrategy() instanceof NoRetryStrategy);
152+
153+
context = operation.createDefaultContext(HttpMethod.GET, "bucket", "key",
154+
new AbortMultipartUploadRequest("bucket", "key", "id"));
155+
Assert.assertEquals(context.getRetryStrategy(), null);
156+
}
67157
}

0 commit comments

Comments
 (0)