From ca475ef062bdbdbd14b9c24c6f377077eaf89d31 Mon Sep 17 00:00:00 2001 From: YangSen-qn Date: Tue, 28 May 2024 10:37:11 +0800 Subject: [PATCH] change dns test --- .../qiniu/android/http/dns/SystemDnsTest.java | 4 +-- .../com/qiniu/android/common/FixedZone.java | 31 ++++++++++++++----- .../com/qiniu/android/common/ZoneInfo.java | 12 +++---- .../com/qiniu/android/http/ResponseInfo.java | 18 ++++++++++- .../http/serverRegion/UploadDomainRegion.java | 25 ++++++++------- .../com/qiniu/android/storage/BaseUpload.java | 3 +- 6 files changed, 65 insertions(+), 28 deletions(-) diff --git a/library/src/androidTest/java/com/qiniu/android/http/dns/SystemDnsTest.java b/library/src/androidTest/java/com/qiniu/android/http/dns/SystemDnsTest.java index 94eebc3a..39e86ca2 100644 --- a/library/src/androidTest/java/com/qiniu/android/http/dns/SystemDnsTest.java +++ b/library/src/androidTest/java/com/qiniu/android/http/dns/SystemDnsTest.java @@ -16,9 +16,9 @@ public class SystemDnsTest extends BaseTest { @Test public void testDnsLookup() { - SystemDns dns = new SystemDns(5); + SystemDns dns = new SystemDns(10); try { - List result = dns.lookup("upload.qiniup.com"); + List result = dns.lookup("uplog.qbox.me"); assertTrue("testDnsLookup fail:", result != null && result.size() > 0); } catch (UnknownHostException e) { e.printStackTrace(); diff --git a/library/src/main/java/com/qiniu/android/common/FixedZone.java b/library/src/main/java/com/qiniu/android/common/FixedZone.java index 74cb34a1..f7d11d6d 100644 --- a/library/src/main/java/com/qiniu/android/common/FixedZone.java +++ b/library/src/main/java/com/qiniu/android/common/FixedZone.java @@ -178,23 +178,40 @@ private FixedZone(String[] upDomains, String[] oldUpDomains, String regionId) { this.zonesInfo = createZonesInfo(upDomains, oldUpDomains, regionId); } + public FixedZone(String[] accUpDomains,String[] upDomains, String[] oldUpDomains, String regionId) { + this.zonesInfo = createZonesInfo(accUpDomains, upDomains, oldUpDomains, regionId); + } + private ZonesInfo createZonesInfo(String[] upDomains, String[] oldUpDomains, String regionId) { + return createZonesInfo(null, upDomains, oldUpDomains, regionId); + } + + private ZonesInfo createZonesInfo(String[] accDomains, + String[] upDomains, + String[] oldUpDomains, + String regionId) { - if (upDomains == null || upDomains.length == 0) { + if ((accDomains == null || accDomains.length == 0) && + (upDomains == null || upDomains.length == 0)) { return null; } - List upDomainsList = new ArrayList(Arrays.asList(upDomains)); - List oldUpDomainsList = null; + List accDomainsList = new ArrayList<>(); + List upDomainsList = new ArrayList<>(); + List oldUpDomainsList = new ArrayList<>(); + if (accDomains != null && accDomains.length > 0) { + accDomainsList = new ArrayList<>(Arrays.asList(accDomains)); + } + if (upDomains != null && upDomains.length > 0) { + upDomainsList = new ArrayList<>(Arrays.asList(upDomains)); + } if (oldUpDomains != null && oldUpDomains.length > 0) { - oldUpDomainsList = new ArrayList(Arrays.asList(oldUpDomains)); - } else { - oldUpDomainsList = new ArrayList<>(); + oldUpDomainsList = new ArrayList<>(Arrays.asList(oldUpDomains)); } - ZoneInfo zoneInfo = ZoneInfo.buildInfo(upDomainsList, oldUpDomainsList, regionId); + ZoneInfo zoneInfo = ZoneInfo.buildInfo(accDomainsList, upDomainsList, oldUpDomainsList, regionId); if (zoneInfo == null) { return null; } diff --git a/library/src/main/java/com/qiniu/android/common/ZoneInfo.java b/library/src/main/java/com/qiniu/android/common/ZoneInfo.java index 4804fbff..07a2c632 100644 --- a/library/src/main/java/com/qiniu/android/common/ZoneInfo.java +++ b/library/src/main/java/com/qiniu/android/common/ZoneInfo.java @@ -112,23 +112,23 @@ public static ZoneInfo buildInfo(List mainHosts, /** * 构造函数 * - * @param accelerateDomains 加速域名 + * @param accelerateHosts 加速域名 * @param mainHosts 主要域名 * @param oldHosts 备用域名 * @param regionId 区域 ID * @return ZoneInfo */ - public static ZoneInfo buildInfo(List accelerateDomains, + public static ZoneInfo buildInfo(List accelerateHosts, List mainHosts, List oldHosts, String regionId) { - if (accelerateDomains == null && mainHosts == null) { + if (accelerateHosts == null && mainHosts == null) { return null; } HashMap up = new HashMap<>(); - if (accelerateDomains != null) { - up.put("acc_domains", new JSONArray(accelerateDomains)); + if (accelerateHosts != null) { + up.put("acc_domains", new JSONArray(accelerateHosts)); } if (mainHosts != null) { up.put("domains", new JSONArray(mainHosts)); @@ -260,7 +260,7 @@ public static ZoneInfo buildFromJson(JSONObject obj) throws JSONException { } } - if (ListUtils.isEmpty(domains)) { + if (ListUtils.isEmpty(accelerateDomains) && ListUtils.isEmpty(domains)) { return null; } diff --git a/library/src/main/java/com/qiniu/android/http/ResponseInfo.java b/library/src/main/java/com/qiniu/android/http/ResponseInfo.java index ca31af60..45625a4d 100644 --- a/library/src/main/java/com/qiniu/android/http/ResponseInfo.java +++ b/library/src/main/java/com/qiniu/android/http/ResponseInfo.java @@ -493,6 +493,10 @@ public boolean couldRetry() { return true; } + if (isTransferAccelerationConfigureError()) { + return true; + } + if (isCancelled() || statusCode == 100 || (statusCode > 300 && statusCode < 400) @@ -517,6 +521,10 @@ public boolean couldRegionRetry() { return true; } + if (isTransferAccelerationConfigureError()) { + return true; + } + if (isCancelled() || statusCode == 100 || (statusCode > 300 && statusCode < 500 && statusCode != 406) @@ -589,7 +597,8 @@ public boolean canConnectToHost() { */ public boolean isHostUnavailable() { // 基本不可恢复,注:会影响下次请求,范围太大可能会造成大量的timeout - if (statusCode == 502 || statusCode == 503 || statusCode == 504 || statusCode == 599) { + if (isTransferAccelerationConfigureError() || + statusCode == 502 || statusCode == 503 || statusCode == 504 || statusCode == 599) { return true; } else { return false; @@ -605,6 +614,13 @@ public boolean isCtxExpiredError() { return statusCode == 701 || (statusCode == 612 && error != null && error.contains("no such uploadId")); } + public boolean isTransferAccelerationConfigureError() { + if (error == null) { + return false; + } + return error.contains("transfer acceleration is not configured on this bucket"); + } + /** * 请求是否因网络问题而失败 * diff --git a/library/src/main/java/com/qiniu/android/http/serverRegion/UploadDomainRegion.java b/library/src/main/java/com/qiniu/android/http/serverRegion/UploadDomainRegion.java index 65e1d5af..0a4a3172 100644 --- a/library/src/main/java/com/qiniu/android/http/serverRegion/UploadDomainRegion.java +++ b/library/src/main/java/com/qiniu/android/http/serverRegion/UploadDomainRegion.java @@ -194,38 +194,41 @@ public IUploadServer getNextServer(UploadRequestState requestState, ResponseInfo boolean accelerate = true; synchronized (this) { - if (enableAccelerateUpload && responseInfo.error != null && - responseInfo.error.contains("transfer acceleration is not configured on this bucket")) { + if (enableAccelerateUpload && + responseInfo != null && + responseInfo.isTransferAccelerationConfigureError()) { enableAccelerateUpload = false; } accelerate = enableAccelerateUpload; } - List hostList = new ArrayList<>(); - Map domainInfo = new HashMap<>(); + ArrayList hostList = new ArrayList<>(); + HashMap domainInfo = new HashMap<>(); if (requestState.isUseOldServer()) { // SNI if (!ListUtils.isEmpty(oldDomainHostList) && !MapUtils.isEmpty(oldDomainHashMap)) { - hostList = oldDomainHostList; - domainInfo = oldDomainHashMap; + hostList.addAll(oldDomainHostList); + domainInfo.putAll(oldDomainHashMap); } } else { - // 如果 + // 优先使用 acc if (accelerate && !ListUtils.isEmpty(accelerateDomainHostList) && !MapUtils.isEmpty(accelerateDomainHashMap)) { hostList.addAll(accelerateDomainHostList); domainInfo.putAll(accelerateDomainHashMap); - } else if (!ListUtils.isEmpty(domainHostList) && + } + + if (!ListUtils.isEmpty(domainHostList) && !MapUtils.isEmpty(domainHashMap)) { - hostList = domainHostList; - domainInfo = domainHashMap; + hostList.addAll(domainHostList); + domainInfo.putAll(domainHashMap); } } - if (ListUtils.isEmpty(hostList) || MapUtils.isEmpty(domainHashMap)) { + if (ListUtils.isEmpty(hostList) || MapUtils.isEmpty(domainInfo)) { return null; } diff --git a/library/src/main/java/com/qiniu/android/storage/BaseUpload.java b/library/src/main/java/com/qiniu/android/storage/BaseUpload.java index 1a48f251..9726f736 100644 --- a/library/src/main/java/com/qiniu/android/storage/BaseUpload.java +++ b/library/src/main/java/com/qiniu/android/storage/BaseUpload.java @@ -104,7 +104,8 @@ public void complete(ResponseInfo responseInfo, UploadRegionRequestMetrics reque if (responseInfo != null && responseInfo.isOK() && zonesInfo != null) { if (!setupRegions(zonesInfo)) { - completeAction(responseInfo, null); + responseInfo = ResponseInfo.invalidArgument("setup regions host fail, origin response:" + responseInfo); + completeAction(responseInfo, responseInfo.response); return; }