Skip to content

Commit ad3d99c

Browse files
committed
add more IT
Signed-off-by: Jackie Han <hnyng@amazon.com>
1 parent 9b371ba commit ad3d99c

File tree

4 files changed

+66
-73
lines changed

4 files changed

+66
-73
lines changed

src/main/java/org/opensearch/timeseries/constant/CommonMessages.java

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static String getTooManyCategoricalFieldErr(int limit) {
4444
public static String FAIL_TO_FIND_CONFIG_MSG = "Can't find config with id: ";
4545
public static final String CAN_NOT_CHANGE_CATEGORY_FIELD = "Can't change category field";
4646
public static final String CAN_NOT_CHANGE_CUSTOM_RESULT_INDEX = "Can't change custom result index";
47+
public static final String CAN_NOT_CHANGE_FLATTEN_RESULT_INDEX = "Can't change flatten result index";
4748
public static final String CATEGORICAL_FIELD_TYPE_ERR_MSG = "Categorical field %s must be of type keyword or ip.";
4849
// Modifying message for FEATURE below may break the parseADValidationException method of ValidateAnomalyDetectorTransportAction
4950
public static final String FEATURE_INVALID_MSG_PREFIX = "Feature has an invalid query";

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,8 @@ public void initFlattenedResultIndex(String flattenedResultIndexAlias, ActionLis
10271027
logger.info("Initializing flattened result index: {}", indexName);
10281028

10291029
CreateIndexRequest request = new CreateIndexRequest(indexName)
1030-
.mapping(getFlattenedResultMappings(), XContentType.JSON)
1031-
.settings(settings);
1030+
.mapping(getFlattenedResultMappings(), XContentType.JSON)
1031+
.settings(settings);
10321032

10331033
if (flattenedResultIndexAlias != null) {
10341034
request.alias(new Alias(flattenedResultIndexAlias));

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

+15-22
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,15 @@ protected void prepareConfigIndexing(boolean indexingDryRun, ActionListener<T> l
454454
}
455455

456456
private void handlePutRequest(boolean indexingDryRun, ActionListener<T> listener) {
457-
handler.confirmJobRunning(clusterService, client, id, listener, () -> {
458-
updateConfig(id, indexingDryRun, listener);
459-
}, xContentRegistry);
457+
handler
458+
.confirmJobRunning(
459+
clusterService,
460+
client,
461+
id,
462+
listener,
463+
() -> { updateConfig(id, indexingDryRun, listener); },
464+
xContentRegistry
465+
);
460466
}
461467

462468
private void handlePostRequest(boolean indexingDryRun, ActionListener<T> listener) {
@@ -573,8 +579,8 @@ private void handleFlattenResultIndexMappingUpdate(Config existingConfig, Action
573579
return;
574580
}
575581
if (Boolean.TRUE.equals(existingConfig.getFlattenResultIndexMapping())
576-
&& Boolean.FALSE.equals(config.getFlattenResultIndexMapping())
577-
&& existingConfig.getCustomResultIndexOrAlias() != null) {
582+
&& Boolean.FALSE.equals(config.getFlattenResultIndexMapping())
583+
&& existingConfig.getCustomResultIndexOrAlias() != null) {
578584
String pipelineId = timeSeriesIndices.getFlattenResultIndexIngestPipelineId(config.getId());
579585
client.admin().cluster().deletePipeline(new DeletePipelineRequest(pipelineId), new ActionListener<AcknowledgedResponse>() {
580586

@@ -606,23 +612,10 @@ public void onFailure(Exception e) {
606612
}
607613
});
608614
} else if (Boolean.FALSE.equals(existingConfig.getFlattenResultIndexMapping())
609-
&& Boolean.TRUE.equals(config.getFlattenResultIndexMapping())
610-
&& existingConfig.getCustomResultIndexOrAlias() != null
611-
) {
612-
String flattenedResultIndexAlias = timeSeriesIndices
613-
.getFlattenedResultIndexAlias(config.getCustomResultIndexOrAlias(), config.getId());
614-
String pipelineId = timeSeriesIndices.getFlattenResultIndexIngestPipelineId(config.getId());
615-
timeSeriesIndices
616-
.initFlattenedResultIndex(
617-
flattenedResultIndexAlias,
618-
ActionListener.wrap(initResponse -> setupIngestPipeline(config.getId(), ActionListener.wrap(pipelineResponse -> {
619-
updateResultIndexSetting(
620-
pipelineId,
621-
flattenedResultIndexAlias,
622-
ActionListener.wrap(updateResponse -> listener.onResponse(updateResponse), listener::onFailure)
623-
);
624-
}, listener::onFailure)), listener::onFailure)
625-
);
615+
&& Boolean.TRUE.equals(config.getFlattenResultIndexMapping())
616+
&& existingConfig.getCustomResultIndexOrAlias() != null) {
617+
listener.onFailure(new OpenSearchStatusException(CommonMessages.CAN_NOT_CHANGE_FLATTEN_RESULT_INDEX, RestStatus.BAD_REQUEST));
618+
return;
626619
}
627620
}
628621

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

+48-49
Original file line numberDiff line numberDiff line change
@@ -331,76 +331,75 @@ public void testUpdateAnomalyDetector_disableFlattenResultIndex_shouldDeletePipe
331331
assertEquals("Expected 404 response but got: " + statusCode, 404, statusCode);
332332
}
333333

334-
public void testUpdateAnomalyDetector_enableFlattenResultIndex_shouldCreatePipeline() throws Exception {
334+
public void testUpdateAnomalyDetector_enableFlattenResultIndex_badRequest() throws Exception {
335335
AnomalyDetector detector = createIndexAndGetAnomalyDetector(
336-
INDEX_NAME,
337-
ImmutableList.of(TestHelpers.randomFeature("feature_bytes", "agg", true)),
338-
false,
339-
false
336+
INDEX_NAME,
337+
ImmutableList.of(TestHelpers.randomFeature("feature_bytes", "agg", true)),
338+
false,
339+
false
340340
);
341341
// test behavior when AD is enabled
342342
updateClusterSettings(ADEnabledSetting.AD_ENABLED, true);
343343
// create a detector with flatten result index disabled, shouldn't find related ingest pipeline
344344
Response response = TestHelpers
345-
.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
345+
.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
346346
assertEquals("Create anomaly detector without flattened result index failed", RestStatus.CREATED, TestHelpers.restStatus(response));
347347
Map<String, Object> responseMap = entityAsMap(response);
348348
String id = (String) responseMap.get("_id");
349349
String expectedPipelineId = String.format(Locale.ROOT, "flatten_result_index_ingest_pipeline%s", id.toLowerCase(Locale.ROOT));
350350
String getIngestPipelineEndpoint = String.format(Locale.ROOT, "_ingest/pipeline/%s", expectedPipelineId);
351351
ResponseException responseException = expectThrows(
352-
ResponseException.class,
353-
() -> TestHelpers.makeRequest(client(), "GET", getIngestPipelineEndpoint, ImmutableMap.of(), "", null)
352+
ResponseException.class,
353+
() -> TestHelpers.makeRequest(client(), "GET", getIngestPipelineEndpoint, ImmutableMap.of(), "", null)
354354
);
355355
int statusCode = responseException.getResponse().getStatusLine().getStatusCode();
356356
assertEquals("Expected 404 response but got: " + statusCode, 404, statusCode);
357357
// update the detector with flatten result index enabled, should be able to find ingest pipeline
358358
List<Feature> features = detector.getFeatureAttributes();
359359
AnomalyDetector newDetector = new AnomalyDetector(
360-
id,
361-
detector.getVersion(),
362-
detector.getName(),
363-
detector.getDescription(),
364-
detector.getTimeField(),
365-
detector.getIndices(),
366-
features,
367-
detector.getFilterQuery(),
368-
detector.getInterval(),
369-
detector.getWindowDelay(),
370-
detector.getShingleSize(),
371-
detector.getUiMetadata(),
372-
detector.getSchemaVersion(),
373-
detector.getLastUpdateTime(),
374-
null,
375-
detector.getUser(),
376-
detector.getCustomResultIndexOrAlias(),
377-
TestHelpers.randomImputationOption(features),
378-
randomIntBetween(1, 10000),
379-
randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2),
380-
randomIntBetween(1, 1000),
381-
null,
382-
null,
383-
null,
384-
null,
385-
true,
386-
detector.getLastBreakingUIChangeTime()
360+
id,
361+
detector.getVersion(),
362+
detector.getName(),
363+
detector.getDescription(),
364+
detector.getTimeField(),
365+
detector.getIndices(),
366+
features,
367+
detector.getFilterQuery(),
368+
detector.getInterval(),
369+
detector.getWindowDelay(),
370+
detector.getShingleSize(),
371+
detector.getUiMetadata(),
372+
detector.getSchemaVersion(),
373+
detector.getLastUpdateTime(),
374+
null,
375+
detector.getUser(),
376+
detector.getCustomResultIndexOrAlias(),
377+
TestHelpers.randomImputationOption(features),
378+
randomIntBetween(1, 10000),
379+
randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2),
380+
randomIntBetween(1, 1000),
381+
null,
382+
null,
383+
null,
384+
null,
385+
true,
386+
detector.getLastBreakingUIChangeTime()
387387
);
388-
Response updateResponse = TestHelpers
388+
389+
ResponseException updateException = expectThrows(
390+
ResponseException.class,
391+
() -> TestHelpers
389392
.makeRequest(
390-
client(),
391-
"PUT",
392-
TestHelpers.AD_BASE_DETECTORS_URI + "/" + id + "?refresh=true",
393-
ImmutableMap.of(),
394-
TestHelpers.toHttpEntity(newDetector),
395-
null
396-
);
397-
assertEquals("Update anomaly detector failed", RestStatus.OK, TestHelpers.restStatus(updateResponse));
398-
Response getPipelineResponse = TestHelpers.makeRequest(client(), "GET", getIngestPipelineEndpoint, ImmutableMap.of(), "", null);
399-
assertEquals(
400-
"Expected 200 response but got: " + getPipelineResponse.getStatusLine().getStatusCode(),
401-
200,
402-
getPipelineResponse.getStatusLine().getStatusCode()
393+
client(),
394+
"PUT",
395+
TestHelpers.AD_BASE_DETECTORS_URI + "/" + id + "?refresh=true",
396+
ImmutableMap.of(),
397+
TestHelpers.toHttpEntity(newDetector),
398+
null
399+
)
403400
);
401+
int updateExceptionStatusCode = updateException.getResponse().getStatusLine().getStatusCode();
402+
assertEquals("Expected 400 response but got: " + updateExceptionStatusCode, 400, updateExceptionStatusCode);
404403
}
405404

406405
public void testCreateAnomalyDetector() throws Exception {

0 commit comments

Comments
 (0)