Search in sources :

Example 86 with JournalWriter

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

the class CachingWriterFactoryTest method testTwoThreadsRaceToAllocate.

@Test
public void testTwoThreadsRaceToAllocate() throws Exception {
    final JournalMetadata<?> m = new JournalStructure("x").$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();
    for (int i = 0; i < n; i++) {
        new Thread(() -> {
            try {
                barrier.await();
                try (JournalWriter ignored = wf.writer(m)) {
                    writerCount.incrementAndGet();
                } catch (WriterBusyException ignored) {
                }
            } catch (Exception e) {
                e.printStackTrace();
                errors.incrementAndGet();
            } finally {
                halt.countDown();
            }
        }).start();
    }
    halt.await();
    // this check is unreliable on slow build servers
    // it is very often the case that there are limited number of cores
    // available and threads execute sequentially rather than
    // simultaneously. We should check that none of the threads
    // receive error.
    // Assert.assertEquals(1, writerCount.get());
    Assert.assertEquals(0, errors.get());
    Assert.assertEquals(1, wf.countFreeWriters());
}
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 87 with JournalWriter

use of com.questdb.store.JournalWriter 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 88 with JournalWriter

use of com.questdb.store.JournalWriter 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 89 with JournalWriter

use of com.questdb.store.JournalWriter 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 90 with JournalWriter

use of com.questdb.store.JournalWriter 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)

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