Search in sources :

Example 16 with Path

use of io.questdb.std.str.Path in project questdb by bluestreak01.

the class CairoMemoryTest method testVirtualMemoryJump.

private void testVirtualMemoryJump(VirtualMemoryFactory factory) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
            try (MemoryARW mem = factory.newInstance(path)) {
                for (int i = 0; i < 100; i++) {
                    mem.putLong(i);
                }
                mem.jumpTo(0);
                for (int i = 0; i < 50; i++) {
                    mem.putLong(50 - i);
                }
                // keep previously written data
                mem.jumpTo(800);
            }
            try (MemoryMR roMem = new MemoryCMRImpl(FF, path, 800, MemoryTag.MMAP_DEFAULT)) {
                for (int i = 0; i < 50; i++) {
                    Assert.assertEquals(50 - i, roMem.getLong(i * 8));
                }
                for (int i = 50; i < 100; i++) {
                    Assert.assertEquals(i, roMem.getLong(i * 8));
                }
            }
        }
    });
}
Also used : Path(io.questdb.std.str.Path) MemoryMR(io.questdb.cairo.vm.api.MemoryMR) MemoryCMRImpl(io.questdb.cairo.vm.MemoryCMRImpl) MemoryARW(io.questdb.cairo.vm.api.MemoryARW)

Example 17 with Path

use of io.questdb.std.str.Path in project questdb by bluestreak01.

the class CairoMemoryTest method testAppendAllocateError.

@Test
public void testAppendAllocateError() throws Exception {
    long used = Unsafe.getMemUsed();
    class X extends FilesFacadeImpl {

        int count = 2;

        boolean allClear = false;

        @Override
        public boolean allocate(long fd, long size) {
            if (allClear || --count > 0) {
                return super.allocate(fd, size);
            }
            allClear = true;
            return false;
        }
    }
    X ff = new X();
    long openFileCount = ff.getOpenFileCount();
    try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
        try (MemoryPMARImpl mem = new MemoryPMARImpl(ff, path, 2 * ff.getPageSize(), MemoryTag.MMAP_DEFAULT)) {
            try {
                for (int i = 0; i < N * 10; i++) {
                    mem.putLong(i);
                }
                Assert.fail();
            } catch (CairoException ignore) {
            }
            Assert.assertTrue(mem.getAppendOffset() > 0);
        }
    }
    Assert.assertEquals(used, Unsafe.getMemUsed());
    Assert.assertEquals(openFileCount, ff.getOpenFileCount());
}
Also used : Path(io.questdb.std.str.Path) MemoryPMARImpl(io.questdb.cairo.vm.MemoryPMARImpl) Test(org.junit.Test)

Example 18 with Path

use of io.questdb.std.str.Path in project questdb by bluestreak01.

the class CairoMemoryTest method testReadWriteMemoryTruncate.

@Test
public void testReadWriteMemoryTruncate() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
            int pageSize = 1024 * 1024;
            try (MemoryCMARW mem = Vm.getSmallCMARWInstance(FF, path, MemoryTag.MMAP_DEFAULT)) {
                int count = 2 * pageSize / Long.BYTES;
                for (int i = 0; i < count; i++) {
                    mem.putLong(i);
                }
                long fileSize = FF.length(path);
                // read the whole file
                long addr = FF.mmap(mem.getFd(), fileSize, 0, Files.MAP_RO, MemoryTag.MMAP_DEFAULT);
                try {
                    for (int i = 0; i < count; i++) {
                        Assert.assertEquals(i, Unsafe.getUnsafe().getLong(addr + i * Long.BYTES));
                    }
                } finally {
                    FF.munmap(addr, fileSize, MemoryTag.MMAP_DEFAULT);
                }
                // truncate
                mem.truncate();
                // ensure that entire file is zeroed out
                fileSize = FF.length(path);
                addr = FF.mmap(mem.getFd(), fileSize, 0, Files.MAP_RO, MemoryTag.MMAP_DEFAULT);
                try {
                    for (int i = 0; i < fileSize / Long.BYTES; i++) {
                        Assert.assertEquals(0, Unsafe.getUnsafe().getLong(addr + i * 8L));
                    }
                } finally {
                    FF.munmap(addr, fileSize, MemoryTag.MMAP_DEFAULT);
                }
            }
        }
    });
}
Also used : Path(io.questdb.std.str.Path) MemoryCMARW(io.questdb.cairo.vm.api.MemoryCMARW) Test(org.junit.Test)

Example 19 with Path

use of io.questdb.std.str.Path in project questdb by bluestreak01.

the class CairoMemoryTest method testAppendCannotOpenFile.

@Test
public void testAppendCannotOpenFile() {
    long used = Unsafe.getMemUsed();
    class X extends FilesFacadeImpl {

        @Override
        public long openRW(LPSZ name) {
            int n = name.length();
            if (n > 5 && Chars.equals(".fail", name, n - 5, n)) {
                return -1;
            }
            return super.openRW(name);
        }
    }
    X ff = new X();
    long openFileCount = ff.getOpenFileCount();
    int successCount = 0;
    int failCount = 0;
    try (Path path = new Path()) {
        path.of(temp.getRoot().getAbsolutePath());
        int prefixLen = path.length();
        try (MemoryMA mem = Vm.getMAInstance()) {
            Rnd rnd = new Rnd();
            for (int k = 0; k < 10; k++) {
                path.trimTo(prefixLen).concat(rnd.nextString(10));
                boolean fail = rnd.nextBoolean();
                if (fail) {
                    path.put(".fail").$();
                    failCount++;
                } else {
                    path.put(".data").$();
                    successCount++;
                }
                if (fail) {
                    try {
                        mem.of(ff, path, 2 * ff.getPageSize(), MemoryTag.MMAP_DEFAULT);
                        Assert.fail();
                    } catch (CairoException ignored) {
                    }
                } else {
                    mem.of(ff, path, 2 * ff.getPageSize(), MemoryTag.MMAP_DEFAULT);
                    for (int i = 0; i < N; i++) {
                        mem.putLong(i);
                    }
                    Assert.assertEquals(N * 8, mem.getAppendOffset());
                }
            }
        }
    }
    Assert.assertEquals(used, Unsafe.getMemUsed());
    Assert.assertEquals(openFileCount, ff.getOpenFileCount());
    Assert.assertTrue(failCount > 0);
    Assert.assertTrue(successCount > 0);
}
Also used : Path(io.questdb.std.str.Path) MemoryMA(io.questdb.cairo.vm.api.MemoryMA) LPSZ(io.questdb.std.str.LPSZ) Test(org.junit.Test)

Example 20 with Path

use of io.questdb.std.str.Path in project questdb by bluestreak01.

the class CairoMemoryTest method testWriteOverMapFailuresAndRead.

@Test
public void testWriteOverMapFailuresAndRead() throws Exception {
    Rnd rnd = new Rnd();
    class X extends FilesFacadeImpl {

        @Override
        public long getMapPageSize() {
            return super.getPageSize();
        }

        @Override
        public long mremap(long fd, long addr, long previousSize, long newSize, long offset, int mode, int memoryTag) {
            if (rnd.nextBoolean()) {
                return -1;
            }
            return super.mremap(fd, addr, previousSize, newSize, offset, mode, memoryTag);
        }
    }
    final X ff = new X();
    TestUtils.assertMemoryLeak(() -> {
        int writeFailureCount = 0;
        try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
            try (MemoryCMARW mem = Vm.getSmallCMARWInstance(ff, path, MemoryTag.MMAP_DEFAULT)) {
                int i = 0;
                while (i < N) {
                    try {
                        mem.putLong(i);
                        i++;
                    } catch (CairoException ignore) {
                        writeFailureCount++;
                        break;
                    }
                }
            }
        }
        Assert.assertTrue(writeFailureCount > 0);
    });
}
Also used : Path(io.questdb.std.str.Path) MemoryCMARW(io.questdb.cairo.vm.api.MemoryCMARW) Test(org.junit.Test)

Aggregations

Path (io.questdb.std.str.Path)141 Test (org.junit.Test)89 File (java.io.File)14 FilesFacade (io.questdb.std.FilesFacade)13 MemoryCMARW (io.questdb.cairo.vm.api.MemoryCMARW)10 MemoryMR (io.questdb.cairo.vm.api.MemoryMR)10 Rnd (io.questdb.std.Rnd)10 AbstractCairoTest (io.questdb.cairo.AbstractCairoTest)7 MemoryMA (io.questdb.cairo.vm.api.MemoryMA)7 MemoryMARW (io.questdb.cairo.vm.api.MemoryMARW)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)6 NativeLPSZ (io.questdb.std.str.NativeLPSZ)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 SOCountDownLatch (io.questdb.mp.SOCountDownLatch)5 LPSZ (io.questdb.std.str.LPSZ)5 RecordCursor (io.questdb.cairo.sql.RecordCursor)4 RowCursor (io.questdb.cairo.sql.RowCursor)4 MemoryARW (io.questdb.cairo.vm.api.MemoryARW)4 RingQueue (io.questdb.mp.RingQueue)4