Search in sources :

Example 11 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class ClearScrollControllerTests method testClearScrollIds.

public void testClearScrollIds() throws IOException, InterruptedException {
    DiscoveryNode node1 = new DiscoveryNode("node_1", buildNewFakeTransportAddress(), Version.CURRENT);
    DiscoveryNode node2 = new DiscoveryNode("node_2", buildNewFakeTransportAddress(), Version.CURRENT);
    DiscoveryNode node3 = new DiscoveryNode("node_3", buildNewFakeTransportAddress(), Version.CURRENT);
    AtomicArray<SearchPhaseResult> array = new AtomicArray<>(3);
    SearchAsyncActionTests.TestSearchPhaseResult testSearchPhaseResult1 = new SearchAsyncActionTests.TestSearchPhaseResult(new ShardSearchContextId(UUIDs.randomBase64UUID(), 1), node1);
    testSearchPhaseResult1.setSearchShardTarget(new SearchShardTarget("node_1", new ShardId("idx", "uuid1", 2), null, null));
    SearchAsyncActionTests.TestSearchPhaseResult testSearchPhaseResult2 = new SearchAsyncActionTests.TestSearchPhaseResult(new ShardSearchContextId(UUIDs.randomBase64UUID(), 12), node2);
    testSearchPhaseResult2.setSearchShardTarget(new SearchShardTarget("node_2", new ShardId("idy", "uuid2", 42), null, null));
    SearchAsyncActionTests.TestSearchPhaseResult testSearchPhaseResult3 = new SearchAsyncActionTests.TestSearchPhaseResult(new ShardSearchContextId(UUIDs.randomBase64UUID(), 42), node3);
    testSearchPhaseResult3.setSearchShardTarget(new SearchShardTarget("node_3", new ShardId("idy", "uuid2", 43), null, null));
    array.setOnce(0, testSearchPhaseResult1);
    array.setOnce(1, testSearchPhaseResult2);
    array.setOnce(2, testSearchPhaseResult3);
    AtomicInteger numFreed = new AtomicInteger(0);
    String scrollId = TransportSearchHelper.buildScrollId(array, VersionUtils.randomVersion(random()));
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(node1).add(node2).add(node3).build();
    CountDownLatch latch = new CountDownLatch(1);
    ActionListener<ClearScrollResponse> listener = new LatchedActionListener<>(new ActionListener<ClearScrollResponse>() {

        @Override
        public void onResponse(ClearScrollResponse clearScrollResponse) {
            assertEquals(numFreed.get(), clearScrollResponse.getNumFreed());
            assertTrue(clearScrollResponse.isSucceeded());
        }

        @Override
        public void onFailure(Exception e) {
            throw new AssertionError(e);
        }
    }, latch);
    List<DiscoveryNode> nodesInvoked = new CopyOnWriteArrayList<>();
    SearchTransportService searchTransportService = new SearchTransportService(null, null) {

        @Override
        public void sendFreeContext(Transport.Connection connection, ShardSearchContextId contextId, ActionListener<SearchFreeContextResponse> listener) {
            nodesInvoked.add(connection.getNode());
            boolean freed = randomBoolean();
            if (freed) {
                numFreed.incrementAndGet();
            }
            Thread t = new Thread(() -> listener.onResponse(new SearchFreeContextResponse(freed)));
            t.start();
        }

        @Override
        public Transport.Connection getConnection(String clusterAlias, DiscoveryNode node) {
            return new SearchAsyncActionTests.MockConnection(node);
        }
    };
    ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
    clearScrollRequest.scrollIds(Arrays.asList(scrollId));
    ClearScrollController controller = new ClearScrollController(clearScrollRequest, listener, nodes, logger, searchTransportService);
    controller.run();
    latch.await();
    assertEquals(3, nodesInvoked.size());
    Collections.sort(nodesInvoked, Comparator.comparing(DiscoveryNode::getId));
    assertEquals(nodesInvoked, Arrays.asList(node1, node2, node3));
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) ShardId(org.opensearch.index.shard.ShardId) LatchedActionListener(org.opensearch.action.LatchedActionListener) SearchPhaseResult(org.opensearch.search.SearchPhaseResult) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) CountDownLatch(java.util.concurrent.CountDownLatch) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) IOException(java.io.IOException) ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SearchShardTarget(org.opensearch.search.SearchShardTarget) Transport(org.opensearch.transport.Transport) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 12 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class TransportSearchActionTests method testCollectSearchShards.

public void testCollectSearchShards() throws Exception {
    int numClusters = randomIntBetween(2, 10);
    DiscoveryNode[] nodes = new DiscoveryNode[numClusters];
    Map<String, OriginalIndices> remoteIndicesByCluster = new HashMap<>();
    Settings.Builder builder = Settings.builder();
    MockTransportService[] mockTransportServices = startTransport(numClusters, nodes, remoteIndicesByCluster, builder);
    Settings settings = builder.build();
    try (MockTransportService service = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null)) {
        service.start();
        service.acceptIncomingRequests();
        RemoteClusterService remoteClusterService = service.getRemoteClusterService();
        {
            final CountDownLatch latch = new CountDownLatch(1);
            AtomicReference<Map<String, ClusterSearchShardsResponse>> response = new AtomicReference<>();
            AtomicInteger skippedClusters = new AtomicInteger();
            TransportSearchAction.collectSearchShards(IndicesOptions.lenientExpandOpen(), null, null, skippedClusters, remoteIndicesByCluster, remoteClusterService, threadPool, new LatchedActionListener<>(ActionListener.wrap(response::set, e -> fail("no failures expected")), latch));
            awaitLatch(latch, 5, TimeUnit.SECONDS);
            assertEquals(0, skippedClusters.get());
            assertNotNull(response.get());
            Map<String, ClusterSearchShardsResponse> map = response.get();
            assertEquals(numClusters, map.size());
            for (int i = 0; i < numClusters; i++) {
                String clusterAlias = "remote" + i;
                assertTrue(map.containsKey(clusterAlias));
                ClusterSearchShardsResponse shardsResponse = map.get(clusterAlias);
                assertEquals(1, shardsResponse.getNodes().length);
            }
        }
        {
            final CountDownLatch latch = new CountDownLatch(1);
            AtomicReference<Exception> failure = new AtomicReference<>();
            AtomicInteger skippedClusters = new AtomicInteger(0);
            TransportSearchAction.collectSearchShards(IndicesOptions.lenientExpandOpen(), "index_not_found", null, skippedClusters, remoteIndicesByCluster, remoteClusterService, threadPool, new LatchedActionListener<>(ActionListener.wrap(r -> fail("no response expected"), failure::set), latch));
            awaitLatch(latch, 5, TimeUnit.SECONDS);
            assertEquals(0, skippedClusters.get());
            assertNotNull(failure.get());
            assertThat(failure.get(), instanceOf(RemoteTransportException.class));
            RemoteTransportException remoteTransportException = (RemoteTransportException) failure.get();
            assertEquals(RestStatus.NOT_FOUND, remoteTransportException.status());
        }
        int numDisconnectedClusters = randomIntBetween(1, numClusters);
        Set<DiscoveryNode> disconnectedNodes = new HashSet<>(numDisconnectedClusters);
        Set<Integer> disconnectedNodesIndices = new HashSet<>(numDisconnectedClusters);
        while (disconnectedNodes.size() < numDisconnectedClusters) {
            int i = randomIntBetween(0, numClusters - 1);
            if (disconnectedNodes.add(nodes[i])) {
                assertTrue(disconnectedNodesIndices.add(i));
            }
        }
        CountDownLatch disconnectedLatch = new CountDownLatch(numDisconnectedClusters);
        RemoteClusterServiceTests.addConnectionListener(remoteClusterService, new TransportConnectionListener() {

            @Override
            public void onNodeDisconnected(DiscoveryNode node, Transport.Connection connection) {
                if (disconnectedNodes.remove(node)) {
                    disconnectedLatch.countDown();
                }
            }
        });
        for (DiscoveryNode disconnectedNode : disconnectedNodes) {
            service.addFailToSendNoConnectRule(disconnectedNode.getAddress());
        }
        {
            final CountDownLatch latch = new CountDownLatch(1);
            AtomicInteger skippedClusters = new AtomicInteger(0);
            AtomicReference<Exception> failure = new AtomicReference<>();
            TransportSearchAction.collectSearchShards(IndicesOptions.lenientExpandOpen(), null, null, skippedClusters, remoteIndicesByCluster, remoteClusterService, threadPool, new LatchedActionListener<>(ActionListener.wrap(r -> fail("no response expected"), failure::set), latch));
            awaitLatch(latch, 5, TimeUnit.SECONDS);
            assertEquals(0, skippedClusters.get());
            assertNotNull(failure.get());
            assertThat(failure.get(), instanceOf(RemoteTransportException.class));
            assertThat(failure.get().getMessage(), containsString("error while communicating with remote cluster ["));
            assertThat(failure.get().getCause(), instanceOf(NodeDisconnectedException.class));
        }
        // setting skip_unavailable to true for all the disconnected clusters will make the request succeed again
        for (int i : disconnectedNodesIndices) {
            RemoteClusterServiceTests.updateSkipUnavailable(remoteClusterService, "remote" + i, true);
        }
        {
            final CountDownLatch latch = new CountDownLatch(1);
            AtomicInteger skippedClusters = new AtomicInteger(0);
            AtomicReference<Map<String, ClusterSearchShardsResponse>> response = new AtomicReference<>();
            TransportSearchAction.collectSearchShards(IndicesOptions.lenientExpandOpen(), null, null, skippedClusters, remoteIndicesByCluster, remoteClusterService, threadPool, new LatchedActionListener<>(ActionListener.wrap(response::set, e -> fail("no failures expected")), latch));
            awaitLatch(latch, 5, TimeUnit.SECONDS);
            assertNotNull(response.get());
            Map<String, ClusterSearchShardsResponse> map = response.get();
            assertEquals(numClusters - disconnectedNodesIndices.size(), map.size());
            assertEquals(skippedClusters.get(), disconnectedNodesIndices.size());
            for (int i = 0; i < numClusters; i++) {
                String clusterAlias = "remote" + i;
                if (disconnectedNodesIndices.contains(i)) {
                    assertFalse(map.containsKey(clusterAlias));
                } else {
                    assertNotNull(map.get(clusterAlias));
                }
            }
        }
        // give transport service enough time to realize that the node is down, and to notify the connection listeners
        // so that RemoteClusterConnection is left with no connected nodes, hence it will retry connecting next
        assertTrue(disconnectedLatch.await(5, TimeUnit.SECONDS));
        service.clearAllRules();
        if (randomBoolean()) {
            for (int i : disconnectedNodesIndices) {
                if (randomBoolean()) {
                    RemoteClusterServiceTests.updateSkipUnavailable(remoteClusterService, "remote" + i, true);
                }
            }
        }
        {
            final CountDownLatch latch = new CountDownLatch(1);
            AtomicInteger skippedClusters = new AtomicInteger(0);
            AtomicReference<Map<String, ClusterSearchShardsResponse>> response = new AtomicReference<>();
            TransportSearchAction.collectSearchShards(IndicesOptions.lenientExpandOpen(), null, null, skippedClusters, remoteIndicesByCluster, remoteClusterService, threadPool, new LatchedActionListener<>(ActionListener.wrap(response::set, e -> fail("no failures expected")), latch));
            awaitLatch(latch, 5, TimeUnit.SECONDS);
            assertEquals(0, skippedClusters.get());
            assertNotNull(response.get());
            Map<String, ClusterSearchShardsResponse> map = response.get();
            assertEquals(numClusters, map.size());
            for (int i = 0; i < numClusters; i++) {
                String clusterAlias = "remote" + i;
                assertTrue(map.containsKey(clusterAlias));
                assertNotNull(map.get(clusterAlias));
            }
        }
        assertEquals(0, service.getConnectionManager().size());
    } finally {
        for (MockTransportService mockTransportService : mockTransportServices) {
            mockTransportService.close();
        }
    }
}
Also used : ClusterSearchShardsResponse(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse) Arrays(java.util.Arrays) SearchContext(org.opensearch.search.internal.SearchContext) BiFunction(java.util.function.BiFunction) TestThreadPool(org.opensearch.threadpool.TestThreadPool) SortBuilders(org.opensearch.search.sort.SortBuilders) Version(org.opensearch.Version) ClusterSearchShardsResponse(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Strings(org.opensearch.common.Strings) Transport(org.opensearch.transport.Transport) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LatchedActionListener(org.opensearch.action.LatchedActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) AliasFilter(org.opensearch.search.internal.AliasFilter) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) Scroll(org.opensearch.search.Scroll) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) OpenSearchAssertions.awaitLatch(org.opensearch.test.hamcrest.OpenSearchAssertions.awaitLatch) InternalAggregationTestCase.emptyReduceContextBuilder(org.opensearch.test.InternalAggregationTestCase.emptyReduceContextBuilder) Index(org.opensearch.index.Index) SearchHit(org.opensearch.search.SearchHit) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) RemoteTransportException(org.opensearch.transport.RemoteTransportException) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) TransportService(org.opensearch.transport.TransportService) OriginalIndices(org.opensearch.action.OriginalIndices) Tuple(org.opensearch.common.collect.Tuple) TransportAddress(org.opensearch.common.transport.TransportAddress) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) TransportException(org.opensearch.transport.TransportException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RemoteClusterServiceTests(org.opensearch.transport.RemoteClusterServiceTests) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ThreadPool(org.opensearch.threadpool.ThreadPool) GroupShardsIteratorTests(org.opensearch.cluster.routing.GroupShardsIteratorTests) HashMap(java.util.HashMap) SearchHits(org.opensearch.search.SearchHits) IndicesOptions(org.opensearch.action.support.IndicesOptions) MockTransportService(org.opensearch.test.transport.MockTransportService) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) QueryBuilders(org.opensearch.index.query.QueryBuilders) SetOnce(org.apache.lucene.util.SetOnce) RemoteClusterService(org.opensearch.transport.RemoteClusterService) TransportRequest(org.opensearch.transport.TransportRequest) TransportConnectionListener(org.opensearch.transport.TransportConnectionListener) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ClusterSearchShardsGroup(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsGroup) TotalHits(org.apache.lucene.search.TotalHits) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) TimeUnit(java.util.concurrent.TimeUnit) GroupShardsIterator(org.opensearch.cluster.routing.GroupShardsIterator) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) SearchShardTarget(org.opensearch.search.SearchShardTarget) RemoteClusterConnectionTests(org.opensearch.transport.RemoteClusterConnectionTests) ClusterName(org.opensearch.cluster.ClusterName) OriginalIndicesTests(org.opensearch.action.OriginalIndicesTests) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) Collections(java.util.Collections) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) HashMap(java.util.HashMap) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TransportConnectionListener(org.opensearch.transport.TransportConnectionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) Settings(org.opensearch.common.settings.Settings) HashSet(java.util.HashSet) RemoteTransportException(org.opensearch.transport.RemoteTransportException) RemoteClusterService(org.opensearch.transport.RemoteClusterService) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transport(org.opensearch.transport.Transport) Map(java.util.Map) HashMap(java.util.HashMap) OriginalIndices(org.opensearch.action.OriginalIndices)

Example 13 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class TransportSearchActionTests method testCCSRemoteReduceMergeFails.

public void testCCSRemoteReduceMergeFails() throws Exception {
    int numClusters = randomIntBetween(2, 10);
    DiscoveryNode[] nodes = new DiscoveryNode[numClusters];
    Map<String, OriginalIndices> remoteIndicesByCluster = new HashMap<>();
    Settings.Builder builder = Settings.builder();
    MockTransportService[] mockTransportServices = startTransport(numClusters, nodes, remoteIndicesByCluster, builder);
    Settings settings = builder.build();
    boolean local = randomBoolean();
    OriginalIndices localIndices = local ? new OriginalIndices(new String[] { "index" }, SearchRequest.DEFAULT_INDICES_OPTIONS) : null;
    TransportSearchAction.SearchTimeProvider timeProvider = new TransportSearchAction.SearchTimeProvider(0, 0, () -> 0);
    Function<Boolean, InternalAggregation.ReduceContext> reduceContext = finalReduce -> null;
    try (MockTransportService service = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null)) {
        service.start();
        service.acceptIncomingRequests();
        RemoteClusterService remoteClusterService = service.getRemoteClusterService();
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.preference("null_target");
        final CountDownLatch latch = new CountDownLatch(1);
        SetOnce<Tuple<SearchRequest, ActionListener<SearchResponse>>> setOnce = new SetOnce<>();
        AtomicReference<Exception> failure = new AtomicReference<>();
        LatchedActionListener<SearchResponse> listener = new LatchedActionListener<>(ActionListener.wrap(r -> fail("no response expected"), failure::set), latch);
        TransportSearchAction.ccsRemoteReduce(searchRequest, localIndices, remoteIndicesByCluster, timeProvider, emptyReduceContextBuilder(), remoteClusterService, threadPool, listener, (r, l) -> setOnce.set(Tuple.tuple(r, l)));
        if (localIndices == null) {
            assertNull(setOnce.get());
        } else {
            Tuple<SearchRequest, ActionListener<SearchResponse>> tuple = setOnce.get();
            assertEquals("", tuple.v1().getLocalClusterAlias());
            assertThat(tuple.v2(), instanceOf(TransportSearchAction.CCSActionListener.class));
            tuple.v2().onResponse(emptySearchResponse());
        }
        awaitLatch(latch, 5, TimeUnit.SECONDS);
        assertNotNull(failure.get());
        // the intention here is not to test that we throw NPE, rather to trigger a situation that makes
        // SearchResponseMerger#getMergedResponse fail unexpectedly and verify that the listener is properly notified with the NPE
        assertThat(failure.get(), instanceOf(NullPointerException.class));
        assertEquals(0, service.getConnectionManager().size());
    } finally {
        for (MockTransportService mockTransportService : mockTransportServices) {
            mockTransportService.close();
        }
    }
}
Also used : Arrays(java.util.Arrays) SearchContext(org.opensearch.search.internal.SearchContext) BiFunction(java.util.function.BiFunction) TestThreadPool(org.opensearch.threadpool.TestThreadPool) SortBuilders(org.opensearch.search.sort.SortBuilders) Version(org.opensearch.Version) ClusterSearchShardsResponse(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Strings(org.opensearch.common.Strings) Transport(org.opensearch.transport.Transport) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LatchedActionListener(org.opensearch.action.LatchedActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) AliasFilter(org.opensearch.search.internal.AliasFilter) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) Scroll(org.opensearch.search.Scroll) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) OpenSearchAssertions.awaitLatch(org.opensearch.test.hamcrest.OpenSearchAssertions.awaitLatch) InternalAggregationTestCase.emptyReduceContextBuilder(org.opensearch.test.InternalAggregationTestCase.emptyReduceContextBuilder) Index(org.opensearch.index.Index) SearchHit(org.opensearch.search.SearchHit) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) RemoteTransportException(org.opensearch.transport.RemoteTransportException) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) TransportService(org.opensearch.transport.TransportService) OriginalIndices(org.opensearch.action.OriginalIndices) Tuple(org.opensearch.common.collect.Tuple) TransportAddress(org.opensearch.common.transport.TransportAddress) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) TransportException(org.opensearch.transport.TransportException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RemoteClusterServiceTests(org.opensearch.transport.RemoteClusterServiceTests) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ThreadPool(org.opensearch.threadpool.ThreadPool) GroupShardsIteratorTests(org.opensearch.cluster.routing.GroupShardsIteratorTests) HashMap(java.util.HashMap) SearchHits(org.opensearch.search.SearchHits) IndicesOptions(org.opensearch.action.support.IndicesOptions) MockTransportService(org.opensearch.test.transport.MockTransportService) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) QueryBuilders(org.opensearch.index.query.QueryBuilders) SetOnce(org.apache.lucene.util.SetOnce) RemoteClusterService(org.opensearch.transport.RemoteClusterService) TransportRequest(org.opensearch.transport.TransportRequest) TransportConnectionListener(org.opensearch.transport.TransportConnectionListener) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ClusterSearchShardsGroup(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsGroup) TotalHits(org.apache.lucene.search.TotalHits) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) TimeUnit(java.util.concurrent.TimeUnit) GroupShardsIterator(org.opensearch.cluster.routing.GroupShardsIterator) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) SearchShardTarget(org.opensearch.search.SearchShardTarget) RemoteClusterConnectionTests(org.opensearch.transport.RemoteClusterConnectionTests) ClusterName(org.opensearch.cluster.ClusterName) OriginalIndicesTests(org.opensearch.action.OriginalIndicesTests) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) Collections(java.util.Collections) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) HashMap(java.util.HashMap) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LatchedActionListener(org.opensearch.action.LatchedActionListener) Settings(org.opensearch.common.settings.Settings) SetOnce(org.apache.lucene.util.SetOnce) RemoteClusterService(org.opensearch.transport.RemoteClusterService) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) RemoteTransportException(org.opensearch.transport.RemoteTransportException) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) TransportException(org.opensearch.transport.TransportException) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) OriginalIndices(org.opensearch.action.OriginalIndices) Tuple(org.opensearch.common.collect.Tuple)

Example 14 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class CRUDDocumentationIT method testUpdate.

@SuppressWarnings("unused")
public void testUpdate() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        IndexRequest indexRequest = new IndexRequest("posts").id("1").source("field", 0);
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        assertSame(RestStatus.CREATED, indexResponse.status());
        Request request = new Request("POST", "/_scripts/increment-field");
        request.setJsonEntity(Strings.toString(JsonXContent.contentBuilder().startObject().startObject("script").field("lang", "painless").field("source", "ctx._source.field += params.count").endObject().endObject()));
        Response response = client().performRequest(request);
        assertEquals(RestStatus.OK.getStatus(), response.getStatusLine().getStatusCode());
    }
    {
        // tag::update-request
        UpdateRequest request = new UpdateRequest(// <1>
        "posts", // <2>
        "1");
        // end::update-request
        request.fetchSource(true);
        // tag::update-request-with-inline-script
        // <1>
        Map<String, Object> parameters = singletonMap("count", 4);
        Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.field += params.count", // <2>
        parameters);
        // <3>
        request.script(inline);
        // end::update-request-with-inline-script
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        assertEquals(4, updateResponse.getGetResult().getSource().get("field"));
        request = new UpdateRequest("posts", "1").fetchSource(true);
        // tag::update-request-with-stored-script
        Script stored = new Script(ScriptType.STORED, null, "increment-field", // <1>
        parameters);
        // <2>
        request.script(stored);
        // end::update-request-with-stored-script
        updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        assertEquals(8, updateResponse.getGetResult().getSource().get("field"));
    }
    {
        // tag::update-request-with-doc-as-map
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("updated", new Date());
        jsonMap.put("reason", "daily update");
        UpdateRequest request = new UpdateRequest("posts", "1").doc(// <1>
        jsonMap);
        // end::update-request-with-doc-as-map
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    }
    {
        // tag::update-request-with-doc-as-xcontent
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.timeField("updated", new Date());
            builder.field("reason", "daily update");
        }
        builder.endObject();
        UpdateRequest request = new UpdateRequest("posts", "1").doc(// <1>
        builder);
        // end::update-request-with-doc-as-xcontent
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    }
    {
        // tag::update-request-shortcut
        UpdateRequest request = new UpdateRequest("posts", "1").doc("updated", new Date(), "reason", // <1>
        "daily update");
        // end::update-request-shortcut
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    }
    {
        // tag::update-request-with-doc-as-string
        UpdateRequest request = new UpdateRequest("posts", "1");
        String jsonString = "{" + "\"updated\":\"2017-01-01\"," + "\"reason\":\"daily update\"" + "}";
        // <1>
        request.doc(jsonString, XContentType.JSON);
        // end::update-request-with-doc-as-string
        request.fetchSource(true);
        // tag::update-execute
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        // end::update-execute
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        // tag::update-response
        String index = updateResponse.getIndex();
        String id = updateResponse.getId();
        long version = updateResponse.getVersion();
        if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) {
        // <1>
        } else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
        // <2>
        } else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) {
        // <3>
        } else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) {
        // <4>
        }
        // end::update-response
        // tag::update-getresult
        // <1>
        GetResult result = updateResponse.getGetResult();
        if (result.isExists()) {
            // <2>
            String sourceAsString = result.sourceAsString();
            // <3>
            Map<String, Object> sourceAsMap = result.sourceAsMap();
            // <4>
            byte[] sourceAsBytes = result.source();
        } else {
        // <5>
        }
        // end::update-getresult
        assertNotNull(result);
        assertEquals(3, result.sourceAsMap().size());
        // tag::update-failure
        ReplicationResponse.ShardInfo shardInfo = updateResponse.getShardInfo();
        if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
        // <1>
        }
        if (shardInfo.getFailed() > 0) {
            for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
                // <2>
                String reason = failure.reason();
            }
        }
    // end::update-failure
    }
    {
        // tag::update-docnotfound
        UpdateRequest request = new UpdateRequest("posts", "does_not_exist").doc("field", "value");
        try {
            UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        } catch (OpenSearchException e) {
            if (e.status() == RestStatus.NOT_FOUND) {
            // <1>
            }
        }
    // end::update-docnotfound
    }
    {
        // tag::update-conflict
        UpdateRequest request = new UpdateRequest("posts", "1").doc("field", "value").setIfSeqNo(101L).setIfPrimaryTerm(200L);
        try {
            UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        } catch (OpenSearchException e) {
            if (e.status() == RestStatus.CONFLICT) {
            // <1>
            }
        }
    // end::update-conflict
    }
    {
        UpdateRequest request = new UpdateRequest("posts", "1").doc("reason", "no source");
        // tag::update-request-no-source
        // <1>
        request.fetchSource(true);
        // end::update-request-no-source
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        assertNotNull(updateResponse.getGetResult());
        assertEquals(3, updateResponse.getGetResult().sourceAsMap().size());
    }
    {
        UpdateRequest request = new UpdateRequest("posts", "1").doc("reason", "source includes");
        // tag::update-request-source-include
        String[] includes = new String[] { "updated", "r*" };
        String[] excludes = Strings.EMPTY_ARRAY;
        request.fetchSource(// <1>
        new FetchSourceContext(true, includes, excludes));
        // end::update-request-source-include
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        Map<String, Object> sourceAsMap = updateResponse.getGetResult().sourceAsMap();
        assertEquals(2, sourceAsMap.size());
        assertEquals("source includes", sourceAsMap.get("reason"));
        assertTrue(sourceAsMap.containsKey("updated"));
    }
    {
        UpdateRequest request = new UpdateRequest("posts", "1").doc("reason", "source excludes");
        // tag::update-request-source-exclude
        String[] includes = Strings.EMPTY_ARRAY;
        String[] excludes = new String[] { "updated" };
        request.fetchSource(// <1>
        new FetchSourceContext(true, includes, excludes));
        // end::update-request-source-exclude
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        Map<String, Object> sourceAsMap = updateResponse.getGetResult().sourceAsMap();
        assertEquals(2, sourceAsMap.size());
        assertEquals("source excludes", sourceAsMap.get("reason"));
        assertTrue(sourceAsMap.containsKey("field"));
    }
    {
        UpdateRequest request = new UpdateRequest("posts", "id");
        // tag::update-request-routing
        // <1>
        request.routing("routing");
        // end::update-request-routing
        // tag::update-request-timeout
        // <1>
        request.timeout(TimeValue.timeValueSeconds(1));
        // <2>
        request.timeout("1s");
        // end::update-request-timeout
        // tag::update-request-retry
        // <1>
        request.retryOnConflict(3);
        // end::update-request-retry
        // tag::update-request-refresh
        // <1>
        request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        // <2>
        request.setRefreshPolicy("wait_for");
        // end::update-request-refresh
        // tag::update-request-cas
        // <1>
        request.setIfSeqNo(2L);
        // <2>
        request.setIfPrimaryTerm(1L);
        // end::update-request-cas
        // tag::update-request-detect-noop
        // <1>
        request.detectNoop(false);
        // end::update-request-detect-noop
        // tag::update-request-upsert
        String jsonString = "{\"created\":\"2017-01-01\"}";
        // <1>
        request.upsert(jsonString, XContentType.JSON);
        // end::update-request-upsert
        // tag::update-request-scripted-upsert
        // <1>
        request.scriptedUpsert(true);
        // end::update-request-scripted-upsert
        // tag::update-request-doc-upsert
        // <1>
        request.docAsUpsert(true);
        // end::update-request-doc-upsert
        // tag::update-request-active-shards
        // <1>
        request.waitForActiveShards(2);
        // <2>
        request.waitForActiveShards(ActiveShardCount.ALL);
    // end::update-request-active-shards
    }
    {
        UpdateRequest request = new UpdateRequest("posts", "async").doc("reason", "async update").docAsUpsert(true);
        ActionListener<UpdateResponse> listener;
        // tag::update-execute-listener
        listener = new ActionListener<UpdateResponse>() {

            @Override
            public void onResponse(UpdateResponse updateResponse) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::update-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::update-execute-async
        // <1>
        client.updateAsync(request, RequestOptions.DEFAULT, listener);
        // end::update-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
}
Also used : Script(org.opensearch.script.Script) GetResult(org.opensearch.index.get.GetResult) UpdateRequest(org.opensearch.action.update.UpdateRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) Request(org.opensearch.client.Request) WriteRequest(org.opensearch.action.support.WriteRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) RethrottleRequest(org.opensearch.client.RethrottleRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) GetRequest(org.opensearch.action.get.GetRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) OpenSearchException(org.opensearch.OpenSearchException) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) IndexResponse(org.opensearch.action.index.IndexResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) GetResponse(org.opensearch.action.get.GetResponse) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) Response(org.opensearch.client.Response) GetSourceResponse(org.opensearch.client.core.GetSourceResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) OpenSearchException(org.opensearch.OpenSearchException) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 15 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testCloneIndex.

public void testCloneIndex() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        createIndex("source_index", Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0).build());
        updateIndexSettings("source_index", Settings.builder().put("index.blocks.write", true));
    }
    // tag::clone-index-request
    // <1>
    ResizeRequest request = new ResizeRequest("target_index", "source_index");
    // <2>
    request.setResizeType(ResizeType.CLONE);
    // end::clone-index-request
    // tag::clone-index-request-timeout
    // <1>
    request.timeout(TimeValue.timeValueMinutes(2));
    // <2>
    request.timeout("2m");
    // end::clone-index-request-timeout
    // tag::clone-index-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::clone-index-request-masterTimeout
    // tag::clone-index-request-waitForActiveShards
    // <1>
    request.setWaitForActiveShards(2);
    // <2>
    request.setWaitForActiveShards(ActiveShardCount.DEFAULT);
    // end::clone-index-request-waitForActiveShards
    // tag::clone-index-request-settings
    request.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_shards", // <1>
    2));
    // end::clone-index-request-settings
    // tag::clone-index-request-aliases
    // <1>
    request.getTargetIndexRequest().alias(new Alias("target_alias"));
    // end::clone-index-request-aliases
    // tag::clone-index-execute
    ResizeResponse resizeResponse = client.indices().clone(request, RequestOptions.DEFAULT);
    // end::clone-index-execute
    // tag::clone-index-response
    // <1>
    boolean acknowledged = resizeResponse.isAcknowledged();
    // <2>
    boolean shardsAcked = resizeResponse.isShardsAcknowledged();
    // end::clone-index-response
    assertTrue(acknowledged);
    assertTrue(shardsAcked);
    // tag::clone-index-execute-listener
    ActionListener<ResizeResponse> listener = new ActionListener<ResizeResponse>() {

        @Override
        public void onResponse(ResizeResponse resizeResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::clone-index-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::clone-index-execute-async
    // <1>
    client.indices().cloneAsync(request, RequestOptions.DEFAULT, listener);
    // end::clone-index-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : ResizeResponse(org.opensearch.action.admin.indices.shrink.ResizeResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) Alias(org.opensearch.action.admin.indices.alias.Alias) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) ResizeRequest(org.opensearch.action.admin.indices.shrink.ResizeRequest) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException)

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