use of java.util.concurrent.BrokenBarrierException in project ignite by apache.
the class BPlusTreeSelfTest method testPutSizeLivelock.
/**
* The test forces {@link BPlusTree#size} method to run into a livelock: during single run
* the method is picking up new pages which are concurrently added to the tree until the new pages are not added
* anymore. Test verifies that despite livelock condition a size from a valid range is returned.
*
* NB: This test has to be changed with the integration of IGNITE-3478.
*
* @throws Exception if test failed
*/
public void testPutSizeLivelock() throws Exception {
MAX_PER_PAGE = 5;
CNT = 800;
final int SLIDING_WINDOW_SIZE = 16;
final boolean DEBUG_PRINT = false;
final TestTree tree = createTestTree(false);
final AtomicLong curRmvKey = new AtomicLong(0);
final AtomicLong curPutKey = new AtomicLong(SLIDING_WINDOW_SIZE);
for (long i = curRmvKey.get(); i < curPutKey.get(); ++i) assertNull(tree.put(i));
final int hwThreads = Runtime.getRuntime().availableProcessors();
final int putRmvThreadCnt = Math.max(1, hwThreads / 2);
final int sizeThreadCnt = hwThreads - putRmvThreadCnt;
final CyclicBarrier putRmvOpBarrier = new CyclicBarrier(putRmvThreadCnt, new Runnable() {
@Override
public void run() {
if (DEBUG_PRINT) {
try {
X.println("===BARRIER=== size=" + tree.size() + " [" + tree.findFirst() + ".." + tree.findLast() + "]");
} catch (IgniteCheckedException e) {
// ignore
}
}
}
});
final int loopCnt = CNT / hwThreads;
IgniteInternalFuture<?> putRmvFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
for (int i = 0; i < loopCnt && !stop.get(); ++i) {
int order;
try {
order = putRmvOpBarrier.await();
} catch (BrokenBarrierException e) {
// barrier reset() has been called: terminate
break;
}
Long putVal = curPutKey.getAndIncrement();
if ((i & 0xff) == 0)
X.println(order + ": --> put(" + putVal + ")");
assertNull(tree.put(putVal));
Long rmvVal = curRmvKey.getAndIncrement();
if ((i & 0xff) == 0)
X.println(order + ": --> rmv(" + rmvVal + ")");
assertEquals(rmvVal, tree.remove(rmvVal));
assertNull(tree.findOne(rmvVal));
}
return null;
}
}, putRmvThreadCnt, "put-remove");
IgniteInternalFuture<?> sizeFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
final List<Long> treeContents = new ArrayList<>(SLIDING_WINDOW_SIZE * 2);
final BPlusTree.TreeRowClosure<Long, Long> rowDumper = new BPlusTree.TreeRowClosure<Long, Long>() {
@Override
public boolean apply(BPlusTree<Long, Long> tree, BPlusIO<Long> io, long pageAddr, int idx) throws IgniteCheckedException {
treeContents.add(io.getLookupRow(tree, pageAddr, idx));
final long endMs = System.currentTimeMillis() + 10;
final long endPutKey = curPutKey.get() + MAX_PER_PAGE;
while (System.currentTimeMillis() < endMs && curPutKey.get() < endPutKey) Thread.yield();
return true;
}
};
while (!stop.get()) {
treeContents.clear();
long treeSize = tree.size(rowDumper);
long curPutVal = curPutKey.get();
X.println(" ======> size=" + treeSize + "; last-put-value=" + curPutVal);
if (treeSize < SLIDING_WINDOW_SIZE || treeSize > curPutVal)
fail("Tree size is not in bounds [" + SLIDING_WINDOW_SIZE + ".." + curPutVal + "]:" + treeSize + "; contents=" + treeContents);
}
return null;
}
}, sizeThreadCnt, "size");
asyncRunFut = new GridCompoundFuture<>();
asyncRunFut.add((IgniteInternalFuture) putRmvFut);
asyncRunFut.add((IgniteInternalFuture) sizeFut);
asyncRunFut.markInitialized();
try {
putRmvFut.get(getTestTimeout(), TimeUnit.MILLISECONDS);
} finally {
stop.set(true);
putRmvOpBarrier.reset();
asyncRunFut.get();
}
tree.validateTree();
assertNoLocks();
}
use of java.util.concurrent.BrokenBarrierException in project ignite by apache.
the class GridNioSelfTest method testConcurrentConnects.
/**
* @throws Exception If failed.
*/
public void testConcurrentConnects() throws Exception {
final CyclicBarrier barrier = new CyclicBarrier(THREAD_CNT);
final AtomicReference<Exception> err = new AtomicReference<>();
final GridNioServer<?> srvr = startServer(new GridBufferedParser(true, ByteOrder.nativeOrder()), new EchoListener());
try {
IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {
@SuppressWarnings("BusyWait")
@Override
public void run() {
try {
for (int i = 0; i < 100 && !Thread.currentThread().isInterrupted(); i++) {
TestClient client = null;
try {
client = createClient(U.getLocalHost(), srvr.port(), U.getLocalHost());
MessageWithId msg = new MessageWithId(idProvider.getAndIncrement());
byte[] data = serializeMessage(msg);
for (int j = 0; j < 10; j++) client.sendMessage(data, data.length);
for (int j = 0; j < 10; j++) {
byte[] res = client.receiveMessage();
if (!Arrays.equals(data, res)) {
info("Invalid response received.");
err.compareAndSet(null, new IgniteCheckedException("Invalid response received."));
barrier.reset();
return;
}
}
} catch (IgniteCheckedException e) {
info("Encountered unexpected exception: " + e);
err.compareAndSet(null, e);
// Break the barrier.
barrier.reset();
break;
} catch (IOException e) {
info("Encountered IO exception: " + e);
err.compareAndSet(null, e);
// Break the barrier.
barrier.reset();
break;
} finally {
if (client != null)
client.close();
}
if ("conn-tester-1".equals(Thread.currentThread().getName()) && i % 10 == 0 && i > 0)
info("Run " + i + " iterations.");
barrier.await();
Thread.sleep(100);
}
} catch (InterruptedException ignored) {
barrier.reset();
info("Test thread was interrupted (will exit).");
} catch (BrokenBarrierException ignored) {
info("Barrier was broken (will exit).");
}
}
}, THREAD_CNT, "conn-tester");
fut.get();
if (err.get() != null)
throw err.get();
} finally {
srvr.stop();
}
}
use of java.util.concurrent.BrokenBarrierException in project ignite by apache.
the class GridTreeBenchmark method doTest.
/**
* @param tree Tree.
* @param data Data.
* @param warmup Warmup.
* @return Time.
* @throws BrokenBarrierException If failed.
* @throws InterruptedException If failed.
*/
private static long doTest(final ConcurrentNavigableMap<UUID, UUID> tree, final UUID[] data, boolean warmup) throws BrokenBarrierException, InterruptedException {
Thread[] ths = new Thread[THREADS];
final CyclicBarrier barrier = new CyclicBarrier(THREADS + 1);
final AtomicInteger cnt = new AtomicInteger();
for (int i = 0; i < ths.length; i++) {
ths[i] = new Thread(new Runnable() {
@Override
public void run() {
int off = cnt.getAndIncrement() * ITERATIONS;
try {
barrier.await();
} catch (Exception e) {
throw new IllegalStateException(e);
}
for (int i = 0; i < ITERATIONS; i++) {
UUID id = data[off + i];
id = tree.put(id, id);
assert id == null;
}
}
});
ths[i].start();
}
barrier.await();
long start = System.currentTimeMillis();
for (Thread t : ths) t.join();
long time = System.currentTimeMillis() - start;
if (!warmup) {
System.out.println(tree.getClass().getSimpleName() + " " + time + " ms");
return time;
}
return 0;
}
use of java.util.concurrent.BrokenBarrierException in project ignite by apache.
the class GridTreeBenchmark method doTestMaps.
/**
* @throws BrokenBarrierException If failed.
* @throws InterruptedException If failed.
*/
private static void doTestMaps() throws BrokenBarrierException, InterruptedException {
final UUID[] data = generate();
@SuppressWarnings("unchecked") final Map<UUID, UUID>[] maps = new Map[4];
for (int i = 0; i < maps.length; i++) maps[i] = new SnapTreeMap<>();
final Thread[] ths = new Thread[THREADS];
final CyclicBarrier barrier = new CyclicBarrier(THREADS + 1);
final AtomicInteger cnt = new AtomicInteger();
for (int i = 0; i < ths.length; i++) {
ths[i] = new Thread(new Runnable() {
@Override
public void run() {
int idx = cnt.getAndIncrement();
int off = idx * ITERATIONS;
Map<UUID, UUID> map = maps[idx % maps.length];
try {
barrier.await();
} catch (Exception e) {
throw new IllegalStateException(e);
}
for (int i = 0; i < ITERATIONS; i++) {
UUID id = data[off + i];
id = map.put(id, id);
assert id == null;
}
}
});
ths[i].start();
}
System.out.println("Sleep");
Thread.sleep(10000);
System.out.println("Go");
barrier.await();
long start = System.currentTimeMillis();
for (Thread t : ths) t.join();
long time = System.currentTimeMillis() - start;
System.out.println(time);
}
use of java.util.concurrent.BrokenBarrierException in project knime-core by knime.
the class TestLoadAndExecManySimultaneously method testConcurrency.
@Test
public void testConcurrency() throws Exception {
final CyclicBarrier barrier = new CyclicBarrier(NR_CONCURRENT);
Future<Void>[] futures = new Future[NR_CONCURRENT];
final AtomicBoolean isDone = new AtomicBoolean();
for (int i = 0; i < NR_CONCURRENT; i++) {
final int index = i;
futures[i] = m_executorService.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
OneInstanceWorkflowTest oneInstance = m_instances[index];
oneInstance.prepareWorkflowDirectory();
barrier.await(10, TimeUnit.SECONDS);
try {
oneInstance.loadWorkflow(InternalNodeContainerState.CONFIGURED);
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
barrier.await(120, TimeUnit.SECONDS);
oneInstance.executeWorkflow();
barrier.await(10, TimeUnit.SECONDS);
oneInstance.saveWorkflow();
barrier.await(10, TimeUnit.SECONDS);
oneInstance.discardWorkflow();
barrier.await(10, TimeUnit.SECONDS);
oneInstance.loadWorkflow(InternalNodeContainerState.EXECUTED);
barrier.await(10, TimeUnit.SECONDS);
oneInstance.discardWorkflow();
return null;
} finally {
isDone.set(true);
}
}
private void await(final int secondsTimeOut) throws InterruptedException, BrokenBarrierException, TimeoutException {
barrier.await(secondsTimeOut, TimeUnit.SECONDS);
}
});
}
ExecutionException brokenBarrierExceptionWrapper = null;
for (int i = 0; i < NR_CONCURRENT; i++) {
try {
futures[i].get();
} catch (ExecutionException e) {
if (e.getCause() instanceof BrokenBarrierException) {
if (brokenBarrierExceptionWrapper == null) {
brokenBarrierExceptionWrapper = e;
}
} else {
throw e;
}
}
}
if (brokenBarrierExceptionWrapper != null) {
throw brokenBarrierExceptionWrapper;
}
}
Aggregations