use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.
the class TransportRolloverActionTests method testConditionEvaluationWhenAliasToWriteAndReadIndicesConsidersOnlyPrimariesFromWriteIndex.
public void testConditionEvaluationWhenAliasToWriteAndReadIndicesConsidersOnlyPrimariesFromWriteIndex() throws Exception {
final TransportService mockTransportService = mock(TransportService.class);
final ClusterService mockClusterService = mock(ClusterService.class);
final DiscoveryNode mockNode = mock(DiscoveryNode.class);
when(mockNode.getId()).thenReturn("mocknode");
when(mockClusterService.localNode()).thenReturn(mockNode);
final ThreadPool mockThreadPool = mock(ThreadPool.class);
final MetadataCreateIndexService mockCreateIndexService = mock(MetadataCreateIndexService.class);
final IndexNameExpressionResolver mockIndexNameExpressionResolver = mock(IndexNameExpressionResolver.class);
when(mockIndexNameExpressionResolver.resolveDateMathExpression(any())).thenReturn("logs-index-000003");
final ActionFilters mockActionFilters = mock(ActionFilters.class);
final MetadataIndexAliasesService mdIndexAliasesService = mock(MetadataIndexAliasesService.class);
final Client mockClient = mock(Client.class);
final Map<String, IndexStats> indexStats = new HashMap<>();
int total = randomIntBetween(500, 1000);
indexStats.put("logs-index-000001", createIndexStats(200L, total));
indexStats.put("logs-index-000002", createIndexStats(300L, total));
final IndicesStatsResponse statsResponse = createAliasToMultipleIndicesStatsResponse(indexStats);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assert args.length == 3;
ActionListener<IndicesStatsResponse> listener = (ActionListener<IndicesStatsResponse>) args[2];
listener.onResponse(statsResponse);
return null;
}).when(mockClient).execute(eq(IndicesStatsAction.INSTANCE), any(ActionRequest.class), any(ActionListener.class));
assert statsResponse.getPrimaries().getDocs().getCount() == 500L;
assert statsResponse.getTotal().getDocs().getCount() == (total + total);
final IndexMetadata.Builder indexMetadata = IndexMetadata.builder("logs-index-000001").putAlias(AliasMetadata.builder("logs-alias").writeIndex(false).build()).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1);
final IndexMetadata.Builder indexMetadata2 = IndexMetadata.builder("logs-index-000002").putAlias(AliasMetadata.builder("logs-alias").writeIndex(true).build()).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1);
final ClusterState stateBefore = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata).put(indexMetadata2)).build();
when(mockCreateIndexService.applyCreateIndexRequest(any(), any(), anyBoolean())).thenReturn(stateBefore);
when(mdIndexAliasesService.applyAliasActions(any(), any())).thenReturn(stateBefore);
MetadataRolloverService rolloverService = new MetadataRolloverService(mockThreadPool, mockCreateIndexService, mdIndexAliasesService, mockIndexNameExpressionResolver);
final TransportRolloverAction transportRolloverAction = new TransportRolloverAction(mockTransportService, mockClusterService, mockThreadPool, mockActionFilters, mockIndexNameExpressionResolver, rolloverService, mockClient);
// For given alias, verify that condition evaluation fails when the condition doc count is greater than the primaries doc count
// (primaries from only write index is considered)
PlainActionFuture<RolloverResponse> future = new PlainActionFuture<>();
RolloverRequest rolloverRequest = new RolloverRequest("logs-alias", "logs-index-000003");
rolloverRequest.addMaxIndexDocsCondition(500L);
rolloverRequest.dryRun(true);
transportRolloverAction.masterOperation(mock(Task.class), rolloverRequest, stateBefore, future);
RolloverResponse response = future.actionGet();
assertThat(response.getOldIndex(), equalTo("logs-index-000002"));
assertThat(response.getNewIndex(), equalTo("logs-index-000003"));
assertThat(response.isDryRun(), equalTo(true));
assertThat(response.isRolledOver(), equalTo(false));
assertThat(response.getConditionStatus().size(), equalTo(1));
assertThat(response.getConditionStatus().get("[max_docs: 500]"), is(false));
// For given alias, verify that the condition evaluation is successful when condition doc count is less than the primaries doc count
// (primaries from only write index is considered)
future = new PlainActionFuture<>();
rolloverRequest = new RolloverRequest("logs-alias", "logs-index-000003");
rolloverRequest.addMaxIndexDocsCondition(300L);
rolloverRequest.dryRun(true);
transportRolloverAction.masterOperation(mock(Task.class), rolloverRequest, stateBefore, future);
response = future.actionGet();
assertThat(response.getOldIndex(), equalTo("logs-index-000002"));
assertThat(response.getNewIndex(), equalTo("logs-index-000003"));
assertThat(response.isDryRun(), equalTo(true));
assertThat(response.isRolledOver(), equalTo(false));
assertThat(response.getConditionStatus().size(), equalTo(1));
assertThat(response.getConditionStatus().get("[max_docs: 300]"), is(true));
}
use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.
the class PutMappingRequestTests method testResolveIndicesWithoutWriteIndexOnlyAndDataStreamsAndWriteAliases.
public void testResolveIndicesWithoutWriteIndexOnlyAndDataStreamsAndWriteAliases() {
String[] dataStreamNames = { "foo", "bar", "baz" };
List<Tuple<String, Integer>> dsMetadata = org.opensearch.common.collect.List.of(tuple(dataStreamNames[0], randomIntBetween(1, 3)), tuple(dataStreamNames[1], randomIntBetween(1, 3)), tuple(dataStreamNames[2], randomIntBetween(1, 3)));
ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, org.opensearch.common.collect.List.of("index1", "index2", "index3"));
cs = addAliases(cs, org.opensearch.common.collect.List.of(tuple("alias1", org.opensearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), tuple("alias2", org.opensearch.common.collect.List.of(tuple("index2", false), tuple("index3", true)))));
PutMappingRequest request = new PutMappingRequest().indices("foo", "alias1", "alias2");
Index[] indices = TransportPutMappingAction.resolveIndices(cs, request, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)));
List<String> indexNames = Arrays.stream(indices).map(Index::getName).collect(Collectors.toList());
IndexAbstraction expectedDs = cs.metadata().getIndicesLookup().get("foo");
List<String> expectedIndices = expectedDs.getIndices().stream().map(im -> im.getIndex().getName()).collect(Collectors.toList());
expectedIndices.addAll(org.opensearch.common.collect.List.of("index1", "index2", "index3"));
// should resolve the data stream and each alias to _all_ their respective indices
assertThat(indexNames, containsInAnyOrder(expectedIndices.toArray()));
}
use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.
the class PutMappingRequestTests method testResolveIndicesWithWriteIndexOnlyAndNoSingleWriteIndex.
public void testResolveIndicesWithWriteIndexOnlyAndNoSingleWriteIndex() {
String[] dataStreamNames = { "foo", "bar", "baz" };
List<Tuple<String, Integer>> dsMetadata = org.opensearch.common.collect.List.of(tuple(dataStreamNames[0], randomIntBetween(1, 3)), tuple(dataStreamNames[1], randomIntBetween(1, 3)), tuple(dataStreamNames[2], randomIntBetween(1, 3)));
ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, org.opensearch.common.collect.List.of("index1", "index2", "index3"));
final ClusterState cs2 = addAliases(cs, org.opensearch.common.collect.List.of(tuple("alias1", org.opensearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), tuple("alias2", org.opensearch.common.collect.List.of(tuple("index2", false), tuple("index3", true)))));
PutMappingRequest request = new PutMappingRequest().indices("*").writeIndexOnly(true);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TransportPutMappingAction.resolveIndices(cs2, request, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY))));
assertThat(e.getMessage(), containsString("The index expression [*] and options provided did not point to a single write-index"));
}
use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.
the class PutMappingRequestTests method testResolveIndicesWithWriteIndexOnlyAndDataStreamsAndWriteAliases.
public void testResolveIndicesWithWriteIndexOnlyAndDataStreamsAndWriteAliases() {
String[] dataStreamNames = { "foo", "bar", "baz" };
List<Tuple<String, Integer>> dsMetadata = org.opensearch.common.collect.List.of(tuple(dataStreamNames[0], randomIntBetween(1, 3)), tuple(dataStreamNames[1], randomIntBetween(1, 3)), tuple(dataStreamNames[2], randomIntBetween(1, 3)));
ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, org.opensearch.common.collect.List.of("index1", "index2", "index3"));
cs = addAliases(cs, org.opensearch.common.collect.List.of(tuple("alias1", org.opensearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), tuple("alias2", org.opensearch.common.collect.List.of(tuple("index2", false), tuple("index3", true)))));
PutMappingRequest request = new PutMappingRequest().indices("foo", "alias1", "alias2").writeIndexOnly(true);
Index[] indices = TransportPutMappingAction.resolveIndices(cs, request, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)));
List<String> indexNames = Arrays.stream(indices).map(Index::getName).collect(Collectors.toList());
IndexAbstraction expectedDs = cs.metadata().getIndicesLookup().get("foo");
// should resolve the data stream and each alias to their respective write indices
assertThat(indexNames, containsInAnyOrder(expectedDs.getWriteIndex().getIndex().getName(), "index2", "index3"));
}
use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.
the class PutMappingRequestTests method testResolveIndicesWithWriteIndexOnlyAndAliasWithoutWriteIndex.
public void testResolveIndicesWithWriteIndexOnlyAndAliasWithoutWriteIndex() {
String[] dataStreamNames = { "foo", "bar", "baz" };
List<Tuple<String, Integer>> dsMetadata = org.opensearch.common.collect.List.of(tuple(dataStreamNames[0], randomIntBetween(1, 3)), tuple(dataStreamNames[1], randomIntBetween(1, 3)), tuple(dataStreamNames[2], randomIntBetween(1, 3)));
ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, org.opensearch.common.collect.List.of("index1", "index2", "index3"));
final ClusterState cs2 = addAliases(cs, org.opensearch.common.collect.List.of(tuple("alias1", org.opensearch.common.collect.List.of(tuple("index1", false), tuple("index2", false))), tuple("alias2", org.opensearch.common.collect.List.of(tuple("index2", false), tuple("index3", false)))));
PutMappingRequest request = new PutMappingRequest().indices("alias2").writeIndexOnly(true);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TransportPutMappingAction.resolveIndices(cs2, request, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY))));
assertThat(e.getMessage(), containsString("no write index is defined for alias [alias2]"));
}
Aggregations