Search in sources :

Example 1 with InvalidIndexNameException

use of org.opensearch.indices.InvalidIndexNameException in project OpenSearch by opensearch-project.

the class RestoreSnapshotIT method testRenameOnRestore.

public void testRenameOnRestore() throws Exception {
    Client client = client();
    createRepository("test-repo", "fs");
    createIndex("test-idx-1", "test-idx-2", "test-idx-3");
    ensureGreen();
    assertAcked(client.admin().indices().prepareAliases().addAlias("test-idx-1", "alias-1", false).addAlias("test-idx-2", "alias-2", false).addAlias("test-idx-3", "alias-3", false));
    indexRandomDocs("test-idx-1", 100);
    indexRandomDocs("test-idx-2", 100);
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx-1", "test-idx-2").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    logger.info("--> restore indices with different names");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+)").setRenameReplacement("$1-copy").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertDocCount("test-idx-1-copy", 100L);
    assertDocCount("test-idx-2-copy", 100L);
    logger.info("--> close just restored indices");
    client.admin().indices().prepareClose("test-idx-1-copy", "test-idx-2-copy").get();
    logger.info("--> and try to restore these indices again");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+)").setRenameReplacement("$1-copy").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertDocCount("test-idx-1-copy", 100L);
    assertDocCount("test-idx-2-copy", 100L);
    logger.info("--> close indices");
    assertAcked(client.admin().indices().prepareClose("test-idx-1", "test-idx-2-copy"));
    logger.info("--> restore indices with different names");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+-2)").setRenameReplacement("$1-copy").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    logger.info("--> delete indices");
    cluster().wipeIndices("test-idx-1", "test-idx-1-copy", "test-idx-2", "test-idx-2-copy");
    logger.info("--> try renaming indices using the same name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+)").setRenameReplacement("same-name").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices using the same name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("test-idx-2").setRenameReplacement("test-idx-1").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices using invalid index name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern(".+").setRenameReplacement("__WRONG__").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (InvalidIndexNameException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern(".+").setRenameReplacement("alias-3").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (InvalidIndexNameException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias of itself");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern("test-idx").setRenameReplacement("alias").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias of another restored index");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1", "test-idx-2").setRenamePattern("test-idx-1").setRenameReplacement("alias-2").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias of itself, but don't restore aliases ");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern("test-idx").setRenameReplacement("alias").setWaitForCompletion(true).setIncludeAliases(false).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
}
Also used : CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 2 with InvalidIndexNameException

use of org.opensearch.indices.InvalidIndexNameException in project OpenSearch by opensearch-project.

the class MetadataCreateIndexServiceTests method validateIndexName.

private void validateIndexName(MetadataCreateIndexService metadataCreateIndexService, String indexName, String errorMessage) {
    InvalidIndexNameException e = expectThrows(InvalidIndexNameException.class, () -> metadataCreateIndexService.validateIndexName(indexName, ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).build()));
    assertThat(e.getMessage(), endsWith(errorMessage));
}
Also used : InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException)

Example 3 with InvalidIndexNameException

use of org.opensearch.indices.InvalidIndexNameException in project OpenSearch by opensearch-project.

the class MetadataRolloverServiceTests method testValidation.

public void testValidation() throws Exception {
    final String rolloverTarget;
    final String sourceIndexName;
    final String defaultRolloverIndexName;
    final boolean useDataStream = randomBoolean();
    final Metadata.Builder builder = Metadata.builder();
    if (useDataStream) {
        DataStream dataStream = DataStreamTests.randomInstance();
        rolloverTarget = dataStream.getName();
        sourceIndexName = dataStream.getIndices().get(dataStream.getIndices().size() - 1).getName();
        defaultRolloverIndexName = DataStream.getDefaultBackingIndexName(dataStream.getName(), dataStream.getGeneration() + 1);
        ComposableIndexTemplate template = new ComposableIndexTemplate(Collections.singletonList(dataStream.getName() + "*"), null, null, null, null, null, new ComposableIndexTemplate.DataStreamTemplate());
        builder.put("template", template);
        for (Index index : dataStream.getIndices()) {
            builder.put(DataStreamTestHelper.getIndexMetadataBuilderForIndex(index));
        }
        builder.put(dataStream);
    } else {
        String indexPrefix = "logs-index-00000";
        rolloverTarget = "logs-alias";
        sourceIndexName = indexPrefix + "1";
        defaultRolloverIndexName = indexPrefix + "2";
        final IndexMetadata.Builder indexMetadata = IndexMetadata.builder(sourceIndexName).putAlias(AliasMetadata.builder(rolloverTarget).writeIndex(true).build()).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1);
        builder.put(indexMetadata);
    }
    final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metadata(builder).build();
    MetadataCreateIndexService createIndexService = mock(MetadataCreateIndexService.class);
    MetadataIndexAliasesService metadataIndexAliasesService = mock(MetadataIndexAliasesService.class);
    IndexNameExpressionResolver mockIndexNameExpressionResolver = mock(IndexNameExpressionResolver.class);
    when(mockIndexNameExpressionResolver.resolveDateMathExpression(any())).then(returnsFirstArg());
    MetadataRolloverService rolloverService = new MetadataRolloverService(null, createIndexService, metadataIndexAliasesService, mockIndexNameExpressionResolver);
    String newIndexName = useDataStream == false && randomBoolean() ? "logs-index-9" : null;
    MetadataRolloverService.RolloverResult rolloverResult = rolloverService.rolloverClusterState(clusterState, rolloverTarget, newIndexName, new CreateIndexRequest("_na_"), null, randomBoolean(), true);
    newIndexName = newIndexName == null ? defaultRolloverIndexName : newIndexName;
    assertEquals(sourceIndexName, rolloverResult.sourceIndexName);
    assertEquals(newIndexName, rolloverResult.rolloverIndexName);
    assertSame(rolloverResult.clusterState, clusterState);
    verify(createIndexService).validateIndexName(any(), same(clusterState));
    verifyNoMoreInteractions(createIndexService);
    verifyNoMoreInteractions(metadataIndexAliasesService);
    reset(createIndexService);
    doThrow(new InvalidIndexNameException("test", "invalid")).when(createIndexService).validateIndexName(any(), any());
    expectThrows(InvalidIndexNameException.class, () -> rolloverService.rolloverClusterState(clusterState, rolloverTarget, null, new CreateIndexRequest("_na_"), null, randomBoolean(), randomBoolean()));
    verify(createIndexService).validateIndexName(any(), same(clusterState));
    verifyNoMoreInteractions(createIndexService);
    verifyNoMoreInteractions(metadataIndexAliasesService);
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) ClusterState(org.opensearch.cluster.ClusterState) DataStream(org.opensearch.cluster.metadata.DataStream) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) MetadataCreateIndexService(org.opensearch.cluster.metadata.MetadataCreateIndexService) MetadataIndexAliasesService(org.opensearch.cluster.metadata.MetadataIndexAliasesService) ClusterName(org.opensearch.cluster.ClusterName) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest)

Example 4 with InvalidIndexNameException

use of org.opensearch.indices.InvalidIndexNameException in project OpenSearch by opensearch-project.

the class IndexNameExpressionResolverTests method testInvalidIndex.

public void testInvalidIndex() {
    Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test"));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
    IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.lenientExpandOpen(), false);
    InvalidIndexNameException iine = expectThrows(InvalidIndexNameException.class, () -> indexNameExpressionResolver.concreteIndexNames(context, "_foo"));
    assertEquals("Invalid index name [_foo], must not start with '_'.", iine.getMessage());
}
Also used : ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ClusterState(org.opensearch.cluster.ClusterState) ClusterName(org.opensearch.cluster.ClusterName) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException)

Aggregations

InvalidIndexNameException (org.opensearch.indices.InvalidIndexNameException)4 ClusterName (org.opensearch.cluster.ClusterName)2 ClusterState (org.opensearch.cluster.ClusterState)2 Matchers.containsString (org.hamcrest.Matchers.containsString)1 CreateSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)1 RestoreSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)1 CreateIndexRequest (org.opensearch.action.admin.indices.create.CreateIndexRequest)1 Client (org.opensearch.client.Client)1 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)1 ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)1 DataStream (org.opensearch.cluster.metadata.DataStream)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 IndexNameExpressionResolver (org.opensearch.cluster.metadata.IndexNameExpressionResolver)1 IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)1 Metadata (org.opensearch.cluster.metadata.Metadata)1 MetadataCreateIndexService (org.opensearch.cluster.metadata.MetadataCreateIndexService)1 MetadataIndexAliasesService (org.opensearch.cluster.metadata.MetadataIndexAliasesService)1 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)1 Index (org.opensearch.index.Index)1