Search in sources :

Example 51 with TableWriter

use of io.questdb.cairo.TableWriter in project questdb by bluestreak01.

the class AlterTableAlterSymbolColumnCacheFlagTest method testAlterSymbolCacheFlagToFalseAndCheckOpenReaderWithCursor.

@Test
public void testAlterSymbolCacheFlagToFalseAndCheckOpenReaderWithCursor() throws Exception {
    String expectedOrdered = "sym\n" + "googl\n" + "googl\n" + "googl\n" + "googl\n" + "googl\n" + "googl\n" + "ibm\n" + "ibm\n" + "msft\n" + "msft\n";
    String expectedChronological = "sym\n" + "msft\n" + "googl\n" + "googl\n" + "ibm\n" + "googl\n" + "ibm\n" + "googl\n" + "googl\n" + "googl\n" + "msft\n";
    final RecordCursorPrinter printer = new SingleColumnRecordCursorPrinter(1);
    assertMemoryLeak(() -> {
        assertMemoryLeak(this::createX);
        assertQueryPlain(expectedOrdered, "select sym from x order by sym");
        try (TableReader reader = engine.getReader(sqlExecutionContext.getCairoSecurityContext(), "x")) {
            // check cursor before altering symbol column
            sink.clear();
            printer.print(reader.getCursor(), reader.getMetadata(), true, sink);
            Assert.assertEquals(expectedChronological, sink.toString());
            try (TableWriter writer = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "x", "testing")) {
                writer.changeCacheFlag(1, false);
            }
            // reload reader
            Assert.assertTrue(reader.reload());
            // check cursor after reload
            sink.clear();
            printer.print(reader.getCursor(), reader.getMetadata(), true, sink);
            Assert.assertEquals(expectedChronological, sink.toString());
            try (TableReader reader2 = engine.getReader(sqlExecutionContext.getCairoSecurityContext(), "x")) {
                sink.clear();
                printer.print(reader2.getCursor(), reader2.getMetadata(), true, sink);
                Assert.assertEquals(expectedChronological, sink.toString());
            }
        }
    });
    assertQueryPlain(expectedOrdered, "select sym from x order by 1 asc");
}
Also used : RecordCursorPrinter(io.questdb.cairo.RecordCursorPrinter) TableWriter(io.questdb.cairo.TableWriter) TableReader(io.questdb.cairo.TableReader) Test(org.junit.Test)

Example 52 with TableWriter

use of io.questdb.cairo.TableWriter in project invesdwin-context-persistence by subes.

the class QuestDBPerformanceTest method testQuestDbPerformance.

@Test
public void testQuestDbPerformance() throws InterruptedException, SqlException, IOException {
    final File directory = new File(ContextProperties.getCacheDirectory(), QuestDBPerformanceTest.class.getSimpleName());
    Files.deleteNative(directory);
    Files.forceMkdir(directory);
    final CairoConfiguration configuration = new DefaultCairoConfiguration(directory.getAbsolutePath());
    final Instant writesStart = new Instant();
    int i = 0;
    final CairoEngine engine = new CairoEngine(configuration);
    final SqlExecutionContextImpl ctx = new SqlExecutionContextImpl(engine, 1);
    final LoopInterruptedCheck loopCheck = new LoopInterruptedCheck(Duration.ONE_SECOND);
    try (SqlCompiler compiler = new SqlCompiler(engine)) {
        compiler.compile("create table abc (value long, key timestamp) timestamp(key)", ctx);
        try (TableWriter writer = engine.getWriter(ctx.getCairoSecurityContext(), "abc", "insert")) {
            for (final FDate date : newValues()) {
                final TableWriter.Row row = writer.newRow(date.millisValue());
                row.putLong(0, date.millisValue());
                row.append();
                i++;
                if (i % FLUSH_INTERVAL == 0) {
                    if (loopCheck.check()) {
                        printProgress("Writes", writesStart, i, VALUES);
                    }
                    writer.commit();
                }
            }
            writer.commit();
        }
        printProgress("WritesFinished", writesStart, VALUES, VALUES);
    }
    readIterator(engine);
    readGet(engine);
    readGetLatest(engine);
    try (SqlCompiler compiler = new SqlCompiler(engine)) {
        compiler.compile("dtop table 'abc';", ctx);
    }
    engine.close();
    Files.deleteNative(directory);
}
Also used : SqlCompiler(io.questdb.griffin.SqlCompiler) Instant(de.invesdwin.util.time.Instant) LoopInterruptedCheck(de.invesdwin.util.concurrent.loop.LoopInterruptedCheck) FDate(de.invesdwin.util.time.date.FDate) SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) TableWriter(io.questdb.cairo.TableWriter) DefaultCairoConfiguration(io.questdb.cairo.DefaultCairoConfiguration) CairoEngine(io.questdb.cairo.CairoEngine) File(java.io.File) DefaultCairoConfiguration(io.questdb.cairo.DefaultCairoConfiguration) CairoConfiguration(io.questdb.cairo.CairoConfiguration) Test(org.junit.jupiter.api.Test)

Example 53 with TableWriter

use of io.questdb.cairo.TableWriter in project questdb by bluestreak01.

the class PGJobContextTest method testCairoException.

@Test
public void testCairoException() throws Exception {
    assertMemoryLeak(() -> {
        try (final PGWireServer ignored = createPGServer(2);
            final Connection connection = getConnection(false, true)) {
            connection.prepareStatement("create table xyz(a int)").execute();
            try (TableWriter ignored1 = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "xyz", "testing")) {
                connection.prepareStatement("drop table xyz").execute();
                Assert.fail();
            } catch (SQLException e) {
                TestUtils.assertContains(e.getMessage(), "Could not lock 'xyz'");
                Assert.assertEquals("00000", e.getSQLState());
            }
        }
    });
}
Also used : TableWriter(io.questdb.cairo.TableWriter) PSQLException(org.postgresql.util.PSQLException) BaseConnection(org.postgresql.core.BaseConnection) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 54 with TableWriter

use of io.questdb.cairo.TableWriter in project questdb by bluestreak01.

the class RetryIODispatcherTest method assertInsertWaitsWhenWriterLocked.

private void assertInsertWaitsWhenWriterLocked() throws Exception {
    final int parallelCount = 2;
    new HttpQueryTestBuilder().withTempFolder(temp).withWorkerCount(parallelCount).withHttpServerConfigBuilder(new HttpServerConfigurationBuilder()).withTelemetry(false).run(engine -> {
        // create table
        new SendAndReceiveRequestBuilder().executeWithStandardHeaders("GET /query?query=%0A%0A%0Acreate+table+balances_x+(%0A%09cust_id+int%2C+%0A%09balance_ccy+symbol%2C+%0A%09balance+double%2C+%0A%09status+byte%2C+%0A%09timestamp+timestamp%0A)&limit=0%2C1000&count=true HTTP/1.1\r\n", IODispatcherTest.JSON_DDL_RESPONSE);
        TableWriter writer = lockWriter(engine, "balances_x");
        final int insertCount = 10;
        CountDownLatch countDownLatch = new CountDownLatch(parallelCount);
        for (int i = 0; i < parallelCount; i++) {
            new Thread(() -> {
                try {
                    for (int r = 0; r < insertCount; r++) {
                        // insert one record
                        try {
                            new SendAndReceiveRequestBuilder().executeWithStandardHeaders("GET /query?query=%0A%0Ainsert+into+balances_x+(cust_id%2C+balance_ccy%2C+balance%2C+timestamp)+values+(1%2C+%27USD%27%2C+1500.00%2C+6000000001)&limit=0%2C1000&count=true HTTP/1.1\r\n", IODispatcherTest.JSON_DDL_RESPONSE);
                        } catch (Exception e) {
                            LOG.error().$("Failed execute insert http request. Server error ").$(e).$();
                        }
                    }
                } finally {
                    countDownLatch.countDown();
                }
            }).start();
        }
        boolean finished = countDownLatch.await(200, TimeUnit.MILLISECONDS);
        // Cairo engine should not allow second writer to be opened on the same table
        // Cairo is expected to have finished == false
        Assert.assertFalse(finished);
        writer.close();
        countDownLatch.await();
        // check if we have parallelCount x insertCount  records
        new SendAndReceiveRequestBuilder().executeWithStandardHeaders("GET /query?query=select+count(*)+from+balances_x&count=true HTTP/1.1\r\n", "71\r\n" + "{\"query\":\"select count(*) from balances_x\",\"columns\":[{\"name\":\"count\",\"type\":\"LONG\"}],\"dataset\":[[" + parallelCount * insertCount + "]],\"count\":1}\r\n" + "00\r\n" + "\r\n");
    });
}
Also used : TableWriter(io.questdb.cairo.TableWriter) CountDownLatch(java.util.concurrent.CountDownLatch) ServerDisconnectException(io.questdb.network.ServerDisconnectException) EntryUnavailableException(io.questdb.cairo.EntryUnavailableException)

Example 55 with TableWriter

use of io.questdb.cairo.TableWriter in project questdb by bluestreak01.

the class RetryIODispatcherTest method assertInsertWaitsExceedsRerunProcessingQueueSize.

private void assertInsertWaitsExceedsRerunProcessingQueueSize() throws Exception {
    final int rerunProcessingQueueSize = 1;
    final int parallelCount = 4;
    new HttpQueryTestBuilder().withTempFolder(temp).withWorkerCount(parallelCount).withHttpServerConfigBuilder(new HttpServerConfigurationBuilder().withRerunProcessingQueueSize(rerunProcessingQueueSize)).withTelemetry(false).run(engine -> {
        // create table
        new SendAndReceiveRequestBuilder().executeWithStandardHeaders("GET /query?query=%0A%0A%0Acreate+table+balances_x+(%0A%09cust_id+int%2C+%0A%09balance_ccy+symbol%2C+%0A%09balance+double%2C+%0A%09status+byte%2C+%0A%09timestamp+timestamp%0A)&limit=0%2C1000&count=true HTTP/1.1\r\n", IODispatcherTest.JSON_DDL_RESPONSE);
        TableWriter writer = lockWriter(engine, "balances_x");
        final int insertCount = rerunProcessingQueueSize * 10;
        CountDownLatch countDownLatch = new CountDownLatch(parallelCount);
        AtomicInteger fails = new AtomicInteger();
        for (int i = 0; i < parallelCount; i++) {
            new Thread(() -> {
                try {
                    for (int r = 0; r < insertCount; r++) {
                        // insert one record
                        try {
                            new SendAndReceiveRequestBuilder().executeWithStandardHeaders("GET /query?query=%0A%0Ainsert+into+balances_x+(cust_id%2C+balance_ccy%2C+balance%2C+timestamp)+values+(1%2C+%27USD%27%2C+1500.00%2C+6000000001)&limit=0%2C1000&count=true HTTP/1.1\r\n", IODispatcherTest.JSON_DDL_RESPONSE);
                        } catch (AssertionError ase) {
                            fails.incrementAndGet();
                        } catch (Exception e) {
                            LOG.error().$("Failed execute insert http request. Server error ").$(e);
                        }
                    }
                } finally {
                    countDownLatch.countDown();
                }
            }).start();
        }
        boolean finished = countDownLatch.await(200, TimeUnit.MILLISECONDS);
        Assert.assertFalse(finished);
        writer.close();
        if (!countDownLatch.await(5000, TimeUnit.MILLISECONDS)) {
            Assert.fail("Wait to process retries exceeded timeout");
        }
        // check if we have parallelCount x insertCount  records
        new SendAndReceiveRequestBuilder().executeWithStandardHeaders("GET /query?query=select+count(*)+from+balances_x&count=true HTTP/1.1\r\n", "71\r\n" + "{\"query\":\"select count(*) from balances_x\",\"columns\":[{\"name\":\"count\",\"type\":\"LONG\"}],\"dataset\":[[" + (parallelCount * insertCount - fails.get()) + "]],\"count\":1}\r\n" + "00\r\n" + "\r\n");
    });
}
Also used : TableWriter(io.questdb.cairo.TableWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) ServerDisconnectException(io.questdb.network.ServerDisconnectException) EntryUnavailableException(io.questdb.cairo.EntryUnavailableException)

Aggregations

TableWriter (io.questdb.cairo.TableWriter)103 Test (org.junit.Test)88 RecordCursor (io.questdb.cairo.sql.RecordCursor)71 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)71 Record (io.questdb.cairo.sql.Record)70 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)68 Rnd (io.questdb.std.Rnd)23 TableReader (io.questdb.cairo.TableReader)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 EntryUnavailableException (io.questdb.cairo.EntryUnavailableException)7 ServerDisconnectException (io.questdb.network.ServerDisconnectException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 CairoConfiguration (io.questdb.cairo.CairoConfiguration)3 CairoEngine (io.questdb.cairo.CairoEngine)3 DefaultCairoConfiguration (io.questdb.cairo.DefaultCairoConfiguration)3 SqlCompiler (io.questdb.griffin.SqlCompiler)3 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)3 RecordCursorPrinter (io.questdb.cairo.RecordCursorPrinter)2 Path (io.questdb.std.str.Path)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2