Search in sources :

Example 1 with SearchPhaseResult

use of org.elasticsearch.search.SearchPhaseResult in project elasticsearch by elastic.

the class TransportSearchHelper method buildScrollId.

static String buildScrollId(AtomicArray<? extends SearchPhaseResult> searchPhaseResults) throws IOException {
    try (RAMOutputStream out = new RAMOutputStream()) {
        out.writeString(searchPhaseResults.length() == 1 ? ParsedScrollId.QUERY_AND_FETCH_TYPE : ParsedScrollId.QUERY_THEN_FETCH_TYPE);
        out.writeVInt(searchPhaseResults.asList().size());
        for (AtomicArray.Entry<? extends SearchPhaseResult> entry : searchPhaseResults.asList()) {
            SearchPhaseResult searchPhaseResult = entry.value;
            out.writeLong(searchPhaseResult.id());
            out.writeString(searchPhaseResult.shardTarget().getNodeId());
        }
        byte[] bytes = new byte[(int) out.getFilePointer()];
        out.writeTo(bytes, 0);
        return Base64.getUrlEncoder().encodeToString(bytes);
    }
}
Also used : AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) RAMOutputStream(org.apache.lucene.store.RAMOutputStream) SearchPhaseResult(org.elasticsearch.search.SearchPhaseResult)

Example 2 with SearchPhaseResult

use of org.elasticsearch.search.SearchPhaseResult in project elasticsearch by elastic.

the class CountedCollectorTests method testCollect.

public void testCollect() throws InterruptedException {
    AtomicArray<SearchPhaseResult> results = new AtomicArray<>(randomIntBetween(1, 100));
    List<Integer> state = new ArrayList<>();
    int numResultsExpected = randomIntBetween(1, results.length());
    MockSearchPhaseContext context = new MockSearchPhaseContext(results.length());
    CountDownLatch latch = new CountDownLatch(1);
    boolean maybeFork = randomBoolean();
    Executor executor = (runnable) -> {
        if (randomBoolean() && maybeFork) {
            new Thread(runnable).start();
        } else {
            runnable.run();
        }
    };
    CountedCollector<SearchPhaseResult> collector = new CountedCollector<>(results::set, numResultsExpected, latch::countDown, context);
    for (int i = 0; i < numResultsExpected; i++) {
        int shardID = i;
        switch(randomIntBetween(0, 2)) {
            case 0:
                state.add(0);
                executor.execute(() -> collector.countDown());
                break;
            case 1:
                state.add(1);
                executor.execute(() -> collector.onResult(shardID, new DfsSearchResult(shardID, null), new SearchShardTarget("foo", new Index("bar", "baz"), shardID)));
                break;
            case 2:
                state.add(2);
                executor.execute(() -> collector.onFailure(shardID, new SearchShardTarget("foo", new Index("bar", "baz"), shardID), new RuntimeException("boom")));
                break;
            default:
                fail("unknown state");
        }
    }
    latch.await();
    assertEquals(numResultsExpected, state.size());
    for (int i = 0; i < numResultsExpected; i++) {
        switch(state.get(i)) {
            case 0:
                assertNull(results.get(i));
                break;
            case 1:
                assertNotNull(results.get(i));
                assertEquals(i, results.get(i).id());
                break;
            case 2:
                final int shardId = i;
                assertEquals(1, context.failures.stream().filter(f -> f.shardId() == shardId).count());
                break;
            default:
                fail("unknown state");
        }
    }
    for (int i = numResultsExpected; i < results.length(); i++) {
        assertNull("index: " + i, results.get(i));
    }
}
Also used : SearchShardTarget(org.elasticsearch.search.SearchShardTarget) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) SearchPhaseResult(org.elasticsearch.search.SearchPhaseResult) Executor(java.util.concurrent.Executor) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) Index(org.elasticsearch.index.Index) ESTestCase(org.elasticsearch.test.ESTestCase) DfsSearchResult(org.elasticsearch.search.dfs.DfsSearchResult) ArrayList(java.util.ArrayList) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) DfsSearchResult(org.elasticsearch.search.dfs.DfsSearchResult) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) CountDownLatch(java.util.concurrent.CountDownLatch) Executor(java.util.concurrent.Executor) SearchPhaseResult(org.elasticsearch.search.SearchPhaseResult) SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Aggregations

AtomicArray (org.elasticsearch.common.util.concurrent.AtomicArray)2 SearchPhaseResult (org.elasticsearch.search.SearchPhaseResult)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executor (java.util.concurrent.Executor)1 RAMOutputStream (org.apache.lucene.store.RAMOutputStream)1 Index (org.elasticsearch.index.Index)1 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)1 DfsSearchResult (org.elasticsearch.search.dfs.DfsSearchResult)1 ESTestCase (org.elasticsearch.test.ESTestCase)1