Skip to content

Commit eb3dd67

Browse files
committed
Merge pull request #56 from longbai/multi-up-host
io Multi up host
2 parents f4ebe4d + 43f131b commit eb3dd67

File tree

6 files changed

+99
-41
lines changed

6 files changed

+99
-41
lines changed

src/com/qiniu/auth/Client.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ public static ClientExecutor get(String url, CallRet ret) {
4848

4949
public ClientExecutor call(ClientExecutor client, String url, HttpEntity entity, CallRet ret) {
5050
Header header = entity.getContentType();
51-
String contentType = "application/octet-stream";
52-
if (header != null) {
53-
contentType = header.getValue();
54-
}
51+
String contentType = header == null ? "application/octet-stream" : header.getValue();
52+
5553
return call(client, url, contentType, entity, ret);
5654
}
5755

@@ -102,8 +100,12 @@ protected Object doInBackground(Object... objects) {
102100
HttpResponse resp = roundtrip(mHttpRequest);
103101
int statusCode = resp.getStatusLine().getStatusCode();
104102
String phrase = resp.getStatusLine().getReasonPhrase();
105-
String xl = resp.getFirstHeader("X-Log").getValue();
106-
String reqId = resp.getFirstHeader("X-Reqid").getValue();
103+
104+
Header h = resp.getFirstHeader("X-Log");
105+
String xl = h == null ? "":h.getValue();
106+
107+
h = resp.getFirstHeader("X-Reqid");
108+
String reqId = h == null ? "":h.getValue();
107109

108110
if (statusCode == 401) {
109111
return new QiniuException(401, reqId, phrase); // android 2.3 will not response
@@ -129,7 +131,9 @@ protected Object doInBackground(Object... objects) {
129131

130132
@Override
131133
protected void onProgressUpdate(Object... values) {
132-
if (failed) return;
134+
if (failed){
135+
return;
136+
}
133137
if (values.length == 1 && values[0] instanceof QiniuException) {
134138
mRet.onFailure((QiniuException) values[0]);
135139
failed = true;
@@ -140,7 +144,9 @@ protected void onProgressUpdate(Object... values) {
140144

141145
@Override
142146
protected void onPostExecute(Object o) {
143-
if (failed) return;
147+
if (failed) {
148+
return;
149+
}
144150
if (o instanceof QiniuException) {
145151
mRet.onFailure((QiniuException) o);
146152
return;

src/com/qiniu/conf/Conf.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.qiniu.conf;
22

33
public class Conf {
4-
private static final String USER_AGENT_PREFIX = "QiniuAndroid/";
54
public static final String VERSION = "6.0.4";
6-
public static final String UP_HOST = "http://upload.qiniu.com";
7-
public static final String UP_HOST2 = "http://up.qbox.me";
5+
public static String UP_HOST = "http://upload.qiniu.com";
6+
public static String UP_HOST2 = "http://up.qiniu.com";
87

98
public static String getUserAgent() {
10-
return USER_AGENT_PREFIX + VERSION + " (" + android.os.Build.VERSION.RELEASE + "; " + android.os.Build.MODEL+ ")";
9+
return "QiniuAndroid/" + VERSION + " (" + android.os.Build.VERSION.RELEASE + "; " + android.os.Build.MODEL+ ")";
1110
}
1211
}

src/com/qiniu/io/IO.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.qiniu.utils.MultipartEntity;
2222
import com.qiniu.utils.FileUri;
2323
import com.qiniu.utils.QiniuException;
24+
import com.qiniu.utils.RetryRet;
2425

2526
public class IO {
2627

@@ -73,7 +74,7 @@ private static MultipartEntity buildMultipartEntity(String key, InputStreamAt is
7374
* @param extra 上传参数
7475
* @param ret 回调函数
7576
*/
76-
public void put(String key, InputStreamAt isa, PutExtra extra, final JSONObjectRet ret) {
77+
public void put(String key, final InputStreamAt isa, PutExtra extra, final JSONObjectRet ret) {
7778
final MultipartEntity m;
7879
try {
7980
m = buildMultipartEntity(key, isa, extra);
@@ -96,7 +97,19 @@ public void onFailure(QiniuException ex) {
9697
}
9798
});
9899

99-
client.call(executor, Conf.UP_HOST, m, ret);
100+
CallRet retryRet = new RetryRet(ret){
101+
@Override
102+
public void onFailure(QiniuException ex) {
103+
if (RetryRet.noRetry(ex)){
104+
ret.onFailure(ex);
105+
return;
106+
}
107+
isa.reset();
108+
final Client.ClientExecutor executor2 = client.makeClientExecutor();
109+
client.call(executor2, Conf.UP_HOST2, m, ret);
110+
}
111+
};
112+
client.call(executor, Conf.UP_HOST, m, retryRet);
100113
}
101114

102115
/**

src/com/qiniu/utils/RetryRet.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,19 @@ public void onProcess(long current, long total){
2222
public void onPause(Object tag){
2323
ret.onPause(tag);
2424
}
25+
26+
public static boolean noRetry(QiniuException ex){
27+
if (ex.code/100 == 5 && ex.code != 579) {
28+
return false;
29+
}
30+
if (ex.code == 996) {
31+
return false;
32+
}
33+
34+
if (ex.reason instanceof java.io.IOException && !(ex.reason instanceof java.io.FileNotFoundException)) {
35+
return false;
36+
}
37+
38+
return true;
39+
}
2540
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.qiniu.test;
2+
3+
import java.io.IOException;
4+
5+
import junit.framework.Assert;
6+
7+
import com.qiniu.conf.Conf;
8+
import com.qiniu.utils.QiniuException;
9+
import com.qiniu.utils.RetryRet;
10+
11+
import android.test.AndroidTestCase;
12+
import android.test.suitebuilder.annotation.MediumTest;
13+
import android.test.suitebuilder.annotation.SmallTest;
14+
import android.util.Log;
15+
16+
public class RetryTest extends AndroidTestCase{
17+
@SmallTest
18+
public void testRetryCheck() {
19+
QiniuException ioe = new QiniuException(QiniuException.IO, "io test", new IOException("test"));
20+
Assert.assertFalse(RetryRet.noRetry(ioe));
21+
}
22+
}

tests/src/com/qiniu/test/UploadTest.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.IOException;
66
import java.util.HashMap;
77
import java.util.UUID;
8+
import java.util.concurrent.Semaphore;
9+
810

911
import junit.framework.Assert;
1012

@@ -15,6 +17,7 @@
1517
import com.qiniu.io.IO;
1618
import com.qiniu.resumableio.ResumableIO;
1719
import com.qiniu.utils.QiniuException;
20+
import com.qiniu.conf.Conf;
1821

1922
import android.content.Context;
2023
import android.net.Uri;
@@ -40,6 +43,7 @@ public class UploadTest extends AndroidTestCase {
4043
private String key = IO.UNDEFINED_KEY;
4144

4245
private com.qiniu.resumableio.PutExtra rextra;
46+
private Semaphore sem = new Semaphore(0, true);
4347

4448
public void setUp() throws Exception {
4549
key = UUID.randomUUID().toString();
@@ -58,8 +62,7 @@ public void setUp() throws Exception {
5862
jsonRet = new JSONObjectRet() {
5963
@Override
6064
public void onProcess(long current, long total) {
61-
//Log.d("UploadTest", current + "/" + total);
62-
// Assert.assertEquals(file.length(), total); // 内部实现原因,可能不相等
65+
Log.d("UploadTest", current + "/" + total);
6366
}
6467

6568
@Override
@@ -68,7 +71,7 @@ public void onSuccess(JSONObject res) {
6871
success = true;
6972
resp = res;
7073
Log.d("UploadTest", "上传成功! " + resp.toString());
71-
System.out.println("上传成功! " + resp.toString());
74+
sem.release();
7275
}
7376

7477
@Override
@@ -77,7 +80,7 @@ public void onFailure(QiniuException ex) {
7780
success = false;
7881
e = ex;
7982
Log.d("UploadTest", "上传失败! " + ex.getMessage());
80-
System.out.println("上传失败! " + ex.getMessage());
83+
sem.release();
8184
}
8285
};
8386
}
@@ -90,47 +93,59 @@ public void tearDown() throws Exception {
9093
}
9194

9295
@SmallTest
93-
public void testS() throws IOException, JSONException {
96+
public void testS() throws IOException, JSONException, InterruptedException {
9497
file = createFile(0.2, ".test");
9598
uri = Uri.fromFile(file);
9699
IO.putFile(context, uptoken, key, uri, extra, jsonRet);
97-
sleepLimit(60);
100+
sem.acquire();
101+
successCheck();
102+
}
103+
104+
@SmallTest
105+
public void testIOMultiHost() throws IOException, JSONException, InterruptedException {
106+
String old = Conf.UP_HOST;
107+
Conf.UP_HOST = "http://127.0.0.1:1";
108+
file = createFile(0.1, "-mup.test");
109+
uri = Uri.fromFile(file);
110+
IO.putFile(context, uptoken, key, uri, extra, jsonRet);
111+
Conf.UP_HOST = old;
112+
sem.acquire();
98113
successCheck();
99114
}
100115

101116
@MediumTest
102-
public void testM() throws IOException, JSONException {
103-
file = createFile(4, "--—— 中 文 .test");
117+
public void testM() throws IOException, JSONException, InterruptedException {
118+
file = createFile(0.1, "--—— 中 文 .test");
104119
uri = Uri.fromFile(file);
105120
IO.putFile(context, uptoken, key, uri, extra, jsonRet);
106-
sleepLimit(60 * 5);
121+
sem.acquire();
107122
successCheck();
108123
}
109124

110125
@SmallTest
111-
public void testRS() throws IOException, JSONException {
126+
public void testRS() throws IOException, JSONException, InterruptedException {
112127
file = createFile(0.2, ".test");
113128
uri = Uri.fromFile(file);
114129
ResumableIO.putFile(context, uptoken, key, uri, rextra, jsonRet);
115-
sleepLimit(60);
130+
sem.acquire();
116131
successCheck();
117132
}
118133

119134
@MediumTest
120-
public void testRM() throws IOException, JSONException {
135+
public void testRM() throws IOException, JSONException, InterruptedException {
121136
file = createFile(4, ".test");
122137
uri = Uri.fromFile(file);
123138
ResumableIO.putFile(context, uptoken, key, uri, rextra, jsonRet);
124-
sleepLimit(60 * 5);
139+
sem.acquire();
125140
successCheck();
126141
}
127142

128143
// @MediumTest
129-
// public void testRL() throws IOException, JSONException {
144+
// public void testRL() throws IOException, JSONException, InterruptedException {
130145
// file = createFile(8.6, ".test");
131146
// uri = Uri.fromFile(file);
132147
// ResumableIO.putFile(context, uptoken, key, uri, rextra, jsonRet);
133-
// sleepLimit(60 * 5);
148+
// sem.acquire();
134149
// successCheck();
135150
// }
136151

@@ -141,18 +156,6 @@ private void successCheck() throws JSONException{
141156
Assert.assertEquals(file.length(), resp.getLong("fsize"));
142157
}
143158

144-
private void sleepLimit(int limit){
145-
int t = 5;
146-
while(uploading && t < limit){
147-
try {
148-
t += 5;
149-
Thread.sleep(1000 * 5);
150-
} catch (InterruptedException e) {
151-
152-
}
153-
}
154-
}
155-
156159
private File createFile(double fileSize, String suf) throws IOException {
157160
FileOutputStream fos = null;
158161
try{

0 commit comments

Comments
 (0)