Skip to content

Commit e42bfe9

Browse files
committed
change get regionid logic & add switch region test
1 parent 984275d commit e42bfe9

File tree

4 files changed

+126
-19
lines changed

4 files changed

+126
-19
lines changed

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,11 @@ class AutoRegion extends Region {
1616
*/
1717
private final String ucServer;
1818

19-
/**
20-
* region id 列表
21-
*/
22-
private static final String[] regionIdList = new String[]{"z0", "z1", "z2", "na0", "as0", "fog-cn-east-1"};
23-
2419
/**
2520
* 空间机房,域名信息缓存
2621
*/
2722
private Map<RegionIndex, RegionGroup> regions;
2823

29-
/**
30-
* 根据API返回的上传域名推导出其他资源管理域名
31-
*/
32-
private Map<String, Region> inferDomainsMap;
33-
3424
/**
3525
* 定义HTTP请求管理相关方法
3626
*/
@@ -99,14 +89,9 @@ static RegionGroup regionGroup(UCRet ret) {
9989
}
10090

10191
// 根据 iovipHost 反推 regionId
102-
String regionId = iovipHost;
103-
if (iovipHost != null) {
104-
for (String regionIdP : regionIdList) {
105-
if (iovipHost.contains("-" + regionIdP)) {
106-
regionId = regionIdP;
107-
break;
108-
}
109-
}
92+
String regionId = host.region;
93+
if (regionId == null) {
94+
regionId = "";
11095
}
11196

11297
Region region = new Region(timestamp, regionId, srcUpHosts, accUpHosts, iovipHost, rsHost, rsfHost, apiHost, ucHost);
@@ -277,6 +262,7 @@ private class UCRet {
277262

278263
private class HostRet {
279264
long ttl;
265+
String region;
280266
HostInfoRet up;
281267
HostInfoRet rs;
282268
HostInfoRet rsf;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ void clearState() {
7676
for (ResumeUploadSource.Block block : blockList) {
7777
block.clearState();
7878
}
79+
uploadId = null;
80+
expireAt = null;
7981
}
8082

8183
// 获取下一个需要上传的块
@@ -210,9 +212,10 @@ boolean isUploaded() {
210212
}
211213

212214
void clearState() {
213-
isUploading = false;
215+
this.isUploading = false;
214216
this.etag = null;
215217
this.context = null;
218+
this.data = null;
216219
}
217220
}
218221
}

src/test/java/test/com/qiniu/TestConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public final class TestConfig {
2121
//test: ak, sk, auth
2222
public static final String testAccessKey = System.getenv("QINIU_ACCESS_KEY");
2323
public static final String testSecretKey = System.getenv("QINIU_SECRET_KEY");
24+
// 内部测试环境 AK/SK
25+
public static final String innerAccessKey = System.getenv("testAK");
26+
public static final String innerSecretKey = System.getenv("testSK");
27+
2428
//sms: ak, sk, auth
2529
public static final String smsAccessKey = "test";
2630
public static final String smsSecretKey = "test";

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import com.qiniu.http.Response;
77
import com.qiniu.storage.*;
88
import com.qiniu.storage.model.FileInfo;
9+
import com.qiniu.util.Auth;
910
import com.qiniu.util.Etag;
1011
import com.qiniu.util.Md5;
1112
import com.qiniu.util.StringMap;
13+
import org.junit.Assert;
1214
import org.junit.Test;
1315
import test.com.qiniu.TempFile;
1416
import test.com.qiniu.TestConfig;
@@ -259,6 +261,118 @@ public void test20M1K() throws Throwable {
259261
}
260262
}
261263

264+
// 内部环境测试
265+
@Test
266+
public void notestInnerEnvSwitchRegion() {
267+
try {
268+
long s = new Date().getTime();
269+
uploadByInnerEnvSwitchRegion(1024 * 500 + 1, httpType, resumableV2Type, concurrentType);
270+
long e = new Date().getTime();
271+
System.out.println("耗时:" + (e - s));
272+
} catch (Exception e) {
273+
e.printStackTrace();
274+
Assert.fail("testInnerEnvSwitchRegion:" + e);
275+
}
276+
}
277+
278+
public void uploadByInnerEnvSwitchRegion(int size, int httpType, int uploadType, int uploadStyle) throws Exception {
279+
280+
boolean isHttps = httpType == httpsType;
281+
boolean isConcurrent = uploadStyle == concurrentType;
282+
String bucket = "aaaabbbbb";
283+
284+
Region region0 = new Region.Builder()
285+
.srcUpHost("10.200.20.23:5010")
286+
.accUpHost("10.200.20.23:5010")
287+
.build();
288+
289+
Region region1 = new Region.Builder()
290+
.srcUpHost("10.200.20.24:5010")
291+
.accUpHost("10.200.20.24:5010")
292+
.build();
293+
294+
RegionGroup regionGroup = new RegionGroup();
295+
regionGroup.addRegion(region0);
296+
regionGroup.addRegion(region1);
297+
298+
Configuration config = new Configuration(regionGroup);
299+
if (uploadType == resumableV2Type) {
300+
config.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;
301+
config.resumableUploadAPIV2BlockSize = 4 * 1024 * 1024;
302+
}
303+
304+
config.useHttpsDomains = isHttps;
305+
String key = "switch_region_";
306+
key += isHttps ? "_https" : "_http";
307+
308+
if (uploadType == resumableV1Type) {
309+
key += "_resumeV1";
310+
} else if (uploadType == resumableV2Type) {
311+
key += "_resumeV2";
312+
}
313+
314+
if (uploadStyle == formType) {
315+
key += "_form";
316+
} else if (uploadStyle == serialType) {
317+
key += "_serial";
318+
} else if (uploadStyle == concurrentType) {
319+
key += "_concurrent";
320+
}
321+
322+
key += "_" + new Date().getTime();
323+
final String expectKey = "\r\n?&r=" + size + "k" + key;
324+
final File f = TempFile.createFile(size);
325+
326+
final String fooKey = "foo";
327+
final String fooValue = "fooValue";
328+
final String metaDataKey = "metaDataKey";
329+
final String metaDataValue = "metaDataValue";
330+
final String returnBody = "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"fsize\":\"$(fsize)\""
331+
+ ",\"fname\":\"$(fname)\",\"mimeType\":\"$(mimeType)\",\"foo\":\"$(x:foo)\"}";
332+
333+
Auth auth = Auth.create(TestConfig.innerAccessKey, TestConfig.innerSecretKey);
334+
;
335+
String token = auth.uploadToken(bucket, expectKey, 3600,
336+
new StringMap().put("returnBody", returnBody));
337+
338+
System.out.printf("\r\nkey:%s zone:%s\n", expectKey, regionGroup);
339+
340+
341+
StringMap param = new StringMap();
342+
param.put("x:" + fooKey, fooValue);
343+
param.put("x-qn-meta-" + metaDataKey, metaDataValue);
344+
try {
345+
BaseUploader up = null;
346+
if (uploadStyle == formType) {
347+
up = new FormUploader(new Client(), token, expectKey, f, param, Client.DefaultMime, false, config);
348+
} else if (uploadStyle == serialType) {
349+
up = new ResumeUploader(new Client(), token, expectKey, f, param, null, null, config);
350+
} else {
351+
config.resumableUploadMaxConcurrentTaskCount = 3;
352+
up = new ConcurrentResumeUploader(new Client(), token, expectKey, f, param, null, null, config);
353+
}
354+
355+
Response r = up.upload();
356+
assertTrue(r + "", r.isOK());
357+
358+
StringMap ret = r.jsonToMap();
359+
assertEquals(expectKey, ret.get("key"));
360+
assertEquals(f.getName(), ret.get("fname"));
361+
assertEquals(String.valueOf(f.length()), ret.get("fsize").toString());
362+
assertEquals(fooValue, ret.get(fooKey).toString());
363+
364+
final String etag = Etag.file(f);
365+
System.out.println(" etag:" + etag);
366+
System.out.println("serverEtag:" + ret.get("hash"));
367+
assertNotNull(ret.get("hash"));
368+
assertEquals(etag, ret.get("hash"));
369+
} catch (QiniuException e) {
370+
assertEquals("", e.response == null ? e + "e.response is null" : e.response.bodyString());
371+
fail();
372+
}
373+
TempFile.remove(f);
374+
}
375+
262376

263377
class MyRet {
264378
public String hash;

0 commit comments

Comments
 (0)