Search in sources :

Example 1 with State

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

the class IndexNameExpressionResolverTests method testConcreteWriteIndexWithInvalidIndicesRequest.

public void testConcreteWriteIndexWithInvalidIndicesRequest() {
    Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test-0").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias")));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
    Function<String[], IndicesRequest> requestGen = (indices) -> new IndicesRequest() {

        @Override
        public String[] indices() {
            return indices;
        }

        @Override
        public IndicesOptions indicesOptions() {
            return IndicesOptions.strictSingleIndexNoExpandForbidClosed();
        }

        @Override
        public boolean includeDataStreams() {
            return false;
        }
    };
    IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> indexNameExpressionResolver.concreteWriteIndex(state, requestGen.apply(null)));
    assertThat(exception.getMessage(), equalTo("indices request must specify a single index expression"));
    exception = expectThrows(IllegalArgumentException.class, () -> indexNameExpressionResolver.concreteWriteIndex(state, requestGen.apply(new String[] { "too", "many" })));
    assertThat(exception.getMessage(), equalTo("indices request must specify a single index expression"));
}
Also used : Matchers.arrayContaining(org.hamcrest.Matchers.arrayContaining) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) Arrays(java.util.Arrays) State(org.opensearch.cluster.metadata.IndexMetadata.State) Matchers.emptyArray(org.hamcrest.Matchers.emptyArray) DocWriteRequest(org.opensearch.action.DocWriteRequest) Version(org.opensearch.Version) IndicesOptions(org.opensearch.action.support.IndicesOptions) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Function(java.util.function.Function) INDEX_HIDDEN_SETTING(org.opensearch.cluster.metadata.IndexMetadata.INDEX_HIDDEN_SETTING) Matchers.arrayContainingInAnyOrder(org.hamcrest.Matchers.arrayContainingInAnyOrder) Strings(org.opensearch.common.Strings) HashSet(java.util.HashSet) Sets.newHashSet(org.opensearch.common.util.set.Sets.newHashSet) IndicesRequest(org.opensearch.action.IndicesRequest) ClusterState(org.opensearch.cluster.ClusterState) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) IndexClosedException(org.opensearch.indices.IndexClosedException) DataStreamTestHelper.createBackingIndex(org.opensearch.cluster.DataStreamTestHelper.createBackingIndex) SearchRequest(org.opensearch.action.search.SearchRequest) EnumSet(java.util.EnumSet) DeleteRequest(org.opensearch.action.delete.DeleteRequest) Matchers.empty(org.hamcrest.Matchers.empty) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Index(org.opensearch.index.Index) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Collectors(java.util.stream.Collectors) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) ClusterName(org.opensearch.cluster.ClusterName) UpdateRequest(org.opensearch.action.update.UpdateRequest) Matchers.is(org.hamcrest.Matchers.is) IndexRequest(org.opensearch.action.index.IndexRequest) Comparator(java.util.Comparator) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.endsWith(org.hamcrest.Matchers.endsWith) DataStreamTestHelper.createTimestampField(org.opensearch.cluster.DataStreamTestHelper.createTimestampField) SYSTEM_INDEX_ACCESS_CONTROL_HEADER_KEY(org.opensearch.cluster.metadata.IndexNameExpressionResolver.SYSTEM_INDEX_ACCESS_CONTROL_HEADER_KEY) ClusterState(org.opensearch.cluster.ClusterState) ClusterName(org.opensearch.cluster.ClusterName) IndicesRequest(org.opensearch.action.IndicesRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with State

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

the class IndexNameExpressionResolverTests method testIndexAliases.

public void testIndexAliases() {
    Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test-0").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias-0").filter("{ \"term\": \"foo\"}")).putAlias(AliasMetadata.builder("test-alias-1").filter("{ \"term\": \"foo\"}")).putAlias(AliasMetadata.builder("test-alias-non-filtering")));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
    Set<String> resolvedExpressions = indexNameExpressionResolver.resolveExpressions(state, "test-*");
    String[] strings = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, true, resolvedExpressions);
    Arrays.sort(strings);
    assertArrayEquals(new String[] { "test-alias-0", "test-alias-1", "test-alias-non-filtering" }, strings);
    strings = indexNameExpressionResolver.indexAliases(state, "test-0", x -> x.alias().equals("test-alias-1"), true, resolvedExpressions);
    assertArrayEquals(null, strings);
}
Also used : Matchers.arrayContaining(org.hamcrest.Matchers.arrayContaining) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) Arrays(java.util.Arrays) State(org.opensearch.cluster.metadata.IndexMetadata.State) Matchers.emptyArray(org.hamcrest.Matchers.emptyArray) DocWriteRequest(org.opensearch.action.DocWriteRequest) Version(org.opensearch.Version) IndicesOptions(org.opensearch.action.support.IndicesOptions) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Function(java.util.function.Function) INDEX_HIDDEN_SETTING(org.opensearch.cluster.metadata.IndexMetadata.INDEX_HIDDEN_SETTING) Matchers.arrayContainingInAnyOrder(org.hamcrest.Matchers.arrayContainingInAnyOrder) Strings(org.opensearch.common.Strings) HashSet(java.util.HashSet) Sets.newHashSet(org.opensearch.common.util.set.Sets.newHashSet) IndicesRequest(org.opensearch.action.IndicesRequest) ClusterState(org.opensearch.cluster.ClusterState) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) IndexClosedException(org.opensearch.indices.IndexClosedException) DataStreamTestHelper.createBackingIndex(org.opensearch.cluster.DataStreamTestHelper.createBackingIndex) SearchRequest(org.opensearch.action.search.SearchRequest) EnumSet(java.util.EnumSet) DeleteRequest(org.opensearch.action.delete.DeleteRequest) Matchers.empty(org.hamcrest.Matchers.empty) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Index(org.opensearch.index.Index) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Collectors(java.util.stream.Collectors) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) ClusterName(org.opensearch.cluster.ClusterName) UpdateRequest(org.opensearch.action.update.UpdateRequest) Matchers.is(org.hamcrest.Matchers.is) IndexRequest(org.opensearch.action.index.IndexRequest) Comparator(java.util.Comparator) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.endsWith(org.hamcrest.Matchers.endsWith) DataStreamTestHelper.createTimestampField(org.opensearch.cluster.DataStreamTestHelper.createTimestampField) SYSTEM_INDEX_ACCESS_CONTROL_HEADER_KEY(org.opensearch.cluster.metadata.IndexNameExpressionResolver.SYSTEM_INDEX_ACCESS_CONTROL_HEADER_KEY) ClusterState(org.opensearch.cluster.ClusterState) ClusterName(org.opensearch.cluster.ClusterName) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with State

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

the class AwarenessAllocationIT method testSimpleAwareness.

public void testSimpleAwareness() throws Exception {
    Settings commonSettings = Settings.builder().put("cluster.routing.allocation.awareness.attributes", "rack_id").build();
    logger.info("--> starting 2 nodes on the same rack");
    internalCluster().startNodes(2, Settings.builder().put(commonSettings).put("node.attr.rack_id", "rack_1").build());
    createIndex("test1");
    createIndex("test2");
    NumShards test1 = getNumShards("test1");
    NumShards test2 = getNumShards("test2");
    // no replicas will be allocated as both indices end up on a single node
    final int totalPrimaries = test1.numPrimaries + test2.numPrimaries;
    ensureGreen();
    final List<String> indicesToClose = randomSubsetOf(Arrays.asList("test1", "test2"));
    indicesToClose.forEach(indexToClose -> assertAcked(client().admin().indices().prepareClose(indexToClose).get()));
    logger.info("--> starting 1 node on a different rack");
    final String node3 = internalCluster().startNode(Settings.builder().put(commonSettings).put("node.attr.rack_id", "rack_2").build());
    // On slow machines the initial relocation might be delayed
    assertBusy(() -> {
        logger.info("--> waiting for no relocation");
        ClusterHealthResponse clusterHealth = client().admin().cluster().prepareHealth().setIndices("test1", "test2").setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("3").setWaitForNoRelocatingShards(true).get();
        assertThat("Cluster health request timed out", clusterHealth.isTimedOut(), equalTo(false));
        logger.info("--> checking current state");
        ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
        // check that closed indices are effectively closed
        final List<String> notClosedIndices = indicesToClose.stream().filter(index -> clusterState.metadata().index(index).getState() != State.CLOSE).collect(Collectors.toList());
        assertThat("Some indices not closed", notClosedIndices, empty());
        // verify that we have all the primaries on node3
        ObjectIntHashMap<String> counts = new ObjectIntHashMap<>();
        for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
            for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
                for (ShardRouting shardRouting : indexShardRoutingTable) {
                    counts.addTo(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1);
                }
            }
        }
        assertThat(counts.get(node3), equalTo(totalPrimaries));
    }, 10, TimeUnit.SECONDS);
}
Also used : IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) Arrays(java.util.Arrays) Matchers.empty(org.hamcrest.Matchers.empty) State(org.opensearch.cluster.metadata.IndexMetadata.State) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AwarenessAllocationDecider(org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider) ClusterScope(org.opensearch.test.OpenSearchIntegTestCase.ClusterScope) Priority(org.opensearch.common.Priority) Settings(org.opensearch.common.settings.Settings) Collectors(java.util.stream.Collectors) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TimeUnit(java.util.concurrent.TimeUnit) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) ClusterState(org.opensearch.cluster.ClusterState) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.anyOf(org.hamcrest.Matchers.anyOf) LogManager(org.apache.logging.log4j.LogManager) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) ClusterState(org.opensearch.cluster.ClusterState) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) ShardRouting(org.opensearch.cluster.routing.ShardRouting) Settings(org.opensearch.common.settings.Settings)

Example 4 with State

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

the class IndexNameExpressionResolverTests method testIndexAliasesSkipIdentity.

public void testIndexAliasesSkipIdentity() {
    Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test-0").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias")).putAlias(AliasMetadata.builder("other-alias")));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
    Set<String> resolvedExpressions = new HashSet<>(Arrays.asList("test-0", "test-alias"));
    String[] aliases = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, false, resolvedExpressions);
    assertNull(aliases);
    aliases = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, true, resolvedExpressions);
    assertArrayEquals(new String[] { "test-alias" }, aliases);
    resolvedExpressions = Collections.singleton("other-alias");
    aliases = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, false, resolvedExpressions);
    assertArrayEquals(new String[] { "other-alias" }, aliases);
    aliases = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, true, resolvedExpressions);
    assertArrayEquals(new String[] { "other-alias" }, aliases);
}
Also used : Matchers.arrayContaining(org.hamcrest.Matchers.arrayContaining) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) Arrays(java.util.Arrays) State(org.opensearch.cluster.metadata.IndexMetadata.State) Matchers.emptyArray(org.hamcrest.Matchers.emptyArray) DocWriteRequest(org.opensearch.action.DocWriteRequest) Version(org.opensearch.Version) IndicesOptions(org.opensearch.action.support.IndicesOptions) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Function(java.util.function.Function) INDEX_HIDDEN_SETTING(org.opensearch.cluster.metadata.IndexMetadata.INDEX_HIDDEN_SETTING) Matchers.arrayContainingInAnyOrder(org.hamcrest.Matchers.arrayContainingInAnyOrder) Strings(org.opensearch.common.Strings) HashSet(java.util.HashSet) Sets.newHashSet(org.opensearch.common.util.set.Sets.newHashSet) IndicesRequest(org.opensearch.action.IndicesRequest) ClusterState(org.opensearch.cluster.ClusterState) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) IndexClosedException(org.opensearch.indices.IndexClosedException) DataStreamTestHelper.createBackingIndex(org.opensearch.cluster.DataStreamTestHelper.createBackingIndex) SearchRequest(org.opensearch.action.search.SearchRequest) EnumSet(java.util.EnumSet) DeleteRequest(org.opensearch.action.delete.DeleteRequest) Matchers.empty(org.hamcrest.Matchers.empty) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Index(org.opensearch.index.Index) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Collectors(java.util.stream.Collectors) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) ClusterName(org.opensearch.cluster.ClusterName) UpdateRequest(org.opensearch.action.update.UpdateRequest) Matchers.is(org.hamcrest.Matchers.is) IndexRequest(org.opensearch.action.index.IndexRequest) Comparator(java.util.Comparator) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.endsWith(org.hamcrest.Matchers.endsWith) DataStreamTestHelper.createTimestampField(org.opensearch.cluster.DataStreamTestHelper.createTimestampField) SYSTEM_INDEX_ACCESS_CONTROL_HEADER_KEY(org.opensearch.cluster.metadata.IndexNameExpressionResolver.SYSTEM_INDEX_ACCESS_CONTROL_HEADER_KEY) ClusterState(org.opensearch.cluster.ClusterState) ClusterName(org.opensearch.cluster.ClusterName) Matchers.containsString(org.hamcrest.Matchers.containsString) HashSet(java.util.HashSet) Sets.newHashSet(org.opensearch.common.util.set.Sets.newHashSet)

Aggregations

Arrays (java.util.Arrays)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Matchers.empty (org.hamcrest.Matchers.empty)4 Matchers.equalTo (org.hamcrest.Matchers.equalTo)4 ClusterState (org.opensearch.cluster.ClusterState)4 State (org.opensearch.cluster.metadata.IndexMetadata.State)4 Settings (org.opensearch.common.settings.Settings)4 Collections (java.util.Collections)3 Comparator (java.util.Comparator)3 EnumSet (java.util.EnumSet)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Function (java.util.function.Function)3 Matchers.arrayContaining (org.hamcrest.Matchers.arrayContaining)3 Matchers.arrayContainingInAnyOrder (org.hamcrest.Matchers.arrayContainingInAnyOrder)3 Matchers.arrayWithSize (org.hamcrest.Matchers.arrayWithSize)3 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Matchers.emptyArray (org.hamcrest.Matchers.emptyArray)3