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"));
}
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);
}
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);
}
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);
}
Aggregations