Search in sources :

Example 1 with AsynchronousSearchStats

use of org.opensearch.search.asynchronous.stats.AsynchronousSearchStats in project asynchronous-search by opensearch-project.

the class AsynchronousSearchStatsIT method testStatsAcrossNodes.

@TestLogging(value = "_root:DEBUG", reason = "flaky")
public void testStatsAcrossNodes() throws InterruptedException, ExecutionException {
    TestThreadPool threadPool = null;
    try {
        threadPool = new TestThreadPool(AsynchronousSearchStatsIT.class.getName());
        String index = "idx";
        createIndex(index);
        indexRandom(super.ignoreExternalCluster(), client().prepareIndex(index).setId("1").setSource("field1", "the quick brown fox jumps"), client().prepareIndex(index).setId("2").setSource("field1", "quick brown"), client().prepareIndex(index).setId("3").setSource("field1", "quick"));
        List<DiscoveryNode> dataNodes = new LinkedList<>();
        clusterService().state().nodes().getDataNodes().iterator().forEachRemaining(node -> {
            dataNodes.add(node.value);
        });
        assertFalse(dataNodes.isEmpty());
        int numThreads = 20;
        List<Runnable> threads = new ArrayList<>();
        AtomicLong expectedNumSuccesses = new AtomicLong();
        AtomicLong expectedNumFailures = new AtomicLong();
        AtomicLong expectedNumPersisted = new AtomicLong();
        CountDownLatch latch = new CountDownLatch(numThreads);
        for (int i = 0; i < numThreads; i++) {
            threads.add(() -> {
                try {
                    boolean success = randomBoolean();
                    boolean keepOnCompletion = randomBoolean();
                    if (keepOnCompletion) {
                        expectedNumPersisted.getAndIncrement();
                    }
                    SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest;
                    if (success) {
                        expectedNumSuccesses.getAndIncrement();
                        submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(new SearchRequest(index));
                        submitAsynchronousSearchRequest.waitForCompletionTimeout(TimeValue.timeValueSeconds(2));
                        submitAsynchronousSearchRequest.keepOnCompletion(keepOnCompletion);
                    } else {
                        expectedNumFailures.getAndIncrement();
                        submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(new SearchRequest("non_existent_index"));
                        submitAsynchronousSearchRequest.keepOnCompletion(keepOnCompletion);
                    }
                    AsynchronousSearchResponse asResponse = executeSubmitAsynchronousSearch(client(dataNodes.get(randomInt(1)).getName()), submitAsynchronousSearchRequest);
                    if (keepOnCompletion) {
                        TestClientUtils.assertResponsePersistence(client(), asResponse.getId());
                    }
                } catch (Exception e) {
                    fail(e.getMessage());
                } finally {
                    latch.countDown();
                }
            });
        }
        TestThreadPool finalThreadPool = threadPool;
        threads.forEach(t -> finalThreadPool.generic().execute(t));
        latch.await();
        AsynchronousSearchStatsResponse statsResponse = client().execute(AsynchronousSearchStatsAction.INSTANCE, new AsynchronousSearchStatsRequest()).get();
        AtomicLong actualNumSuccesses = new AtomicLong();
        AtomicLong actualNumFailures = new AtomicLong();
        AtomicLong actualNumPersisted = new AtomicLong();
        for (AsynchronousSearchStats node : statsResponse.getNodes()) {
            AsynchronousSearchCountStats asCountStats = node.getAsynchronousSearchCountStats();
            assertEquals(asCountStats.getRunningCount(), 0);
            assertThat(expectedNumSuccesses.get(), greaterThanOrEqualTo(asCountStats.getCompletedCount()));
            actualNumSuccesses.getAndAdd(asCountStats.getCompletedCount());
            assertThat(expectedNumFailures.get(), greaterThanOrEqualTo(asCountStats.getFailedCount()));
            actualNumFailures.getAndAdd(asCountStats.getFailedCount());
            assertThat(expectedNumPersisted.get(), greaterThanOrEqualTo(asCountStats.getPersistedCount()));
            actualNumPersisted.getAndAdd(asCountStats.getPersistedCount());
        }
        assertEquals(expectedNumPersisted.get(), actualNumPersisted.get());
        assertEquals(expectedNumFailures.get(), actualNumFailures.get());
        assertEquals(expectedNumSuccesses.get(), actualNumSuccesses.get());
        waitForAsyncSearchTasksToComplete();
    } finally {
        ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS);
    }
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) SearchRequest(org.opensearch.action.search.SearchRequest) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) AsynchronousSearchStats(org.opensearch.search.asynchronous.stats.AsynchronousSearchStats) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) AsynchronousSearchCountStats(org.opensearch.search.asynchronous.stats.AsynchronousSearchCountStats) TestThreadPool(org.opensearch.threadpool.TestThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousSearchStatsRequest(org.opensearch.search.asynchronous.request.AsynchronousSearchStatsRequest) LinkedList(java.util.LinkedList) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AsynchronousSearchStatsResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchStatsResponse) TestLogging(org.opensearch.test.junit.annotations.TestLogging)

Example 2 with AsynchronousSearchStats

use of org.opensearch.search.asynchronous.stats.AsynchronousSearchStats in project asynchronous-search by opensearch-project.

the class AsynchronousSearchStatsIT method testRunningAsynchronousSearchCountStat.

public void testRunningAsynchronousSearchCountStat() throws InterruptedException, ExecutionException {
    String index = "idx";
    createIndex(index);
    indexRandom(super.ignoreExternalCluster(), client().prepareIndex(index).setId("1").setSource("field1", "the quick brown fox jumps"), client().prepareIndex(index).setId("2").setSource("field1", "quick brown"), client().prepareIndex(index).setId("3").setSource("field1", "quick"));
    List<ScriptedBlockPlugin> plugins = initBlockFactory();
    SearchRequest searchRequest = client().prepareSearch(index).setQuery(scriptQuery(new Script(ScriptType.INLINE, "mockscript", SCRIPT_NAME, Collections.emptyMap()))).request();
    SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(searchRequest);
    submitAsynchronousSearchRequest.keepOnCompletion(true);
    AsynchronousSearchResponse asResponse = executeSubmitAsynchronousSearch(client(), submitAsynchronousSearchRequest);
    AsynchronousSearchStatsResponse statsResponse = client().execute(AsynchronousSearchStatsAction.INSTANCE, new AsynchronousSearchStatsRequest()).get();
    long runningSearchCount = 0;
    for (AsynchronousSearchStats node : statsResponse.getNodes()) {
        runningSearchCount += node.getAsynchronousSearchCountStats().getRunningCount();
        assertEquals(node.getAsynchronousSearchCountStats().getCompletedCount(), 0L);
        assertEquals(node.getAsynchronousSearchCountStats().getFailedCount(), 0L);
        assertEquals(node.getAsynchronousSearchCountStats().getPersistedCount(), 0L);
    }
    assertEquals(runningSearchCount, 1L);
    disableBlocks(plugins);
    TestClientUtils.assertResponsePersistence(client(), asResponse.getId());
    statsResponse = client().execute(AsynchronousSearchStatsAction.INSTANCE, new AsynchronousSearchStatsRequest()).get();
    long persistedCount = 0;
    long completedCount = 0;
    for (AsynchronousSearchStats node : statsResponse.getNodes()) {
        persistedCount += node.getAsynchronousSearchCountStats().getPersistedCount();
        completedCount += node.getAsynchronousSearchCountStats().getCompletedCount();
        assertEquals(node.getAsynchronousSearchCountStats().getRunningCount(), 0L);
        assertEquals(node.getAsynchronousSearchCountStats().getFailedCount(), 0L);
    }
    assertEquals(runningSearchCount, 1L);
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) SearchRequest(org.opensearch.action.search.SearchRequest) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) Script(org.opensearch.script.Script) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) AsynchronousSearchStatsResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchStatsResponse) AsynchronousSearchStats(org.opensearch.search.asynchronous.stats.AsynchronousSearchStats) Matchers.containsString(org.hamcrest.Matchers.containsString) AsynchronousSearchStatsRequest(org.opensearch.search.asynchronous.request.AsynchronousSearchStatsRequest)

Example 3 with AsynchronousSearchStats

use of org.opensearch.search.asynchronous.stats.AsynchronousSearchStats in project asynchronous-search by opensearch-project.

the class AsynchronousSearchStatsResponse method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject("nodes");
    for (AsynchronousSearchStats stats : getNodes()) {
        builder.startObject(stats.getNode().getId());
        stats.toXContent(builder, params);
        builder.endObject();
    }
    builder.endObject();
    return builder;
}
Also used : AsynchronousSearchStats(org.opensearch.search.asynchronous.stats.AsynchronousSearchStats)

Aggregations

AsynchronousSearchStats (org.opensearch.search.asynchronous.stats.AsynchronousSearchStats)3 Matchers.containsString (org.hamcrest.Matchers.containsString)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 AsynchronousSearchStatsRequest (org.opensearch.search.asynchronous.request.AsynchronousSearchStatsRequest)2 SubmitAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest)2 AsynchronousSearchResponse (org.opensearch.search.asynchronous.response.AsynchronousSearchResponse)2 AsynchronousSearchStatsResponse (org.opensearch.search.asynchronous.response.AsynchronousSearchStatsResponse)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)1 Script (org.opensearch.script.Script)1 AsynchronousSearchCountStats (org.opensearch.search.asynchronous.stats.AsynchronousSearchCountStats)1 TestLogging (org.opensearch.test.junit.annotations.TestLogging)1 TestThreadPool (org.opensearch.threadpool.TestThreadPool)1