Search in sources :

Example 31 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project asynchronous-search by opensearch-project.

the class MixedOperationSingleNodeIT method assertConcurrentGetOrUpdatesWithDeletes.

private void assertConcurrentGetOrUpdatesWithDeletes(AsynchronousSearchResponse submitResponse, QuadConsumer<AtomicInteger, AtomicInteger, AtomicInteger, AtomicInteger> assertionConsumer, boolean update, int concurrentRuns, boolean retainResponse) throws InterruptedException {
    AtomicInteger numSuccess = new AtomicInteger();
    AtomicInteger numGetFailures = new AtomicInteger();
    AtomicInteger numVersionConflictFailures = new AtomicInteger();
    AtomicInteger numResourceNotFoundFailures = new AtomicInteger();
    AtomicInteger numTimeouts = new AtomicInteger();
    TestThreadPool testThreadPool = null;
    try {
        testThreadPool = new TestThreadPool(GetAsynchronousSearchSingleNodeIT.class.getName());
        int numThreads = concurrentRuns;
        // 5 hours in millis
        long lowerKeepAliveMillis = 5 * 1000 * 60 * 60;
        // 10 hours in millis
        long higherKeepAliveMillis = 10 * 1000 * 60 * 60;
        List<Runnable> operationThreads = new ArrayList<>();
        CountDownLatch countDownLatch = new CountDownLatch(numThreads);
        long randomDeleteThread = randomLongBetween(0, numThreads - 1);
        for (int i = 0; i < numThreads; i++) {
            long keepAlive = randomLongBetween(lowerKeepAliveMillis, higherKeepAliveMillis);
            int currentThreadIteration = i;
            Runnable thread = () -> {
                if (currentThreadIteration == randomDeleteThread) {
                    DeleteAsynchronousSearchRequest deleteAsynchronousSearchRequest = new DeleteAsynchronousSearchRequest(submitResponse.getId());
                    executeDeleteAsynchronousSearch(client(), deleteAsynchronousSearchRequest, new LatchedActionListener<>(new ActionListener<AcknowledgedResponse>() {

                        @Override
                        public void onResponse(AcknowledgedResponse acknowledgedResponse) {
                            assertTrue(acknowledgedResponse.isAcknowledged());
                            numSuccess.incrementAndGet();
                        }

                        @Override
                        public void onFailure(Exception e) {
                            if (e instanceof OpenSearchTimeoutException) {
                                numTimeouts.incrementAndGet();
                            } else {
                                fail("Unexpected exception " + e.getMessage());
                            }
                        }
                    }, countDownLatch));
                } else {
                    GetAsynchronousSearchRequest getAsynchronousSearchRequest = new GetAsynchronousSearchRequest(submitResponse.getId());
                    long requestedTime = System.currentTimeMillis() + keepAlive;
                    if (update) {
                        logger.info("Triggering asynchronous search gets with keep alive [{}] --->", requestedTime);
                        getAsynchronousSearchRequest.setKeepAlive(TimeValue.timeValueMillis(keepAlive));
                    }
                    getAsynchronousSearchRequest.setWaitForCompletionTimeout(TimeValue.timeValueMillis(randomLongBetween(1, 5000)));
                    executeGetAsynchronousSearch(client(), getAsynchronousSearchRequest, new LatchedActionListener<>(new ActionListener<AsynchronousSearchResponse>() {

                        @Override
                        public void onResponse(AsynchronousSearchResponse asResponse) {
                            if (update) {
                                assertThat(asResponse.getExpirationTimeMillis(), greaterThanOrEqualTo(System.currentTimeMillis() + lowerKeepAliveMillis));
                                assertThat(asResponse.getExpirationTimeMillis(), lessThanOrEqualTo(System.currentTimeMillis() + higherKeepAliveMillis));
                            }
                            numSuccess.incrementAndGet();
                        }

                        @Override
                        public void onFailure(Exception e) {
                            if (e instanceof VersionConflictEngineException) {
                                numVersionConflictFailures.incrementAndGet();
                            } else if (e instanceof ResourceNotFoundException) {
                                numResourceNotFoundFailures.incrementAndGet();
                            } else if (e instanceof OpenSearchTimeoutException) {
                                numTimeouts.incrementAndGet();
                            } else {
                                numGetFailures.incrementAndGet();
                            }
                        }
                    }, countDownLatch));
                }
            };
            operationThreads.add(thread);
        }
        TestThreadPool finalTestThreadPool = testThreadPool;
        operationThreads.forEach(runnable -> finalTestThreadPool.executor("generic").execute(runnable));
        countDownLatch.await();
        if (retainResponse && update) {
            assertEquals(numTimeouts.get(), 0);
            assertionConsumer.apply(numSuccess, numGetFailures, numVersionConflictFailures, numTimeouts);
        } else {
            assertionConsumer.apply(numSuccess, numGetFailures, numVersionConflictFailures, numResourceNotFoundFailures);
        }
    } finally {
        ThreadPool.terminate(testThreadPool, 500, TimeUnit.MILLISECONDS);
    }
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) ArrayList(java.util.ArrayList) AcknowledgedResponse(org.opensearch.search.asynchronous.response.AcknowledgedResponse) TestThreadPool(org.opensearch.threadpool.TestThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) LatchedActionListener(org.opensearch.action.LatchedActionListener) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GetAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest)

Example 32 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project asynchronous-search by opensearch-project.

the class SubmitAsynchronousSearchSingleNodeIT method assertConcurrentSubmits.

private void assertConcurrentSubmits(SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest, SearchResponse searchResponse, TriConsumer<AtomicInteger, AtomicInteger, AtomicInteger> assertionConsumer, int concurrentRuns) throws InterruptedException {
    AtomicInteger numStartedAsynchronousSearch = new AtomicInteger();
    AtomicInteger numFailedAsynchronousSearch = new AtomicInteger();
    AtomicInteger numErrorResponseAsynchronousSearch = new AtomicInteger();
    final ClusterState state = getInstanceFromNode(ClusterService.class).state();
    TestThreadPool testThreadPool = null;
    CountDownLatch countDownLatch;
    try {
        testThreadPool = new TestThreadPool(SubmitAsynchronousSearchSingleNodeIT.class.getName());
        int numThreads = concurrentRuns;
        List<Runnable> operationThreads = new ArrayList<>();
        if (submitAsynchronousSearchRequest.getKeepOnCompletion()) {
            // we also need to delete asynchronous search response to ensure test completes gracefully with no background tasks
            // running
            countDownLatch = new CountDownLatch(2 * numThreads);
        } else {
            countDownLatch = new CountDownLatch(numThreads);
        }
        for (int i = 0; i < numThreads; i++) {
            CountDownLatch finalCountDownLatch = countDownLatch;
            Runnable thread = () -> {
                logger.info("Triggering asynchronous search submit --->");
                executeSubmitAsynchronousSearch(client(), submitAsynchronousSearchRequest, new ActionListener<AsynchronousSearchResponse>() {

                    @Override
                    public void onResponse(AsynchronousSearchResponse asResponse) {
                        if (asResponse.getId() != null) {
                            AsynchronousSearchId asId = AsynchronousSearchIdConverter.parseAsyncId(asResponse.getId());
                            assertEquals(state.nodes().getLocalNodeId(), asId.getNode());
                            AsynchronousSearchAssertions.assertSearchResponses(searchResponse, asResponse.getSearchResponse());
                            numStartedAsynchronousSearch.incrementAndGet();
                        }
                        if (asResponse.getError() != null) {
                            numErrorResponseAsynchronousSearch.incrementAndGet();
                        }
                        finalCountDownLatch.countDown();
                        if (submitAsynchronousSearchRequest.getKeepOnCompletion()) {
                            DeleteAsynchronousSearchRequest deleteAsynchronousSearchRequest = new DeleteAsynchronousSearchRequest(asResponse.getId());
                            executeDeleteAsynchronousSearch(client(), deleteAsynchronousSearchRequest, new LatchedActionListener<>(new ActionListener<AcknowledgedResponse>() {

                                @Override
                                public void onResponse(AcknowledgedResponse acknowledgedResponse) {
                                    assertTrue(acknowledgedResponse.isAcknowledged());
                                }

                                @Override
                                public void onFailure(Exception e) {
                                    fail("Search deletion failed for asynchronous search id " + e.getMessage());
                                }
                            }, finalCountDownLatch));
                        }
                        ;
                    }

                    @Override
                    public void onFailure(Exception e) {
                        numFailedAsynchronousSearch.incrementAndGet();
                        finalCountDownLatch.countDown();
                    }
                });
            };
            operationThreads.add(thread);
        }
        TestThreadPool finalTestThreadPool = testThreadPool;
        operationThreads.forEach(runnable -> finalTestThreadPool.executor("generic").execute(runnable));
        countDownLatch.await();
        assertionConsumer.apply(numStartedAsynchronousSearch, numFailedAsynchronousSearch, numErrorResponseAsynchronousSearch);
    } finally {
        ThreadPool.terminate(testThreadPool, 500, TimeUnit.MILLISECONDS);
    }
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) ClusterState(org.opensearch.cluster.ClusterState) ArrayList(java.util.ArrayList) AcknowledgedResponse(org.opensearch.search.asynchronous.response.AcknowledgedResponse) TestThreadPool(org.opensearch.threadpool.TestThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) ClusterService(org.opensearch.cluster.service.ClusterService) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest)

Example 33 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project asynchronous-search by opensearch-project.

the class AsynchronousSearchRequestRoutingIT method testRequestForwardingToCoordinatorNodeForRunningAsynchronousSearch.

public void testRequestForwardingToCoordinatorNodeForRunningAsynchronousSearch() throws Exception {
    List<ScriptedBlockPlugin> plugins = initBlockFactory();
    String index = "idx";
    assertAcked(prepareCreate(index).addMapping("type", "ip", "type=ip", "ips", "type=ip"));
    waitForRelocation(ClusterHealthStatus.GREEN);
    indexRandom(true, client().prepareIndex(index).setId("1").setSource("ip", "192.168.1.7", "ips", Arrays.asList("192.168.0.13", "192.168.1.2")), client().prepareIndex(index).setId("2").setSource("ip", "192.168.1.10", "ips", Arrays.asList("192.168.1.25", "192.168.1.28")), client().prepareIndex(index).setId("3").setSource("ip", "2001:db8::ff00:42:8329", "ips", Arrays.asList("2001:db8::ff00:42:8329", "2001:db8::ff00:42:8380")));
    assertAcked(prepareCreate("idx_unmapped"));
    waitForRelocation(ClusterHealthStatus.GREEN);
    refresh();
    SearchRequest searchRequest = client().prepareSearch(index).setQuery(scriptQuery(new Script(ScriptType.INLINE, "mockscript", SCRIPT_NAME, Collections.emptyMap()))).request();
    SubmitAsynchronousSearchRequest request = new SubmitAsynchronousSearchRequest(searchRequest);
    request.keepOnCompletion(false);
    request.waitForCompletionTimeout(TimeValue.timeValueMillis(0));
    CountDownLatch latch = new CountDownLatch(1);
    client().execute(SubmitAsynchronousSearchAction.INSTANCE, request, new LatchedActionListener<>(new ActionListener<AsynchronousSearchResponse>() {

        @Override
        public void onResponse(AsynchronousSearchResponse submitResponse) {
            String id = submitResponse.getId();
            assertNotNull(id);
            assertEquals(AsynchronousSearchState.RUNNING, submitResponse.getState());
            AsynchronousSearchId asId = AsynchronousSearchIdConverter.parseAsyncId(id);
            ClusterService clusterService = internalCluster().getInstance(ClusterService.class);
            assertEquals(clusterService.state().nodes().getDataNodes().size(), 5);
            List<String> nonCoordinatorNodeNames = new LinkedList<>();
            clusterService.state().nodes().iterator().forEachRemaining(node -> {
                if (asId.getNode().equals(node.getId()) == false)
                    nonCoordinatorNodeNames.add(node.getName());
            });
            nonCoordinatorNodeNames.forEach(n -> {
                try {
                    AsynchronousSearchResponse getResponse = client(n).execute(GetAsynchronousSearchAction.INSTANCE, new GetAsynchronousSearchRequest(id)).get();
                    assertEquals(getResponse.getState(), AsynchronousSearchState.RUNNING);
                } catch (InterruptedException | ExecutionException e) {
                    fail("Get asynchronous search request should not have failed");
                }
            });
            String randomNonCoordinatorNode = nonCoordinatorNodeNames.get(randomInt(nonCoordinatorNodeNames.size() - 1));
            try {
                AcknowledgedResponse acknowledgedResponse = client(randomNonCoordinatorNode).execute(DeleteAsynchronousSearchAction.INSTANCE, new DeleteAsynchronousSearchRequest(id)).get();
                assertTrue(acknowledgedResponse.isAcknowledged());
                ExecutionException executionException = expectThrows(ExecutionException.class, () -> client().execute(GetAsynchronousSearchAction.INSTANCE, new GetAsynchronousSearchRequest(id)).get());
                assertThat(executionException.getMessage(), containsString("ResourceNotFoundException"));
            } catch (InterruptedException | ExecutionException e) {
                fail("Delete asynchronous search request from random non-coordinator node should have succeeded.");
            }
        }

        @Override
        public void onFailure(Exception e) {
            fail(e.getMessage());
        }
    }, latch));
    latch.await();
    disableBlocks(plugins);
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) SearchRequest(org.opensearch.action.search.SearchRequest) Script(org.opensearch.script.Script) AcknowledgedResponse(org.opensearch.search.asynchronous.response.AcknowledgedResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) ExecutionException(java.util.concurrent.ExecutionException) ClusterService(org.opensearch.cluster.service.ClusterService) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) ExecutionException(java.util.concurrent.ExecutionException)

Example 34 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project asynchronous-search by opensearch-project.

the class AsynchronousSearchServiceTests method testFindContextOnNonExistentSearch.

public void testFindContextOnNonExistentSearch() throws InterruptedException {
    DiscoveryNode discoveryNode = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
    ThreadPool testThreadPool = null;
    try {
        testThreadPool = new TestThreadPool(AsynchronousSearchPlugin.OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME, executorBuilder);
        ClusterService mockClusterService = ClusterServiceUtils.createClusterService(testThreadPool, discoveryNode, clusterSettings);
        FakeClient fakeClient = new FakeClient(testThreadPool);
        AsynchronousSearchActiveStore asActiveStore = new AsynchronousSearchActiveStore(mockClusterService);
        AsynchronousSearchPersistenceService persistenceService = new AsynchronousSearchPersistenceService(fakeClient, mockClusterService, testThreadPool);
        AsynchronousSearchService asService = new AsynchronousSearchService(persistenceService, asActiveStore, fakeClient, mockClusterService, testThreadPool, new InternalAsynchronousSearchStats(), new NamedWriteableRegistry(emptyList()));
        TimeValue keepAlive = timeValueHours(9);
        User user1 = TestClientUtils.randomUser();
        SearchRequest searchRequest = new SearchRequest();
        SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(searchRequest);
        submitAsynchronousSearchRequest.keepOnCompletion(false);
        submitAsynchronousSearchRequest.keepAlive(keepAlive);
        CountDownLatch findContextLatch = new CountDownLatch(2);
        ActionListener<AsynchronousSearchContext> failureExpectingListener = new LatchedActionListener<>(wrap(r -> fail(), e -> assertTrue(e instanceof ResourceNotFoundException)), findContextLatch);
        asService.findContext("nonExistentId", new AsynchronousSearchContextId(randomAlphaOfLength(10), randomNonNegativeLong()), null, failureExpectingListener);
        asService.findContext("nonExistentId", new AsynchronousSearchContextId(randomAlphaOfLength(10), randomNonNegativeLong()), user1, failureExpectingListener);
        findContextLatch.await();
    } finally {
        ThreadPool.terminate(testThreadPool, 200, TimeUnit.MILLISECONDS);
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) InternalAsynchronousSearchStats(org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats) ExecutorBuilder(org.opensearch.threadpool.ExecutorBuilder) AsynchronousSearchState(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Version(org.opensearch.Version) ActionRequest(org.opensearch.action.ActionRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LatchedActionListener(org.opensearch.action.LatchedActionListener) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ScalingExecutorBuilder(org.opensearch.threadpool.ScalingExecutorBuilder) ActionListener(org.opensearch.action.ActionListener) ActionResponse(org.opensearch.action.ActionResponse) ActionType(org.opensearch.action.ActionType) AsynchronousSearchContext(org.opensearch.search.asynchronous.context.AsynchronousSearchContext) TimeValue(org.opensearch.common.unit.TimeValue) SearchHit(org.opensearch.search.SearchHit) Collections.emptyList(java.util.Collections.emptyList) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) NoOpClient(org.opensearch.test.client.NoOpClient) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) AsynchronousSearchTask(org.opensearch.search.asynchronous.task.AsynchronousSearchTask) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) IndexAction(org.opensearch.action.index.IndexAction) ActionListener.wrap(org.opensearch.action.ActionListener.wrap) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) AsynchronousSearchActiveContext(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveContext) ClusterServiceUtils(org.opensearch.test.ClusterServiceUtils) SearchProfileShardResults(org.opensearch.search.profile.SearchProfileShardResults) SearchAction(org.opensearch.action.search.SearchAction) ThreadPool(org.opensearch.threadpool.ThreadPool) AsynchronousSearchProgressListener(org.opensearch.search.asynchronous.listener.AsynchronousSearchProgressListener) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) AsynchronousSearchPlugin(org.opensearch.search.asynchronous.plugin.AsynchronousSearchPlugin) TimeValue.timeValueHours(org.opensearch.common.unit.TimeValue.timeValueHours) SearchHits(org.opensearch.search.SearchHits) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) OpenSearchExecutors(org.opensearch.common.util.concurrent.OpenSearchExecutors) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ArrayList(java.util.ArrayList) SearchRequest(org.opensearch.action.search.SearchRequest) SearchResponse(org.opensearch.action.search.SearchResponse) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) Setting(org.opensearch.common.settings.Setting) TestUtils(org.opensearch.search.asynchronous.utils.TestUtils) TaskId(org.opensearch.tasks.TaskId) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) TotalHits(org.apache.lucene.search.TotalHits) TimeUnit(java.util.concurrent.TimeUnit) User(org.opensearch.commons.authuser.User) TestClientUtils(org.opensearch.search.asynchronous.utils.TestClientUtils) Suggest(org.opensearch.search.suggest.Suggest) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) ClusterService(org.opensearch.cluster.service.ClusterService) AsynchronousSearchActiveStore(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore) Assert(org.junit.Assert) Collections(java.util.Collections) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) User(org.opensearch.commons.authuser.User) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) AsynchronousSearchContext(org.opensearch.search.asynchronous.context.AsynchronousSearchContext) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadPool(org.opensearch.threadpool.ThreadPool) InternalAsynchronousSearchStats(org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats) TestThreadPool(org.opensearch.threadpool.TestThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousSearchActiveStore(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore) LatchedActionListener(org.opensearch.action.LatchedActionListener) ClusterService(org.opensearch.cluster.service.ClusterService) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) TimeValue(org.opensearch.common.unit.TimeValue)

Example 35 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project asynchronous-search by opensearch-project.

the class AsynchronousSearchPersistenceServiceIT method testCreateConcurrentDocsWhenIndexNotExists.

public void testCreateConcurrentDocsWhenIndexNotExists() throws InterruptedException, IOException, ExecutionException {
    AsynchronousSearchPersistenceService persistenceService = getInstanceFromNode(AsynchronousSearchPersistenceService.class);
    TransportService transportService = getInstanceFromNode(TransportService.class);
    SearchResponse searchResponse = client().search(new SearchRequest(TEST_INDEX)).get();
    AsynchronousSearchId asId1 = generateNewAsynchronousSearchId(transportService);
    AsynchronousSearchId asId2 = generateNewAsynchronousSearchId(transportService);
    AsynchronousSearchPersistenceModel model1 = new AsynchronousSearchPersistenceModel(System.currentTimeMillis(), System.currentTimeMillis() + new TimeValue(10, TimeUnit.DAYS).getMillis(), searchResponse, null, null);
    String id1 = AsynchronousSearchIdConverter.buildAsyncId(asId1);
    AsynchronousSearchPersistenceModel model2 = new AsynchronousSearchPersistenceModel(System.currentTimeMillis(), System.currentTimeMillis() + new TimeValue(10, TimeUnit.DAYS).getMillis(), searchResponse, null, null);
    String id2 = AsynchronousSearchIdConverter.buildAsyncId(asId2);
    CountDownLatch createLatch = new CountDownLatch(2);
    threadPool.generic().execute(() -> persistenceService.storeResponse(id1, model1, new LatchedActionListener<>(ActionListener.wrap(r -> assertSuccessfulResponseCreation(id1, r), e -> {
        logger.debug("expect successful create, got", e);
        fail("Expected successful create, got " + e.getMessage());
    }), createLatch)));
    threadPool.generic().execute(() -> persistenceService.storeResponse(id2, model2, new LatchedActionListener<>(ActionListener.wrap(r -> assertSuccessfulResponseCreation(id2, r), e -> {
        logger.debug("expect successful create, got", e);
        fail("Expected successful create, got " + e.getMessage());
    }), createLatch)));
    createLatch.await();
    CountDownLatch getLatch1 = new CountDownLatch(1);
    persistenceService.getResponse(id1, null, new LatchedActionListener<>(ActionListener.wrap((AsynchronousSearchPersistenceModel r) -> assertEquals(model1, r), e -> {
        logger.debug("expect successful get result, got", e);
        fail("Expected successful get result, got " + e.getMessage());
    }), getLatch1));
    getLatch1.await();
    CountDownLatch getLatch2 = new CountDownLatch(1);
    persistenceService.getResponse(id2, null, new LatchedActionListener<>(ActionListener.wrap((AsynchronousSearchPersistenceModel r) -> assertEquals(model2, r), e -> {
        logger.debug("expect successful create, got", e);
        fail("Expected successful create, got " + e.getMessage());
    }), getLatch2));
    getLatch2.await();
}
Also used : Arrays(java.util.Arrays) ConfigConstants(org.opensearch.commons.ConfigConstants) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexResponse(org.opensearch.action.index.IndexResponse) AsynchronousSearchState(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) GetAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest) AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ArrayList(java.util.ArrayList) AcknowledgedResponse(org.opensearch.search.asynchronous.response.AcknowledgedResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) UUIDs(org.opensearch.common.UUIDs) AsynchronousSearchPersistenceModel(org.opensearch.search.asynchronous.context.persistence.AsynchronousSearchPersistenceModel) TimeValue.timeValueDays(org.opensearch.common.unit.TimeValue.timeValueDays) AsynchronousSearchPersistenceService(org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService) TimeValue(org.opensearch.common.unit.TimeValue) Iterator(java.util.Iterator) AsynchronousSearchIdConverter(org.opensearch.search.asynchronous.id.AsynchronousSearchIdConverter) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) AsynchronousSearchSingleNodeTestCase(org.opensearch.search.asynchronous.commons.AsynchronousSearchSingleNodeTestCase) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest) TransportService(org.opensearch.transport.TransportService) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) User(org.opensearch.commons.authuser.User) TestClientUtils(org.opensearch.search.asynchronous.utils.TestClientUtils) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) DocWriteResponse(org.opensearch.action.DocWriteResponse) Assert(org.junit.Assert) GetAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) TransportService(org.opensearch.transport.TransportService) AsynchronousSearchPersistenceService(org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousSearchPersistenceModel(org.opensearch.search.asynchronous.context.persistence.AsynchronousSearchPersistenceModel) TimeValue(org.opensearch.common.unit.TimeValue) AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)63 LatchedActionListener (org.opensearch.action.LatchedActionListener)63 ActionListener (org.opensearch.action.ActionListener)57 IOException (java.io.IOException)43 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)38 OpenSearchException (org.opensearch.OpenSearchException)24 HashMap (java.util.HashMap)20 Map (java.util.Map)19 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)18 ArrayList (java.util.ArrayList)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15 Settings (org.opensearch.common.settings.Settings)15 TestThreadPool (org.opensearch.threadpool.TestThreadPool)14 List (java.util.List)12 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)12 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)11 CreateIndexResponse (org.opensearch.client.indices.CreateIndexResponse)11 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)10 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)10