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();
}
}
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());
}
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());
}
Aggregations