Search in sources :

Example 1 with MetaStateService

use of org.opensearch.gateway.MetaStateService in project OpenSearch by opensearch-project.

the class IndicesServiceTests method testVerifyIfIndexContentDeleted.

public void testVerifyIfIndexContentDeleted() throws Exception {
    final Index index = new Index("test", UUIDs.randomBase64UUID());
    final IndicesService indicesService = getIndicesService();
    final NodeEnvironment nodeEnv = getNodeEnvironment();
    final MetaStateService metaStateService = getInstanceFromNode(MetaStateService.class);
    final ClusterService clusterService = getInstanceFromNode(ClusterService.class);
    final Settings idxSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()).build();
    final IndexMetadata indexMetadata = new IndexMetadata.Builder(index.getName()).settings(idxSettings).numberOfShards(1).numberOfReplicas(0).build();
    metaStateService.writeIndex("test index being created", indexMetadata);
    final Metadata metadata = Metadata.builder(clusterService.state().metadata()).put(indexMetadata, true).build();
    final ClusterState csWithIndex = new ClusterState.Builder(clusterService.state()).metadata(metadata).build();
    try {
        indicesService.verifyIndexIsDeleted(index, csWithIndex);
        fail("Should not be able to delete index contents when the index is part of the cluster state.");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Cannot delete index"));
    }
    final ClusterState withoutIndex = new ClusterState.Builder(csWithIndex).metadata(Metadata.builder(csWithIndex.metadata()).remove(index.getName())).build();
    indicesService.verifyIndexIsDeleted(index, withoutIndex);
    assertFalse("index files should be deleted", FileSystemUtils.exists(nodeEnv.indexPaths(index)));
}
Also used : MetaStateService(org.opensearch.gateway.MetaStateService) ClusterState(org.opensearch.cluster.ClusterState) ClusterService(org.opensearch.cluster.service.ClusterService) NodeEnvironment(org.opensearch.env.NodeEnvironment) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Index(org.opensearch.index.Index) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Aggregations

ClusterState (org.opensearch.cluster.ClusterState)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 Metadata (org.opensearch.cluster.metadata.Metadata)1 ClusterService (org.opensearch.cluster.service.ClusterService)1 Settings (org.opensearch.common.settings.Settings)1 NodeEnvironment (org.opensearch.env.NodeEnvironment)1 MetaStateService (org.opensearch.gateway.MetaStateService)1 Index (org.opensearch.index.Index)1 IndexSettings (org.opensearch.index.IndexSettings)1