Search in sources :

Example 91 with TestThreadPool

use of org.opensearch.threadpool.TestThreadPool in project asynchronous-search by opensearch-project.

the class AsynchronousSearchActiveContextTests method testProcessSearchCompletion.

public void testProcessSearchCompletion() throws InterruptedException {
    TestThreadPool threadPool = null;
    try {
        int writeThreadPoolSize = randomIntBetween(1, 2);
        int writeThreadPoolQueueSize = randomIntBetween(1, 2);
        Settings settings = Settings.builder().put("thread_pool." + AsynchronousSearchPlugin.OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME + ".size", writeThreadPoolSize).put("thread_pool." + AsynchronousSearchPlugin.OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME + ".queue_size", writeThreadPoolQueueSize).build();
        final int availableProcessors = OpenSearchExecutors.allocatedProcessors(settings);
        ScalingExecutorBuilder scalingExecutorBuilder = new ScalingExecutorBuilder(AsynchronousSearchPlugin.OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME, 1, Math.min(2 * availableProcessors, Math.max(128, 512)), TimeValue.timeValueMinutes(30));
        threadPool = new TestThreadPool("Tests", settings, scalingExecutorBuilder);
        String node = UUID.randomUUID().toString();
        AsynchronousSearchProgressListener asProgressListener = mockAsynchronousSearchProgressListener(threadPool);
        AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
        boolean keepOnCompletion = true;
        TimeValue keepAlive = TimeValue.timeValueDays(randomInt(100));
        AsynchronousSearchActiveContext context = new AsynchronousSearchActiveContext(asContextId, node, keepAlive, keepOnCompletion, threadPool, threadPool::absoluteTimeInMillis, asProgressListener, null, () -> true);
        if (randomBoolean()) {
            SearchResponse mockSearchResponse = getMockSearchResponse();
            try {
                context.processSearchResponse(mockSearchResponse);
            } catch (Exception ex) {
                fail("Unexpected exception " + ex);
            }
            if (mockSearchResponse.equals(context.getSearchResponse())) {
                assertNull(context.getSearchError());
            }
        } else {
            RuntimeException e = new RuntimeException(UUID.randomUUID().toString());
            try {
                context.processSearchFailure(e);
            } catch (Exception ex) {
                fail("Unexpected exception " + ex);
            }
            if (e.equals(context.getSearchError())) {
                assertNull(context.getSearchResponse());
            }
        }
    } finally {
        ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS);
    }
}
Also used : AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) Matchers.containsString(org.hamcrest.Matchers.containsString) TestThreadPool(org.opensearch.threadpool.TestThreadPool) AsynchronousSearchProgressListener(org.opensearch.search.asynchronous.listener.AsynchronousSearchProgressListener) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ScalingExecutorBuilder(org.opensearch.threadpool.ScalingExecutorBuilder) Settings(org.opensearch.common.settings.Settings) TimeValue(org.opensearch.common.unit.TimeValue)

Example 92 with TestThreadPool

use of org.opensearch.threadpool.TestThreadPool in project asynchronous-search by opensearch-project.

the class AsynchronousSearchActiveStoreTests method testContextFoundWithContextIdMismatch.

public void testContextFoundWithContextIdMismatch() {
    DiscoveryNode node = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
    ThreadPool testThreadPool = null;
    try {
        testThreadPool = new TestThreadPool(this.getClass().getName(), executorBuilder);
        ClusterService mockClusterService = ClusterServiceUtils.createClusterService(testThreadPool, node, clusterSettings);
        AsynchronousSearchActiveStore activeStore = new AsynchronousSearchActiveStore(mockClusterService);
        long id = randomNonNegativeLong();
        AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), id);
        assertFalse(activeStore.freeContext(new AsynchronousSearchContextId(UUID.randomUUID().toString(), id)));
        assertEquals(activeStore.getAllContexts().size(), 0);
    } finally {
        ThreadPool.terminate(testThreadPool, 10, TimeUnit.SECONDS);
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterService(org.opensearch.cluster.service.ClusterService) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool)

Example 93 with TestThreadPool

use of org.opensearch.threadpool.TestThreadPool in project asynchronous-search by opensearch-project.

the class AsynchronousSearchActiveStoreTests method testFreeNonExistentContext.

public void testFreeNonExistentContext() {
    DiscoveryNode node = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
    ThreadPool testThreadPool = null;
    try {
        testThreadPool = new TestThreadPool(this.getClass().getName(), executorBuilder);
        ClusterService mockClusterService = ClusterServiceUtils.createClusterService(testThreadPool, node, clusterSettings);
        AsynchronousSearchActiveStore activeStore = new AsynchronousSearchActiveStore(mockClusterService);
        AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
        assertFalse(activeStore.freeContext(asContextId));
        assertEquals(activeStore.getAllContexts().size(), 0);
    } finally {
        ThreadPool.terminate(testThreadPool, 10, TimeUnit.SECONDS);
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterService(org.opensearch.cluster.service.ClusterService) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool)

Example 94 with TestThreadPool

use of org.opensearch.threadpool.TestThreadPool in project asynchronous-search by opensearch-project.

the class AsynchronousSearchActiveStoreTests method testPutContextRejection.

public void testPutContextRejection() throws InterruptedException, BrokenBarrierException, TimeoutException {
    DiscoveryNode node = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
    ThreadPool testThreadPool = null;
    try {
        testThreadPool = new TestThreadPool(this.getClass().getName(), executorBuilder);
        AtomicInteger runningContexts = new AtomicInteger();
        ClusterService mockClusterService = ClusterServiceUtils.createClusterService(testThreadPool, node, clusterSettings);
        AsynchronousSearchActiveStore activeStore = new AsynchronousSearchActiveStore(mockClusterService);
        List<Runnable> runnables = new ArrayList<>();
        AtomicInteger numRejected = new AtomicInteger();
        AtomicInteger numFailures = new AtomicInteger();
        AtomicInteger numSuccesses = new AtomicInteger();
        int numThreads = maxRunningContexts + 1;
        CyclicBarrier barrier = new CyclicBarrier(numThreads + 1);
        CountDownLatch assertsLatch = new CountDownLatch(1);
        for (int i = 0; i < numThreads; i++) {
            ThreadPool finalTestThreadPool1 = testThreadPool;
            runnables.add(() -> {
                try {
                    AsynchronousSearchProgressListener asProgressListener = mockAsynchronousSearchProgressListener(finalTestThreadPool1);
                    AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), runningContexts.incrementAndGet());
                    boolean keepOnCompletion = randomBoolean();
                    TimeValue keepAlive = TimeValue.timeValueDays(randomInt(100));
                    AsynchronousSearchContextEventListener asContextEventListener = new AsynchronousSearchContextEventListener() {
                    };
                    AsynchronousSearchActiveContext context = new AsynchronousSearchActiveContext(asContextId, node.getId(), keepAlive, keepOnCompletion, finalTestThreadPool1, finalTestThreadPool1::absoluteTimeInMillis, asProgressListener, null, () -> true);
                    activeStore.putContext(asContextId, context, asContextEventListener::onContextRejected);
                    numSuccesses.getAndIncrement();
                    Optional<AsynchronousSearchActiveContext> optional = activeStore.getContext(asContextId);
                    assert (optional.isPresent());
                    assertEquals(optional.get(), context);
                    barrier.await(5, TimeUnit.SECONDS);
                    assertsLatch.await();
                    activeStore.freeContext(context.getContextId());
                    assertFalse(activeStore.getContext(context.getContextId()).isPresent());
                    barrier.await();
                } catch (OpenSearchRejectedExecutionException e) {
                    numRejected.getAndIncrement();
                    try {
                        barrier.await();
                        barrier.await();
                    } catch (InterruptedException | BrokenBarrierException ex) {
                        numFailures.getAndIncrement();
                    }
                } catch (InterruptedException | BrokenBarrierException e) {
                    numFailures.getAndIncrement();
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                    numFailures.getAndIncrement();
                }
            });
        }
        ThreadPool finalTestThreadPool = testThreadPool;
        runnables.forEach(r -> finalTestThreadPool.generic().execute(r));
        // create contexts
        barrier.await(5, TimeUnit.SECONDS);
        assertEquals(activeStore.getAllContexts().size(), maxRunningContexts);
        assertEquals(numFailures.get(), 0);
        assertEquals(numRejected.get(), 1);
        assertEquals(numSuccesses.get(), maxRunningContexts);
        assertsLatch.countDown();
        // free contexts
        barrier.await(5, TimeUnit.SECONDS);
        assertEquals(activeStore.getAllContexts().size(), 0);
        assertEquals(numFailures.get(), 0);
    } finally {
        ThreadPool.terminate(testThreadPool, 10, TimeUnit.SECONDS);
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ArrayList(java.util.ArrayList) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) TestThreadPool(org.opensearch.threadpool.TestThreadPool) TimeValue(org.opensearch.common.unit.TimeValue) AsynchronousSearchContextEventListener(org.opensearch.search.asynchronous.listener.AsynchronousSearchContextEventListener) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) TimeoutException(java.util.concurrent.TimeoutException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AsynchronousSearchProgressListener(org.opensearch.search.asynchronous.listener.AsynchronousSearchProgressListener) AsynchronousSearchTestCase.mockAsynchronousSearchProgressListener(org.opensearch.search.asynchronous.commons.AsynchronousSearchTestCase.mockAsynchronousSearchProgressListener) ClusterService(org.opensearch.cluster.service.ClusterService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 95 with TestThreadPool

use of org.opensearch.threadpool.TestThreadPool in project OpenSearch by opensearch-project.

the class Netty4HttpServerTransportTests method setup.

@Before
public void setup() throws Exception {
    networkService = new NetworkService(Collections.emptyList());
    threadPool = new TestThreadPool("test");
    bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
    clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) NetworkService(org.opensearch.common.network.NetworkService) MockBigArrays(org.opensearch.common.util.MockBigArrays) TestThreadPool(org.opensearch.threadpool.TestThreadPool) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) Before(org.junit.Before)

Aggregations

TestThreadPool (org.opensearch.threadpool.TestThreadPool)151 ThreadPool (org.opensearch.threadpool.ThreadPool)79 ClusterService (org.opensearch.cluster.service.ClusterService)55 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)52 CountDownLatch (java.util.concurrent.CountDownLatch)50 TimeValue (org.opensearch.common.unit.TimeValue)43 Settings (org.opensearch.common.settings.Settings)41 Before (org.junit.Before)40 SearchRequest (org.opensearch.action.search.SearchRequest)37 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)33 SubmitAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest)32 AsynchronousSearchActiveStore (org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore)31 InternalAsynchronousSearchStats (org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats)30 ArrayList (java.util.ArrayList)27 AsynchronousSearchContextId (org.opensearch.search.asynchronous.context.AsynchronousSearchContextId)24 AsynchronousSearchTask (org.opensearch.search.asynchronous.task.AsynchronousSearchTask)24 User (org.opensearch.commons.authuser.User)22 AsynchronousSearchActiveContext (org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveContext)21 TimeUnit (java.util.concurrent.TimeUnit)20 AsynchronousSearchProgressListener (org.opensearch.search.asynchronous.listener.AsynchronousSearchProgressListener)20