Search in sources :

Example 1 with INDEX_READ_ONLY_BLOCK

use of org.opensearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_BLOCK in project OpenSearch by opensearch-project.

the class MetadataCreateIndexServiceTests method testClusterStateCreateIndex.

public void testClusterStateCreateIndex() {
    ClusterState currentClusterState = ClusterState.builder(ClusterState.EMPTY_STATE).build();
    IndexMetadata newIndexMetadata = IndexMetadata.builder("test").settings(settings(Version.CURRENT).put(SETTING_READ_ONLY, true)).numberOfShards(1).numberOfReplicas(0).putAlias(AliasMetadata.builder("alias1").writeIndex(true).build()).build();
    // used as a value container, not for the concurrency and visibility guarantees
    AtomicBoolean allocationRerouted = new AtomicBoolean(false);
    BiFunction<ClusterState, String, ClusterState> rerouteRoutingTable = (clusterState, reason) -> {
        allocationRerouted.compareAndSet(false, true);
        return clusterState;
    };
    ClusterState updatedClusterState = clusterStateCreateIndex(currentClusterState, org.opensearch.common.collect.Set.of(INDEX_READ_ONLY_BLOCK), newIndexMetadata, rerouteRoutingTable, null);
    assertThat(updatedClusterState.blocks().getIndexBlockWithId("test", INDEX_READ_ONLY_BLOCK.id()), is(INDEX_READ_ONLY_BLOCK));
    assertThat(updatedClusterState.routingTable().index("test"), is(notNullValue()));
    assertThat(allocationRerouted.get(), is(true));
}
Also used : ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) Arrays(java.util.Arrays) MetadataCreateIndexService.resolveAndValidateAliases(org.opensearch.cluster.metadata.MetadataCreateIndexService.resolveAndValidateAliases) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) TestThreadPool(org.opensearch.threadpool.TestThreadPool) INDEX_NUMBER_OF_SHARDS_SETTING(org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING) Version(org.opensearch.Version) MetadataCreateIndexService.getIndexNumberOfRoutingShards(org.opensearch.cluster.metadata.MetadataCreateIndexService.getIndexNumberOfRoutingShards) Matchers.hasValue(org.hamcrest.Matchers.hasValue) INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING(org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING) Strings(org.opensearch.common.Strings) Collections.singletonList(java.util.Collections.singletonList) INDEX_SOFT_DELETES_SETTING(org.opensearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING) Alias(org.opensearch.action.admin.indices.alias.Alias) OpenSearchAllocationTestCase(org.opensearch.cluster.OpenSearchAllocationTestCase) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) IndexModule(org.opensearch.index.IndexModule) AllocationDeciders(org.opensearch.cluster.routing.allocation.decider.AllocationDeciders) TimeValue(org.opensearch.common.unit.TimeValue) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Index(org.opensearch.index.Index) MetadataCreateIndexService.parseV1Mappings(org.opensearch.cluster.metadata.MetadataCreateIndexService.parseV1Mappings) ExceptionsHelper(org.opensearch.ExceptionsHelper) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) Matchers.startsWith(org.hamcrest.Matchers.startsWith) Stream(java.util.stream.Stream) SystemIndexDescriptor(org.opensearch.indices.SystemIndexDescriptor) Matchers.is(org.hamcrest.Matchers.is) Matchers.endsWith(org.hamcrest.Matchers.endsWith) MetadataCreateIndexService.aggregateIndexSettings(org.opensearch.cluster.metadata.MetadataCreateIndexService.aggregateIndexSettings) ThreadPool(org.opensearch.threadpool.ThreadPool) ArrayList(java.util.ArrayList) ClusterState(org.opensearch.cluster.ClusterState) LegacyESVersion(org.opensearch.LegacyESVersion) VersionUtils(org.opensearch.test.VersionUtils) INDEX_READ_ONLY_BLOCK(org.opensearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_BLOCK) BiConsumer(java.util.function.BiConsumer) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) Before(org.junit.Before) Environment(org.opensearch.env.Environment) IOException(java.io.IOException) SystemIndices(org.opensearch.indices.SystemIndices) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) RoutingTable(org.opensearch.cluster.routing.RoutingTable) SETTING_NUMBER_OF_SHARDS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS) CreateIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) InvalidAliasNameException(org.opensearch.indices.InvalidAliasNameException) SETTING_VERSION_CREATED(org.opensearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED) BiFunction(java.util.function.BiFunction) Matchers.hasKey(org.hamcrest.Matchers.hasKey) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MapperService(org.opensearch.index.mapper.MapperService) Collections.singleton(java.util.Collections.singleton) XContentFactory(org.opensearch.common.xcontent.XContentFactory) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) SETTING_NUMBER_OF_REPLICAS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS) Collections.emptyList(java.util.Collections.emptyList) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) MetadataCreateIndexService.clusterStateCreateIndex(org.opensearch.cluster.metadata.MetadataCreateIndexService.clusterStateCreateIndex) SETTING_READ_ONLY(org.opensearch.cluster.metadata.IndexMetadata.SETTING_READ_ONLY) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) List(java.util.List) ShardLimitValidatorTests.createTestShardLimitService(org.opensearch.indices.ShardLimitValidatorTests.createTestShardLimitService) Matchers.equalTo(org.hamcrest.Matchers.equalTo) EmptyClusterInfoService(org.opensearch.cluster.EmptyClusterInfoService) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) BigArrays(org.opensearch.common.util.BigArrays) ClusterServiceUtils(org.opensearch.test.ClusterServiceUtils) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) CompressedXContent(org.opensearch.common.compress.CompressedXContent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) ResizeType(org.opensearch.action.admin.indices.shrink.ResizeType) Setting(org.opensearch.common.settings.Setting) ShardLimitValidator(org.opensearch.indices.ShardLimitValidator) MaxRetryAllocationDecider(org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Matchers(org.hamcrest.Matchers) Consumer(java.util.function.Consumer) EmptySnapshotsInfoService(org.opensearch.snapshots.EmptySnapshotsInfoService) ClusterName(org.opensearch.cluster.ClusterName) Comparator(java.util.Comparator) Collections(java.util.Collections) MetadataCreateIndexService.buildIndexMetadata(org.opensearch.cluster.metadata.MetadataCreateIndexService.buildIndexMetadata) ClusterState(org.opensearch.cluster.ClusterState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MetadataCreateIndexService.buildIndexMetadata(org.opensearch.cluster.metadata.MetadataCreateIndexService.buildIndexMetadata)

Example 2 with INDEX_READ_ONLY_BLOCK

use of org.opensearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_BLOCK in project OpenSearch by opensearch-project.

the class MetadataCreateIndexServiceTests method testClusterStateCreateIndexWithMetadataTransaction.

public void testClusterStateCreateIndexWithMetadataTransaction() {
    ClusterState currentClusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().put(IndexMetadata.builder("my-index").settings(settings(Version.CURRENT).put(SETTING_READ_ONLY, true)).numberOfShards(1).numberOfReplicas(0))).build();
    IndexMetadata newIndexMetadata = IndexMetadata.builder("test").settings(settings(Version.CURRENT).put(SETTING_READ_ONLY, true)).numberOfShards(1).numberOfReplicas(0).putAlias(AliasMetadata.builder("alias1").writeIndex(true).build()).build();
    // adds alias from new index to existing index
    BiConsumer<Metadata.Builder, IndexMetadata> metadataTransformer = (builder, indexMetadata) -> {
        AliasMetadata newAlias = indexMetadata.getAliases().iterator().next().value;
        IndexMetadata myIndex = builder.get("my-index");
        builder.put(IndexMetadata.builder(myIndex).putAlias(AliasMetadata.builder(newAlias.getAlias()).build()));
    };
    ClusterState updatedClusterState = clusterStateCreateIndex(currentClusterState, org.opensearch.common.collect.Set.of(INDEX_READ_ONLY_BLOCK), newIndexMetadata, (clusterState, y) -> clusterState, metadataTransformer);
    assertTrue(updatedClusterState.metadata().findAllAliases(new String[] { "my-index" }).containsKey("my-index"));
    assertNotNull(updatedClusterState.metadata().findAllAliases(new String[] { "my-index" }).get("my-index"));
    assertNotNull(updatedClusterState.metadata().findAllAliases(new String[] { "my-index" }).get("my-index").get(0).alias(), equalTo("alias1"));
}
Also used : ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) Arrays(java.util.Arrays) MetadataCreateIndexService.resolveAndValidateAliases(org.opensearch.cluster.metadata.MetadataCreateIndexService.resolveAndValidateAliases) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) TestThreadPool(org.opensearch.threadpool.TestThreadPool) INDEX_NUMBER_OF_SHARDS_SETTING(org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING) Version(org.opensearch.Version) MetadataCreateIndexService.getIndexNumberOfRoutingShards(org.opensearch.cluster.metadata.MetadataCreateIndexService.getIndexNumberOfRoutingShards) Matchers.hasValue(org.hamcrest.Matchers.hasValue) INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING(org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING) Strings(org.opensearch.common.Strings) Collections.singletonList(java.util.Collections.singletonList) INDEX_SOFT_DELETES_SETTING(org.opensearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING) Alias(org.opensearch.action.admin.indices.alias.Alias) OpenSearchAllocationTestCase(org.opensearch.cluster.OpenSearchAllocationTestCase) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) IndexModule(org.opensearch.index.IndexModule) AllocationDeciders(org.opensearch.cluster.routing.allocation.decider.AllocationDeciders) TimeValue(org.opensearch.common.unit.TimeValue) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Index(org.opensearch.index.Index) MetadataCreateIndexService.parseV1Mappings(org.opensearch.cluster.metadata.MetadataCreateIndexService.parseV1Mappings) ExceptionsHelper(org.opensearch.ExceptionsHelper) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) Matchers.startsWith(org.hamcrest.Matchers.startsWith) Stream(java.util.stream.Stream) SystemIndexDescriptor(org.opensearch.indices.SystemIndexDescriptor) Matchers.is(org.hamcrest.Matchers.is) Matchers.endsWith(org.hamcrest.Matchers.endsWith) MetadataCreateIndexService.aggregateIndexSettings(org.opensearch.cluster.metadata.MetadataCreateIndexService.aggregateIndexSettings) ThreadPool(org.opensearch.threadpool.ThreadPool) ArrayList(java.util.ArrayList) ClusterState(org.opensearch.cluster.ClusterState) LegacyESVersion(org.opensearch.LegacyESVersion) VersionUtils(org.opensearch.test.VersionUtils) INDEX_READ_ONLY_BLOCK(org.opensearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_BLOCK) BiConsumer(java.util.function.BiConsumer) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) Before(org.junit.Before) Environment(org.opensearch.env.Environment) IOException(java.io.IOException) SystemIndices(org.opensearch.indices.SystemIndices) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) RoutingTable(org.opensearch.cluster.routing.RoutingTable) SETTING_NUMBER_OF_SHARDS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS) CreateIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) InvalidAliasNameException(org.opensearch.indices.InvalidAliasNameException) SETTING_VERSION_CREATED(org.opensearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED) BiFunction(java.util.function.BiFunction) Matchers.hasKey(org.hamcrest.Matchers.hasKey) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MapperService(org.opensearch.index.mapper.MapperService) Collections.singleton(java.util.Collections.singleton) XContentFactory(org.opensearch.common.xcontent.XContentFactory) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) SETTING_NUMBER_OF_REPLICAS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS) Collections.emptyList(java.util.Collections.emptyList) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) MetadataCreateIndexService.clusterStateCreateIndex(org.opensearch.cluster.metadata.MetadataCreateIndexService.clusterStateCreateIndex) SETTING_READ_ONLY(org.opensearch.cluster.metadata.IndexMetadata.SETTING_READ_ONLY) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) List(java.util.List) ShardLimitValidatorTests.createTestShardLimitService(org.opensearch.indices.ShardLimitValidatorTests.createTestShardLimitService) Matchers.equalTo(org.hamcrest.Matchers.equalTo) EmptyClusterInfoService(org.opensearch.cluster.EmptyClusterInfoService) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) BigArrays(org.opensearch.common.util.BigArrays) ClusterServiceUtils(org.opensearch.test.ClusterServiceUtils) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) CompressedXContent(org.opensearch.common.compress.CompressedXContent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) ResizeType(org.opensearch.action.admin.indices.shrink.ResizeType) Setting(org.opensearch.common.settings.Setting) ShardLimitValidator(org.opensearch.indices.ShardLimitValidator) MaxRetryAllocationDecider(org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Matchers(org.hamcrest.Matchers) Consumer(java.util.function.Consumer) EmptySnapshotsInfoService(org.opensearch.snapshots.EmptySnapshotsInfoService) ClusterName(org.opensearch.cluster.ClusterName) Comparator(java.util.Comparator) Collections(java.util.Collections) MetadataCreateIndexService.buildIndexMetadata(org.opensearch.cluster.metadata.MetadataCreateIndexService.buildIndexMetadata) ClusterState(org.opensearch.cluster.ClusterState) MetadataCreateIndexService.buildIndexMetadata(org.opensearch.cluster.metadata.MetadataCreateIndexService.buildIndexMetadata)

Aggregations

ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Collections.emptyList (java.util.Collections.emptyList)2 Collections.emptyMap (java.util.Collections.emptyMap)2 Collections.singleton (java.util.Collections.singleton)2 Collections.singletonList (java.util.Collections.singletonList)2 Collections.singletonMap (java.util.Collections.singletonMap)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 UUID (java.util.UUID)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 BiConsumer (java.util.function.BiConsumer)2