Search in sources :

Example 1 with OpenIndexRequest

use of org.opensearch.action.admin.indices.open.OpenIndexRequest in project OpenSearch by opensearch-project.

the class IndicesClusterStateServiceRandomUpdatesTests method randomlyUpdateClusterState.

public ClusterState randomlyUpdateClusterState(ClusterState state, Map<DiscoveryNode, IndicesClusterStateService> clusterStateServiceMap, Supplier<MockIndicesService> indicesServiceSupplier) {
    // randomly remove no_master blocks
    if (randomBoolean() && state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID)) {
        state = ClusterState.builder(state).blocks(ClusterBlocks.builder().blocks(state.blocks()).removeGlobalBlock(NoMasterBlockService.NO_MASTER_BLOCK_ID)).build();
    }
    // randomly add no_master blocks
    if (rarely() && state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID) == false) {
        ClusterBlock block = randomBoolean() ? NoMasterBlockService.NO_MASTER_BLOCK_ALL : NoMasterBlockService.NO_MASTER_BLOCK_WRITES;
        state = ClusterState.builder(state).blocks(ClusterBlocks.builder().blocks(state.blocks()).addGlobalBlock(block)).build();
    }
    // if no_master block is in place, make no other cluster state changes
    if (state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID)) {
        return state;
    }
    // randomly create new indices (until we have 200 max)
    for (int i = 0; i < randomInt(5); i++) {
        if (state.metadata().indices().size() > 200) {
            break;
        }
        String name = "index_" + randomAlphaOfLength(15).toLowerCase(Locale.ROOT);
        Settings.Builder settingsBuilder = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 3));
        if (randomBoolean()) {
            int min = randomInt(2);
            int max = min + randomInt(3);
            settingsBuilder.put(SETTING_AUTO_EXPAND_REPLICAS, randomBoolean() ? min + "-" + max : min + "-all");
        } else {
            settingsBuilder.put(SETTING_NUMBER_OF_REPLICAS, randomInt(2));
        }
        CreateIndexRequest request = new CreateIndexRequest(name, settingsBuilder.build()).waitForActiveShards(ActiveShardCount.NONE);
        state = cluster.createIndex(state, request);
        assertTrue(state.metadata().hasIndex(name));
    }
    // randomly delete indices
    Set<String> indicesToDelete = new HashSet<>();
    int numberOfIndicesToDelete = randomInt(Math.min(2, state.metadata().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToDelete, state.metadata().indices().keys().toArray(String.class))) {
        indicesToDelete.add(state.metadata().index(index).getIndex().getName());
    }
    if (indicesToDelete.isEmpty() == false) {
        DeleteIndexRequest deleteRequest = new DeleteIndexRequest(indicesToDelete.toArray(new String[indicesToDelete.size()]));
        state = cluster.deleteIndices(state, deleteRequest);
        for (String index : indicesToDelete) {
            assertFalse(state.metadata().hasIndex(index));
        }
    }
    // randomly close indices
    int numberOfIndicesToClose = randomInt(Math.min(1, state.metadata().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToClose, state.metadata().indices().keys().toArray(String.class))) {
        CloseIndexRequest closeIndexRequest = new CloseIndexRequest(state.metadata().index(index).getIndex().getName());
        state = cluster.closeIndices(state, closeIndexRequest);
    }
    // randomly open indices
    int numberOfIndicesToOpen = randomInt(Math.min(1, state.metadata().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToOpen, state.metadata().indices().keys().toArray(String.class))) {
        OpenIndexRequest openIndexRequest = new OpenIndexRequest(state.metadata().index(index).getIndex().getName());
        state = cluster.openIndices(state, openIndexRequest);
    }
    // randomly update settings
    Set<String> indicesToUpdate = new HashSet<>();
    boolean containsClosedIndex = false;
    int numberOfIndicesToUpdate = randomInt(Math.min(2, state.metadata().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToUpdate, state.metadata().indices().keys().toArray(String.class))) {
        indicesToUpdate.add(state.metadata().index(index).getIndex().getName());
        if (state.metadata().index(index).getState() == IndexMetadata.State.CLOSE) {
            containsClosedIndex = true;
        }
    }
    if (indicesToUpdate.isEmpty() == false) {
        UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indicesToUpdate.toArray(new String[indicesToUpdate.size()]));
        Settings.Builder settings = Settings.builder();
        if (containsClosedIndex == false) {
            settings.put(SETTING_NUMBER_OF_REPLICAS, randomInt(2));
        }
        settings.put("index.refresh_interval", randomIntBetween(1, 5) + "s");
        updateSettingsRequest.settings(settings.build());
        state = cluster.updateSettings(state, updateSettingsRequest);
    }
    // randomly reroute
    if (rarely()) {
        state = cluster.reroute(state, new ClusterRerouteRequest());
    }
    // randomly start and fail allocated shards
    final Map<ShardRouting, Long> startedShards = new HashMap<>();
    List<FailedShard> failedShards = new ArrayList<>();
    for (DiscoveryNode node : state.nodes()) {
        IndicesClusterStateService indicesClusterStateService = clusterStateServiceMap.get(node);
        MockIndicesService indicesService = (MockIndicesService) indicesClusterStateService.indicesService;
        for (MockIndexService indexService : indicesService) {
            for (MockIndexShard indexShard : indexService) {
                ShardRouting persistedShardRouting = indexShard.routingEntry();
                if (persistedShardRouting.initializing() && randomBoolean()) {
                    startedShards.put(persistedShardRouting, indexShard.term());
                } else if (rarely()) {
                    failedShards.add(new FailedShard(persistedShardRouting, "fake shard failure", new Exception(), randomBoolean()));
                }
            }
        }
    }
    state = cluster.applyFailedShards(state, failedShards);
    state = cluster.applyStartedShards(state, startedShards);
    // randomly add and remove nodes (except current master)
    if (rarely()) {
        if (randomBoolean()) {
            // add node
            if (state.nodes().getSize() < 10) {
                state = cluster.addNodes(state, Collections.singletonList(createNode()));
                updateNodes(state, clusterStateServiceMap, indicesServiceSupplier);
            }
        } else {
            // remove node
            if (state.nodes().getDataNodes().size() > 3) {
                DiscoveryNode discoveryNode = randomFrom(state.nodes().getNodes().values().toArray(DiscoveryNode.class));
                if (discoveryNode.equals(state.nodes().getMasterNode()) == false) {
                    state = cluster.removeNodes(state, Collections.singletonList(discoveryNode));
                    updateNodes(state, clusterStateServiceMap, indicesServiceSupplier);
                }
                if (randomBoolean()) {
                    // and add it back
                    state = cluster.addNodes(state, Collections.singletonList(discoveryNode));
                    updateNodes(state, clusterStateServiceMap, indicesServiceSupplier);
                }
            }
        }
    }
    return state;
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) ClusterRerouteRequest(org.opensearch.action.admin.cluster.reroute.ClusterRerouteRequest) Settings(org.opensearch.common.settings.Settings) HashSet(java.util.HashSet) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) FailedShard(org.opensearch.cluster.routing.allocation.FailedShard) CloseIndexRequest(org.opensearch.action.admin.indices.close.CloseIndexRequest) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 2 with OpenIndexRequest

use of org.opensearch.action.admin.indices.open.OpenIndexRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConverters method openIndex.

static Request openIndex(OpenIndexRequest openIndexRequest) {
    String endpoint = RequestConverters.endpoint(openIndexRequest.indices(), "_open");
    Request request = new Request(HttpPost.METHOD_NAME, endpoint);
    RequestConverters.Params parameters = new RequestConverters.Params();
    parameters.withTimeout(openIndexRequest.timeout());
    parameters.withMasterTimeout(openIndexRequest.masterNodeTimeout());
    parameters.withWaitForActiveShards(openIndexRequest.waitForActiveShards());
    parameters.withIndicesOptions(openIndexRequest.indicesOptions());
    request.addParameters(parameters.asMap());
    return request;
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) ResizeRequest(org.opensearch.client.indices.ResizeRequest) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) DataStreamsStatsRequest(org.opensearch.client.indices.DataStreamsStatsRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest)

Example 3 with OpenIndexRequest

use of org.opensearch.action.admin.indices.open.OpenIndexRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testOpenExistingIndex.

public void testOpenExistingIndex() throws IOException {
    String index = "index";
    createIndex(index, Settings.EMPTY);
    closeIndex(index);
    ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search")));
    assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus()));
    assertThat(exception.getMessage().contains(index), equalTo(true));
    OpenIndexRequest openIndexRequest = new OpenIndexRequest(index);
    OpenIndexResponse openIndexResponse = execute(openIndexRequest, highLevelClient().indices()::open, highLevelClient().indices()::openAsync);
    assertTrue(openIndexResponse.isAcknowledged());
    Response response = client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search"));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
}
Also used : CloseIndexResponse(org.opensearch.client.indices.CloseIndexResponse) SimulateIndexTemplateResponse(org.opensearch.client.indices.SimulateIndexTemplateResponse) RolloverResponse(org.opensearch.client.indices.rollover.RolloverResponse) FlushResponse(org.opensearch.action.admin.indices.flush.FlushResponse) GetMappingsResponse(org.opensearch.client.indices.GetMappingsResponse) OpenIndexResponse(org.opensearch.action.admin.indices.open.OpenIndexResponse) GetIndexResponse(org.opensearch.client.indices.GetIndexResponse) DataStreamsStatsResponse(org.opensearch.client.indices.DataStreamsStatsResponse) GetDataStreamResponse(org.opensearch.client.indices.GetDataStreamResponse) ValidateQueryResponse(org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse) RefreshResponse(org.opensearch.action.admin.indices.refresh.RefreshResponse) ResizeResponse(org.opensearch.action.admin.indices.shrink.ResizeResponse) BroadcastResponse(org.opensearch.action.support.broadcast.BroadcastResponse) GetIndexTemplatesResponse(org.opensearch.client.indices.GetIndexTemplatesResponse) GetFieldMappingsResponse(org.opensearch.client.indices.GetFieldMappingsResponse) AnalyzeResponse(org.opensearch.client.indices.AnalyzeResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) ForceMergeResponse(org.opensearch.action.admin.indices.forcemerge.ForceMergeResponse) GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) GetComposableIndexTemplatesResponse(org.opensearch.client.indices.GetComposableIndexTemplatesResponse) ClearIndicesCacheResponse(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) WriteRequest(org.opensearch.action.support.WriteRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) DataStreamsStatsRequest(org.opensearch.client.indices.DataStreamsStatsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) ResizeRequest(org.opensearch.action.admin.indices.shrink.ResizeRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) IndexRequest(org.opensearch.action.index.IndexRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) OpenIndexResponse(org.opensearch.action.admin.indices.open.OpenIndexResponse) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with OpenIndexRequest

use of org.opensearch.action.admin.indices.open.OpenIndexRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testOpenNonExistentIndex.

public void testOpenNonExistentIndex() throws IOException {
    String nonExistentIndex = "non_existent_index";
    assertFalse(indexExists(nonExistentIndex));
    OpenIndexRequest openIndexRequest = new OpenIndexRequest(nonExistentIndex);
    OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(openIndexRequest, highLevelClient().indices()::open, highLevelClient().indices()::openAsync));
    assertEquals(RestStatus.NOT_FOUND, exception.status());
    OpenIndexRequest lenientOpenIndexRequest = new OpenIndexRequest(nonExistentIndex);
    lenientOpenIndexRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
    OpenIndexResponse lenientOpenIndexResponse = execute(lenientOpenIndexRequest, highLevelClient().indices()::open, highLevelClient().indices()::openAsync);
    assertThat(lenientOpenIndexResponse.isAcknowledged(), equalTo(true));
    OpenIndexRequest strictOpenIndexRequest = new OpenIndexRequest(nonExistentIndex);
    strictOpenIndexRequest.indicesOptions(IndicesOptions.strictExpandOpen());
    OpenSearchException strictException = expectThrows(OpenSearchException.class, () -> execute(openIndexRequest, highLevelClient().indices()::open, highLevelClient().indices()::openAsync));
    assertEquals(RestStatus.NOT_FOUND, strictException.status());
}
Also used : OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) OpenIndexResponse(org.opensearch.action.admin.indices.open.OpenIndexResponse) OpenSearchException(org.opensearch.OpenSearchException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 5 with OpenIndexRequest

use of org.opensearch.action.admin.indices.open.OpenIndexRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConvertersTests method testOpenIndexEmptyIndices.

public void testOpenIndexEmptyIndices() {
    String[] indices = OpenSearchTestCase.randomBoolean() ? null : Strings.EMPTY_ARRAY;
    ActionRequestValidationException validationException = new OpenIndexRequest(indices).validate();
    Assert.assertNotNull(validationException);
}
Also used : ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest)

Aggregations

OpenIndexRequest (org.opensearch.action.admin.indices.open.OpenIndexRequest)9 DeleteIndexRequest (org.opensearch.action.admin.indices.delete.DeleteIndexRequest)4 UpdateSettingsRequest (org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest)4 IndicesAliasesRequest (org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)3 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)3 ClearIndicesCacheRequest (org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest)3 FlushRequest (org.opensearch.action.admin.indices.flush.FlushRequest)3 ForceMergeRequest (org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest)3 RefreshRequest (org.opensearch.action.admin.indices.refresh.RefreshRequest)3 GetSettingsRequest (org.opensearch.action.admin.indices.settings.get.GetSettingsRequest)3 DeleteIndexTemplateRequest (org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)3 ValidateQueryRequest (org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest)3 AnalyzeRequest (org.opensearch.client.indices.AnalyzeRequest)3 CloseIndexRequest (org.opensearch.client.indices.CloseIndexRequest)3 CreateDataStreamRequest (org.opensearch.client.indices.CreateDataStreamRequest)3 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)3 DeleteAliasRequest (org.opensearch.client.indices.DeleteAliasRequest)3 DeleteDataStreamRequest (org.opensearch.client.indices.DeleteDataStreamRequest)3 GetDataStreamRequest (org.opensearch.client.indices.GetDataStreamRequest)3 GetFieldMappingsRequest (org.opensearch.client.indices.GetFieldMappingsRequest)3