@AfterClass public static void stopThreadPool() { if (threadPool != null) { threadPool.shutdownNow(); threadPool = null; } }
@AfterMethod public void tearDown() throws Exception { replicaEngine.close(); storeReplica.close(); engine.close(); store.close(); if (threadPool != null) { threadPool.shutdownNow(); } }
public void testShutdownNowInterrupts() throws Exception { String threadPoolName = randomThreadPool(ThreadPool.ThreadPoolType.FIXED); ThreadPool threadPool = null; try { Settings nodeSettings = Settings.builder() .put("threadpool." + threadPoolName + ".queue_size", 1000) .put("node.name", "testShutdownNowInterrupts") .build(); threadPool = new ThreadPool(nodeSettings); ClusterSettings clusterSettings = new ClusterSettings(nodeSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); threadPool.setClusterSettings(clusterSettings); assertEquals(info(threadPool, threadPoolName).getQueueSize().getSingles(), 1000L); final CountDownLatch latch = new CountDownLatch(1); ThreadPoolExecutor oldExecutor = (ThreadPoolExecutor) threadPool.executor(threadPoolName); threadPool .executor(threadPoolName) .execute( () -> { try { new CountDownLatch(1).await(); } catch (InterruptedException ex) { latch.countDown(); Thread.currentThread().interrupt(); } }); clusterSettings.applySettings( Settings.builder().put("threadpool." + threadPoolName + ".queue_size", 2000).build()); assertThat(threadPool.executor(threadPoolName), not(sameInstance(oldExecutor))); assertThat(oldExecutor.isShutdown(), equalTo(true)); assertThat(oldExecutor.isTerminating(), equalTo(true)); assertThat(oldExecutor.isTerminated(), equalTo(false)); threadPool.shutdownNow(); // should interrupt the thread latch.await( 3, TimeUnit.SECONDS); // If this throws then ThreadPool#shutdownNow didn't interrupt } finally { terminateThreadPoolIfNeeded(threadPool); } }
public static void main(String[] args) { final String executor = ThreadPool.Names.GENERIC; final boolean waitForRequest = true; final ByteSizeValue payloadSize = new ByteSizeValue(100, ByteSizeUnit.BYTES); final int NUMBER_OF_CLIENTS = 10; final int NUMBER_OF_ITERATIONS = 100000; final byte[] payload = new byte[(int) payloadSize.bytes()]; final AtomicLong idGenerator = new AtomicLong(); final Type type = Type.NETTY; Settings settings = ImmutableSettings.settingsBuilder().build(); final ThreadPool serverThreadPool = new ThreadPool(); final TransportService serverTransportService = new TransportService(type.newTransport(settings, serverThreadPool), serverThreadPool) .start(); final ThreadPool clientThreadPool = new ThreadPool(); final TransportService clientTransportService = new TransportService(type.newTransport(settings, clientThreadPool), clientThreadPool) .start(); final DiscoveryNode node = new DiscoveryNode("server", serverTransportService.boundAddress().publishAddress()); serverTransportService.registerHandler( "benchmark", new BaseTransportRequestHandler<BenchmarkMessage>() { @Override public BenchmarkMessage newInstance() { return new BenchmarkMessage(); } @Override public String executor() { return executor; } @Override public void messageReceived(BenchmarkMessage request, TransportChannel channel) throws Exception { channel.sendResponse(request); } }); clientTransportService.connectToNode(node); for (int i = 0; i < 10000; i++) { BenchmarkMessage message = new BenchmarkMessage(1, payload); clientTransportService .submitRequest( node, "benchmark", message, new BaseTransportResponseHandler<BenchmarkMessage>() { @Override public BenchmarkMessage newInstance() { return new BenchmarkMessage(); } @Override public String executor() { return ThreadPool.Names.SAME; } @Override public void handleResponse(BenchmarkMessage response) {} @Override public void handleException(TransportException exp) { exp.printStackTrace(); } }) .txGet(); } Thread[] clients = new Thread[NUMBER_OF_CLIENTS]; final CountDownLatch latch = new CountDownLatch(NUMBER_OF_CLIENTS * NUMBER_OF_ITERATIONS); for (int i = 0; i < NUMBER_OF_CLIENTS; i++) { clients[i] = new Thread( new Runnable() { @Override public void run() { for (int j = 0; j < NUMBER_OF_ITERATIONS; j++) { final long id = idGenerator.incrementAndGet(); BenchmarkMessage message = new BenchmarkMessage(id, payload); BaseTransportResponseHandler<BenchmarkMessage> handler = new BaseTransportResponseHandler<BenchmarkMessage>() { @Override public BenchmarkMessage newInstance() { return new BenchmarkMessage(); } @Override public String executor() { return executor; } @Override public void handleResponse(BenchmarkMessage response) { if (response.id != id) { System.out.println( "NO ID MATCH [" + response.id + "] and [" + id + "]"); } latch.countDown(); } @Override public void handleException(TransportException exp) { exp.printStackTrace(); latch.countDown(); } }; if (waitForRequest) { clientTransportService .submitRequest(node, "benchmark", message, handler) .txGet(); } else { clientTransportService.sendRequest(node, "benchmark", message, handler); } } } }); } StopWatch stopWatch = new StopWatch().start(); for (int i = 0; i < NUMBER_OF_CLIENTS; i++) { clients[i].start(); } try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } stopWatch.stop(); System.out.println( "Ran [" + NUMBER_OF_CLIENTS + "], each with [" + NUMBER_OF_ITERATIONS + "] iterations, payload [" + payloadSize + "]: took [" + stopWatch.totalTime() + "], TPS: " + (NUMBER_OF_CLIENTS * NUMBER_OF_ITERATIONS) / stopWatch.totalTime().secondsFrac()); clientTransportService.close(); clientThreadPool.shutdownNow(); serverTransportService.close(); serverThreadPool.shutdownNow(); }
@After public void cleanUp() throws Exception { testThreadPool.shutdownNow(); }
@After public void cleanup() { threadPool.shutdownNow(); }