use of java.util.concurrent.ExecutorService in project pinot by linkedin.
the class ScatterGatherTest method testMultipleServerHappy.
@Test
public void testMultipleServerHappy() throws Exception {
MetricsRegistry registry = new MetricsRegistry();
// Server start
int serverPort1 = 7071;
int serverPort2 = 7072;
int serverPort3 = 7073;
int serverPort4 = 7074;
NettyTCPServer server1 = new NettyTCPServer(serverPort1, new TestRequestHandlerFactory(0, 1), null);
NettyTCPServer server2 = new NettyTCPServer(serverPort2, new TestRequestHandlerFactory(1, 1), null);
NettyTCPServer server3 = new NettyTCPServer(serverPort3, new TestRequestHandlerFactory(2, 1), null);
NettyTCPServer server4 = new NettyTCPServer(serverPort4, new TestRequestHandlerFactory(3, 1), null);
Thread t1 = new Thread(server1);
Thread t2 = new Thread(server2);
Thread t3 = new Thread(server3);
Thread t4 = new Thread(server4);
t1.start();
t2.start();
t3.start();
t4.start();
//Client setup
ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1);
ExecutorService poolExecutor = MoreExecutors.sameThreadExecutor();
ExecutorService service = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>());
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_");
PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(eventLoopGroup, new HashedWheelTimer(), clientMetrics);
KeyedPoolImpl<ServerInstance, NettyClientConnection> pool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>(1, 1, 300000, 1, rm, timedExecutor, poolExecutor, registry);
rm.setPool(pool);
SegmentIdSet pg1 = new SegmentIdSet();
pg1.addSegment(new SegmentId("0"));
SegmentIdSet pg2 = new SegmentIdSet();
pg2.addSegment(new SegmentId("1"));
SegmentIdSet pg3 = new SegmentIdSet();
pg3.addSegment(new SegmentId("2"));
SegmentIdSet pg4 = new SegmentIdSet();
pg4.addSegment(new SegmentId("3"));
ServerInstance serverInstance1 = new ServerInstance("localhost", serverPort1);
ServerInstance serverInstance2 = new ServerInstance("localhost", serverPort2);
ServerInstance serverInstance3 = new ServerInstance("localhost", serverPort3);
ServerInstance serverInstance4 = new ServerInstance("localhost", serverPort4);
Map<ServerInstance, SegmentIdSet> pgMap = new HashMap<ServerInstance, SegmentIdSet>();
pgMap.put(serverInstance1, pg1);
pgMap.put(serverInstance2, pg2);
pgMap.put(serverInstance3, pg3);
pgMap.put(serverInstance4, pg4);
String request1 = "request_0";
String request2 = "request_1";
String request3 = "request_2";
String request4 = "request_3";
Map<SegmentIdSet, String> pgMapStr = new HashMap<SegmentIdSet, String>();
pgMapStr.put(pg1, request1);
pgMapStr.put(pg2, request2);
pgMapStr.put(pg3, request3);
pgMapStr.put(pg4, request4);
ScatterGatherRequest req = new TestScatterGatherRequest(pgMap, pgMapStr);
ScatterGatherImpl scImpl = new ScatterGatherImpl(pool, service);
final ScatterGatherStats scatterGatherStats = new ScatterGatherStats();
BrokerMetrics brokerMetrics = new BrokerMetrics(new MetricsRegistry());
CompositeFuture<ServerInstance, ByteBuf> fut = scImpl.scatterGather(req, scatterGatherStats, brokerMetrics);
Map<ServerInstance, ByteBuf> v = fut.get();
Assert.assertEquals(v.size(), 4);
ByteBuf b = v.get(serverInstance1);
byte[] b2 = new byte[b.readableBytes()];
b.readBytes(b2);
String response = new String(b2);
Assert.assertEquals(response, "response_0_0");
b = v.get(serverInstance2);
b2 = new byte[b.readableBytes()];
b.readBytes(b2);
response = new String(b2);
Assert.assertEquals(response, "response_1_0");
b = v.get(serverInstance3);
b2 = new byte[b.readableBytes()];
b.readBytes(b2);
response = new String(b2);
Assert.assertEquals(response, "response_2_0");
b = v.get(serverInstance4);
b2 = new byte[b.readableBytes()];
b.readBytes(b2);
response = new String(b2);
Assert.assertEquals(response, "response_3_0");
server1.shutdownGracefully();
server2.shutdownGracefully();
server3.shutdownGracefully();
server4.shutdownGracefully();
pool.shutdown();
service.shutdown();
eventLoopGroup.shutdownGracefully();
}
use of java.util.concurrent.ExecutorService in project rest.li by linkedin.
the class DegraderLoadBalancerTest method runMultiThreadedTest.
private void runMultiThreadedTest(final DegraderLoadBalancerStrategyAdapter strategyAdapter, final List<TrackerClient> clients, final int numberOfThread, final boolean trackerClientMustNotBeNull) {
final CountDownLatch exitLatch = new CountDownLatch(numberOfThread);
final CountDownLatch startLatch = new CountDownLatch(numberOfThread);
ExecutorService executorService = Executors.newFixedThreadPool(numberOfThread);
List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
for (int i = 0; i < numberOfThread; i++) {
// testNG won't be able to "know" if a child thread's assertion is false, so to get around it we
// need to throw an exception so executorService can notify the main thread about the false assertion
// even though executorService has an invokeAll method, it doesn't seem run the threads at the same time
// so we have to use a latch to make sure all the threads run at the same time to simulate concurrent access
Future<Boolean> future = executorService.submit(new Runnable() {
@Override
public void run() {
startLatch.countDown();
try {
//wait until all threads are ready to run together
startLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Failed the test because thread was interrupted");
}
// if config is broken, every thread would fail and state update would be unsuccessful
try {
TrackerClient resultTC = getTrackerClient(strategyAdapter, null, new RequestContext(), 1, clients);
if (trackerClientMustNotBeNull && resultTC == null) {
throw new RuntimeException("Failed the test because resultTC returns null");
}
} catch (DummyException ex) {
// expected
}
exitLatch.countDown();
try {
//make sure all threads are not stuck on WAIT_THREAD
if (!exitLatch.await(5, TimeUnit.SECONDS)) {
throw new RuntimeException("Failed the test because we waited longer than 1 second");
}
} catch (InterruptedException e) {
throw new RuntimeException("Failed the test because thread was interrupted");
}
}
}, Boolean.TRUE);
futures.add(future);
}
for (Future<Boolean> future : futures) {
try {
assertTrue(future.get());
} catch (Exception e) {
fail("something is failing", e);
}
}
executorService.shutdownNow();
}
use of java.util.concurrent.ExecutorService in project javaslang by javaslang.
the class Future method find.
/**
* Returns a {@code Future} that eventually succeeds with the first result of the given {@code Future}s which
* matches the given {@code predicate}. If no result matches, the {@code Future} will contain {@link Option.None}.
* <p>
* The returned {@code Future} is backed by the given {@link ExecutorService}.
*
* @param executorService An executor service.
* @param futures An iterable of futures.
* @param predicate A predicate that tests successful future results.
* @param <T> Result type of the futures.
* @return A Future of an {@link Option} of the first result of the given {@code futures} that satisfies the given {@code predicate}.
* @throws NullPointerException if one of the arguments is null
*/
static <T> Future<Option<T>> find(ExecutorService executorService, Iterable<? extends Future<? extends T>> futures, Predicate<? super T> predicate) {
Objects.requireNonNull(executorService, "executorService is null");
Objects.requireNonNull(futures, "futures is null");
Objects.requireNonNull(predicate, "predicate is null");
final Promise<Option<T>> promise = Promise.make(executorService);
final List<Future<? extends T>> list = List.ofAll(futures);
if (list.isEmpty()) {
promise.success(Option.none());
} else {
final AtomicInteger count = new AtomicInteger(list.length());
list.forEach(future -> future.onComplete(result -> {
synchronized (count) {
if (!promise.isCompleted()) {
final boolean wasLast = count.decrementAndGet() == 0;
result.filter(predicate).onSuccess(value -> promise.trySuccess(Option.some(value))).onFailure(ignored -> {
if (wasLast) {
promise.trySuccess(Option.none());
}
});
}
}
}));
}
return promise.future();
}
use of java.util.concurrent.ExecutorService in project javaslang by javaslang.
the class IteratorTest method shouldNotDeadlockOnConcurrentClassInitialization.
// -- class initialization (see #1773)
@Test(timeout = 5_000)
public void shouldNotDeadlockOnConcurrentClassInitialization() throws InterruptedException {
final ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(new ClassInitializer("javaslang.collection.Iterator"));
executorService.execute(new ClassInitializer("javaslang.collection.AbstractIterator"));
executorService.shutdown();
// try to access javaslang iterator and it will hang
Iterator.empty().iterator();
}
use of java.util.concurrent.ExecutorService in project hibernate-orm by hibernate.
the class CollectionRegionAccessStrategyTest method testPutFromLoadRemoveDoesNotProduceStaleData.
@Test
public void testPutFromLoadRemoveDoesNotProduceStaleData() throws Exception {
if (!cacheMode.isInvalidation()) {
return;
}
final CountDownLatch pferLatch = new CountDownLatch(1);
final CountDownLatch removeLatch = new CountDownLatch(1);
// remove the interceptor inserted by default PutFromLoadValidator, we're using different one
PutFromLoadValidator originalValidator = PutFromLoadValidator.removeFromCache(localRegion.getCache());
PutFromLoadValidator mockValidator = spy(originalValidator);
doAnswer(invocation -> {
try {
return invocation.callRealMethod();
} finally {
try {
removeLatch.countDown();
assertFalse(pferLatch.await(2, TimeUnit.SECONDS));
} catch (InterruptedException e) {
log.debug("Interrupted");
Thread.currentThread().interrupt();
} catch (Exception e) {
log.error("Error", e);
throw new RuntimeException("Error", e);
}
}
}).when(mockValidator).acquirePutFromLoadLock(any(), any(), anyLong());
PutFromLoadValidator.addToCache(localRegion.getCache(), mockValidator);
cleanup.add(() -> {
PutFromLoadValidator.removeFromCache(localRegion.getCache());
PutFromLoadValidator.addToCache(localRegion.getCache(), originalValidator);
});
final AccessDelegate delegate = localRegion.getCache().getCacheConfiguration().transaction().transactionMode().isTransactional() ? new TxInvalidationCacheAccessDelegate(localRegion, mockValidator) : new NonTxInvalidationCacheAccessDelegate(localRegion, mockValidator);
ExecutorService executorService = Executors.newCachedThreadPool();
cleanup.add(() -> executorService.shutdownNow());
final String KEY = "k1";
Future<Void> pferFuture = executorService.submit(() -> {
SharedSessionContractImplementor session = mockedSession();
delegate.putFromLoad(session, KEY, "v1", session.getTimestamp(), null);
return null;
});
Future<Void> removeFuture = executorService.submit(() -> {
removeLatch.await();
SharedSessionContractImplementor session = mockedSession();
withTx(localEnvironment, session, () -> {
delegate.remove(session, KEY);
return null;
});
pferLatch.countDown();
return null;
});
pferFuture.get();
removeFuture.get();
assertFalse(localRegion.getCache().containsKey(KEY));
assertFalse(remoteRegion.getCache().containsKey(KEY));
}
Aggregations