Search in sources :

Example 41 with JournalWriter

use of com.questdb.store.JournalWriter in project questdb by bluestreak01.

the class MultiColumnJoinTest method setUp.

@Before
public void setUp() throws Exception {
    getFactory().getConfiguration().exists("");
    try (JournalWriter a = getFactory().writer(new JournalStructure("a").$int("x").$str("y").$double("amount").$())) {
        try (JournalWriter b = getFactory().writer(new JournalStructure("b").$int("x").$str("y").$str("name").$())) {
            Rnd rnd = new Rnd();
            for (int i = 0; i < 10; i++) {
                int x = rnd.nextInt();
                String y = rnd.nextString(rnd.nextPositiveInt() % 15);
                JournalEntryWriter ewa = a.entryWriter();
                JournalEntryWriter ewb = b.entryWriter();
                ewa.putInt(0, x);
                ewa.putStr(1, y);
                ewa.putDouble(2, rnd.nextDouble());
                ewa.append();
                ewb.putInt(0, x);
                ewb.putStr(1, y);
                ewb.putStr(2, rnd.nextChars(rnd.nextPositiveInt() % 20));
                ewb.append();
            }
        }
    // a.commit();
    // b.commit();
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Rnd(com.questdb.std.Rnd) JournalEntryWriter(com.questdb.store.JournalEntryWriter) Before(org.junit.Before)

Example 42 with JournalWriter

use of com.questdb.store.JournalWriter in project questdb by bluestreak01.

the class RecordKeyCopierCompilerTest method testCompiler.

@Test
public void testCompiler() throws Exception {
    try (JournalWriter w = compiler.createWriter(FACTORY_CONTAINER.getFactory(), "create table x (a INT, b BYTE, c SHORT, d LONG, e FLOAT, f DOUBLE, g DATE, h BINARY, t DATE, x SYMBOL, z STRING, y BOOLEAN) timestamp(t) partition by MONTH record hint 100")) {
        JournalEntryWriter ew = w.entryWriter();
        IntList keyColumns = new IntList();
        ew.putInt(0, 12345);
        keyColumns.add(0);
        ew.put(1, (byte) 128);
        keyColumns.add(1);
        ew.putShort(2, (short) 6500);
        keyColumns.add(2);
        ew.putLong(3, 123456789);
        keyColumns.add(3);
        ew.putFloat(4, 0.345f);
        keyColumns.add(4);
        ew.putDouble(5, 0.123456789);
        keyColumns.add(5);
        ew.putDate(6, 10000000000L);
        keyColumns.add(6);
        ew.putSym(9, "xyz");
        keyColumns.add(9);
        ew.putStr(10, "abc");
        keyColumns.add(10);
        ew.putBool(11, true);
        keyColumns.add(11);
        ew.append();
        w.commit();
        try (RecordSource src = compileSource("x")) {
            RecordKeyCopierCompiler cc = new RecordKeyCopierCompiler(new BytecodeAssembler());
            RecordKeyCopier copier = cc.compile(src.getMetadata(), keyColumns);
            IntList valueTypes = new IntList();
            valueTypes.add(ColumnType.DOUBLE);
            MetadataTypeResolver metadataTypeResolver = new MetadataTypeResolver();
            TypeListResolver typeListResolver = new TypeListResolver();
            try (DirectMap map = new DirectMap(1024, metadataTypeResolver.of(src.getMetadata(), keyColumns), typeListResolver.of(valueTypes))) {
                RecordCursor cursor = src.prepareCursor(FACTORY_CONTAINER.getFactory());
                try {
                    while (cursor.hasNext()) {
                        Record r = cursor.next();
                        DirectMap.KeyWriter kw = map.keyWriter();
                        copier.copy(r, kw);
                        DirectMapValues val = map.getOrCreateValues();
                        val.putDouble(0, 5000.01);
                    }
                    cursor.toTop();
                    while (cursor.hasNext()) {
                        Record r = cursor.next();
                        DirectMap.KeyWriter kw = map.keyWriter();
                        copier.copy(r, kw);
                        Assert.assertEquals(map.getValues().getDouble(0), 5000.01, 0.00000001);
                    }
                } finally {
                    cursor.releaseCursor();
                }
            }
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) RecordCursor(com.questdb.common.RecordCursor) BytecodeAssembler(com.questdb.std.BytecodeAssembler) IntList(com.questdb.std.IntList) RecordSource(com.questdb.ql.RecordSource) Record(com.questdb.common.Record) JournalEntryWriter(com.questdb.store.JournalEntryWriter) Test(org.junit.Test) AbstractOptimiserTest(com.questdb.parser.sql.AbstractOptimiserTest)

Example 43 with JournalWriter

use of com.questdb.store.JournalWriter in project questdb by bluestreak01.

the class CachingWriterFactoryTest method testLockUnlock.

@Test
public void testLockUnlock() throws Exception {
    final JournalMetadata<?> x = new JournalStructure("x").$date("ts").$().build();
    final JournalMetadata<?> y = new JournalStructure("y").$date("ts").$().build();
    JournalWriter wx = wf.writer(x);
    Assert.assertNotNull(wx);
    Assert.assertTrue(wx.isOpen());
    JournalWriter wy = wf.writer(y);
    Assert.assertNotNull(wy);
    Assert.assertTrue(wy.isOpen());
    try {
        // check that lock is successful
        wf.lock(x.getName());
        // check that writer x is closed and writer y is open (lock must not spill out to other writers)
        Assert.assertFalse(wx.isOpen());
        Assert.assertTrue(wy.isOpen());
        // check that when name is locked writers are not created
        try {
            wf.writer(x);
        } catch (JournalLockedException ignored) {
        }
        final CountDownLatch done = new CountDownLatch(1);
        final AtomicBoolean result = new AtomicBoolean();
        // have new thread try to allocated this writers
        new Thread(() -> {
            try (JournalWriter ignored = wf.writer(x)) {
                result.set(false);
            } catch (WriterBusyException ignored) {
                result.set(true);
            } catch (JournalException e) {
                e.printStackTrace();
                result.set(false);
            }
            done.countDown();
        }).start();
        Assert.assertTrue(done.await(1, TimeUnit.SECONDS));
        Assert.assertTrue(result.get());
        wf.unlock(x.getName());
        wx = wf.writer(x);
        Assert.assertNotNull(wx);
        Assert.assertTrue(wx.isOpen());
        try {
            // unlocking writer that has not been locked must produce exception
            // and not affect open writer
            wf.unlock(wx.getName());
            Assert.fail();
        } catch (IllegalStateException ignored) {
        }
        Assert.assertTrue(wx.isOpen());
    } finally {
        wx.close();
        wy.close();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JournalWriter(com.questdb.store.JournalWriter) JournalLockedException(com.questdb.ex.JournalLockedException) JournalException(com.questdb.std.ex.JournalException) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) WriterBusyException(com.questdb.ex.WriterBusyException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 44 with JournalWriter

use of com.questdb.store.JournalWriter in project questdb by bluestreak01.

the class ClusterControllerTest method testStaggeredStartup.

@Test
public void testStaggeredStartup() throws Exception {
    final CountDownLatch active1Latch = new CountDownLatch(1);
    final CountDownLatch active2Latch = new CountDownLatch(1);
    final CountDownLatch standby1Latch = new CountDownLatch(1);
    final CountDownLatch standby2Latch = new CountDownLatch(1);
    final CountDownLatch shutdown1 = new CountDownLatch(1);
    final CountDownLatch shutdown2 = new CountDownLatch(1);
    try (JournalWriter writer1 = getFactory().writer(Quote.class)) {
        try (JournalWriter writer2 = tf.getFactory().writer(Quote.class)) {
            ClusterController controller1 = createControllerX(writer1, 0, getFactory(), active1Latch, standby1Latch, shutdown1);
            controller1.start();
            Assert.assertTrue(active1Latch.await(5, TimeUnit.SECONDS));
            Assert.assertEquals("Node 1 is expected to be active", 0, active1Latch.getCount());
            standby1Latch.await(200, TimeUnit.MILLISECONDS);
            Assert.assertEquals("Node 1 standby callback is not expected to be called", 1, standby1Latch.getCount());
            ClusterController controller2 = createControllerX(writer2, 1, tf.getFactory(), active2Latch, standby2Latch, shutdown2);
            controller2.start();
            standby2Latch.await(5, TimeUnit.SECONDS);
            Assert.assertEquals("Node 2 is expected to be standing by", 0, standby2Latch.getCount());
            active2Latch.await(200, TimeUnit.MILLISECONDS);
            Assert.assertEquals("Node 2 active() callback is not expected to be called", 1, active2Latch.getCount());
            controller2.halt();
            shutdown2.await(5, TimeUnit.SECONDS);
            Assert.assertEquals(0, shutdown2.getCount());
            controller1.halt();
            shutdown1.await(5, TimeUnit.SECONDS);
            Assert.assertEquals(0, shutdown1.getCount());
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractTest(com.questdb.test.tools.AbstractTest)

Example 45 with JournalWriter

use of com.questdb.store.JournalWriter in project questdb by bluestreak01.

the class ClusterControllerTest method testTiebreakFailOver.

@Test
public void testTiebreakFailOver() throws Exception {
    final CountDownLatch active1Latch = new CountDownLatch(1);
    final CountDownLatch active2Latch = new CountDownLatch(1);
    final CountDownLatch standby1Latch = new CountDownLatch(1);
    final CountDownLatch standby2Latch = new CountDownLatch(1);
    final CountDownLatch shutdown1 = new CountDownLatch(1);
    final CountDownLatch shutdown2 = new CountDownLatch(1);
    try (JournalWriter writer1 = getFactory().writer(Quote.class)) {
        try (JournalWriter writer2 = tf.getFactory().writer(Quote.class)) {
            ClusterController controller1 = createControllerX(writer1, 0, getFactory(), active1Latch, standby1Latch, shutdown1);
            ClusterController controller2 = createControllerX(writer2, 1, tf.getFactory(), active2Latch, standby2Latch, shutdown2);
            // start two controller without pause
            controller2.start();
            controller1.start();
            getFactory().close();
            long t = System.currentTimeMillis();
            do {
                active1Latch.await(1, TimeUnit.MICROSECONDS);
                active2Latch.await(1, TimeUnit.MICROSECONDS);
            } while (active1Latch.getCount() > 0 && active2Latch.getCount() > 0 && (System.currentTimeMillis() - t) < 2000);
            Assert.assertFalse("Two nodes are active simultaneously", active1Latch.getCount() == 0 && active2Latch.getCount() == 0);
            Assert.assertFalse("No leader", active1Latch.getCount() > 0 && active2Latch.getCount() > 0);
            if (active1Latch.getCount() == 0) {
                standby2Latch.await(2, TimeUnit.SECONDS);
                Assert.assertEquals("Node 2 is expected to be on standby", 0, standby2Latch.getCount());
                standby1Latch.await(200, TimeUnit.MILLISECONDS);
                Assert.assertEquals("Node 1 is NOT expected to be on standby", 1, standby1Latch.getCount());
            } else {
                standby1Latch.await(2, TimeUnit.SECONDS);
                Assert.assertEquals("Node 1 is expected to be on standby", 0, standby1Latch.getCount());
                standby2Latch.await(200, TimeUnit.MILLISECONDS);
                Assert.assertEquals("Node 2 is NOT expected to be on standby", 1, standby2Latch.getCount());
            }
            controller2.halt();
            shutdown2.await(5, TimeUnit.SECONDS);
            Assert.assertEquals("Controller 2 should have shut down", 0, shutdown2.getCount());
            active1Latch.await(10, TimeUnit.SECONDS);
            Assert.assertEquals("Node 1 is expected to become active", 0, active1Latch.getCount());
            controller1.halt();
            shutdown1.await(10, TimeUnit.SECONDS);
            Assert.assertEquals("Controller 1 should have shut down", 0, shutdown1.getCount());
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractTest(com.questdb.test.tools.AbstractTest)

Aggregations

JournalWriter (com.questdb.store.JournalWriter)93 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)60 JournalEntryWriter (com.questdb.store.JournalEntryWriter)59 AbstractTest (com.questdb.test.tools.AbstractTest)54 Test (org.junit.Test)52 Rnd (com.questdb.std.Rnd)42 BeforeClass (org.junit.BeforeClass)12 CountDownLatch (java.util.concurrent.CountDownLatch)9 RecordCursor (com.questdb.common.RecordCursor)6 JournalException (com.questdb.std.ex.JournalException)6 Record (com.questdb.common.Record)5 RecordSource (com.questdb.ql.RecordSource)5 Factory (com.questdb.store.factory.Factory)5 BootstrapEnv (com.questdb.BootstrapEnv)4 ServerConfiguration (com.questdb.ServerConfiguration)4 JournalLockedException (com.questdb.ex.JournalLockedException)4 ClientConfig (com.questdb.net.ha.config.ClientConfig)4 ServerConfig (com.questdb.net.ha.config.ServerConfig)4 ServerNode (com.questdb.net.ha.config.ServerNode)4 StringSink (com.questdb.std.str.StringSink)4