Search in sources :

Example 66 with JournalStructure

use of com.questdb.store.factory.configuration.JournalStructure in project questdb by bluestreak01.

the class CachingWriterFactoryTest method testLockNonExisting.

@Test
public void testLockNonExisting() throws Exception {
    final JournalMetadata<?> x = new JournalStructure("x").$date("ts").$().build();
    wf.lock(x.getName());
    try {
        wf.writer(x);
        Assert.fail();
    } catch (JournalLockedException ignored) {
    }
    wf.unlock(x.getName());
    try (JournalWriter wx = wf.writer(x)) {
        Assert.assertNotNull(wx);
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalLockedException(com.questdb.ex.JournalLockedException) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 67 with JournalStructure

use of com.questdb.store.factory.configuration.JournalStructure in project questdb by bluestreak01.

the class CachingWriterFactoryTest method testOneThreadGetRelease.

@Test
public void testOneThreadGetRelease() throws Exception {
    final JournalMetadata<?> m = new JournalStructure("x").$date("ts").$().build();
    JournalWriter x;
    JournalWriter y;
    x = wf.writer(m);
    try {
        Assert.assertEquals(0, wf.countFreeWriters());
        Assert.assertNotNull(x);
        Assert.assertTrue(x.isOpen());
        Assert.assertTrue(x == wf.writer(m));
    } finally {
        x.close();
    }
    Assert.assertEquals(1, wf.countFreeWriters());
    y = wf.writer(m);
    try {
        Assert.assertNotNull(y);
        Assert.assertTrue(y.isOpen());
        Assert.assertTrue(y == x);
    } finally {
        y.close();
    }
    Assert.assertEquals(1, wf.countFreeWriters());
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 68 with JournalStructure

use of com.questdb.store.factory.configuration.JournalStructure in project questdb by bluestreak01.

the class CachingWriterFactoryTest method testAllocateAndClear.

@Test
public void testAllocateAndClear() throws Exception {
    final JournalMetadata<?> m = new JournalStructure("z").$date("ts").$().build();
    int n = 2;
    final CyclicBarrier barrier = new CyclicBarrier(n);
    final CountDownLatch halt = new CountDownLatch(n);
    final AtomicInteger errors = new AtomicInteger();
    final AtomicInteger writerCount = new AtomicInteger();
    new Thread(() -> {
        try {
            for (int i = 0; i < 1000; i++) {
                try (JournalWriter ignored = wf.writer(m)) {
                    writerCount.incrementAndGet();
                } catch (WriterBusyException ignored) {
                }
                if (i == 1) {
                    barrier.await();
                }
                LockSupport.parkNanos(10L);
            }
        } catch (Exception e) {
            e.printStackTrace();
            errors.incrementAndGet();
        } finally {
            halt.countDown();
        }
    }).start();
    new Thread(() -> {
        try {
            barrier.await();
            for (int i = 0; i < 1000; i++) {
                wf.releaseInactive();
                LockSupport.parkNanos(10L);
            }
        } catch (Exception e) {
            e.printStackTrace();
            errors.incrementAndGet();
        } finally {
            halt.countDown();
        }
    }).start();
    halt.await();
    Assert.assertTrue(writerCount.get() > 0);
    Assert.assertEquals(0, errors.get());
}
Also used : JournalWriter(com.questdb.store.JournalWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) WriterBusyException(com.questdb.ex.WriterBusyException) CountDownLatch(java.util.concurrent.CountDownLatch) JournalLockedException(com.questdb.ex.JournalLockedException) JournalException(com.questdb.std.ex.JournalException) FactoryClosedException(com.questdb.ex.FactoryClosedException) WriterBusyException(com.questdb.ex.WriterBusyException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 69 with JournalStructure

use of com.questdb.store.factory.configuration.JournalStructure in project questdb by bluestreak01.

the class CachingWriterFactoryTest method testFactoryCloseBeforeRelease.

@Test
public void testFactoryCloseBeforeRelease() throws Exception {
    final JournalMetadata<?> m = new JournalStructure("x").$date("ts").$().build();
    JournalWriter x;
    x = wf.writer(m);
    try {
        Assert.assertEquals(0, wf.countFreeWriters());
        Assert.assertNotNull(x);
        Assert.assertTrue(x.isOpen());
        Assert.assertTrue(x == wf.writer(m));
        wf.close();
    } finally {
        x.close();
    }
    Assert.assertFalse(x.isOpen());
    try {
        wf.writer(m);
    } catch (FactoryClosedException ignored) {
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) FactoryClosedException(com.questdb.ex.FactoryClosedException) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 70 with JournalStructure

use of com.questdb.store.factory.configuration.JournalStructure in project questdb by bluestreak01.

the class AppendRawPartitioned method main.

public static void main(String[] args) throws JournalException, ParserException {
    if (args.length < 1) {
        System.out.println("Usage: AppendRawPartitioned <path>");
        System.exit(1);
    }
    final String location = args[0];
    // factory can be reused in application and must be explicitly closed when no longer needed.
    try (Factory factory = new Factory(location, 1000, 1, 0)) {
        // to populate it.
        try (JournalWriter writer = factory.writer(new JournalStructure("customers").$int("id").$str("name").$ts("updateDate").partitionBy(PartitionBy.DAY).$())) {
            Rnd rnd = new Rnd();
            long timestamp = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++) {
                // enforce timestamp order
                JournalEntryWriter ew = writer.entryWriter(timestamp);
                // columns accessed by index
                ew.putInt(0, rnd.nextPositiveInt());
                ew.putStr(1, rnd.nextChars(25));
                // increment timestamp by 30 seconds
                timestamp += 30000;
                // append record to journal
                ew.append();
            }
            // commit all records at once
            // there is no limit on how many records can be in the same transaction
            writer.commit();
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Factory(com.questdb.store.factory.Factory) Rnd(com.questdb.std.Rnd) JournalEntryWriter(com.questdb.store.JournalEntryWriter)

Aggregations

JournalStructure (com.questdb.store.factory.configuration.JournalStructure)74 JournalWriter (com.questdb.store.JournalWriter)60 JournalEntryWriter (com.questdb.store.JournalEntryWriter)47 Rnd (com.questdb.std.Rnd)43 Test (org.junit.Test)43 AbstractTest (com.questdb.test.tools.AbstractTest)40 BeforeClass (org.junit.BeforeClass)11 JournalException (com.questdb.std.ex.JournalException)9 Journal (com.questdb.store.Journal)9 JournalLockedException (com.questdb.ex.JournalLockedException)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 RetryLockException (com.questdb.ex.RetryLockException)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 BootstrapEnv (com.questdb.BootstrapEnv)4 ServerConfiguration (com.questdb.ServerConfiguration)4 FactoryFullException (com.questdb.ex.FactoryFullException)4 StringSink (com.questdb.std.str.StringSink)4 Factory (com.questdb.store.factory.Factory)4 FactoryClosedException (com.questdb.ex.FactoryClosedException)3