Search in sources :

Example 1 with FilePathReorderWrites

use of org.h2.test.utils.FilePathReorderWrites in project h2database by h2database.

the class TestReorderWrites method testFileSystem.

private void testFileSystem() throws IOException {
    FilePathReorderWrites fs = FilePathReorderWrites.register();
    // disable this for now, still bug(s) in our code
    FilePathReorderWrites.setPartialWrites(false);
    String fileName = "reorder:memFS:test";
    final ByteBuffer empty = ByteBuffer.allocate(1024);
    Random r = new Random(1);
    long minSize = Long.MAX_VALUE;
    long maxSize = 0;
    int minWritten = Integer.MAX_VALUE;
    int maxWritten = 0;
    for (int i = 0; i < 100; i++) {
        fs.setPowerOffCountdown(100, i);
        FileUtils.delete(fileName);
        FileChannel fc = FilePath.get(fileName).open("rw");
        for (int j = 0; j < 20; j++) {
            fc.write(empty, j * 1024);
            empty.flip();
        }
        fs.setPowerOffCountdown(4 + r.nextInt(20), i);
        int lastWritten = 0;
        int lastTruncated = 0;
        for (int j = 20; j >= 0; j--) {
            try {
                byte[] bytes = new byte[1024];
                Arrays.fill(bytes, (byte) j);
                ByteBuffer data = ByteBuffer.wrap(bytes);
                fc.write(data, 0);
                lastWritten = j;
            } catch (IOException e) {
                // expected
                break;
            }
            try {
                fc.truncate(j * 1024);
                lastTruncated = j * 1024;
            } catch (IOException e) {
                // expected
                break;
            }
        }
        if (lastTruncated <= 0 || lastWritten <= 0) {
            fail();
        }
        fs.setPowerOffCountdown(100, 0);
        fc = FilePath.get(fileName).open("rw");
        ByteBuffer data = ByteBuffer.allocate(1024);
        fc.read(data, 0);
        data.flip();
        int got = data.get();
        long size = fc.size();
        minSize = Math.min(minSize, size);
        maxSize = Math.max(minSize, size);
        minWritten = Math.min(minWritten, got);
        maxWritten = Math.max(maxWritten, got);
    }
    assertTrue(minSize < maxSize);
    assertTrue(minWritten < maxWritten);
    // release the static data this test generates
    FileUtils.delete(fileName);
}
Also used : Random(java.util.Random) FileChannel(java.nio.channels.FileChannel) FilePathReorderWrites(org.h2.test.utils.FilePathReorderWrites) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with FilePathReorderWrites

use of org.h2.test.utils.FilePathReorderWrites in project h2database by h2database.

the class TestReorderWrites method testMVStore.

private void testMVStore() {
    FilePathReorderWrites fs = FilePathReorderWrites.register();
    String fileName = "reorder:memFS:test.mv";
    try {
        for (int i = 0; i < 1000; i++) {
            log(i + " --------------------------------");
            // this test is not interested in power off failures during
            // initial creation
            fs.setPowerOffCountdown(0, 0);
            // release the static data this test generates
            FileUtils.delete("memFS:test.mv");
            FileUtils.delete("memFS:test.mv.copy");
            MVStore store = new MVStore.Builder().fileName(fileName).autoCommitDisabled().open();
            // store.setRetentionTime(10);
            Map<Integer, byte[]> map = store.openMap("data");
            map.put(-1, new byte[1]);
            store.commit();
            store.getFileStore().sync();
            Random r = new Random(i);
            int stop = 4 + r.nextInt(20);
            log("countdown start");
            fs.setPowerOffCountdown(stop, i);
            try {
                for (int j = 1; j < 100; j++) {
                    Map<Integer, Integer> newMap = store.openMap("d" + j);
                    newMap.put(j, j * 10);
                    int key = r.nextInt(10);
                    int len = 10 * r.nextInt(1000);
                    if (r.nextBoolean()) {
                        map.remove(key);
                    } else {
                        map.put(key, new byte[len]);
                    }
                    log("op " + j + ": ");
                    store.commit();
                    switch(r.nextInt(10)) {
                        case 0:
                            log("op compact");
                            store.compact(100, 10 * 1024);
                            break;
                        case 1:
                            log("op compactMoveChunks");
                            store.compactMoveChunks();
                            log("op compactMoveChunks done");
                            break;
                    }
                }
                // write has to fail at some point
                fail();
            } catch (IllegalStateException e) {
                log("stop " + e + ", cause: " + e.getCause());
            // expected
            }
            try {
                store.close();
            } catch (IllegalStateException e) {
                // expected
                store.closeImmediately();
            }
            log("verify");
            fs.setPowerOffCountdown(100, 0);
            if (LOG) {
                MVStoreTool.dump(fileName, true);
            }
            store = new MVStore.Builder().fileName(fileName).autoCommitDisabled().open();
            map = store.openMap("data");
            if (!map.containsKey(-1)) {
                fail("key not found, size=" + map.size() + " i=" + i);
            } else {
                assertEquals("i=" + i, 1, map.get(-1).length);
            }
            for (int j = 0; j < 100; j++) {
                Map<Integer, Integer> newMap = store.openMap("d" + j);
                newMap.get(j);
            }
            map.keySet();
            store.close();
        }
    } finally {
        // release the static data this test generates
        FileUtils.delete("memFS:test.mv");
        FileUtils.delete("memFS:test.mv.copy");
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) Random(java.util.Random) FilePathReorderWrites(org.h2.test.utils.FilePathReorderWrites)

Aggregations

Random (java.util.Random)2 FilePathReorderWrites (org.h2.test.utils.FilePathReorderWrites)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 MVStore (org.h2.mvstore.MVStore)1