Skip to content

Commit 5b9165f

Browse files
author
YangSen-qn
committed
optimize resume upload get data logic
1 parent 3a438a9 commit 5b9165f

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

library/src/androidTest/java/com/qiniu/android/ConcurrentResumeUploadTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ public void testHttpV2(){
229229
.chunkSize(4*1024*1024)
230230
.useHttps(false)
231231
.build();
232-
int[] sizeArray = {500, 1000, 3000, 4000, 5000, 8000, 10000, 20000};
232+
// int[] sizeArray = {500, 1000, 3000, 4000, 5000, 8000, 10000, 20000};
233+
int[] sizeArray = {200000};
233234
long timestamp = new Date().getTime();
234235
for (int size : sizeArray) {
235236
String key = "android_concurrent_resume_http_v2_" + timestamp + "_" + size + "k";

library/src/main/java/com/qiniu/android/storage/PartsUploadPerformerV1.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void uploadNextData(final PartsUploadPerformerDataCompleteHandler completeHandle
7171
return;
7272
}
7373

74-
chunk.data = getDataWithChunk(chunk, block);
74+
chunk.data = getChunkDataWithRetry(chunk, block);
7575
if (chunk.data == null) {
7676
LogUtil.i("key:" + StringUtils.toNonnullString(key) + " no chunk left");
7777

@@ -184,21 +184,42 @@ public void complete(ResponseInfo responseInfo, UploadRegionRequestMetrics reque
184184

185185
}
186186

187-
private synchronized byte[] getDataWithChunk(UploadData chunk, UploadBlock block) {
187+
private byte[] getChunkDataWithRetry(UploadData chunk, UploadBlock block) {
188+
byte[] uploadData = null;
189+
190+
int maxTime = 3;
191+
int index = 0;
192+
while (index < maxTime) {
193+
uploadData = getChunkData(chunk, block);
194+
if (uploadData != null) {
195+
break;
196+
}
197+
index ++;
198+
}
199+
200+
return uploadData;
201+
}
202+
203+
private synchronized byte[] getChunkData(UploadData chunk, UploadBlock block) {
188204
if (randomAccessFile == null || chunk == null || block == null) {
189205
return null;
190206
}
191207
int readSize = 0;
192-
byte[] data = new byte[(int) chunk.size];
208+
byte[] data = new byte[chunk.size];
193209
try {
194210
randomAccessFile.seek((chunk.offset + block.offset));
195211
while (readSize < chunk.size) {
196-
int ret = randomAccessFile.read(data, readSize, (int) (chunk.size - readSize));
212+
int ret = randomAccessFile.read(data, readSize, (chunk.size - readSize));
197213
if (ret < 0) {
198214
break;
199215
}
200216
readSize += ret;
201217
}
218+
219+
// 读数据非预期
220+
if (readSize != chunk.size) {
221+
data = null;
222+
}
202223
} catch (IOException e) {
203224
data = null;
204225
}

library/src/main/java/com/qiniu/android/storage/PartsUploadPerformerV2.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void uploadNextData(final PartsUploadPerformerDataCompleteHandler completeHandle
9898
return;
9999
}
100100

101-
data.data = getUploadData(data);
101+
data.data = getUploadDataWithRetry(data);
102102
if (data.data == null) {
103103
LogUtil.i("key:" + StringUtils.toNonnullString(key) + " get data error");
104104

@@ -168,22 +168,42 @@ public void complete(ResponseInfo responseInfo, UploadRegionRequestMetrics reque
168168
});
169169
}
170170

171+
private byte[] getUploadDataWithRetry(UploadData data) {
172+
byte[] uploadData = null;
173+
174+
int maxTime = 3;
175+
int index = 0;
176+
while (index < maxTime) {
177+
uploadData = getUploadData(data);
178+
if (uploadData != null) {
179+
break;
180+
}
181+
index ++;
182+
}
183+
184+
return uploadData;
185+
}
186+
171187
private synchronized byte[] getUploadData(UploadData data) {
172188
if (randomAccessFile == null || data == null) {
173189
return null;
174190
}
175191

176192
int readSize = 0;
177-
byte[] uploadData = new byte[(int) data.size];
193+
byte[] uploadData = new byte[data.size];
178194
try {
179195
randomAccessFile.seek(data.offset);
180196
while (readSize < data.size) {
181-
int ret = randomAccessFile.read(uploadData, readSize, (int) (data.size - readSize));
197+
int ret = randomAccessFile.read(uploadData, readSize, data.size - readSize);
182198
if (ret < 0) {
183199
break;
184200
}
185201
readSize += ret;
186202
}
203+
// 读数据非预期
204+
if (readSize != data.size) {
205+
uploadData = null;
206+
}
187207
} catch (IOException e) {
188208
uploadData = null;
189209
}

0 commit comments

Comments
 (0)