Search in sources :

Example 31 with ThreadPool

use of org.elasticsearch.threadpool.ThreadPool in project crate by crate.

the class TestingHelpers method newMockedThreadPool.

public static ThreadPool newMockedThreadPool() {
    ThreadPool threadPool = Mockito.mock(ThreadPool.class);
    final ExecutorService executorService = Executors.newSingleThreadExecutor();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            executorService.shutdown();
            return null;
        }
    }).when(threadPool).shutdown();
    when(threadPool.executor(anyString())).thenReturn(executorService);
    try {
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                executorService.awaitTermination(1, TimeUnit.SECONDS);
                return null;
            }
        }).when(threadPool).awaitTermination(anyLong(), any(TimeUnit.class));
    } catch (InterruptedException e) {
        throw Throwables.propagate(e);
    }
    return threadPool;
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ExecutorService(java.util.concurrent.ExecutorService) TimeUnit(java.util.concurrent.TimeUnit)

Example 32 with ThreadPool

use of org.elasticsearch.threadpool.ThreadPool in project crate by crate.

the class DecommissioningServiceTest method setUp.

@Before
public void setUp() throws Exception {
    NodeSettingsService settingsService = new NodeSettingsService(Settings.EMPTY);
    threadPool = mock(ThreadPool.class, Answers.RETURNS_MOCKS.get());
    jobsLogs = new JobsLogs(() -> true);
    sqlOperations = mock(SQLOperations.class, Answers.RETURNS_MOCKS.get());
    decommissioningService = new TestableDecommissioningService(Settings.EMPTY, new NoopClusterService(), jobsLogs, threadPool, settingsService, sqlOperations, mock(TransportClusterHealthAction.class), mock(TransportClusterUpdateSettingsAction.class));
}
Also used : ThreadPool(org.elasticsearch.threadpool.ThreadPool) JobsLogs(io.crate.operation.collect.stats.JobsLogs) NodeSettingsService(org.elasticsearch.node.settings.NodeSettingsService) SQLOperations(io.crate.action.sql.SQLOperations) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) Before(org.junit.Before)

Example 33 with ThreadPool

use of org.elasticsearch.threadpool.ThreadPool in project elasticsearch by elastic.

the class NetworkModuleTests method testOverrideDefault.

public void testOverrideDefault() {
    Settings settings = Settings.builder().put(NetworkModule.HTTP_TYPE_SETTING.getKey(), "custom").put(NetworkModule.HTTP_DEFAULT_TYPE_SETTING.getKey(), "default_custom").put(NetworkModule.TRANSPORT_DEFAULT_TYPE_SETTING.getKey(), "local").put(NetworkModule.TRANSPORT_TYPE_KEY, "default_custom").build();
    // content doesn't matter we check reference equality
    Supplier<Transport> customTransport = () -> null;
    Supplier<HttpServerTransport> custom = FakeHttpTransport::new;
    Supplier<HttpServerTransport> def = FakeHttpTransport::new;
    NetworkModule module = newNetworkModule(settings, false, new NetworkPlugin() {

        @Override
        public Map<String, Supplier<Transport>> getTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService) {
            return Collections.singletonMap("default_custom", customTransport);
        }

        @Override
        public Map<String, Supplier<HttpServerTransport>> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NamedXContentRegistry xContentRegistry, NetworkService networkService, HttpServerTransport.Dispatcher requestDispatcher) {
            Map<String, Supplier<HttpServerTransport>> supplierMap = new HashMap<>();
            supplierMap.put("custom", custom);
            supplierMap.put("default_custom", def);
            return supplierMap;
        }
    });
    assertSame(custom, module.getHttpServerTransportSupplier());
    assertSame(customTransport, module.getTransportSupplier());
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) NetworkPlugin(org.elasticsearch.plugins.NetworkPlugin) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) BigArrays(org.elasticsearch.common.util.BigArrays) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) Transport(org.elasticsearch.transport.Transport) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) HashMap(java.util.HashMap) Map(java.util.Map) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Settings(org.elasticsearch.common.settings.Settings)

Example 34 with ThreadPool

use of org.elasticsearch.threadpool.ThreadPool in project elasticsearch by elastic.

the class NetworkModuleTests method testRegisterTransport.

public void testRegisterTransport() {
    Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "custom").put(NetworkModule.HTTP_ENABLED.getKey(), false).build();
    // content doesn't matter we check reference equality
    Supplier<Transport> custom = () -> null;
    NetworkPlugin plugin = new NetworkPlugin() {

        @Override
        public Map<String, Supplier<Transport>> getTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService) {
            return Collections.singletonMap("custom", custom);
        }
    };
    NetworkModule module = newNetworkModule(settings, false, plugin);
    assertFalse(module.isTransportClient());
    assertFalse(module.isHttpEnabled());
    assertSame(custom, module.getTransportSupplier());
    // check it works with transport only as well
    module = newNetworkModule(settings, true, plugin);
    assertSame(custom, module.getTransportSupplier());
    assertTrue(module.isTransportClient());
    assertFalse(module.isHttpEnabled());
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) NetworkPlugin(org.elasticsearch.plugins.NetworkPlugin) BigArrays(org.elasticsearch.common.util.BigArrays) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Supplier(java.util.function.Supplier) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) Transport(org.elasticsearch.transport.Transport) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) Settings(org.elasticsearch.common.settings.Settings)

Example 35 with ThreadPool

use of org.elasticsearch.threadpool.ThreadPool in project elasticsearch by elastic.

the class PrioritizedExecutorsTests method testTimeoutCleanup.

public void testTimeoutCleanup() throws Exception {
    ThreadPool threadPool = new TestThreadPool("test");
    final ScheduledThreadPoolExecutor timer = (ScheduledThreadPoolExecutor) threadPool.scheduler();
    final AtomicBoolean timeoutCalled = new AtomicBoolean();
    PrioritizedEsThreadPoolExecutor executor = EsExecutors.newSinglePrioritizing(getTestName(), EsExecutors.daemonThreadFactory(getTestName()), holder);
    final CountDownLatch invoked = new CountDownLatch(1);
    executor.execute(new Runnable() {

        @Override
        public void run() {
            invoked.countDown();
        }
    }, timer, TimeValue.timeValueHours(1), new Runnable() {

        @Override
        public void run() {
            // We should never get here
            timeoutCalled.set(true);
        }
    });
    invoked.await();
    // the timeout handler is added post execution (and quickly cancelled). We have allow for this
    // and use assert busy
    assertBusy(new Runnable() {

        @Override
        public void run() {
            assertThat(timer.getQueue().size(), equalTo(0));
        }
    }, 5, TimeUnit.SECONDS);
    assertThat(timeoutCalled.get(), equalTo(false));
    assertTrue(terminate(executor));
    assertTrue(terminate(threadPool));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ThreadPool (org.elasticsearch.threadpool.ThreadPool)74 Settings (org.elasticsearch.common.settings.Settings)48 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)45 TimeUnit (java.util.concurrent.TimeUnit)25 Before (org.junit.Before)25 IOException (java.io.IOException)22 TimeValue (org.elasticsearch.common.unit.TimeValue)22 Collections (java.util.Collections)21 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)21 ClusterState (org.elasticsearch.cluster.ClusterState)20 BigArrays (org.elasticsearch.common.util.BigArrays)20 TransportService (org.elasticsearch.transport.TransportService)19 List (java.util.List)18 ESTestCase (org.elasticsearch.test.ESTestCase)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 Version (org.elasticsearch.Version)17 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)17 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 ActionListener (org.elasticsearch.action.ActionListener)16 Arrays (java.util.Arrays)15