Skip to content

Commit 980a1c6

Browse files
author
YangSen-qn
committed
upload hosts support null
1 parent 47f9adf commit 980a1c6

File tree

3 files changed

+105
-29
lines changed

3 files changed

+105
-29
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,13 @@ public Region autoRegion(int retryMax, int retryInterval, int hostFreezeDuration
506506
* @return 区域信息
507507
*/
508508
public Region build() {
509+
if (region.srcUpHosts == null) {
510+
region.srcUpHosts = new ArrayList<>();
511+
}
512+
if (region.accUpHosts == null) {
513+
region.accUpHosts = new ArrayList<>();
514+
}
515+
509516
return region;
510517
}
511518
}

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

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.qiniu.storage;
22

33
import com.qiniu.common.QiniuException;
4+
import com.qiniu.util.StringUtils;
45

56
import java.util.*;
67

@@ -32,11 +33,15 @@ String upHost(Region region, String upToken, String lastUsedHost, boolean change
3233
try {
3334
accHosts = region.getAccUpHost(regionReqInfo);
3435
srcHosts = region.getSrcUpHost(regionReqInfo);
35-
} catch (QiniuException e) { // it will success soon.
36+
} catch (Exception e) { // it will success soon.
3637
if (mustReturnUpHost && conf.useDefaultUpHostIfNone) {
3738
return failedUpHost("failed_get_region");
3839
} else {
39-
throw e;
40+
if (e instanceof QiniuException) {
41+
throw e;
42+
} else {
43+
throw QiniuException.unrecoverable(e);
44+
}
4045
}
4146
}
4247

@@ -50,7 +55,7 @@ String upHost(Region region, String upToken, String lastUsedHost, boolean change
5055
return regionHost.upHost(accHosts, srcHosts, lastUsedHost, changeHost);
5156
}
5257

53-
private String failedUpHost(String regionKey) {
58+
private String failedUpHost(String regionKey) throws QiniuException {
5459
List<String> srcHosts;
5560
List<String> accHosts;
5661

@@ -75,42 +80,44 @@ private String failedUpHost(String regionKey) {
7580
srcHosts = regionHost.lastSrcHosts;
7681
accHosts = regionHost.lastAccHosts;
7782
}
78-
String host = regionHost.upHost(accHosts, srcHosts, null, false);
79-
return host;
83+
return regionHost.upHost(accHosts, srcHosts, null, false);
8084
}
8185

8286

8387
String getRegionKey(List<String> srcHosts, List<String> accHosts) {
8488
String host = null;
8589
String lhost = null;
86-
for (String a : srcHosts) {
87-
if (host == null) {
88-
host = a;
89-
lhost = a;
90-
}
91-
if (a.length() < host.length()) {
92-
host = a;
93-
}
94-
if (a.length() > lhost.length()) {
95-
lhost = a;
90+
91+
if (srcHosts != null) {
92+
for (String a : srcHosts) {
93+
if (host == null) {
94+
host = a;
95+
lhost = a;
96+
}
97+
if (a.length() < host.length()) {
98+
host = a;
99+
}
100+
if (a.length() > lhost.length()) {
101+
lhost = a;
102+
}
96103
}
97104
}
98-
if (host != null) {
99-
return host + ";+=-_" + lhost;
100-
}
101105

102-
for (String a : accHosts) {
103-
if (host == null) {
104-
host = a;
105-
lhost = a;
106-
}
107-
if (a.length() < host.length()) {
108-
host = a;
109-
}
110-
if (a.length() > lhost.length()) {
111-
lhost = a;
106+
if (host == null && accHosts != null) {
107+
for (String a : accHosts) {
108+
if (host == null) {
109+
host = a;
110+
lhost = a;
111+
}
112+
if (a.length() < host.length()) {
113+
host = a;
114+
}
115+
if (a.length() > lhost.length()) {
116+
lhost = a;
117+
}
112118
}
113119
}
120+
114121
return host + ";+=-_" + lhost;
115122
}
116123

@@ -126,6 +133,14 @@ class RegionUpHost {
126133
volatile String lastHost;
127134

128135
private void initHostMark(List<String> f, List<String> s) {
136+
if (f == null) {
137+
f = new ArrayList<>();
138+
}
139+
140+
if (s == null) {
141+
s = new ArrayList<>();
142+
}
143+
129144
ArrayList<String> _lastHosts = new ArrayList<>();
130145
int _mainHostCount = 0;
131146

@@ -181,7 +196,12 @@ private void randomAdd(ArrayList<String> _lastHosts, ArrayList<Integer> iidx, Ra
181196
}
182197
}
183198

184-
String upHost(List<String> accHosts, List<String> srcHosts, String lastUsedHost, boolean changeHost) {
199+
String upHost(List<String> accHosts, List<String> srcHosts, String lastUsedHost, boolean changeHost) throws QiniuException {
200+
if ((accHosts == null || accHosts.isEmpty()) &&
201+
(srcHosts == null || srcHosts.isEmpty())) {
202+
throw QiniuException.unrecoverable("no up host found");
203+
}
204+
185205
if (lastAccHosts != accHosts || lastSrcHosts != srcHosts) {
186206
lastAccHosts = accHosts;
187207
lastSrcHosts = srcHosts;

src/test/java/test/com/qiniu/storage/FormUploadTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.qiniu.processing.OperationManager;
66
import com.qiniu.processing.OperationStatus;
77
import com.qiniu.storage.Configuration;
8+
import com.qiniu.storage.Region;
89
import com.qiniu.storage.UpCompletionHandler;
910
import com.qiniu.storage.UploadManager;
1011
import com.qiniu.util.StringMap;
@@ -93,6 +94,54 @@ public void testSimple() {
9394
}
9495
}
9596

97+
@Test
98+
@Tag("IntegrationTest")
99+
public void testEmptyUploadHosts() {
100+
Region region = new Region.Builder()
101+
.build();
102+
Configuration config = new Configuration(region);
103+
UploadManager uploadManager = new UploadManager(config);
104+
try {
105+
String key = "empty_upload_hosts";
106+
String token = TestConfig.testAuth.uploadToken(TestConfig.testBucket_na0, key);
107+
Response response = uploadManager.put("".getBytes(), key, token, null, "", true);
108+
} catch (QiniuException e) {
109+
e.printStackTrace();
110+
} catch (Exception e) {
111+
fail(e);
112+
}
113+
114+
region = new Region.Builder()
115+
.srcUpHost("aaa")
116+
.build();
117+
config = new Configuration(region);
118+
uploadManager = new UploadManager(config);
119+
try {
120+
String key = "empty_upload_hosts";
121+
String token = TestConfig.testAuth.uploadToken(TestConfig.testBucket_na0, key);
122+
Response response = uploadManager.put("".getBytes(), key, token, null, "", true);
123+
} catch (QiniuException e) {
124+
e.printStackTrace();
125+
} catch (Exception e) {
126+
fail(e);
127+
}
128+
129+
region = new Region.Builder()
130+
.accUpHost("aaa")
131+
.build();
132+
config = new Configuration(region);
133+
uploadManager = new UploadManager(config);
134+
try {
135+
String key = "empty_upload_hosts";
136+
String token = TestConfig.testAuth.uploadToken(TestConfig.testBucket_na0, key);
137+
Response response = uploadManager.put("".getBytes(), key, token, null, "", true);
138+
} catch (QiniuException e) {
139+
e.printStackTrace();
140+
} catch (Exception e) {
141+
fail(e);
142+
}
143+
}
144+
96145
@Test
97146
@Tag("IntegrationTest")
98147
public void testSyncRetry() {

0 commit comments

Comments
 (0)