Skip to content
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

FIX: issue #415#425 #485

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.iml
.gradle
/.idea
/local.properties
/.idea/workspace.xml
/.idea/libraries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,11 @@ void inspectAndPersist() throws IOException {
synchronized void close(int blockIndex) throws IOException {
final DownloadOutputStream outputStream = outputStreamMap.get(blockIndex);
if (outputStream != null) {
outputStream.close();
outputStreamMap.remove(blockIndex);
synchronized (noSyncLengthMap) {
outputStream.close();
outputStreamMap.remove(blockIndex);
noSyncLengthMap.remove(blockIndex);
}
Util.d(TAG, "OutputStream close task[" + task.getId() + "] block[" + blockIndex + "]");
}
}
Expand Down Expand Up @@ -447,50 +450,46 @@ long now() {

void flushProcess() throws IOException {
boolean success;
final int size;
synchronized (noSyncLengthMap) {
// make sure the length of noSyncLengthMap is equal to outputStreamMap
size = noSyncLengthMap.size();
}

final SparseArray<Long> increaseLengthMap = new SparseArray<>(size);

try {
for (int i = 0; i < size; i++) {
final int blockIndex = outputStreamMap.keyAt(i);
// because we get no sync length value before flush and sync,
// so the length only possible less than or equal to the real persist
// length.
final long noSyncLength = noSyncLengthMap.get(blockIndex).get();
if (noSyncLength > 0) {
increaseLengthMap.put(blockIndex, noSyncLength);
final DownloadOutputStream outputStream = outputStreamMap
.get(blockIndex);
outputStream.flushAndSync();
final int size = noSyncLengthMap.size();
final SparseArray<Long> increaseLengthMap = new SparseArray<>(size);
try {
for (int i = 0; i < size; i++) {
final int blockIndex = noSyncLengthMap.keyAt(i);
// because we get no sync length value before flush and sync,
// so the length only possible less than or equal to the real persist
// length.
final long noSyncLength = noSyncLengthMap.get(blockIndex).get();
if (noSyncLength > 0) {
increaseLengthMap.put(blockIndex, noSyncLength);
final DownloadOutputStream outputStream = outputStreamMap
.get(blockIndex);
outputStream.flushAndSync();
}
}
success = true;
} catch (IOException ex) {
Util.w(TAG, "OutputStream flush and sync data to filesystem failed " + ex);
success = false;
}
success = true;
} catch (IOException ex) {
Util.w(TAG, "OutputStream flush and sync data to filesystem failed " + ex);
success = false;
}

if (success) {
final int increaseLengthSize = increaseLengthMap.size();
long allIncreaseLength = 0;
for (int i = 0; i < increaseLengthSize; i++) {
final int blockIndex = increaseLengthMap.keyAt(i);
final long noSyncLength = increaseLengthMap.valueAt(i);
store.onSyncToFilesystemSuccess(info, blockIndex, noSyncLength);
allIncreaseLength += noSyncLength;
noSyncLengthMap.get(blockIndex).addAndGet(-noSyncLength);
Util.d(TAG, "OutputStream sync success (" + task.getId() + ") "
+ "block(" + blockIndex + ") " + " syncLength(" + noSyncLength + ")"
+ " currentOffset(" + info.getBlock(blockIndex).getCurrentOffset()
+ ")");
if (success) {
final int increaseLengthSize = increaseLengthMap.size();
long allIncreaseLength = 0;
for (int i = 0; i < increaseLengthSize; i++) {
final int blockIndex = increaseLengthMap.keyAt(i);
final long noSyncLength = increaseLengthMap.valueAt(i);
store.onSyncToFilesystemSuccess(info, blockIndex, noSyncLength);
allIncreaseLength += noSyncLength;
noSyncLengthMap.get(blockIndex).addAndGet(-noSyncLength);
Util.d(TAG, "OutputStream sync success (" + task.getId() + ") "
+ "block(" + blockIndex + ") " + " syncLength(" + noSyncLength + ")"
+ " currentOffset(" + info.getBlock(blockIndex).getCurrentOffset()
+ ")");
}
allNoSyncLength.addAndGet(-allIncreaseLength);
lastSyncTimestamp.set(SystemClock.uptimeMillis());
}
allNoSyncLength.addAndGet(-allIncreaseLength);
lastSyncTimestamp.set(SystemClock.uptimeMillis());
}
}

Expand Down