Search in sources :

Example 6 with CreateIndexRequest

use of org.opensearch.action.admin.indices.create.CreateIndexRequest in project anomaly-detection by opensearch-project.

the class InitAnomalyDetectionIndicesTests method fixedPrimaryShardsIndexCreationTemplate.

@SuppressWarnings("unchecked")
private void fixedPrimaryShardsIndexCreationTemplate(String index) throws IOException {
    doAnswer(invocation -> {
        CreateIndexRequest request = invocation.getArgument(0);
        assertEquals(index, request.index());
        ActionListener<CreateIndexResponse> listener = (ActionListener<CreateIndexResponse>) invocation.getArgument(1);
        listener.onResponse(new CreateIndexResponse(true, true, index));
        return null;
    }).when(indicesClient).create(any(), any());
    ActionListener<CreateIndexResponse> listener = mock(ActionListener.class);
    if (index.equals(AnomalyDetector.ANOMALY_DETECTORS_INDEX)) {
        adIndices.initAnomalyDetectorIndexIfAbsent(listener);
    } else {
        adIndices.initDetectionStateIndex(listener);
    }
    ArgumentCaptor<CreateIndexResponse> captor = ArgumentCaptor.forClass(CreateIndexResponse.class);
    verify(listener).onResponse(captor.capture());
    CreateIndexResponse result = captor.getValue();
    assertEquals(index, result.index());
}
Also used : ActionListener(org.opensearch.action.ActionListener) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse)

Example 7 with CreateIndexRequest

use of org.opensearch.action.admin.indices.create.CreateIndexRequest in project anomaly-detection by opensearch-project.

the class PreviewAnomalyDetectorTransportActionTests method testPreviewTransportActionWithIndex.

@Test
public void testPreviewTransportActionWithIndex() throws IOException, InterruptedException {
    // When AD index exists, and detector does not exist
    final CountDownLatch inProgressLatch = new CountDownLatch(1);
    PreviewAnomalyDetectorRequest request = new PreviewAnomalyDetectorRequest(null, "1234", Instant.now(), Instant.now());
    Settings indexSettings = Settings.builder().put("index.number_of_shards", 5).put("index.number_of_replicas", 1).build();
    CreateIndexRequest indexRequest = new CreateIndexRequest(AnomalyDetector.ANOMALY_DETECTORS_INDEX, indexSettings);
    client().admin().indices().create(indexRequest).actionGet();
    ActionListener<PreviewAnomalyDetectorResponse> previewResponse = new ActionListener<PreviewAnomalyDetectorResponse>() {

        @Override
        public void onResponse(PreviewAnomalyDetectorResponse response) {
            Assert.assertTrue(false);
        }

        @Override
        public void onFailure(Exception e) {
            Assert.assertTrue(e.getMessage().contains("Can't find anomaly detector with id:1234"));
            inProgressLatch.countDown();
        }
    };
    action.doExecute(task, request, previewResponse);
    assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Also used : ActionListener(org.opensearch.action.ActionListener) CountDownLatch(java.util.concurrent.CountDownLatch) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) Settings(org.opensearch.common.settings.Settings) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with CreateIndexRequest

use of org.opensearch.action.admin.indices.create.CreateIndexRequest in project OpenSearch by opensearch-project.

the class TransportBulkAction method createIndex.

void createIndex(String index, TimeValue timeout, Version minNodeVersion, ActionListener<CreateIndexResponse> listener) {
    CreateIndexRequest createIndexRequest = new CreateIndexRequest();
    createIndexRequest.index(index);
    createIndexRequest.cause("auto(bulk api)");
    createIndexRequest.masterNodeTimeout(timeout);
    if (minNodeVersion.onOrAfter(LegacyESVersion.V_7_8_0)) {
        client.execute(AutoCreateAction.INSTANCE, createIndexRequest, listener);
    } else {
        client.admin().indices().create(createIndexRequest, listener);
    }
}
Also used : CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest)

Example 9 with CreateIndexRequest

use of org.opensearch.action.admin.indices.create.CreateIndexRequest in project OpenSearch by opensearch-project.

the class TransportResizeAction method prepareCreateIndexRequest.

// static for unittesting this method
static CreateIndexClusterStateUpdateRequest prepareCreateIndexRequest(final ResizeRequest resizeRequest, final ClusterState state, final IntFunction<DocsStats> perShardDocStats, String sourceIndexName, String targetIndexName) {
    final CreateIndexRequest targetIndex = resizeRequest.getTargetIndexRequest();
    final IndexMetadata metadata = state.metadata().index(sourceIndexName);
    if (metadata == null) {
        throw new IndexNotFoundException(sourceIndexName);
    }
    final Settings.Builder targetIndexSettingsBuilder = Settings.builder().put(targetIndex.settings()).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX);
    targetIndexSettingsBuilder.remove(IndexMetadata.SETTING_HISTORY_UUID);
    final Settings targetIndexSettings = targetIndexSettingsBuilder.build();
    final int numShards;
    if (IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.exists(targetIndexSettings)) {
        numShards = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(targetIndexSettings);
    } else {
        assert resizeRequest.getResizeType() != ResizeType.SPLIT : "split must specify the number of shards explicitly";
        if (resizeRequest.getResizeType() == ResizeType.SHRINK) {
            numShards = 1;
        } else {
            assert resizeRequest.getResizeType() == ResizeType.CLONE;
            numShards = metadata.getNumberOfShards();
        }
    }
    for (int i = 0; i < numShards; i++) {
        if (resizeRequest.getResizeType() == ResizeType.SHRINK) {
            Set<ShardId> shardIds = IndexMetadata.selectShrinkShards(i, metadata, numShards);
            long count = 0;
            for (ShardId id : shardIds) {
                DocsStats docsStats = perShardDocStats.apply(id.id());
                if (docsStats != null) {
                    count += docsStats.getCount();
                }
                if (count > IndexWriter.MAX_DOCS) {
                    throw new IllegalStateException("Can't merge index with more than [" + IndexWriter.MAX_DOCS + "] docs - too many documents in shards " + shardIds);
                }
            }
        } else if (resizeRequest.getResizeType() == ResizeType.SPLIT) {
            Objects.requireNonNull(IndexMetadata.selectSplitShard(i, metadata, numShards));
        // we just execute this to ensure we get the right exceptions if the number of shards is wrong or less then etc.
        } else {
            Objects.requireNonNull(IndexMetadata.selectCloneShard(i, metadata, numShards));
        // we just execute this to ensure we get the right exceptions if the number of shards is wrong etc.
        }
    }
    if (IndexMetadata.INDEX_ROUTING_PARTITION_SIZE_SETTING.exists(targetIndexSettings)) {
        throw new IllegalArgumentException("cannot provide a routing partition size value when resizing an index");
    }
    if (IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.exists(targetIndexSettings)) {
        // if we have a source index with 1 shards it's legal to set this
        final boolean splitFromSingleShards = resizeRequest.getResizeType() == ResizeType.SPLIT && metadata.getNumberOfShards() == 1;
        if (splitFromSingleShards == false) {
            throw new IllegalArgumentException("cannot provide index.number_of_routing_shards on resize");
        }
    }
    if (IndexSettings.INDEX_SOFT_DELETES_SETTING.get(metadata.getSettings()) && IndexSettings.INDEX_SOFT_DELETES_SETTING.exists(targetIndexSettings) && IndexSettings.INDEX_SOFT_DELETES_SETTING.get(targetIndexSettings) == false) {
        throw new IllegalArgumentException("Can't disable [index.soft_deletes.enabled] setting on resize");
    }
    String cause = resizeRequest.getResizeType().name().toLowerCase(Locale.ROOT) + "_index";
    targetIndex.cause(cause);
    Settings.Builder settingsBuilder = Settings.builder().put(targetIndexSettings);
    settingsBuilder.put("index.number_of_shards", numShards);
    targetIndex.settings(settingsBuilder);
    return new CreateIndexClusterStateUpdateRequest(cause, targetIndex.index(), targetIndexName).ackTimeout(targetIndex.timeout()).masterNodeTimeout(targetIndex.masterNodeTimeout()).settings(targetIndex.settings()).aliases(targetIndex.aliases()).waitForActiveShards(targetIndex.waitForActiveShards()).recoverFrom(metadata.getIndex()).resizeType(resizeRequest.getResizeType()).copySettings(resizeRequest.getCopySettings() == null ? false : resizeRequest.getCopySettings());
}
Also used : ShardId(org.opensearch.index.shard.ShardId) CreateIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) DocsStats(org.opensearch.index.shard.DocsStats) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 10 with CreateIndexRequest

use of org.opensearch.action.admin.indices.create.CreateIndexRequest in project OpenSearch by opensearch-project.

the class MetadataRolloverServiceTests method testAliasValidation.

public void testAliasValidation() {
    String index1 = randomAlphaOfLength(10);
    String aliasWithWriteIndex = randomAlphaOfLength(10);
    String index2 = randomAlphaOfLength(10);
    String aliasWithNoWriteIndex = randomAlphaOfLength(10);
    Boolean firstIsWriteIndex = randomFrom(false, null);
    final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build();
    Metadata.Builder metadataBuilder = Metadata.builder().put(IndexMetadata.builder(index1).settings(settings).putAlias(AliasMetadata.builder(aliasWithWriteIndex)).putAlias(AliasMetadata.builder(aliasWithNoWriteIndex).writeIndex(firstIsWriteIndex)));
    IndexMetadata.Builder indexTwoBuilder = IndexMetadata.builder(index2).settings(settings);
    if (firstIsWriteIndex == null) {
        indexTwoBuilder.putAlias(AliasMetadata.builder(aliasWithNoWriteIndex).writeIndex(randomFrom(false, null)));
    }
    metadataBuilder.put(indexTwoBuilder);
    Metadata metadata = metadataBuilder.build();
    CreateIndexRequest req = new CreateIndexRequest();
    IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.validate(metadata, aliasWithNoWriteIndex, randomAlphaOfLength(5), req));
    assertThat(exception.getMessage(), equalTo("rollover target [" + aliasWithNoWriteIndex + "] does not point to a write index"));
    exception = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.validate(metadata, randomFrom(index1, index2), randomAlphaOfLength(5), req));
    assertThat(exception.getMessage(), equalTo("rollover target is a [concrete index] but one of [alias,data_stream] was expected"));
    final String aliasName = randomAlphaOfLength(5);
    exception = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.validate(metadata, aliasName, randomAlphaOfLength(5), req));
    assertThat(exception.getMessage(), equalTo("rollover target [" + aliasName + "] does not exist"));
    MetadataRolloverService.validate(metadata, aliasWithWriteIndex, randomAlphaOfLength(5), req);
}
Also used : Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Settings(org.opensearch.common.settings.Settings)

Aggregations

CreateIndexRequest (org.opensearch.action.admin.indices.create.CreateIndexRequest)79 IndexRequest (org.opensearch.action.index.IndexRequest)34 Settings (org.opensearch.common.settings.Settings)31 Client (org.opensearch.client.Client)29 Test (org.junit.Test)27 SingleClusterTest (org.opensearch.security.test.SingleClusterTest)22 RestHelper (org.opensearch.security.test.helper.rest.RestHelper)19 IOException (java.io.IOException)18 HttpResponse (org.opensearch.security.test.helper.rest.RestHelper.HttpResponse)15 CreateIndexResponse (org.opensearch.action.admin.indices.create.CreateIndexResponse)13 DynamicSecurityConfig (org.opensearch.security.test.DynamicSecurityConfig)13 IndicesAliasesRequest (org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)12 ActionListener (org.opensearch.action.ActionListener)11 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)10 ThreadPool (org.opensearch.threadpool.ThreadPool)10 Alias (org.opensearch.action.admin.indices.alias.Alias)9 ClusterState (org.opensearch.cluster.ClusterState)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7