Skip to content

Commit

Permalink
Merge pull request #225 from longbai/multizone-auto
Browse files Browse the repository at this point in the history
Multizone auto
  • Loading branch information
longbai authored Nov 7, 2016
2 parents e02dc3b + 35c4acc commit 9ce5473
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## 7.3.0 (2016-09-30)

### 增加
* 北美上传地址

## 7.3.0 (2016-09-30)

### 增加
* 自动判断上传存储区

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Jul 12 16:36:40 CST 2015
#Mon Oct 24 19:53:45 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ String version = versionName()
int code = versionNameToCode(version)

android {
compileSdkVersion 23
buildToolsVersion '23.0.1'
compileSdkVersion 24
buildToolsVersion '24.0.2'
defaultConfig {
//applicationId "com.qiniu.android"
minSdkVersion 9
Expand Down
18 changes: 9 additions & 9 deletions library/library.iml
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
Expand All @@ -81,6 +73,14 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
Expand All @@ -106,7 +106,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="jdk" jdkName="Android API 24 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="okio-1.6.0" level="project" />
<orderEntry type="library" exported="" name="okhttp-3.2.0" level="project" />
Expand Down
25 changes: 2 additions & 23 deletions library/src/main/java/com/qiniu/android/common/AutoZone.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,6 @@ public AutoZone(boolean https, DnsManager dns) {
this.dns = dns;
}

// private ZoneInfo getZoneJson(ZoneIndex index) {
// String address = ucServer + "/v1/query?ak=" + index.accessKey + "&bucket=" + index.bucket;
//
// ResponseInfo r = client.syncGet(address, null);
// if (r.isOK()){
// try {
// return ZoneInfo.buildFromJson(r.response);
// } catch (JSONException e) {
// e.printStackTrace();
// return null;
// }
// }
// return null;
// }

private void getZoneJsonAsync(ZoneIndex index, CompletionHandler handler) {
String address = ucServer + "/v1/query?ak=" + index.accessKey + "&bucket=" + index.bucket;
client.asyncGet(address, null, handler);
Expand All @@ -75,13 +60,6 @@ private void putHosts(ZoneInfo info) {
ZoneInfo zoneInfo(String ak, String bucket) {
ZoneIndex index = new ZoneIndex(ak, bucket);
ZoneInfo info = zones.get(index);
// if (info == null) {
// info = getZoneJson(index);
// if (info != null) {
// zones.put(index, info);
// putHosts(info);
// }
// }
return info;
}

Expand Down Expand Up @@ -125,7 +103,7 @@ public ServiceAddress upHostBackup(String token) {
return new ServiceAddress(info.upBackup, new String[]{info.upIp});
}

void preQueryIndex(final ZoneIndex index, final QueryHandler complete){
void preQueryIndex(final ZoneIndex index, final QueryHandler complete) {
if (index == null) {
complete.onFailure(ResponseInfo.InvalidToken);
return;
Expand Down Expand Up @@ -153,6 +131,7 @@ public void complete(ResponseInfo info, JSONObject response) {
}
});
}

@Override
public void preQuery(String token, QueryHandler complete) {
ZoneIndex index = ZoneIndex.getFromToken(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


public final class Constants {
public static final String VERSION = "7.3.0";
public static final String VERSION = "7.3.1";

public static final String UTF_8 = "utf-8";
}
5 changes: 3 additions & 2 deletions library/src/main/java/com/qiniu/android/common/FixedZone.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ public FixedZone(ServiceAddress up, ServiceAddress upBackup) {
this.upBackup = upBackup;
}

public ServiceAddress upHost(String token){
public ServiceAddress upHost(String token) {
return up;
}
public ServiceAddress upHostBackup(String token){

public ServiceAddress upHostBackup(String token) {
return upBackup;
}

Expand Down
32 changes: 18 additions & 14 deletions library/src/main/java/com/qiniu/android/common/Zone.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
package com.qiniu.android.common;

import com.qiniu.android.dns.DnsManager;
import com.qiniu.android.http.CompletionHandler;

/**
* Created by bailong on 15/10/10.
*/
public abstract class Zone {
public static final Zone zone0 =
createZone("upload.qiniu.com", "up.qiniu.com", "183.136.139.10", "115.231.182.136");

public static final Zone zone1 =
createZone("upload-z1.qiniu.com", "up-z1.qiniu.com", "106.38.227.27", "106.38.227.28");

public static final Zone zone2 =
createZone("upload-z2.qiniu.com", "up-z2.qiniu.com", "183.60.214.197", "14.152.37.7");

/**
* 默认上传服务器
*/
public abstract ServiceAddress upHost(String token);

/**
* 备用上传服务器,当默认服务器网络连接失败时使用
*/
public abstract ServiceAddress upHostBackup(String token);
public static final Zone zoneNa0 =
createZone("upload-na0.qiniu.com", "up-na0.qiniu.com", "23.236.102.3", "23.236.102.2");

private static Zone createZone(String upHost, String upHostBackup, String upIp, String upIp2) {
String[] upIps = {upIp, upIp2};
Expand All @@ -32,7 +25,7 @@ private static Zone createZone(String upHost, String upHostBackup, String upIp,
return new FixedZone(up, upBackup);
}

public static void addDnsIp(DnsManager dns){
public static void addDnsIp(DnsManager dns) {
zone0.upHost("").addIpToDns(dns);
zone0.upHostBackup("").addIpToDns(dns);

Expand All @@ -43,10 +36,21 @@ public static void addDnsIp(DnsManager dns){
zone2.upHostBackup("").addIpToDns(dns);
}

public interface QueryHandler{
/**
* 默认上传服务器
*/
public abstract ServiceAddress upHost(String token);

/**
* 备用上传服务器,当默认服务器网络连接失败时使用
*/
public abstract ServiceAddress upHostBackup(String token);

public abstract void preQuery(String token, QueryHandler complete);

public interface QueryHandler {
void onSuccess();

void onFailure(int reason);
}

public abstract void preQuery(String token, QueryHandler complete);
}
92 changes: 46 additions & 46 deletions library/src/main/java/com/qiniu/android/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,51 @@ private static JSONObject buildJsonResp(byte[] body) throws Exception {
return new JSONObject(str);
}

private static ResponseInfo buildResponseInfo(okhttp3.Response response, String ip, long duration) {
int code = response.code();
String reqId = response.header("X-Reqid");
reqId = (reqId == null) ? null : reqId.trim();
byte[] body = null;
String error = null;
try {
body = response.body().bytes();
} catch (IOException e) {
error = e.getMessage();
}
JSONObject json = null;
if (ctype(response).equals(Client.JsonMime) && body != null) {
try {
json = buildJsonResp(body);
if (response.code() != 200) {
String err = new String(body, Constants.UTF_8);
error = json.optString("error", err);
}
} catch (Exception e) {
if (response.code() < 300) {
error = e.getMessage();
}
}
} else {
error = body == null ? "null body" : new String(body);
}

HttpUrl u = response.request().url();
return new ResponseInfo(json, code, reqId, response.header("X-Log"),
via(response), u.host(), u.encodedPath(), ip, u.port(), duration, 0, error);
}

private static void onRet(okhttp3.Response response, String ip, long duration,
final CompletionHandler complete) {
final ResponseInfo info = buildResponseInfo(response, ip, duration);

AsyncRun.runInMain(new Runnable() {
@Override
public void run() {
complete.complete(info, info.response);
}
});
}

public void asyncSend(final Request.Builder requestBuilder, StringMap headers, final CompletionHandler complete) {
if (headers != null) {
headers.forEach(new StringMap.Consumer() {
Expand Down Expand Up @@ -252,52 +297,7 @@ public void accept(String key, Object value) {
asyncSend(requestBuilder, null, completionHandler);
}

private static ResponseInfo buildResponseInfo(okhttp3.Response response, String ip, long duration){
int code = response.code();
String reqId = response.header("X-Reqid");
reqId = (reqId == null) ? null : reqId.trim();
byte[] body = null;
String error = null;
try {
body = response.body().bytes();
} catch (IOException e) {
error = e.getMessage();
}
JSONObject json = null;
if (ctype(response).equals(Client.JsonMime) && body != null) {
try {
json = buildJsonResp(body);
if (response.code() != 200) {
String err = new String(body, Constants.UTF_8);
error = json.optString("error", err);
}
} catch (Exception e) {
if (response.code() < 300) {
error = e.getMessage();
}
}
} else {
error = body == null ? "null body" : new String(body);
}

HttpUrl u = response.request().url();
return new ResponseInfo(json, code, reqId, response.header("X-Log"),
via(response), u.host(), u.encodedPath(), ip, u.port(), duration, 0, error);
}

private static void onRet(okhttp3.Response response, String ip, long duration,
final CompletionHandler complete) {
final ResponseInfo info = buildResponseInfo(response, ip, duration);

AsyncRun.runInMain(new Runnable() {
@Override
public void run() {
complete.complete(info, info.response);
}
});
}

public void asyncGet(String url, StringMap headers, CompletionHandler completionHandler){
public void asyncGet(String url, StringMap headers, CompletionHandler completionHandler) {
Request.Builder requestBuilder = new Request.Builder().get().url(url);
asyncSend(requestBuilder, headers, completionHandler);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.qiniu.android.storage;


import com.qiniu.android.common.ServiceAddress;
import com.qiniu.android.common.Zone;
import com.qiniu.android.dns.DnsManager;
import com.qiniu.android.dns.IResolver;
Expand Down Expand Up @@ -83,13 +82,13 @@ private Configuration(Builder builder) {

urlConverter = builder.urlConverter;

zone = builder.zone == null? Zone.zone0 : builder.zone;
zone = builder.zone == null ? Zone.zone0 : builder.zone;
dns = initDns(builder);
}

private static DnsManager initDns(Builder builder) {
DnsManager d = builder.dns;
if (d != null){
if (d != null) {
Zone.addDnsIp(d);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void complete(ResponseInfo info, JSONObject response) {
};
URI u = config.zone.upHost(token.token).address;
if (config.zone.upHostBackup(token.token) != null
&&(info.needSwitchServer() || info.isNotQiniu())) {
&& (info.needSwitchServer() || info.isNotQiniu())) {
u = config.zone.upHostBackup(token.token).address;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void complete(ResponseInfo info, JSONObject response) {
}

if (config.zone.upHostBackup(token.token) != null
&&((isNotChunkToQiniu(info, response) || info.needRetry())
&& ((isNotChunkToQiniu(info, response) || info.needRetry())
&& retried < config.retryMax)) {
nextTask(offset, retried + 1, config.zone.upHostBackup(token.token).address);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static void runInMain(Runnable r) {
h.post(r);
}

public static void runInBack(Runnable r){
public static void runInBack(Runnable r) {

}
}

0 comments on commit 9ce5473

Please sign in to comment.