Skip to content

Commit e94845e

Browse files
committed
Merge pull request #57 from longbai/multi-up-host
断点续上传多host重试
2 parents eb3dd67 + c9da511 commit e94845e

File tree

3 files changed

+72
-21
lines changed

3 files changed

+72
-21
lines changed

src/com/qiniu/io/IO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void onFailure(QiniuException ex) {
105105
return;
106106
}
107107
isa.reset();
108-
final Client.ClientExecutor executor2 = client.makeClientExecutor();
108+
Client.ClientExecutor executor2 = client.makeClientExecutor();
109109
client.call(executor2, Conf.UP_HOST2, m, ret);
110110
}
111111
};

src/com/qiniu/resumableio/ResumableClient.java

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.qiniu.utils.InputStreamAt;
1919
import com.qiniu.utils.Base64;
2020
import com.qiniu.utils.QiniuException;
21+
import com.qiniu.utils.RetryRet;
2122

2223
public class ResumableClient extends Client {
2324
String mUpToken;
@@ -101,39 +102,77 @@ public void onFailure(QiniuException ex) {
101102
return canceler;
102103
}
103104

104-
public ICancel mkblk(InputStreamAt input, long offset, int blockSize, int writeSize, CallRet ret) {
105+
public ICancel mkblk(final InputStreamAt input, final long offset, final int blockSize, final int writeSize, final CallRet ret) {
105106
String url = Conf.UP_HOST + "/mkblk/" + blockSize;
106-
ClientExecutor client = makeClientExecutor();
107-
call(client, url, input.toHttpEntity(offset, writeSize, client), ret);
108-
return client;
107+
ClientExecutor executor = makeClientExecutor();
108+
CallRet retryRet = new RetryRet(ret){
109+
@Override
110+
public void onFailure(QiniuException ex) {
111+
if (RetryRet.noRetry(ex)){
112+
ret.onFailure(ex);
113+
return;
114+
}
115+
ClientExecutor executor2 = makeClientExecutor();
116+
String url2 = Conf.UP_HOST2 + "/mkblk/" + blockSize;
117+
call(executor2, url2, input.toHttpEntity(offset, writeSize, executor2), ret);
118+
}
119+
};
120+
121+
call(executor, url, input.toHttpEntity(offset, writeSize, executor), retryRet);
122+
return executor;
109123
}
110124

111125
public ICancel bput(String host, InputStreamAt input, String ctx, long blockOffset, long offset, int writeLength, CallRet ret) {
112126
String url = host + "/bput/" + ctx + "/" + offset;
113127
ClientExecutor client = makeClientExecutor();
128+
114129
call(client, url, input.toHttpEntity(blockOffset+offset, writeLength, client), ret);
115130
return client;
116131
}
117132

118-
public ICancel mkfile(String key, long fsize, String mimeType, Map<String, String> params, String ctxs, CallRet ret) {
119-
String url = Conf.UP_HOST + "/mkfile/" + fsize;
133+
public ICancel mkfile(final String key, final long fsize, final String mimeType, final Map<String, String> params, final String ctxs, final CallRet ret) {
134+
String url = Conf.UP_HOST + mkfilePath(key, fsize, mimeType, params);
135+
StringEntity entity = null;
136+
try {
137+
entity = new StringEntity(ctxs);
138+
} catch (UnsupportedEncodingException e) {
139+
e.printStackTrace();
140+
ret.onFailure(new QiniuException(QiniuException.InvalidEncode, "mkfile", e));
141+
return null;
142+
}
143+
144+
CallRet retryRet = new RetryRet(ret){
145+
@Override
146+
public void onFailure(QiniuException ex) {
147+
if (RetryRet.noRetry(ex)){
148+
ret.onFailure(ex);
149+
return;
150+
}
151+
String url2 = Conf.UP_HOST2 + mkfilePath(key, fsize, mimeType, params);
152+
StringEntity entity2 = null;
153+
try {
154+
entity2 = new StringEntity(ctxs);
155+
} catch (UnsupportedEncodingException e) {
156+
}
157+
call(makeClientExecutor(), url2, entity2, ret);
158+
}
159+
};
160+
return call(makeClientExecutor(), url, entity, retryRet);
161+
}
162+
163+
private static String mkfilePath(String key, long fsize, String mimeType, Map<String, String> params){
164+
String path = "/mkfile/" + fsize;
120165
if (mimeType != null && mimeType.length() > 0) {
121-
url += "/mimeType/" + Base64.encode(mimeType);
166+
path += "/mimeType/" + Base64.encode(mimeType);
122167
}
123168
if (key != null && key.length() > 0) {
124-
url += "/key/" + Base64.encode(key);
169+
path += "/key/" + Base64.encode(key);
125170
}
126171
if (params != null && params.size() > 0) {
127172
for (Map.Entry<String, String> a: params.entrySet()) {
128-
url += "/" + a.getKey() + "/" + Base64.encode(a.getValue());
173+
path += "/" + a.getKey() + "/" + Base64.encode(a.getValue());
129174
}
130175
}
131-
try {
132-
return call(makeClientExecutor(), url, new StringEntity(ctxs), ret);
133-
} catch (UnsupportedEncodingException e) {
134-
e.printStackTrace();
135-
ret.onFailure(new QiniuException(QiniuException.InvalidEncode, "mkfile", e));
136-
return null;
137-
}
176+
return path;
138177
}
139178
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void tearDown() throws Exception {
9393
}
9494

9595
@SmallTest
96-
public void testS() throws IOException, JSONException, InterruptedException {
96+
public void testIOkilo() throws IOException, JSONException, InterruptedException {
9797
file = createFile(0.2, ".test");
9898
uri = Uri.fromFile(file);
9999
IO.putFile(context, uptoken, key, uri, extra, jsonRet);
@@ -114,7 +114,7 @@ public void testIOMultiHost() throws IOException, JSONException, InterruptedExce
114114
}
115115

116116
@MediumTest
117-
public void testM() throws IOException, JSONException, InterruptedException {
117+
public void testIOMega() throws IOException, JSONException, InterruptedException {
118118
file = createFile(0.1, "--—— 中 文 .test");
119119
uri = Uri.fromFile(file);
120120
IO.putFile(context, uptoken, key, uri, extra, jsonRet);
@@ -123,7 +123,7 @@ public void testM() throws IOException, JSONException, InterruptedException {
123123
}
124124

125125
@SmallTest
126-
public void testRS() throws IOException, JSONException, InterruptedException {
126+
public void testRIOkilo() throws IOException, JSONException, InterruptedException {
127127
file = createFile(0.2, ".test");
128128
uri = Uri.fromFile(file);
129129
ResumableIO.putFile(context, uptoken, key, uri, rextra, jsonRet);
@@ -132,14 +132,26 @@ public void testRS() throws IOException, JSONException, InterruptedException {
132132
}
133133

134134
@MediumTest
135-
public void testRM() throws IOException, JSONException, InterruptedException {
135+
public void testRIOMega() throws IOException, JSONException, InterruptedException {
136136
file = createFile(4, ".test");
137137
uri = Uri.fromFile(file);
138138
ResumableIO.putFile(context, uptoken, key, uri, rextra, jsonRet);
139139
sem.acquire();
140140
successCheck();
141141
}
142142

143+
@MediumTest
144+
public void testRIOMutiHost() throws IOException, JSONException, InterruptedException {
145+
String old = Conf.UP_HOST;
146+
Conf.UP_HOST = "http://127.0.0.1:1";
147+
file = createFile(4, ".test");
148+
uri = Uri.fromFile(file);
149+
ResumableIO.putFile(context, uptoken, key, uri, rextra, jsonRet);
150+
sem.acquire();
151+
Conf.UP_HOST = old;
152+
successCheck();
153+
}
154+
143155
// @MediumTest
144156
// public void testRL() throws IOException, JSONException, InterruptedException {
145157
// file = createFile(8.6, ".test");

0 commit comments

Comments
 (0)