Skip to content

Commit cf2ea94

Browse files
authored
Merge pull request #561 from YangSen-qn/resume-check-ctx-expire
Resume check ctx expire
2 parents 701a4e3 + 870186c commit cf2ea94

11 files changed

+110
-27
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# Changelog
2+
## 7.12.0(2022-10-25)
3+
* 增加亚太-首尔区域固定 Region
4+
* 移除雾存储区域:华东一区
5+
* 升级 gson 版本至 2.8.9
6+
* 表单上传支持主备域名
7+
* 优化私有云使用姿势,不再单独配置 rs api uc域名
8+
* 优化分片上传 ctx 超时检测
29

310
## 7.11.0(2022-06-08)
411
* 对象存储,管理类 API 发送请求时增加 [X-Qiniu-Date](https://developer.qiniu.com/kodo/3924/common-request-headers) (生成请求的时间) header

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
<dependency>
1313
<groupId>com.qiniu</groupId>
1414
<artifactId>qiniu-java-sdk</artifactId>
15-
<version>[7.11.0, 7.11.99]</version>
15+
<version>[7.12.0, 7.12.99]</version>
1616
</dependency>
1717
```
1818
或者 Gradle:
1919
```groovy
20-
compile 'com.qiniu:qiniu-java-sdk:7.11.+'
20+
compile 'com.qiniu:qiniu-java-sdk:7.12.+'
2121
```
2222

2323
## 运行环境

src/main/java/com/qiniu/common/Constants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public final class Constants {
99
/**
1010
* 版本号
1111
*/
12-
public static final String VERSION = "7.11.0";
12+
public static final String VERSION = "7.12.0";
1313
/**
1414
* 块大小,不能改变
1515
*/

src/main/java/com/qiniu/http/Response.java

+5
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ public boolean isServerError() {
179179
return (statusCode >= 500 && statusCode < 600 && statusCode != 579) || statusCode == 996;
180180
}
181181

182+
public boolean isContextExpiredError() {
183+
return statusCode == 701 || (statusCode == 612 && error.contains("no such uploadId"));
184+
}
185+
186+
182187
public boolean needSwitchServer() {
183188
return isNetworkBroken() || (statusCode >= 500 && statusCode < 600 && statusCode != 579);
184189
}

src/main/java/com/qiniu/storage/BaseUploader.java

+26-10
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,38 @@ public Response upload() throws QiniuException {
3333

3434
private Response uploadWithRegionRetry() throws QiniuException {
3535
Response response = null;
36+
QiniuException exception = null;
3637
while (true) {
38+
response = null;
39+
exception = null;
40+
3741
try {
3842
response = uploadFlows();
39-
if (!Retry.shouldSwitchRegionAndRetry(response, null)
40-
|| !couldReloadSource() || !reloadSource()
41-
|| config.region == null || !config.region.switchRegion(new UploadToken(upToken))) {
42-
break;
43-
}
4443
} catch (QiniuException e) {
45-
if (!Retry.shouldSwitchRegionAndRetry(null, e)
46-
|| !couldReloadSource() || !reloadSource()
47-
|| config.region == null || !config.region.switchRegion(new UploadToken(upToken))) {
48-
throw e;
49-
}
44+
exception = e;
45+
}
46+
47+
if (!Retry.shouldUploadAgain(response, exception)
48+
|| !couldReloadSource() || !reloadSource()) {
49+
break;
50+
}
51+
52+
// context 过期,不需要切换 region
53+
if (response != null && response.isContextExpiredError() ||
54+
exception != null && exception.response != null && exception.response.isContextExpiredError()) {
55+
continue;
56+
}
57+
58+
// 切换 region
59+
if (config.region == null || !config.region.switchRegion(new UploadToken(upToken))) {
60+
break;
5061
}
5162
}
63+
64+
if (exception != null) {
65+
throw exception;
66+
}
67+
5268
return response;
5369
}
5470

src/main/java/com/qiniu/storage/FormUploader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private void asyncRetryUploadBetweenRegion(final UpCompletionHandler handler) {
116116
asyncRetryUploadBetweenHosts(0, new UpCompletionHandler() {
117117
@Override
118118
public void complete(String key, Response r) {
119-
if (!Retry.shouldSwitchRegionAndRetry(r, null)
119+
if (!Retry.shouldUploadAgain(r, null)
120120
|| !couldReloadSource() || !reloadSource()
121121
|| config.region == null || !config.region.switchRegion(finalToken)) {
122122
handler.complete(key, r);

src/main/java/com/qiniu/storage/ResumeUploadPerformerV1.java

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ private Response makeBlock(String host, ResumeUploadSource.Block block) throws Q
6767
throw new QiniuException(new Exception("block's ctx is empty"));
6868
}
6969
block.context = ctx;
70+
71+
Long expiredAt = response.getExpiredAt();
72+
if (expiredAt == null) {
73+
throw new QiniuException(new Exception("block's expiredAt is empty"));
74+
}
75+
block.expiredAt = expiredAt;
76+
7077
block.data = null;
7178
}
7279

src/main/java/com/qiniu/storage/ResumeUploadSource.java

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ static class Block {
180180

181181
// context: 块上传上下文信息 【resume v1 特有】
182182
String context;
183+
184+
// expiredAt: 上传有效期 【resume v1 特有】
185+
long expiredAt;
186+
183187
// etag: 块etag【resume v2 特有】
184188
String etag;
185189

src/main/java/com/qiniu/storage/ResumeUploadSourceFile.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,29 @@ boolean recoverFromRecordInfo(ResumeUploadSource source) {
108108
}
109109

110110
boolean needRecovered = true;
111-
if (source.resumableUploadAPIVersion == Configuration.ResumableUploadAPIVersion.V2) {
111+
112+
long currentTimestamp = new Date().getTime() / 1000;
113+
if (source.resumableUploadAPIVersion == Configuration.ResumableUploadAPIVersion.V1) {
114+
if (source.blockList == null || source.blockList.size() == 0) {
115+
return false;
116+
}
117+
118+
for (int i = 0; i < source.blockList.size(); i++) {
119+
Block block = source.blockList.get(i);
120+
// 服务端是 7 天,此处有效期少 2h
121+
long expireAtTimestamp = block.expiredAt - 2 * 3600;
122+
needRecovered = expireAtTimestamp > currentTimestamp;
123+
if (!needRecovered) {
124+
break;
125+
}
126+
}
127+
} else if (source.resumableUploadAPIVersion == Configuration.ResumableUploadAPIVersion.V2) {
112128
if (StringUtils.isNullOrEmpty(source.uploadId)) {
113129
return false;
114130
}
115-
// 服务端是 7 天,此处有效期少 1 天,为 6 天
116-
long currentTimestamp = new Date().getTime() / 1000;
117-
long expireAtTimestamp = source.expireAt - 24 * 3600;
131+
132+
// 服务端是 7 天,此处有效期少 2h
133+
long expireAtTimestamp = source.expireAt - 2 * 3600;
118134
needRecovered = expireAtTimestamp > currentTimestamp;
119135
}
120136

src/main/java/com/qiniu/storage/ResumeUploader.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public Response upload() throws QiniuException {
138138
try {
139139
recoverUploadProgressFromLocal();
140140
Response response = super.upload();
141-
if (response != null && response.isOK()) {
141+
if (response != null && (response.isOK() || response.isContextExpiredError())) {
142142
removeUploadProgressFromLocal();
143143
}
144144
return response;
@@ -278,8 +278,7 @@ void recoverUploadProgressFromLocal() {
278278
return;
279279
}
280280

281-
boolean isCopy = source.recoverFromRecordInfo(uploadSource);
282-
if (!isCopy) {
281+
if (!source.recoverFromRecordInfo(uploadSource)) {
283282
removeUploadProgressFromLocal();
284283
return;
285284
}

src/main/java/com/qiniu/storage/Retry.java

+35-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Retry {
88

9-
static boolean shouldSwitchRegionAndRetry(Response response, QiniuException exception) {
9+
static boolean shouldUploadAgain(Response response, QiniuException exception) {
1010
Response checkResponse = response;
1111
if (checkResponse == null && exception != null) {
1212
checkResponse = exception.response;
@@ -23,19 +23,41 @@ static boolean shouldSwitchRegionAndRetry(Response response, QiniuException exce
2323
}
2424

2525
static Boolean requestShouldRetry(Response response, QiniuException exception) {
26-
if (exception != null && !exception.isUnrecoverable() && (exception.response == null || exception.response.needRetry())) {
26+
if (response != null && response.needRetry()) {
27+
return true;
28+
}
29+
30+
if (exception == null || exception.isUnrecoverable()) {
31+
return false;
32+
}
33+
34+
if (exception.response != null && exception.response.needRetry()) {
2735
// 异常需可恢复
2836
return true;
2937
}
30-
return response == null || response.needRetry();
38+
39+
return false;
3140
}
3241

3342
static Boolean requestShouldSwitchHost(Response response, QiniuException exception) {
34-
if (exception != null && !exception.isUnrecoverable() && (exception.response == null || exception.response.needSwitchServer())) {
43+
if (response != null && response.needSwitchServer()) {
44+
return true;
45+
}
46+
47+
if (exception == null) {
48+
return true;
49+
}
50+
51+
if (exception.isUnrecoverable()) {
52+
return false;
53+
}
54+
55+
if (exception.response == null || exception.response.needSwitchServer()) {
3556
// 异常需可恢复
3657
return true;
3758
}
38-
return response == null || response.needSwitchServer();
59+
60+
return false;
3961
}
4062

4163
static Response retryRequestAction(RequestRetryConfig config, RequestRetryAction action) throws QiniuException {
@@ -48,12 +70,15 @@ static Response retryRequestAction(RequestRetryConfig config, RequestRetryAction
4870
}
4971

5072
Response response = null;
73+
QiniuException exception = null;
74+
5175
int retryCount = 0;
5276

5377
do {
5478
boolean shouldSwitchHost = false;
5579
boolean shouldRetry = false;
56-
QiniuException exception = null;
80+
81+
exception = null;
5782
String host = action.getRequestHost();
5883
try {
5984
response = action.doRequest(host);
@@ -90,6 +115,10 @@ static Response retryRequestAction(RequestRetryConfig config, RequestRetryAction
90115

91116
} while (true);
92117

118+
if (exception != null) {
119+
throw exception;
120+
}
121+
93122
return response;
94123
}
95124

0 commit comments

Comments
 (0)