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