Search in sources :

Example 1 with IndexShardSegments

use of org.opensearch.action.admin.indices.segments.IndexShardSegments 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 IndexShardSegments

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

the class CompletionSuggestSearchIT method testPrunedSegments.

// see #3555
public void testPrunedSegments() throws IOException {
    createIndexAndMappingAndSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).build(), completionMappingBuilder);
    client().prepareIndex(INDEX).setId("1").setSource(jsonBuilder().startObject().startObject(FIELD).startArray("input").value("The Beatles").endArray().endObject().endObject()).get();
    client().prepareIndex(INDEX).setId("2").setSource(jsonBuilder().startObject().field("somefield", "somevalue").endObject()).get();
    ForceMergeResponse actionGet = client().admin().indices().prepareForceMerge().setFlush(true).setMaxNumSegments(1).get();
    assertAllSuccessful(actionGet);
    refresh();
    // update the first one and then merge.. the target segment will have no value in FIELD
    client().prepareIndex(INDEX).setId("1").setSource(jsonBuilder().startObject().field("somefield", "somevalue").endObject()).get();
    actionGet = client().admin().indices().prepareForceMerge().setFlush(true).setMaxNumSegments(1).get();
    assertAllSuccessful(actionGet);
    refresh();
    assertSuggestions("b");
    assertThat(2L, equalTo(client().prepareSearch(INDEX).setSize(0).get().getHits().getTotalHits().value));
    for (IndexShardSegments seg : client().admin().indices().prepareSegments().get().getIndices().get(INDEX)) {
        ShardSegments[] shards = seg.getShards();
        for (ShardSegments shardSegments : shards) {
            assertThat(shardSegments.getSegments().size(), equalTo(1));
        }
    }
}
Also used : ForceMergeResponse(org.opensearch.action.admin.indices.forcemerge.ForceMergeResponse) ShardSegments(org.opensearch.action.admin.indices.segments.ShardSegments) IndexShardSegments(org.opensearch.action.admin.indices.segments.IndexShardSegments) IndexShardSegments(org.opensearch.action.admin.indices.segments.IndexShardSegments)

Example 3 with IndexShardSegments

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

the class RestSegmentsAction method buildTable.

private Table buildTable(final RestRequest request, ClusterStateResponse state, Map<String, IndexSegments> indicesSegments) {
    Table table = getTableWithHeader(request);
    DiscoveryNodes nodes = state.getState().nodes();
    for (IndexSegments indexSegments : indicesSegments.values()) {
        Map<Integer, IndexShardSegments> shards = indexSegments.getShards();
        for (IndexShardSegments indexShardSegments : shards.values()) {
            ShardSegments[] shardSegments = indexShardSegments.getShards();
            for (ShardSegments shardSegment : shardSegments) {
                List<Segment> segments = shardSegment.getSegments();
                for (Segment segment : segments) {
                    table.startRow();
                    table.addCell(shardSegment.getShardRouting().getIndexName());
                    table.addCell(shardSegment.getShardRouting().getId());
                    table.addCell(shardSegment.getShardRouting().primary() ? "p" : "r");
                    table.addCell(nodes.get(shardSegment.getShardRouting().currentNodeId()).getHostAddress());
                    table.addCell(shardSegment.getShardRouting().currentNodeId());
                    table.addCell(segment.getName());
                    table.addCell(segment.getGeneration());
                    table.addCell(segment.getNumDocs());
                    table.addCell(segment.getDeletedDocs());
                    table.addCell(segment.getSize());
                    table.addCell(0L);
                    table.addCell(segment.isCommitted());
                    table.addCell(segment.isSearch());
                    table.addCell(segment.getVersion());
                    table.addCell(segment.isCompound());
                    table.endRow();
                }
            }
        }
    }
    return table;
}
Also used : Table(org.opensearch.common.Table) ShardSegments(org.opensearch.action.admin.indices.segments.ShardSegments) IndexShardSegments(org.opensearch.action.admin.indices.segments.IndexShardSegments) IndexShardSegments(org.opensearch.action.admin.indices.segments.IndexShardSegments) IndexSegments(org.opensearch.action.admin.indices.segments.IndexSegments) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) Segment(org.opensearch.index.engine.Segment)

Example 4 with IndexShardSegments

use of org.opensearch.action.admin.indices.segments.IndexShardSegments 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)4 ShardSegments (org.opensearch.action.admin.indices.segments.ShardSegments)4 IndexSegments (org.opensearch.action.admin.indices.segments.IndexSegments)2 IndicesSegmentResponse (org.opensearch.action.admin.indices.segments.IndicesSegmentResponse)2 Segment (org.opensearch.index.engine.Segment)2 Matchers.containsString (org.hamcrest.Matchers.containsString)1 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)1 ForceMergeResponse (org.opensearch.action.admin.indices.forcemerge.ForceMergeResponse)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)1 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)1 Table (org.opensearch.common.Table)1 IndexService (org.opensearch.index.IndexService)1 IndexShard (org.opensearch.index.shard.IndexShard)1 IndicesService (org.opensearch.indices.IndicesService)1