提交 032e0cc5 authored 作者: Noel Grandin's avatar Noel Grandin

fix bug in partial writes test

unfortunately can't enable this yet - I'm pretty sure the FileSystem
side is fine, but the MVStore obviously needs more sync points somewhere
上级 9a35cf9e
...@@ -22,7 +22,7 @@ import org.h2.test.utils.FilePathReorderWrites; ...@@ -22,7 +22,7 @@ import org.h2.test.utils.FilePathReorderWrites;
* Tests that the MVStore recovers from a power failure if the file system or * Tests that the MVStore recovers from a power failure if the file system or
* disk re-ordered the write operations. * disk re-ordered the write operations.
*/ */
public class TestReorderWrites extends TestBase { public class TestReorderWrites extends TestBase {
private static final boolean LOG = false; private static final boolean LOG = false;
...@@ -93,7 +93,7 @@ public class TestReorderWrites extends TestBase { ...@@ -93,7 +93,7 @@ public class TestReorderWrites extends TestBase {
// write has to fail at some point // write has to fail at some point
fail(); fail();
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
log("stop " + e); log("stop " + e + ", cause: " + e.getCause());
// expected // expected
} }
try { try {
...@@ -138,10 +138,10 @@ public class TestReorderWrites extends TestBase { ...@@ -138,10 +138,10 @@ public class TestReorderWrites extends TestBase {
private void testFileSystem() throws IOException { private void testFileSystem() throws IOException {
FilePathReorderWrites fs = FilePathReorderWrites.register(); FilePathReorderWrites fs = FilePathReorderWrites.register();
// disable this for now, still a bug somewhere // disable this for now, still bug(s) in our code
FilePathReorderWrites.setPartialWrites(false); FilePathReorderWrites.setPartialWrites(false);
String fileName = "reorder:memFS:test"; String fileName = "reorder:memFS:test";
ByteBuffer empty = ByteBuffer.allocate(1024); final ByteBuffer empty = ByteBuffer.allocate(1024);
Random r = new Random(1); Random r = new Random(1);
long minSize = Long.MAX_VALUE; long minSize = Long.MAX_VALUE;
long maxSize = 0; long maxSize = 0;
......
...@@ -69,13 +69,13 @@ public class FilePathReorderWrites extends FilePathWrapper { ...@@ -69,13 +69,13 @@ public class FilePathReorderWrites extends FilePathWrapper {
/** /**
* Whether partial writes are possible (writing only part of the data). * Whether partial writes are possible (writing only part of the data).
* *
* @param partialWrites true to enable * @param b true to enable
*/ */
public static void setPartialWrites(boolean partialWrites) { public static void setPartialWrites(boolean b) {
FilePathReorderWrites.partialWrites = partialWrites; partialWrites = b;
} }
static boolean getPartialWrites() { static boolean isPartialWrites() {
return partialWrites; return partialWrites;
} }
...@@ -123,6 +123,8 @@ public class FilePathReorderWrites extends FilePathWrapper { ...@@ -123,6 +123,8 @@ public class FilePathReorderWrites extends FilePathWrapper {
FilePath copy = FilePath.get(getBase().toString() + ".copy"); FilePath copy = FilePath.get(getBase().toString() + ".copy");
OutputStream out = copy.newOutputStream(false); OutputStream out = copy.newOutputStream(false);
IOUtils.copy(in, out); IOUtils.copy(in, out);
in.close();
out.close();
FileChannel base = getBase().open(mode); FileChannel base = getBase().open(mode);
FileChannel readBase = copy.open(mode); FileChannel readBase = copy.open(mode);
return new FileReorderWrites(this, base, readBase); return new FileReorderWrites(this, base, readBase);
...@@ -143,11 +145,10 @@ public class FilePathReorderWrites extends FilePathWrapper { ...@@ -143,11 +145,10 @@ public class FilePathReorderWrites extends FilePathWrapper {
super.delete(); super.delete();
FilePath.get(getBase().toString() + ".copy").delete(); FilePath.get(getBase().toString() + ".copy").delete();
} }
} }
/** /**
* An file that checks for errors before each write operation. * A write-reordering file implementation.
*/ */
class FileReorderWrites extends FileBase { class FileReorderWrites extends FileBase {
...@@ -272,7 +273,8 @@ class FileReorderWrites extends FileBase { ...@@ -272,7 +273,8 @@ class FileReorderWrites extends FileBase {
@Override @Override
public int write(ByteBuffer src, long position) throws IOException { public int write(ByteBuffer src, long position) throws IOException {
if (FilePathReorderWrites.getPartialWrites() && src.remaining() > 2) { if (FilePathReorderWrites.isPartialWrites() && src.remaining() > 2) {
final int tmp = src.remaining();
ByteBuffer buf1 = src.slice(); ByteBuffer buf1 = src.slice();
ByteBuffer buf2 = src.slice(); ByteBuffer buf2 = src.slice();
int len1 = src.remaining() / 2; int len1 = src.remaining() / 2;
...@@ -282,6 +284,7 @@ class FileReorderWrites extends FileBase { ...@@ -282,6 +284,7 @@ class FileReorderWrites extends FileBase {
int x = addOperation(new FileWriteOperation(id++, position, buf1)); int x = addOperation(new FileWriteOperation(id++, position, buf1));
x += addOperation( x += addOperation(
new FileWriteOperation(id++, position + len1, buf2)); new FileWriteOperation(id++, position + len1, buf2));
src.position( src.position() + x );
return x; return x;
} }
return addOperation(new FileWriteOperation(id++, position, src)); return addOperation(new FileWriteOperation(id++, position, src));
...@@ -383,7 +386,6 @@ class FileReorderWrites extends FileBase { ...@@ -383,7 +386,6 @@ class FileReorderWrites extends FileBase {
channel.truncate(position); channel.truncate(position);
return -1; return -1;
} }
// TODO support the case where part is not written
int len = channel.write(buffer, position); int len = channel.write(buffer, position);
buffer.flip(); buffer.flip();
return len; return len;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论