Search in sources :

Example 61 with JournalStructure

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

the class JournalTest method testInvalidColumnName.

@Test
public void testInvalidColumnName() {
    File base = getFactory().getConfiguration().getJournalBase();
    File dir = new File(base, "x");
    Assert.assertFalse(dir.exists());
    try {
        getFactory().writer(new JournalStructure("x").$sym("x").index().$sym("y").index().$sym("z\0is\0bad").index().$());
        Assert.fail();
    } catch (JournalException ignore) {
    }
    getFactory().expire();
    Assert.assertTrue(dir.exists());
    Assert.assertTrue(Files.delete(dir));
}
Also used : JournalException(com.questdb.std.ex.JournalException) JournalStructure(com.questdb.store.factory.configuration.JournalStructure) File(java.io.File) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 62 with JournalStructure

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

the class CachingReaderFactoryTest method testLockUnlockMultiple.

@Test
public void testLockUnlockMultiple() throws Exception {
    final JournalMetadata<?> m1 = new JournalStructure("x").$date("ts").$().build();
    getFactory().writer(m1).close();
    try (final CachingReaderFactory rf = new CachingReaderFactory(factoryContainer.getConfiguration(), 1000, 2)) {
        Journal r1 = rf.reader(m1);
        Journal r2 = rf.reader(m1);
        r1.close();
        try {
            rf.lock(m1.getName());
        } catch (RetryLockException e) {
            e.printStackTrace();
        }
        r2.close();
        rf.lock(m1.getName());
        rf.unlock(m1.getName());
    }
}
Also used : 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 63 with JournalStructure

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

the class CachingReaderFactoryTest method testCloseWithInactiveReader.

@Test
public void testCloseWithInactiveReader() 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);
        reader.close();
        Assert.assertTrue(reader.isOpen());
        rf.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 64 with JournalStructure

use of com.questdb.store.factory.configuration.JournalStructure 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 65 with JournalStructure

use of com.questdb.store.factory.configuration.JournalStructure 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)

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