Search in sources :

Example 1 with WriterBusyException

use of com.questdb.ex.WriterBusyException 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 2 with WriterBusyException

use of com.questdb.ex.WriterBusyException 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 3 with WriterBusyException

use of com.questdb.ex.WriterBusyException 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)

Aggregations

JournalLockedException (com.questdb.ex.JournalLockedException)3 WriterBusyException (com.questdb.ex.WriterBusyException)3 JournalException (com.questdb.std.ex.JournalException)3 JournalWriter (com.questdb.store.JournalWriter)3 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)3 AbstractTest (com.questdb.test.tools.AbstractTest)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 FactoryClosedException (com.questdb.ex.FactoryClosedException)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1