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));
}
}
}
});
}
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());
}
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);
}
}
}
});
}
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);
}
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);
});
}
Aggregations