Search in sources :

Example 1 with TableWriter

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

the class TruncateTest method testTableBusy.

@Test
public void testTableBusy() throws Exception {
    assertMemoryLeak(() -> {
        createX();
        createY();
        assertQuery("count\n" + "10\n", "select count() from x", null, false, true);
        assertQuery("count\n" + "20\n", "select count() from y", null, false, true);
        CyclicBarrier useBarrier = new CyclicBarrier(2);
        CyclicBarrier releaseBarrier = new CyclicBarrier(2);
        CountDownLatch haltLatch = new CountDownLatch(1);
        new Thread(() -> {
            // lock table and wait until main thread uses it
            try (TableWriter ignore = engine.getWriter(AllowAllCairoSecurityContext.INSTANCE, "y", "testing")) {
                useBarrier.await();
                releaseBarrier.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
            haltLatch.countDown();
        }).start();
        useBarrier.await();
        try {
            Assert.assertNull(compiler.compile("truncate table x,y", sqlExecutionContext));
            Assert.fail();
        } catch (SqlException e) {
            Assert.assertEquals(17, e.getPosition());
            TestUtils.assertContains(e.getFlyweightMessage(), "table 'y' could not be truncated: [0]: table busy");
        }
        releaseBarrier.await();
        assertQuery("count\n" + "10\n", "select count() from x", null, false, true);
        assertQuery("count\n" + "20\n", "select count() from y", null, false, true);
        Assert.assertTrue(haltLatch.await(1, TimeUnit.SECONDS));
    });
}
Also used : TableWriter(io.questdb.cairo.TableWriter) CountDownLatch(java.util.concurrent.CountDownLatch) ReaderOutOfDateException(io.questdb.cairo.sql.ReaderOutOfDateException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 2 with TableWriter

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

the class TableRepairTest method testDeletePartitionInTheMiddle.

@Test
public void testDeletePartitionInTheMiddle() throws Exception {
    // this delete partition actually deletes files, simulating manual intervention
    assertMemoryLeak(() -> {
        compiler.compile("create table tst as (select * from (select rnd_int() a, rnd_double() b, timestamp_sequence(0, 10000000l) t from long_sequence(100000)) timestamp (t)) timestamp(t) partition by DAY", sqlExecutionContext);
        engine.releaseAllWriters();
        try (TableReader reader = new TableReader(configuration, "tst")) {
            Assert.assertEquals(100000, reader.size());
            try (Path path = new Path()) {
                path.of(configuration.getRoot()).concat("tst").concat("1970-01-09").$();
                Assert.assertEquals(0, Files.rmdir(path));
            }
            Assert.assertEquals(100000, reader.size());
            // repair by opening and closing writer
            new TableWriter(configuration, "tst").close();
            Assert.assertTrue(reader.reload());
            Assert.assertEquals(91360, reader.size());
        }
    });
}
Also used : Path(io.questdb.std.str.Path) TableWriter(io.questdb.cairo.TableWriter) TableReader(io.questdb.cairo.TableReader) Test(org.junit.Test)

Example 3 with TableWriter

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

the class TableRepairTest method testDeleteActivePartition.

@Test
public void testDeleteActivePartition() throws Exception {
    // this delete partition actually deletes files, simulating manual intervention
    assertMemoryLeak(() -> {
        compiler.compile("create table tst as (select * from (select rnd_int() a, rnd_double() b, timestamp_sequence(0, 10000000l) t from long_sequence(100000)) timestamp (t)) timestamp(t) partition by DAY", sqlExecutionContext);
        engine.releaseAllWriters();
        try (TableReader reader = new TableReader(configuration, "tst")) {
            Assert.assertEquals(100000, reader.size());
            // last and "active" partition is "1970-01-12"
            try (Path path = new Path()) {
                path.of(configuration.getRoot()).concat("tst").concat("1970-01-12").$();
                Assert.assertEquals(0, Files.rmdir(path));
            }
            Assert.assertEquals(100000, reader.size());
            try (TableWriter w = new TableWriter(configuration, "tst")) {
                Assert.assertTrue(reader.reload());
                Assert.assertEquals(95040, reader.size());
                Assert.assertEquals(950390000000L, w.getMaxTimestamp());
                TableWriter.Row row = w.newRow(w.getMaxTimestamp());
                row.putInt(0, 150);
                row.putDouble(1, 0.67);
                row.append();
                w.commit();
            }
            Assert.assertTrue(reader.reload());
            Assert.assertEquals(95041, reader.size());
        }
    });
}
Also used : Path(io.questdb.std.str.Path) TableWriter(io.questdb.cairo.TableWriter) TableReader(io.questdb.cairo.TableReader) Test(org.junit.Test)

Example 4 with TableWriter

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

the class TelemetryJob method appendConfigRow.

private void appendConfigRow(SqlCompiler compiler, TableWriter configWriter, Long256 id, boolean enabled) {
    TableWriter.Row row = configWriter.newRow();
    if (null == id) {
        final MicrosecondClock clock = compiler.getEngine().getConfiguration().getMicrosecondClock();
        final NanosecondClock nanosecondClock = compiler.getEngine().getConfiguration().getNanosecondClock();
        final long a = nanosecondClock.getTicks();
        final long b = clock.getTicks();
        row.putLong256(0, a, b, 0, 0);
        LOG.info().$("new instance [id=").$256(a, b, 0, 0).$(", enabled=").$(enabled).$(']').$();
    } else {
        row.putLong256(0, id);
    }
    row.putBool(1, enabled);
    row.putSym(2, configuration.getBuildInformation().getQuestDbVersion());
    row.putSym(3, System.getProperty(OS_NAME));
    String packageStr = System.getenv().get(QDB_PACKAGE);
    if (null != packageStr) {
        row.putSym(4, packageStr);
    }
    row.append();
    configWriter.commit();
}
Also used : MicrosecondClock(io.questdb.std.datetime.microtime.MicrosecondClock) TableWriter(io.questdb.cairo.TableWriter) NanosecondClock(io.questdb.std.NanosecondClock)

Example 5 with TableWriter

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

the class TelemetryJob method updateTelemetryConfig.

private TableWriter updateTelemetryConfig(SqlCompiler compiler, SqlExecutionContextImpl sqlExecutionContext, boolean enabled) throws SqlException {
    final TableWriter configWriter = compiler.getEngine().getWriter(AllowAllCairoSecurityContext.INSTANCE, configTableName, WRITER_LOCK_REASON);
    final CompiledQuery cc = compiler.compile(configTableName + " LIMIT -1", sqlExecutionContext);
    try (final RecordCursor cursor = cc.getRecordCursorFactory().getCursor(sqlExecutionContext)) {
        if (cursor.hasNext()) {
            final Record record = cursor.getRecord();
            final boolean _enabled = record.getBool(1);
            Long256 l256 = record.getLong256A(0);
            final CharSequence lastVersion = record.getSym(2);
            // we need to update the table to reflect that
            if (enabled != _enabled || !configuration.getBuildInformation().getQuestDbVersion().equals(lastVersion)) {
                appendConfigRow(compiler, configWriter, l256, enabled);
                LOG.info().$("instance config changes [id=").$256(l256.getLong0(), l256.getLong1(), 0, 0).$(", enabled=").$(enabled).$(']').$();
            } else {
                LOG.error().$("instance [id=").$256(l256.getLong0(), l256.getLong1(), 0, 0).$(", enabled=").$(enabled).$(']').$();
            }
        } else {
            // if there are no record for telemetry id we need to create one using clocks
            appendConfigRow(compiler, configWriter, null, enabled);
        }
    }
    return configWriter;
}
Also used : TableWriter(io.questdb.cairo.TableWriter) RecordCursor(io.questdb.cairo.sql.RecordCursor) Long256(io.questdb.std.Long256) Record(io.questdb.cairo.sql.Record)

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