Search in sources :

Example 1 with LPSZ

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

the class TableReaderTest method testUnsuccessfulRemoveAndReloadSym.

@Test
public void testUnsuccessfulRemoveAndReloadSym() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        // create table with two string columns
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.SYMBOL).col("b", ColumnType.SYMBOL)) {
            CairoTestUtils.create(model);
        }
        Rnd rnd = new Rnd();
        final int N = 1000;
        // make sure we forbid deleting column "b" files
        TestFilesFacade ff = new TestFilesFacade() {

            int counter = 5;

            @Override
            public boolean remove(LPSZ name) {
                if (counter > 0 && ((Chars.endsWith(name, "b.i") || Chars.endsWith(name, "b.d") || Chars.endsWith(name, "b.o") || Chars.endsWith(name, "b.k") || Chars.endsWith(name, "b.c") || Chars.endsWith(name, "b.v")))) {
                    counter--;
                    return false;
                }
                return super.remove(name);
            }

            @Override
            public boolean wasCalled() {
                return counter < 1;
            }
        };
        CairoConfiguration configuration = new DefaultCairoConfiguration(root) {

            @Override
            public FilesFacade getFilesFacade() {
                return ff;
            }
        };
        // populate table and delete column
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            appendTwoSymbols(writer, rnd);
            writer.commit();
            try (TableReader reader = new TableReader(configuration, "x")) {
                long counter = 0;
                rnd.reset();
                RecordCursor cursor = reader.getCursor();
                final Record record = cursor.getRecord();
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getSym(0));
                    Assert.assertEquals(rnd.nextChars(15), record.getSym(1));
                    counter++;
                }
                Assert.assertEquals(N, counter);
                // this should write metadata without column "b" but will ignore
                // file delete failures
                writer.removeColumn("b");
                if (configuration.getFilesFacade().isRestrictedFileSystem()) {
                    reader.closeColumnForRemove("b");
                }
                // reader.reload();
                // now when we add new column by same name it must not pick up files we failed to delete previously
                writer.addColumn("b", ColumnType.SYMBOL);
                // SymbolMap must be cleared when we try to do add values to new column
                appendTwoSymbols(writer, rnd);
                writer.commit();
                // now assert what reader sees
                Assert.assertTrue(reader.reload());
                Assert.assertEquals(N * 2, reader.size());
                rnd.reset();
                cursor.toTop();
                counter = 0;
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getSym(0));
                    if (counter < N) {
                        // roll random generator to make sure it returns same values
                        rnd.nextChars(15);
                        Assert.assertNull(record.getSym(1));
                    } else {
                        Assert.assertEquals(rnd.nextChars(15), record.getSym(1));
                    }
                    counter++;
                }
                Assert.assertEquals(N * 2, counter);
            }
        }
        Assert.assertTrue(ff.wasCalled());
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) LPSZ(io.questdb.std.str.LPSZ) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 2 with LPSZ

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

the class TableReaderTest method testUnsuccessfulFileRemoveAndReloadStr.

@Test
public void testUnsuccessfulFileRemoveAndReloadStr() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        // create table with two string columns
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.SYMBOL).col("b", ColumnType.STRING)) {
            CairoTestUtils.create(model);
        }
        Rnd rnd = new Rnd();
        final int N = 1000;
        // make sure we forbid deleting column "b" files
        TestFilesFacade ff = new TestFilesFacade() {

            int counter = 2;

            @Override
            public boolean remove(LPSZ name) {
                if (counter > 0 && ((Chars.endsWith(name, "b.i") || Chars.endsWith(name, "b.d") || Chars.endsWith(name, "b.o") || Chars.endsWith(name, "b.k") || Chars.endsWith(name, "b.c") || Chars.endsWith(name, "b.v")))) {
                    counter--;
                    return false;
                }
                return super.remove(name);
            }

            @Override
            public boolean wasCalled() {
                return counter < 1;
            }
        };
        CairoConfiguration configuration = new DefaultCairoConfiguration(root) {

            @Override
            public FilesFacade getFilesFacade() {
                return ff;
            }
        };
        // populate table and delete column
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            for (int i = 0; i < N; i++) {
                TableWriter.Row row = writer.newRow();
                row.putSym(0, rnd.nextChars(10));
                row.putStr(1, rnd.nextChars(15));
                row.append();
            }
            writer.commit();
            try (TableReader reader = new TableReader(configuration, "x")) {
                long counter = 0;
                rnd.reset();
                RecordCursor cursor = reader.getCursor();
                final Record record = cursor.getRecord();
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getSym(0));
                    Assert.assertEquals(rnd.nextChars(15), record.getStr(1));
                    counter++;
                }
                Assert.assertEquals(N, counter);
                // this should write metadata without column "b" but will ignore
                // file delete failures
                writer.removeColumn("b");
                if (configuration.getFilesFacade().isRestrictedFileSystem()) {
                    reader.closeColumnForRemove("b");
                }
                // now when we add new column by same name it must not pick up files we failed to delete previously
                writer.addColumn("b", ColumnType.STRING);
                for (int i = 0; i < N; i++) {
                    TableWriter.Row row = writer.newRow();
                    row.putSym(0, rnd.nextChars(10));
                    row.putStr(1, rnd.nextChars(15));
                    row.append();
                }
                writer.commit();
                // now assert what reader sees
                Assert.assertTrue(reader.reload());
                Assert.assertEquals(N * 2, reader.size());
                rnd.reset();
                cursor.toTop();
                counter = 0;
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getSym(0));
                    if (counter < N) {
                        // roll random generator to make sure it returns same values
                        rnd.nextChars(15);
                        Assert.assertNull(record.getStr(1));
                    } else {
                        Assert.assertEquals(rnd.nextChars(15), record.getStr(1));
                    }
                    counter++;
                }
                Assert.assertEquals(N * 2, counter);
            }
        }
        Assert.assertTrue(ff.wasCalled());
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) LPSZ(io.questdb.std.str.LPSZ) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 3 with LPSZ

use of io.questdb.std.str.LPSZ 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 4 with LPSZ

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

the class SqlParserTest method testTableNameCannotOpen.

@Test
public void testTableNameCannotOpen() throws Exception {
    final FilesFacade ff = new FilesFacadeImpl() {

        @Override
        public long openRO(LPSZ name) {
            if (Chars.endsWith(name, TableUtils.META_FILE_NAME)) {
                return -1;
            }
            return super.openRO(name);
        }
    };
    CairoConfiguration configuration = new DefaultCairoConfiguration(root) {

        @Override
        public FilesFacade getFilesFacade() {
            return ff;
        }
    };
    assertMemoryLeak(() -> {
        try (CairoEngine engine = new CairoEngine(configuration);
            SqlCompiler compiler = new SqlCompiler(engine)) {
            TableModel[] tableModels = new TableModel[] { modelOf("tab").col("x", ColumnType.INT) };
            try {
                try {
                    for (int i = 0, n = tableModels.length; i < n; i++) {
                        CairoTestUtils.create(tableModels[i]);
                    }
                    compiler.compile("select * from tab", sqlExecutionContext);
                    Assert.fail("Exception expected");
                } catch (SqlException e) {
                    Assert.assertEquals(14, e.getPosition());
                    TestUtils.assertContains(e.getFlyweightMessage(), "could not open");
                }
            } finally {
                for (int i = 0, n = tableModels.length; i < n; i++) {
                    TableModel tableModel = tableModels[i];
                    Path path = tableModel.getPath().of(tableModel.getConfiguration().getRoot()).concat(tableModel.getName()).slash$();
                    Assert.assertEquals(0, configuration.getFilesFacade().rmdir(path));
                    tableModel.close();
                }
            }
        }
    });
}
Also used : Path(io.questdb.std.str.Path) FilesFacade(io.questdb.std.FilesFacade) LPSZ(io.questdb.std.str.LPSZ) FilesFacadeImpl(io.questdb.std.FilesFacadeImpl) Test(org.junit.Test)

Example 5 with LPSZ

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

the class O3FailureTest method testPartitionedOOPrefixesExistingPartitionsCreateDirs.

@Test
public void testPartitionedOOPrefixesExistingPartitionsCreateDirs() throws Exception {
    counter.set(2);
    executeWithoutPool(O3FailureTest::testPartitionedOOPrefixesExistingPartitionsFailRetry0, new FilesFacadeImpl() {

        @Override
        public int mkdirs(LPSZ path, int mode) {
            if (Chars.contains(path, "1970-01-01") && counter.decrementAndGet() == 0) {
                return -1;
            }
            return super.mkdirs(path, mode);
        }
    });
}
Also used : LPSZ(io.questdb.std.str.LPSZ) FilesFacadeImpl(io.questdb.std.FilesFacadeImpl) Test(org.junit.Test)

Aggregations

LPSZ (io.questdb.std.str.LPSZ)50 Test (org.junit.Test)49 FilesFacadeImpl (io.questdb.std.FilesFacadeImpl)35 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 RecordCursor (io.questdb.cairo.sql.RecordCursor)8 Record (io.questdb.cairo.sql.Record)7 Path (io.questdb.std.str.Path)5 FilesFacade (io.questdb.std.FilesFacade)3 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)1 MemoryCMARW (io.questdb.cairo.vm.api.MemoryCMARW)1 MemoryMA (io.questdb.cairo.vm.api.MemoryMA)1 TestMicroClock (io.questdb.test.tools.TestMicroClock)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1