Search in sources :

Example 1 with JournalLockedException

use of com.questdb.ex.JournalLockedException in project questdb by bluestreak01.

the class CachingReaderFactoryTest method testLockUnlock.

@Test
public void testLockUnlock() throws Exception {
    // create journals
    final JournalMetadata<?> m1 = new JournalStructure("x").$date("ts").$().build();
    ((WriterFactory) getFactory()).writer(m1).close();
    final JournalMetadata<?> m2 = new JournalStructure("y").$date("ts").$().build();
    ((WriterFactory) getFactory()).writer(m2).close();
    Journal x, y;
    try (final CachingReaderFactory rf = new CachingReaderFactory(factoryContainer.getConfiguration(), 1000, 2)) {
        x = rf.reader(m1);
        Assert.assertNotNull(x);
        y = rf.reader(m2);
        Assert.assertNotNull(y);
        // expect lock to fail because we have "x" open
        try {
            rf.lock(m1.getName());
            Assert.fail();
        } catch (RetryLockException ignore) {
        }
        x.close();
        // expect lock to succeed after we closed "x"
        rf.lock(m1.getName());
        // expect "x" to be physically closed
        Assert.assertFalse(x.isOpen());
        // "x" is locked, expect this to fail
        try {
            Assert.assertNull(rf.reader(m1));
        } catch (JournalLockedException ignored) {
        }
        rf.unlock(m1.getName());
        x = rf.reader(m1);
        Assert.assertNotNull(x);
        x.close();
        Assert.assertTrue(x.isOpen());
    }
    Assert.assertTrue(y.isOpen());
    y.close();
    Assert.assertFalse(y.isOpen());
    // "x" was not busy and should be closed by factory
    Assert.assertFalse(x.isOpen());
}
Also used : JournalLockedException(com.questdb.ex.JournalLockedException) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Journal(com.questdb.store.Journal) RetryLockException(com.questdb.ex.RetryLockException) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 2 with JournalLockedException

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

use of com.questdb.ex.JournalLockedException in project questdb by bluestreak01.

the class CachingReaderFactoryTest method testLockBusyReader.

@Test
public void testLockBusyReader() throws Exception {
    final int readerCount = 5;
    int threadCount = 2;
    final int iterations = 10000;
    // create journals to read
    final JournalMetadata<?>[] meta = new JournalMetadata[readerCount];
    for (int i = 0; i < readerCount; i++) {
        final JournalMetadata<?> m = new JournalStructure("x" + i).$date("ts").$().build();
        ((WriterFactory) getFactory()).writer(m).close();
        meta[i] = m;
    }
    try {
        try (final CachingReaderFactory rf = new CachingReaderFactory(factoryContainer.getConfiguration(), 1000, 2)) {
            final CyclicBarrier barrier = new CyclicBarrier(threadCount);
            final CountDownLatch halt = new CountDownLatch(threadCount);
            final AtomicInteger errors = new AtomicInteger();
            final LongList lockTimes = new LongList();
            final LongList workerTimes = new LongList();
            new Thread(() -> {
                Rnd rnd = new Rnd();
                try {
                    barrier.await();
                    String name = null;
                    for (int i = 0; i < iterations; i++) {
                        if (name == null) {
                            name = meta[rnd.nextPositiveInt() % readerCount].getName();
                        }
                        while (true) {
                            try {
                                rf.lock(name);
                                lockTimes.add(System.currentTimeMillis());
                                LockSupport.parkNanos(100L);
                                rf.unlock(name);
                                name = null;
                                break;
                            } catch (JournalException e) {
                                if (!(e instanceof RetryLockException)) {
                                    e.printStackTrace();
                                    errors.incrementAndGet();
                                    break;
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    errors.incrementAndGet();
                }
                halt.countDown();
            }).start();
            new Thread(() -> {
                Rnd rnd = new Rnd();
                workerTimes.add(System.currentTimeMillis());
                for (int i = 0; i < iterations; i++) {
                    JournalMetadata<?> metadata = meta[rnd.nextPositiveInt() % readerCount];
                    try (Journal<?> ignored = rf.reader(metadata)) {
                        if (metadata == meta[readerCount - 1] && barrier.getNumberWaiting() > 0) {
                            barrier.await();
                        }
                        LockSupport.parkNanos(10L);
                    } catch (JournalLockedException ignored) {
                    } catch (Exception e) {
                        e.printStackTrace();
                        errors.incrementAndGet();
                    }
                }
                workerTimes.add(System.currentTimeMillis());
                halt.countDown();
            }).start();
            halt.await();
            Assert.assertEquals(0, errors.get());
            // check that there are lock times between worker times
            int count = 0;
            // ensure that we have worker times
            Assert.assertEquals(2, workerTimes.size());
            long lo = workerTimes.get(0);
            long hi = workerTimes.get(1);
            Assert.assertTrue(lockTimes.size() > 0);
            for (int i = 0, n = lockTimes.size(); i < n; i++) {
                long t = lockTimes.getQuick(i);
                if (t > lo && t < hi) {
                    count++;
                }
            }
            Assert.assertTrue(count > 0);
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : JournalLockedException(com.questdb.ex.JournalLockedException) JournalException(com.questdb.std.ex.JournalException) Rnd(com.questdb.std.Rnd) Journal(com.questdb.store.Journal) LongList(com.questdb.std.LongList) CountDownLatch(java.util.concurrent.CountDownLatch) JournalLockedException(com.questdb.ex.JournalLockedException) JournalException(com.questdb.std.ex.JournalException) FactoryFullException(com.questdb.ex.FactoryFullException) RetryLockException(com.questdb.ex.RetryLockException) CyclicBarrier(java.util.concurrent.CyclicBarrier) RetryLockException(com.questdb.ex.RetryLockException) JournalMetadata(com.questdb.store.factory.configuration.JournalMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 4 with JournalLockedException

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

Aggregations

JournalLockedException (com.questdb.ex.JournalLockedException)4 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)4 AbstractTest (com.questdb.test.tools.AbstractTest)4 Test (org.junit.Test)4 RetryLockException (com.questdb.ex.RetryLockException)2 JournalException (com.questdb.std.ex.JournalException)2 Journal (com.questdb.store.Journal)2 JournalWriter (com.questdb.store.JournalWriter)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 FactoryFullException (com.questdb.ex.FactoryFullException)1 WriterBusyException (com.questdb.ex.WriterBusyException)1 LongList (com.questdb.std.LongList)1 Rnd (com.questdb.std.Rnd)1 JournalMetadata (com.questdb.store.factory.configuration.JournalMetadata)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1