Skip to content

Commit

Permalink
change dns test
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn committed May 28, 2024
1 parent 8557029 commit ca475ef
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDnsNetworkAddress> result = dns.lookup("upload.qiniup.com");
List<IDnsNetworkAddress> result = dns.lookup("uplog.qbox.me");
assertTrue("testDnsLookup fail:", result != null && result.size() > 0);
} catch (UnknownHostException e) {
e.printStackTrace();
Expand Down
31 changes: 24 additions & 7 deletions library/src/main/java/com/qiniu/android/common/FixedZone.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> upDomainsList = new ArrayList<String>(Arrays.asList(upDomains));
List<String> oldUpDomainsList = null;
List<String> accDomainsList = new ArrayList<>();
List<String> upDomainsList = new ArrayList<>();
List<String> 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<String>(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;
}
Expand Down
12 changes: 6 additions & 6 deletions library/src/main/java/com/qiniu/android/common/ZoneInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ public static ZoneInfo buildInfo(List<String> mainHosts,
/**
* 构造函数
*
* @param accelerateDomains 加速域名
* @param accelerateHosts 加速域名
* @param mainHosts 主要域名
* @param oldHosts 备用域名
* @param regionId 区域 ID
* @return ZoneInfo
*/
public static ZoneInfo buildInfo(List<String> accelerateDomains,
public static ZoneInfo buildInfo(List<String> accelerateHosts,
List<String> mainHosts,
List<String> oldHosts,
String regionId) {
if (accelerateDomains == null && mainHosts == null) {
if (accelerateHosts == null && mainHosts == null) {
return null;
}

HashMap<String, Object> 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));
Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 17 additions & 1 deletion library/src/main/java/com/qiniu/android/http/ResponseInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ public boolean couldRetry() {
return true;
}

if (isTransferAccelerationConfigureError()) {
return true;
}

if (isCancelled()
|| statusCode == 100
|| (statusCode > 300 && statusCode < 400)
Expand All @@ -517,6 +521,10 @@ public boolean couldRegionRetry() {
return true;
}

if (isTransferAccelerationConfigureError()) {
return true;
}

if (isCancelled()
|| statusCode == 100
|| (statusCode > 300 && statusCode < 500 && statusCode != 406)
Expand Down Expand Up @@ -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;
Expand All @@ -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");
}

/**
* 请求是否因网络问题而失败
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> hostList = new ArrayList<>();
Map<String, UploadServerDomain> domainInfo = new HashMap<>();
ArrayList<String> hostList = new ArrayList<>();
HashMap<String, UploadServerDomain> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit ca475ef

Please sign in to comment.