提交 9aa05223 authored 作者: Andrei Tokar's avatar Andrei Tokar

retry intermittent truncate failures on windows

close file/release lock on failures
上级 9bcb2942
...@@ -168,12 +168,14 @@ public class FileStore { ...@@ -168,12 +168,14 @@ public class FileStore {
"The file is locked: {0}", fileName, e); "The file is locked: {0}", fileName, e);
} }
if (fileLock == null) { if (fileLock == null) {
try { close(); } catch (Exception ignore) {}
throw DataUtils.newIllegalStateException( throw DataUtils.newIllegalStateException(
DataUtils.ERROR_FILE_LOCKED, DataUtils.ERROR_FILE_LOCKED,
"The file is locked: {0}", fileName); "The file is locked: {0}", fileName);
} }
fileSize = file.size(); fileSize = file.size();
} catch (IOException e) { } catch (IOException e) {
try { close(); } catch (Exception ignore) {}
throw DataUtils.newIllegalStateException( throw DataUtils.newIllegalStateException(
DataUtils.ERROR_READING_FAILED, DataUtils.ERROR_READING_FAILED,
"Could not open file {0}", fileName, e); "Could not open file {0}", fileName, e);
...@@ -184,6 +186,7 @@ public class FileStore { ...@@ -184,6 +186,7 @@ public class FileStore {
* Close this store. * Close this store.
*/ */
public void close() { public void close() {
if(file != null) {
try { try {
if (fileLock != null) { if (fileLock != null) {
fileLock.release(); fileLock.release();
...@@ -199,11 +202,13 @@ public class FileStore { ...@@ -199,11 +202,13 @@ public class FileStore {
file = null; file = null;
} }
} }
}
/** /**
* Flush all changes. * Flush all changes.
*/ */
public void sync() { public void sync() {
if (file != null) {
try { try {
file.force(true); file.force(true);
} catch (IOException e) { } catch (IOException e) {
...@@ -212,6 +217,7 @@ public class FileStore { ...@@ -212,6 +217,7 @@ public class FileStore {
"Could not sync file {0}", fileName, e); "Could not sync file {0}", fileName, e);
} }
} }
}
/** /**
* Get the file size. * Get the file size.
...@@ -228,16 +234,24 @@ public class FileStore { ...@@ -228,16 +234,24 @@ public class FileStore {
* @param size the new file size * @param size the new file size
*/ */
public void truncate(long size) { public void truncate(long size) {
int attemptCount = 0;
while (true) {
try { try {
writeCount.incrementAndGet(); writeCount.incrementAndGet();
file.truncate(size); file.truncate(size);
fileSize = Math.min(fileSize, size); fileSize = Math.min(fileSize, size);
return;
} catch (IOException e) { } catch (IOException e) {
if (++attemptCount == 10) {
throw DataUtils.newIllegalStateException( throw DataUtils.newIllegalStateException(
DataUtils.ERROR_WRITING_FAILED, DataUtils.ERROR_WRITING_FAILED,
"Could not truncate file {0} to size {1}", "Could not truncate file {0} to size {1}",
fileName, size, e); fileName, size, e);
} }
System.gc();
Thread.yield();
}
}
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论