Search in sources :

Example 1 with IndicesSegmentResponse

use of org.opensearch.action.admin.indices.segments.IndicesSegmentResponse in project OpenSearch by opensearch-project.

the class ShrinkIndexIT method testShrinkCommitsMergeOnIdle.

public void testShrinkCommitsMergeOnIdle() throws Exception {
    prepareCreate("source").setSettings(Settings.builder().put(indexSettings()).put("index.number_of_replicas", 0).put("number_of_shards", 5)).get();
    for (int i = 0; i < 30; i++) {
        client().prepareIndex("source").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    client().admin().indices().prepareFlush("source").get();
    ImmutableOpenMap<String, DiscoveryNode> dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes();
    DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class);
    // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node
    // if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due
    // to the require._name below.
    ensureGreen();
    // relocate all shards to one node such that we can merge it.
    client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder().put("index.routing.allocation.require._name", discoveryNodes[0].getName()).put("index.blocks.write", true)).get();
    ensureGreen();
    IndicesSegmentResponse sourceStats = client().admin().indices().prepareSegments("source").get();
    // disable rebalancing to be able to capture the right stats. balancing can move the target primary
    // making it hard to pin point the source shards.
    client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), "none")).get();
    // now merge source into a single shard index
    assertAcked(client().admin().indices().prepareResizeIndex("source", "target").setSettings(Settings.builder().put("index.number_of_replicas", 0).build()).get());
    ensureGreen();
    ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().get();
    IndexMetadata target = clusterStateResponse.getState().getMetadata().index("target");
    client().admin().indices().prepareForceMerge("target").setMaxNumSegments(1).setFlush(false).get();
    IndicesSegmentResponse targetSegStats = client().admin().indices().prepareSegments("target").get();
    ShardSegments segmentsStats = targetSegStats.getIndices().get("target").getShards().get(0).getShards()[0];
    assertTrue(segmentsStats.getNumberOfCommitted() > 0);
    assertNotEquals(segmentsStats.getSegments(), segmentsStats.getNumberOfCommitted());
    Iterable<IndicesService> dataNodeInstances = internalCluster().getDataNodeInstances(IndicesService.class);
    for (IndicesService service : dataNodeInstances) {
        if (service.hasIndex(target.getIndex())) {
            IndexService indexShards = service.indexService(target.getIndex());
            IndexShard shard = indexShards.getShard(0);
            assertTrue(shard.isActive());
            shard.flushOnIdle(0);
            assertFalse(shard.isActive());
        }
    }
    assertBusy(() -> {
        IndicesSegmentResponse targetStats = client().admin().indices().prepareSegments("target").get();
        ShardSegments targetShardSegments = targetStats.getIndices().get("target").getShards().get(0).getShards()[0];
        Map<Integer, IndexShardSegments> source = sourceStats.getIndices().get("source").getShards();
        int numSourceSegments = 0;
        for (IndexShardSegments s : source.values()) {
            numSourceSegments += s.getAt(0).getNumberOfCommitted();
        }
        assertTrue(targetShardSegments.getSegments().size() < numSourceSegments);
        assertEquals(targetShardSegments.getNumberOfCommitted(), targetShardSegments.getNumberOfSearch());
        assertEquals(targetShardSegments.getNumberOfCommitted(), targetShardSegments.getSegments().size());
        assertEquals(1, targetShardSegments.getSegments().size());
    });
    // clean up
    client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), (String) null)).get();
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexService(org.opensearch.index.IndexService) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) IndexShard(org.opensearch.index.shard.IndexShard) IndicesService(org.opensearch.indices.IndicesService) IndicesSegmentResponse(org.opensearch.action.admin.indices.segments.IndicesSegmentResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexShardSegments(org.opensearch.action.admin.indices.segments.IndexShardSegments) ShardSegments(org.opensearch.action.admin.indices.segments.ShardSegments) IndexShardSegments(org.opensearch.action.admin.indices.segments.IndexShardSegments) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 2 with IndicesSegmentResponse

use of org.opensearch.action.admin.indices.segments.IndicesSegmentResponse in project OpenSearch by opensearch-project.

the class RestSegmentsAction method doCatRequest.

@Override
protected RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
    clusterStateRequest.clear().nodes(true).routingTable(true).indices(indices);
    return channel -> client.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {

        @Override
        public void processResponse(final ClusterStateResponse clusterStateResponse) {
            final IndicesSegmentsRequest indicesSegmentsRequest = new IndicesSegmentsRequest();
            indicesSegmentsRequest.indices(indices);
            client.admin().indices().segments(indicesSegmentsRequest, new RestResponseListener<IndicesSegmentResponse>(channel) {

                @Override
                public RestResponse buildResponse(final IndicesSegmentResponse indicesSegmentResponse) throws Exception {
                    final Map<String, IndexSegments> indicesSegments = indicesSegmentResponse.getIndices();
                    Table tab = buildTable(request, clusterStateResponse, indicesSegments);
                    return RestTable.buildResponse(tab, channel);
                }
            });
        }
    });
}
Also used : ShardSegments(org.opensearch.action.admin.indices.segments.ShardSegments) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) IndicesSegmentsRequest(org.opensearch.action.admin.indices.segments.IndicesSegmentsRequest) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) IndexSegments(org.opensearch.action.admin.indices.segments.IndexSegments) RestRequest(org.opensearch.rest.RestRequest) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) IndexShardSegments(org.opensearch.action.admin.indices.segments.IndexShardSegments) Table(org.opensearch.common.Table) RestResponse(org.opensearch.rest.RestResponse) Strings(org.opensearch.common.Strings) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) RestActionListener(org.opensearch.rest.action.RestActionListener) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Segment(org.opensearch.index.engine.Segment) IndicesSegmentResponse(org.opensearch.action.admin.indices.segments.IndicesSegmentResponse) RestResponseListener(org.opensearch.rest.action.RestResponseListener) Table(org.opensearch.common.Table) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) RestResponseListener(org.opensearch.rest.action.RestResponseListener) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) IndicesSegmentResponse(org.opensearch.action.admin.indices.segments.IndicesSegmentResponse) IndicesSegmentsRequest(org.opensearch.action.admin.indices.segments.IndicesSegmentsRequest) IndexSegments(org.opensearch.action.admin.indices.segments.IndexSegments)

Example 3 with IndicesSegmentResponse

use of org.opensearch.action.admin.indices.segments.IndicesSegmentResponse in project OpenSearch by opensearch-project.

the class OpenSearchIntegTestCase method assertSortedSegments.

/**
 * Asserts that all segments are sorted with the provided {@link Sort}.
 */
public void assertSortedSegments(String indexName, Sort expectedIndexSort) {
    IndicesSegmentResponse segmentResponse = client().admin().indices().prepareSegments(indexName).execute().actionGet();
    IndexSegments indexSegments = segmentResponse.getIndices().get(indexName);
    for (IndexShardSegments indexShardSegments : indexSegments.getShards().values()) {
        for (ShardSegments shardSegments : indexShardSegments.getShards()) {
            for (Segment segment : shardSegments) {
                assertThat(expectedIndexSort, equalTo(segment.getSegmentSort()));
            }
        }
    }
}
Also used : ShardSegments(org.opensearch.action.admin.indices.segments.ShardSegments) IndexShardSegments(org.opensearch.action.admin.indices.segments.IndexShardSegments) IndicesSegmentResponse(org.opensearch.action.admin.indices.segments.IndicesSegmentResponse) IndexShardSegments(org.opensearch.action.admin.indices.segments.IndexShardSegments) IndexSegments(org.opensearch.action.admin.indices.segments.IndexSegments) Segment(org.opensearch.index.engine.Segment)

Aggregations

IndexShardSegments (org.opensearch.action.admin.indices.segments.IndexShardSegments)3 IndicesSegmentResponse (org.opensearch.action.admin.indices.segments.IndicesSegmentResponse)3 ShardSegments (org.opensearch.action.admin.indices.segments.ShardSegments)3 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)2 IndexSegments (org.opensearch.action.admin.indices.segments.IndexSegments)2 Segment (org.opensearch.index.engine.Segment)2 Arrays.asList (java.util.Arrays.asList)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 List (java.util.List)1 Map (java.util.Map)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 ClusterStateRequest (org.opensearch.action.admin.cluster.state.ClusterStateRequest)1 IndicesSegmentsRequest (org.opensearch.action.admin.indices.segments.IndicesSegmentsRequest)1 NodeClient (org.opensearch.client.node.NodeClient)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)1 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)1 Strings (org.opensearch.common.Strings)1 Table (org.opensearch.common.Table)1 IndexService (org.opensearch.index.IndexService)1