Search in sources :

Example 1 with Log

use of io.questdb.log.Log in project questdb by bluestreak01.

the class EmbeddedApiTest method testConcurrentSQLExec.

@Test
public void testConcurrentSQLExec() throws Exception {
    final CairoConfiguration configuration = new DefaultCairoConfiguration(temp.getRoot().getAbsolutePath());
    final Log log = LogFactory.getLog("testConcurrentSQLExec");
    TestUtils.assertMemoryLeak(() -> {
        WorkerPool workerPool = new WorkerPool(new WorkerPoolConfiguration() {

            @Override
            public int[] getWorkerAffinity() {
                return new int[] { -1, -1 };
            }

            @Override
            public int getWorkerCount() {
                return 2;
            }

            @Override
            public boolean haltOnError() {
                return false;
            }
        });
        Rnd rnd = new Rnd();
        try (final CairoEngine engine = new CairoEngine(configuration)) {
            workerPool.assign(new GroupByJob(engine.getMessageBus()));
            workerPool.start(log);
            try {
                // number of cores is current thread + workers in the pool
                final SqlExecutionContextImpl ctx = new SqlExecutionContextImpl(engine, 2);
                try (SqlCompiler compiler = new SqlCompiler(engine)) {
                    compiler.compile("create table abc (g double, ts timestamp) timestamp(ts) partition by DAY", ctx);
                    long timestamp = 0;
                    try (TableWriter writer = engine.getWriter(ctx.getCairoSecurityContext(), "abc", "testing")) {
                        for (int i = 0; i < 10_000_000; i++) {
                            TableWriter.Row row = writer.newRow(timestamp);
                            row.putDouble(0, rnd.nextDouble());
                            row.append();
                            timestamp += 1_000_000;
                        }
                        writer.commit();
                    }
                    try (RecordCursorFactory factory = compiler.compile("select sum(g) from abc", ctx).getRecordCursorFactory()) {
                        try (RecordCursor cursor = factory.getCursor(ctx)) {
                            final Record ignored = cursor.getRecord();
                            // noinspection StatementWithEmptyBody
                            while (cursor.hasNext()) {
                            // access 'record' instance for field values
                            }
                        }
                    }
                }
            } finally {
                workerPool.halt();
            }
        }
    });
}
Also used : SqlCompiler(io.questdb.griffin.SqlCompiler) RecordCursor(io.questdb.cairo.sql.RecordCursor) Log(io.questdb.log.Log) Rnd(io.questdb.std.Rnd) GroupByJob(io.questdb.griffin.engine.groupby.vect.GroupByJob) SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) WorkerPool(io.questdb.mp.WorkerPool) TableWriter(io.questdb.cairo.TableWriter) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) DefaultCairoConfiguration(io.questdb.cairo.DefaultCairoConfiguration) Record(io.questdb.cairo.sql.Record) CairoEngine(io.questdb.cairo.CairoEngine) DefaultCairoConfiguration(io.questdb.cairo.DefaultCairoConfiguration) CairoConfiguration(io.questdb.cairo.CairoConfiguration) WorkerPoolConfiguration(io.questdb.mp.WorkerPoolConfiguration) Test(org.junit.Test)

Aggregations

CairoConfiguration (io.questdb.cairo.CairoConfiguration)1 CairoEngine (io.questdb.cairo.CairoEngine)1 DefaultCairoConfiguration (io.questdb.cairo.DefaultCairoConfiguration)1 TableWriter (io.questdb.cairo.TableWriter)1 Record (io.questdb.cairo.sql.Record)1 RecordCursor (io.questdb.cairo.sql.RecordCursor)1 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)1 SqlCompiler (io.questdb.griffin.SqlCompiler)1 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)1 GroupByJob (io.questdb.griffin.engine.groupby.vect.GroupByJob)1 Log (io.questdb.log.Log)1 WorkerPool (io.questdb.mp.WorkerPool)1 WorkerPoolConfiguration (io.questdb.mp.WorkerPoolConfiguration)1 Rnd (io.questdb.std.Rnd)1 Test (org.junit.Test)1