Search in sources :

Example 11 with Journal

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

the class CachingReaderFactoryTest method testSerialOpenClose.

@Test
public void testSerialOpenClose() throws Exception {
    // create journal
    final JournalMetadata<?> m = new JournalStructure("x").$date("ts").$().build();
    ((WriterFactory) getFactory()).writer(m).close();
    try (final CachingReaderFactory rf = new CachingReaderFactory(factoryContainer.getConfiguration(), 1000, 2)) {
        Journal firstReader = null;
        for (int i = 0; i < 1000; i++) {
            try (Journal reader = rf.reader(m)) {
                if (firstReader == null) {
                    firstReader = reader;
                }
                Assert.assertNotNull(reader);
                Assert.assertSame(firstReader, reader);
            }
        }
    }
}
Also used : JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Journal(com.questdb.store.Journal) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 12 with Journal

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

the class CachingReaderFactoryTest method testCloseWithActiveReader.

@Test
public void testCloseWithActiveReader() throws Exception {
    // create journal
    final JournalMetadata<?> m = new JournalStructure("x").$date("ts").$().build();
    ((WriterFactory) getFactory()).writer(m).close();
    try (final CachingReaderFactory rf = new CachingReaderFactory(factoryContainer.getConfiguration(), 1000, 2)) {
        Journal reader = rf.reader(m);
        Assert.assertNotNull(reader);
        rf.close();
        Assert.assertTrue(reader.isOpen());
        reader.close();
        Assert.assertFalse(reader.isOpen());
    }
}
Also used : JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Journal(com.questdb.store.Journal) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 13 with Journal

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

the class CachingReaderFactoryTest method testAllocateAndClear.

@Test
public void testAllocateAndClear() throws Exception {
    final JournalMetadata<?> m = new JournalStructure("z").$date("ts").$().build();
    getFactory().writer(m).close();
    try (CachingReaderFactory rf = new CachingReaderFactory(factoryContainer.getConfiguration(), 1, 2)) {
        int n = 2;
        final CyclicBarrier barrier = new CyclicBarrier(n);
        final CountDownLatch halt = new CountDownLatch(n);
        final AtomicInteger errors = new AtomicInteger();
        final AtomicInteger readerCount = new AtomicInteger();
        new Thread(() -> {
            try {
                for (int i = 0; i < 1000; i++) {
                    try (Journal ignored = rf.reader(m)) {
                        readerCount.incrementAndGet();
                    } catch (FactoryFullException 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++) {
                    rf.releaseInactive();
                    LockSupport.parkNanos(10L);
                }
            } catch (Exception e) {
                e.printStackTrace();
                errors.incrementAndGet();
            } finally {
                halt.countDown();
            }
        }).start();
        halt.await();
        Assert.assertTrue(readerCount.get() > 0);
        Assert.assertEquals(0, errors.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Journal(com.questdb.store.Journal) CountDownLatch(java.util.concurrent.CountDownLatch) FactoryFullException(com.questdb.ex.FactoryFullException) 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) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 14 with Journal

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

the class CachingReaderFactoryTest method testGetReadersBeforeFailure.

@SuppressWarnings("InfiniteLoopStatement")
@Test
public void testGetReadersBeforeFailure() throws Exception {
    // create journal
    final JournalMetadata<?> m = new JournalStructure("x").$date("ts").$().build();
    ((WriterFactory) getFactory()).writer(m).close();
    try (final CachingReaderFactory rf = new CachingReaderFactory(factoryContainer.getConfiguration(), 1000, 2)) {
        ObjList<Journal> readers = new ObjList<>();
        try {
            do {
                readers.add(rf.reader(m));
            } while (true);
        } catch (FactoryFullException e) {
            Assert.assertEquals(rf.getMaxEntries(), readers.size());
        } finally {
            for (int i = 0, n = readers.size(); i < n; i++) {
                readers.getQuick(i).close();
            }
        }
    }
}
Also used : ObjList(com.questdb.std.ObjList) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) Journal(com.questdb.store.Journal) FactoryFullException(com.questdb.ex.FactoryFullException) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 15 with Journal

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

the class CachingReaderFactoryTest method testConcurrentOpenAndClose.

@Test
public void testConcurrentOpenAndClose() throws Exception {
    final int readerCount = 5;
    int threadCount = 2;
    final int iterations = 1000;
    // 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 (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();
        for (int i = 0; i < threadCount; i++) {
            final int x = i;
            new Thread(() -> {
                Rnd rnd = new Rnd(x, -x);
                try {
                    barrier.await();
                    for (int i1 = 0; i1 < iterations; i1++) {
                        JournalMetadata<?> m = meta[rnd.nextPositiveInt() % readerCount];
                        try (Journal ignored = rf.reader(m)) {
                            LockSupport.parkNanos(100);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    errors.incrementAndGet();
                } finally {
                    halt.countDown();
                }
            }).start();
        }
        halt.await();
        Assert.assertEquals(0, errors.get());
    }
}
Also used : Rnd(com.questdb.std.Rnd) Journal(com.questdb.store.Journal) 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) 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)

Aggregations

Journal (com.questdb.store.Journal)27 Test (org.junit.Test)24 AbstractTest (com.questdb.test.tools.AbstractTest)23 JournalMetadata (com.questdb.store.factory.configuration.JournalMetadata)14 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)9 RetryLockException (com.questdb.ex.RetryLockException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 FactoryFullException (com.questdb.ex.FactoryFullException)4 JournalLockedException (com.questdb.ex.JournalLockedException)4 JournalException (com.questdb.std.ex.JournalException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 JournalDeltaProducer (com.questdb.net.ha.producer.JournalDeltaProducer)2 RecordSource (com.questdb.ql.RecordSource)2 Rnd (com.questdb.std.Rnd)2 File (java.io.File)2 BootstrapEnv (com.questdb.BootstrapEnv)1 ServerConfiguration (com.questdb.ServerConfiguration)1 Record (com.questdb.common.Record)1 RecordCursor (com.questdb.common.RecordCursor)1