Skip to content

Commit 4400f44

Browse files
committed
add more it
Signed-off-by: Jackie Han <hnyng@amazon.com>
1 parent b876a22 commit 4400f44

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

src/main/java/org/opensearch/timeseries/indices/IndexManagement.java

+28-25
Original file line numberDiff line numberDiff line change
@@ -1020,36 +1020,39 @@ public <T> void initCustomResultIndexAndExecute(String resultIndexOrAlias, Execu
10201020
* creates flattened result index
10211021
* @param flattenedResultIndexAlias the flattened result index alias
10221022
* @param actionListener the action listener
1023-
* @throws IOException
10241023
*/
1025-
public void initFlattenedResultIndex(String flattenedResultIndexAlias, ActionListener<CreateIndexResponse> actionListener)
1026-
throws IOException {
1027-
String indexName = getCustomResultIndexPattern(flattenedResultIndexAlias);
1028-
logger.info("Initializing flattened result index: {}", indexName);
1024+
public void initFlattenedResultIndex(String flattenedResultIndexAlias, ActionListener<Void> actionListener) {
1025+
try {
1026+
String indexName = getCustomResultIndexPattern(flattenedResultIndexAlias);
1027+
logger.info("Initializing flattened result index: {}", indexName);
10291028

1030-
CreateIndexRequest request = new CreateIndexRequest(indexName)
1031-
.mapping(getFlattenedResultMappings(), XContentType.JSON)
1032-
.settings(settings);
1029+
CreateIndexRequest request = new CreateIndexRequest(indexName)
1030+
.mapping(getFlattenedResultMappings(), XContentType.JSON)
1031+
.settings(settings);
10331032

1034-
if (flattenedResultIndexAlias != null) {
1035-
request.alias(new Alias(flattenedResultIndexAlias));
1036-
}
1033+
if (flattenedResultIndexAlias != null) {
1034+
request.alias(new Alias(flattenedResultIndexAlias));
1035+
}
10371036

1038-
choosePrimaryShards(request, false);
1037+
choosePrimaryShards(request, false);
10391038

1040-
adminClient.indices().create(request, ActionListener.wrap(response -> {
1041-
if (response.isAcknowledged()) {
1042-
logger.info("Successfully created flattened result index: {} with alias: {}", indexName, flattenedResultIndexAlias);
1043-
actionListener.onResponse(response);
1044-
} else {
1045-
String errorMsg = "Index creation not acknowledged for index: " + indexName;
1046-
logger.error(errorMsg);
1047-
actionListener.onFailure(new IllegalStateException(errorMsg));
1048-
}
1049-
}, exception -> {
1050-
logger.error("Failed to create flattened result index: {}", indexName, exception);
1051-
actionListener.onFailure(exception);
1052-
}));
1039+
adminClient.indices().create(request, ActionListener.wrap(response -> {
1040+
if (response.isAcknowledged()) {
1041+
logger.info("Successfully created flattened result index: {} with alias: {}", indexName, flattenedResultIndexAlias);
1042+
actionListener.onResponse(null);
1043+
} else {
1044+
String errorMsg = "Index creation not acknowledged for index: " + indexName;
1045+
logger.error(errorMsg);
1046+
actionListener.onFailure(new IllegalStateException(errorMsg));
1047+
}
1048+
}, exception -> {
1049+
logger.error("Failed to create flattened result index: {}", indexName, exception);
1050+
actionListener.onFailure(exception);
1051+
}));
1052+
} catch (IOException e) {
1053+
logger.error("Error while building mappings for flattened result index: {}", flattenedResultIndexAlias, e);
1054+
actionListener.onFailure(e);
1055+
}
10531056
}
10541057

10551058
public String getFlattenedResultIndexAlias(String indexOrAliasName, String configId) {

src/main/java/org/opensearch/timeseries/rest/handler/AbstractTimeSeriesActionHandler.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,20 @@ private void handleFlattenResultIndexMappingUpdate(ActionListener<T> listener) {
579579
return;
580580
}
581581
if (config.getFlattenResultIndexMapping() != null && config.getFlattenResultIndexMapping()) {
582-
setupIngestPipeline(id, listener);
582+
String flattenedResultIndexAlias = timeSeriesIndices
583+
.getFlattenedResultIndexAlias(config.getCustomResultIndexOrAlias(), config.getId());
584+
String pipelineId = timeSeriesIndices.getFlattenResultIndexIngestPipelineId(config.getId());
585+
timeSeriesIndices
586+
.initFlattenedResultIndex(
587+
flattenedResultIndexAlias,
588+
ActionListener.wrap(initResponse -> setupIngestPipeline(config.getId(), ActionListener.wrap(pipelineResponse -> {
589+
updateResultIndexSetting(
590+
pipelineId,
591+
flattenedResultIndexAlias,
592+
ActionListener.wrap(updateResponse -> listener.onResponse(updateResponse), listener::onFailure)
593+
);
594+
}, listener::onFailure)), listener::onFailure)
595+
);
583596
} else {
584597
String pipelineId = timeSeriesIndices.getFlattenResultIndexIngestPipelineId(config.getId());
585598
client.admin().cluster().deletePipeline(new DeletePipelineRequest(pipelineId), new ActionListener<AcknowledgedResponse>() {

src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public void testUpdateAnomalyDetector_enableFlattenResultIndex_shouldCreatePipel
272272
// create a detector with flatten result index disabled, shouldn't find related ingest pipeline
273273
Response response = TestHelpers
274274
.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
275-
assertEquals("Create anomaly detector with flattened result index failed", RestStatus.CREATED, TestHelpers.restStatus(response));
275+
assertEquals("Create anomaly detector without flattened result index failed", RestStatus.CREATED, TestHelpers.restStatus(response));
276276
Map<String, Object> responseMap = entityAsMap(response);
277277
String id = (String) responseMap.get("_id");
278278
String expectedPipelineId = String.format(Locale.ROOT, "flatten_result_index_ingest_pipeline%s", id.toLowerCase(Locale.ROOT));

0 commit comments

Comments
 (0)