Skip to content

Multizone auto #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
format
  • Loading branch information
longbai committed Nov 7, 2016
commit 35c4acc095fb2ada5321e48684adc0b412575b57
26 changes: 2 additions & 24 deletions library/src/main/java/com/qiniu/android/common/AutoZone.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingDeque;

/**
* Created by long on 2016/9/29.
Expand All @@ -37,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 @@ -76,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 @@ -126,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 @@ -154,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
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
30 changes: 15 additions & 15 deletions library/src/main/java/com/qiniu/android/common/Zone.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.qiniu.android.common;

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

/**
* Created by bailong on 15/10/10.
Expand All @@ -19,24 +18,14 @@ public abstract class Zone {
public static final Zone zoneNa0 =
createZone("upload-na0.qiniu.com", "up-na0.qiniu.com", "23.236.102.3", "23.236.102.2");

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

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

private static Zone createZone(String upHost, String upHostBackup, String upIp, String upIp2) {
String[] upIps = {upIp, upIp2};
ServiceAddress up = new ServiceAddress("http://" + upHost, upIps);
ServiceAddress upBackup = new ServiceAddress("http://" + upHostBackup, upIps);
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 @@ -47,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) {

}
}