Search in sources :

Example 6 with GetSettingsResponse

use of org.opensearch.action.admin.indices.settings.get.GetSettingsResponse in project OpenSearch by opensearch-project.

the class SettingsFilteringIT method testSettingsFiltering.

public void testSettingsFiltering() {
    assertAcked(client().admin().indices().prepareCreate("test-idx").setSettings(Settings.builder().put("filter_test.foo", "test").put("filter_test.bar1", "test").put("filter_test.bar2", "test").put("filter_test.notbar", "test").put("filter_test.notfoo", "test").build()).get());
    GetSettingsResponse response = client().admin().indices().prepareGetSettings("test-idx").get();
    Settings settings = response.getIndexToSettings().get("test-idx");
    assertThat(settings.get("index.filter_test.foo"), nullValue());
    assertThat(settings.get("index.filter_test.bar1"), nullValue());
    assertThat(settings.get("index.filter_test.bar2"), nullValue());
    assertThat(settings.get("index.filter_test.notbar"), equalTo("test"));
    assertThat(settings.get("index.filter_test.notfoo"), equalTo("test"));
}
Also used : GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) Settings(org.opensearch.common.settings.Settings)

Example 7 with GetSettingsResponse

use of org.opensearch.action.admin.indices.settings.get.GetSettingsResponse in project OpenSearch by opensearch-project.

the class SplitIndexIT method testCreateSplitWithIndexSort.

public void testCreateSplitWithIndexSort() throws Exception {
    SortField expectedSortField = new SortedSetSortField("id", true, SortedSetSelector.Type.MAX);
    expectedSortField.setMissingValue(SortedSetSortField.STRING_FIRST);
    Sort expectedIndexSort = new Sort(expectedSortField);
    internalCluster().ensureAtLeastNumDataNodes(2);
    prepareCreate("source").setSettings(Settings.builder().put(indexSettings()).put("sort.field", "id").put("sort.order", "desc").put("number_of_shards", 2).put("number_of_replicas", 0)).addMapping(MapperService.SINGLE_MAPPING_NAME, "id", "type=keyword,doc_values=true").get();
    for (int i = 0; i < 20; i++) {
        client().prepareIndex("source").setId(Integer.toString(i)).setSource("{\"foo\" : \"bar\", \"id\" : " + i + "}", XContentType.JSON).get();
    }
    // 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();
    flushAndRefresh();
    assertSortedSegments("source", expectedIndexSort);
    client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder().put("index.blocks.write", true)).get();
    ensureYellow();
    // check that index sort cannot be set on the target index
    IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> client().admin().indices().prepareResizeIndex("source", "target").setResizeType(ResizeType.SPLIT).setSettings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", 4).put("index.sort.field", "foo").build()).get());
    assertThat(exc.getMessage(), containsString("can't override index sort when resizing an index"));
    // check that the index sort order of `source` is correctly applied to the `target`
    assertAcked(client().admin().indices().prepareResizeIndex("source", "target").setResizeType(ResizeType.SPLIT).setSettings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", 4).putNull("index.blocks.write").build()).get());
    ensureGreen();
    flushAndRefresh();
    GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("target").execute().actionGet();
    assertEquals(settingsResponse.getSetting("target", "index.sort.field"), "id");
    assertEquals(settingsResponse.getSetting("target", "index.sort.order"), "desc");
    assertSortedSegments("target", expectedIndexSort);
    // ... and that the index sort is also applied to updates
    for (int i = 20; i < 40; i++) {
        client().prepareIndex("target").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    flushAndRefresh();
    assertSortedSegments("target", expectedIndexSort);
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField)

Example 8 with GetSettingsResponse

use of org.opensearch.action.admin.indices.settings.get.GetSettingsResponse in project OpenSearch by opensearch-project.

the class SplitIndexIT method testCreateSplitIndex.

public void testCreateSplitIndex() throws Exception {
    internalCluster().ensureAtLeastNumDataNodes(2);
    Version version = VersionUtils.randomIndexCompatibleVersion(random());
    prepareCreate("source").setSettings(Settings.builder().put(indexSettings()).put("number_of_shards", 1).put("index.version.created", version)).get();
    final int docs = randomIntBetween(0, 128);
    for (int i = 0; i < docs; i++) {
        client().prepareIndex("source").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    // 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.blocks.write", true)).get();
    ensureGreen();
    final IndicesStatsResponse sourceStats = client().admin().indices().prepareStats("source").setSegments(true).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();
    try {
        final boolean createWithReplicas = randomBoolean();
        assertAcked(client().admin().indices().prepareResizeIndex("source", "target").setResizeType(ResizeType.SPLIT).setSettings(Settings.builder().put("index.number_of_replicas", createWithReplicas ? 1 : 0).put("index.number_of_shards", 2).putNull("index.blocks.write").build()).get());
        ensureGreen();
        final ClusterState state = client().admin().cluster().prepareState().get().getState();
        DiscoveryNode mergeNode = state.nodes().get(state.getRoutingTable().index("target").shard(0).primaryShard().currentNodeId());
        logger.info("split node {}", mergeNode);
        final long maxSeqNo = Arrays.stream(sourceStats.getShards()).filter(shard -> shard.getShardRouting().currentNodeId().equals(mergeNode.getId())).map(ShardStats::getSeqNoStats).mapToLong(SeqNoStats::getMaxSeqNo).max().getAsLong();
        final long maxUnsafeAutoIdTimestamp = Arrays.stream(sourceStats.getShards()).filter(shard -> shard.getShardRouting().currentNodeId().equals(mergeNode.getId())).map(ShardStats::getStats).map(CommonStats::getSegments).mapToLong(SegmentsStats::getMaxUnsafeAutoIdTimestamp).max().getAsLong();
        final IndicesStatsResponse targetStats = client().admin().indices().prepareStats("target").get();
        for (final ShardStats shardStats : targetStats.getShards()) {
            final SeqNoStats seqNoStats = shardStats.getSeqNoStats();
            final ShardRouting shardRouting = shardStats.getShardRouting();
            assertThat("failed on " + shardRouting, seqNoStats.getMaxSeqNo(), equalTo(maxSeqNo));
            assertThat("failed on " + shardRouting, seqNoStats.getLocalCheckpoint(), equalTo(maxSeqNo));
            assertThat("failed on " + shardRouting, shardStats.getStats().getSegments().getMaxUnsafeAutoIdTimestamp(), equalTo(maxUnsafeAutoIdTimestamp));
        }
        final int size = docs > 0 ? 2 * docs : 1;
        assertHitCount(client().prepareSearch("target").setSize(size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), docs);
        if (createWithReplicas == false) {
            // bump replicas
            client().admin().indices().prepareUpdateSettings("target").setSettings(Settings.builder().put("index.number_of_replicas", 1)).get();
            ensureGreen();
            assertHitCount(client().prepareSearch("target").setSize(size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), docs);
        }
        for (int i = docs; i < 2 * docs; i++) {
            client().prepareIndex("target").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
        }
        flushAndRefresh();
        assertHitCount(client().prepareSearch("target").setSize(2 * size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 2 * docs);
        assertHitCount(client().prepareSearch("source").setSize(size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), docs);
        GetSettingsResponse target = client().admin().indices().prepareGetSettings("target").get();
        assertEquals(version, target.getIndexToSettings().get("target").getAsVersion("index.version.created", null));
    } finally {
        // clean up
        client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), (String) null)).get();
    }
}
Also used : CommonStats(org.opensearch.action.admin.indices.stats.CommonStats) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) SeqNoStats(org.opensearch.index.seqno.SeqNoStats) Arrays(java.util.Arrays) EnableAllocationDecider(org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) BiFunction(java.util.function.BiFunction) Version(org.opensearch.Version) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MapperService(org.opensearch.index.mapper.MapperService) OpenSearchAssertions.assertHitCount(org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount) SortField(org.apache.lucene.search.SortField) SegmentsStats(org.opensearch.index.engine.SegmentsStats) GetResponse(org.opensearch.action.get.GetResponse) QueryBuilders.termQuery(org.opensearch.index.query.QueryBuilders.termQuery) Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) OpenSearchAssertions.assertNoFailures(org.opensearch.test.hamcrest.OpenSearchAssertions.assertNoFailures) Sort(org.apache.lucene.search.Sort) Index(org.opensearch.index.Index) IndicesService(org.opensearch.indices.IndicesService) Set(java.util.Set) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) Settings(org.opensearch.common.settings.Settings) ScoreMode(org.apache.lucene.search.join.ScoreMode) UncheckedIOException(java.io.UncheckedIOException) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) XContentType(org.opensearch.common.xcontent.XContentType) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) Matchers.containsString(org.hamcrest.Matchers.containsString) IntStream(java.util.stream.IntStream) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) MetadataCreateIndexService(org.opensearch.cluster.metadata.MetadataCreateIndexService) IndexShard(org.opensearch.index.shard.IndexShard) VersionUtils(org.opensearch.test.VersionUtils) Murmur3HashFunction(org.opensearch.cluster.routing.Murmur3HashFunction) SearchResponse(org.opensearch.action.search.SearchResponse) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) ResizeType(org.opensearch.action.admin.indices.shrink.ResizeType) QueryBuilders.nestedQuery(org.opensearch.index.query.QueryBuilders.nestedQuery) IOException(java.io.IOException) GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) IndexService(org.opensearch.index.IndexService) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) Constants(org.apache.lucene.util.Constants) SortedSetSelector(org.apache.lucene.search.SortedSetSelector) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) IndexRequest(org.opensearch.action.index.IndexRequest) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) SegmentsStats(org.opensearch.index.engine.SegmentsStats) SeqNoStats(org.opensearch.index.seqno.SeqNoStats) Version(org.opensearch.Version) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 9 with GetSettingsResponse

use of org.opensearch.action.admin.indices.settings.get.GetSettingsResponse in project OpenSearch by opensearch-project.

the class ShrinkIndexIT method testCreateShrinkIndex.

public void testCreateShrinkIndex() {
    internalCluster().ensureAtLeastNumDataNodes(2);
    Version version = VersionUtils.randomVersion(random());
    prepareCreate("source").setSettings(Settings.builder().put(indexSettings()).put("number_of_shards", randomIntBetween(2, 7)).put("index.version.created", version)).get();
    final int docs = randomIntBetween(0, 128);
    for (int i = 0; i < docs; i++) {
        client().prepareIndex("source").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    ImmutableOpenMap<String, DiscoveryNode> dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes();
    assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2);
    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();
    final IndicesStatsResponse sourceStats = client().admin().indices().prepareStats("source").setSegments(true).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
    final boolean createWithReplicas = randomBoolean();
    assertAcked(client().admin().indices().prepareResizeIndex("source", "target").setSettings(Settings.builder().put("index.number_of_replicas", createWithReplicas ? 1 : 0).putNull("index.blocks.write").putNull("index.routing.allocation.require._name").build()).get());
    ensureGreen();
    // resolve true merge node - this is not always the node we required as all shards may be on another node
    final ClusterState state = client().admin().cluster().prepareState().get().getState();
    DiscoveryNode mergeNode = state.nodes().get(state.getRoutingTable().index("target").shard(0).primaryShard().currentNodeId());
    logger.info("merge node {}", mergeNode);
    final long maxSeqNo = Arrays.stream(sourceStats.getShards()).filter(shard -> shard.getShardRouting().currentNodeId().equals(mergeNode.getId())).map(ShardStats::getSeqNoStats).mapToLong(SeqNoStats::getMaxSeqNo).max().getAsLong();
    final long maxUnsafeAutoIdTimestamp = Arrays.stream(sourceStats.getShards()).filter(shard -> shard.getShardRouting().currentNodeId().equals(mergeNode.getId())).map(ShardStats::getStats).map(CommonStats::getSegments).mapToLong(SegmentsStats::getMaxUnsafeAutoIdTimestamp).max().getAsLong();
    final IndicesStatsResponse targetStats = client().admin().indices().prepareStats("target").get();
    for (final ShardStats shardStats : targetStats.getShards()) {
        final SeqNoStats seqNoStats = shardStats.getSeqNoStats();
        final ShardRouting shardRouting = shardStats.getShardRouting();
        assertThat("failed on " + shardRouting, seqNoStats.getMaxSeqNo(), equalTo(maxSeqNo));
        assertThat("failed on " + shardRouting, seqNoStats.getLocalCheckpoint(), equalTo(maxSeqNo));
        assertThat("failed on " + shardRouting, shardStats.getStats().getSegments().getMaxUnsafeAutoIdTimestamp(), equalTo(maxUnsafeAutoIdTimestamp));
    }
    final int size = docs > 0 ? 2 * docs : 1;
    assertHitCount(client().prepareSearch("target").setSize(size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), docs);
    if (createWithReplicas == false) {
        // bump replicas
        client().admin().indices().prepareUpdateSettings("target").setSettings(Settings.builder().put("index.number_of_replicas", 1)).get();
        ensureGreen();
        assertHitCount(client().prepareSearch("target").setSize(size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), docs);
    }
    for (int i = docs; i < 2 * docs; i++) {
        client().prepareIndex("target").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    flushAndRefresh();
    assertHitCount(client().prepareSearch("target").setSize(2 * size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 2 * docs);
    assertHitCount(client().prepareSearch("source").setSize(size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), docs);
    GetSettingsResponse target = client().admin().indices().prepareGetSettings("target").get();
    assertEquals(version, target.getIndexToSettings().get("target").getAsVersion("index.version.created", null));
    // clean up
    client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), (String) null)).get();
}
Also used : CommonStats(org.opensearch.action.admin.indices.stats.CommonStats) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) SeqNoStats(org.opensearch.index.seqno.SeqNoStats) Arrays(java.util.Arrays) EnableAllocationDecider(org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) Version(org.opensearch.Version) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MapperService(org.opensearch.index.mapper.MapperService) OpenSearchAssertions.assertHitCount(org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount) ClusterRerouteResponse(org.opensearch.action.admin.cluster.reroute.ClusterRerouteResponse) Map(java.util.Map) SortField(org.apache.lucene.search.SortField) SegmentsStats(org.opensearch.index.engine.SegmentsStats) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) Sort(org.apache.lucene.search.Sort) Index(org.opensearch.index.Index) IndicesService(org.opensearch.indices.IndicesService) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) Settings(org.opensearch.common.settings.Settings) InternalClusterInfoService(org.opensearch.cluster.InternalClusterInfoService) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) XContentType(org.opensearch.common.xcontent.XContentType) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) Matchers.containsString(org.hamcrest.Matchers.containsString) IntStream(java.util.stream.IntStream) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ClusterInfoService(org.opensearch.cluster.ClusterInfoService) Priority(org.opensearch.common.Priority) InternalTestCluster(org.opensearch.test.InternalTestCluster) ClusterState(org.opensearch.cluster.ClusterState) IndexShard(org.opensearch.index.shard.IndexShard) VersionUtils(org.opensearch.test.VersionUtils) Murmur3HashFunction(org.opensearch.cluster.routing.Murmur3HashFunction) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) ShardSegments(org.opensearch.action.admin.indices.segments.ShardSegments) ResizeType(org.opensearch.action.admin.indices.shrink.ResizeType) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) IndexShardSegments(org.opensearch.action.admin.indices.segments.IndexShardSegments) GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) IndexService(org.opensearch.index.IndexService) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) Constants(org.apache.lucene.util.Constants) SortedSetSelector(org.apache.lucene.search.SortedSetSelector) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) IndexRequest(org.opensearch.action.index.IndexRequest) IndicesSegmentResponse(org.opensearch.action.admin.indices.segments.IndicesSegmentResponse) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) SegmentsStats(org.opensearch.index.engine.SegmentsStats) SeqNoStats(org.opensearch.index.seqno.SeqNoStats) Version(org.opensearch.Version) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 10 with GetSettingsResponse

use of org.opensearch.action.admin.indices.settings.get.GetSettingsResponse in project OpenSearch by opensearch-project.

the class CloneIndexIT method testCreateCloneIndex.

public void testCreateCloneIndex() {
    Version version = VersionUtils.randomIndexCompatibleVersion(random());
    int numPrimaryShards = randomIntBetween(1, 5);
    prepareCreate("source").setSettings(Settings.builder().put(indexSettings()).put("number_of_shards", numPrimaryShards).put("index.version.created", version)).get();
    final int docs = randomIntBetween(0, 128);
    for (int i = 0; i < docs; i++) {
        client().prepareIndex("source").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    internalCluster().ensureAtLeastNumDataNodes(2);
    // 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.blocks.write", true)).get();
    ensureGreen();
    final IndicesStatsResponse sourceStats = client().admin().indices().prepareStats("source").setSegments(true).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();
    try {
        final boolean createWithReplicas = randomBoolean();
        assertAcked(client().admin().indices().prepareResizeIndex("source", "target").setResizeType(ResizeType.CLONE).setSettings(Settings.builder().put("index.number_of_replicas", createWithReplicas ? 1 : 0).putNull("index.blocks.write").build()).get());
        ensureGreen();
        final IndicesStatsResponse targetStats = client().admin().indices().prepareStats("target").get();
        assertThat(targetStats.getIndex("target").getIndexShards().keySet().size(), equalTo(numPrimaryShards));
        for (int i = 0; i < numPrimaryShards; i++) {
            final SeqNoStats sourceSeqNoStats = sourceStats.getIndex("source").getIndexShards().get(i).getAt(0).getSeqNoStats();
            final SeqNoStats targetSeqNoStats = targetStats.getIndex("target").getIndexShards().get(i).getAt(0).getSeqNoStats();
            assertEquals(sourceSeqNoStats.getMaxSeqNo(), targetSeqNoStats.getMaxSeqNo());
            assertEquals(targetSeqNoStats.getMaxSeqNo(), targetSeqNoStats.getLocalCheckpoint());
        }
        final int size = docs > 0 ? 2 * docs : 1;
        assertHitCount(client().prepareSearch("target").setSize(size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), docs);
        if (createWithReplicas == false) {
            // bump replicas
            client().admin().indices().prepareUpdateSettings("target").setSettings(Settings.builder().put("index.number_of_replicas", 1)).get();
            ensureGreen();
            assertHitCount(client().prepareSearch("target").setSize(size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), docs);
        }
        for (int i = docs; i < 2 * docs; i++) {
            client().prepareIndex("target").setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
        }
        flushAndRefresh();
        assertHitCount(client().prepareSearch("target").setSize(2 * size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 2 * docs);
        assertHitCount(client().prepareSearch("source").setSize(size).setQuery(new TermsQueryBuilder("foo", "bar")).get(), docs);
        GetSettingsResponse target = client().admin().indices().prepareGetSettings("target").get();
        assertEquals(version, target.getIndexToSettings().get("target").getAsVersion("index.version.created", null));
    } finally {
        // clean up
        client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), (String) null)).get();
    }
}
Also used : IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) SeqNoStats(org.opensearch.index.seqno.SeqNoStats) Version(org.opensearch.Version) GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder)

Aggregations

GetSettingsResponse (org.opensearch.action.admin.indices.settings.get.GetSettingsResponse)31 Settings (org.opensearch.common.settings.Settings)14 Matchers.containsString (org.hamcrest.Matchers.containsString)10 GetSettingsRequest (org.opensearch.action.admin.indices.settings.get.GetSettingsRequest)8 IndexSettings (org.opensearch.index.IndexSettings)7 Sort (org.apache.lucene.search.Sort)4 SortField (org.apache.lucene.search.SortField)4 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)4 IndicesStatsResponse (org.opensearch.action.admin.indices.stats.IndicesStatsResponse)4 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)4 Version (org.opensearch.Version)3 ActionListener (org.opensearch.action.ActionListener)3 ClusterStateRequest (org.opensearch.action.admin.cluster.state.ClusterStateRequest)3 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)3 CommonStats (org.opensearch.action.admin.indices.stats.CommonStats)3 Client (org.opensearch.client.Client)3 ClusterState (org.opensearch.cluster.ClusterState)3 TermsQueryBuilder (org.opensearch.index.query.TermsQueryBuilder)3 SeqNoStats (org.opensearch.index.seqno.SeqNoStats)3 IOException (java.io.IOException)2