Skip to content

Commit

Permalink
add more IT
Browse files Browse the repository at this point in the history
Signed-off-by: Jackie Han <hnyng@amazon.com>
  • Loading branch information
jackiehanyang committed Jan 28, 2025
1 parent 9b371ba commit f1e21cf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1027,8 +1027,8 @@ public void initFlattenedResultIndex(String flattenedResultIndexAlias, ActionLis
logger.info("Initializing flattened result index: {}", indexName);

CreateIndexRequest request = new CreateIndexRequest(indexName)
.mapping(getFlattenedResultMappings(), XContentType.JSON)
.settings(settings);
.mapping(getFlattenedResultMappings(), XContentType.JSON)
.settings(settings);

if (flattenedResultIndexAlias != null) {
request.alias(new Alias(flattenedResultIndexAlias));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,15 @@ protected void prepareConfigIndexing(boolean indexingDryRun, ActionListener<T> l
}

private void handlePutRequest(boolean indexingDryRun, ActionListener<T> listener) {
handler.confirmJobRunning(clusterService, client, id, listener, () -> {
updateConfig(id, indexingDryRun, listener);
}, xContentRegistry);
handler
.confirmJobRunning(
clusterService,
client,
id,
listener,
() -> { updateConfig(id, indexingDryRun, listener); },
xContentRegistry
);
}

private void handlePostRequest(boolean indexingDryRun, ActionListener<T> listener) {
Expand Down Expand Up @@ -573,8 +579,8 @@ private void handleFlattenResultIndexMappingUpdate(Config existingConfig, Action
return;
}
if (Boolean.TRUE.equals(existingConfig.getFlattenResultIndexMapping())
&& Boolean.FALSE.equals(config.getFlattenResultIndexMapping())
&& existingConfig.getCustomResultIndexOrAlias() != null) {
&& Boolean.FALSE.equals(config.getFlattenResultIndexMapping())
&& existingConfig.getCustomResultIndexOrAlias() != null) {
String pipelineId = timeSeriesIndices.getFlattenResultIndexIngestPipelineId(config.getId());
client.admin().cluster().deletePipeline(new DeletePipelineRequest(pipelineId), new ActionListener<AcknowledgedResponse>() {

Expand Down Expand Up @@ -606,23 +612,10 @@ public void onFailure(Exception e) {
}
});
} else if (Boolean.FALSE.equals(existingConfig.getFlattenResultIndexMapping())
&& Boolean.TRUE.equals(config.getFlattenResultIndexMapping())
&& existingConfig.getCustomResultIndexOrAlias() != null
) {
String flattenedResultIndexAlias = timeSeriesIndices
.getFlattenedResultIndexAlias(config.getCustomResultIndexOrAlias(), config.getId());
String pipelineId = timeSeriesIndices.getFlattenResultIndexIngestPipelineId(config.getId());
timeSeriesIndices
.initFlattenedResultIndex(
flattenedResultIndexAlias,
ActionListener.wrap(initResponse -> setupIngestPipeline(config.getId(), ActionListener.wrap(pipelineResponse -> {
updateResultIndexSetting(
pipelineId,
flattenedResultIndexAlias,
ActionListener.wrap(updateResponse -> listener.onResponse(updateResponse), listener::onFailure)
);
}, listener::onFailure)), listener::onFailure)
);
&& Boolean.TRUE.equals(config.getFlattenResultIndexMapping())
&& existingConfig.getCustomResultIndexOrAlias() != null) {
listener.onFailure(new OpenSearchStatusException(CommonMessages.CAN_NOT_CHANGE_FLATTEN_RESULT_INDEX, RestStatus.BAD_REQUEST));
return;
}
}

Expand Down
72 changes: 0 additions & 72 deletions src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,78 +331,6 @@ public void testUpdateAnomalyDetector_disableFlattenResultIndex_shouldDeletePipe
assertEquals("Expected 404 response but got: " + statusCode, 404, statusCode);
}

public void testUpdateAnomalyDetector_enableFlattenResultIndex_shouldCreatePipeline() throws Exception {
AnomalyDetector detector = createIndexAndGetAnomalyDetector(
INDEX_NAME,
ImmutableList.of(TestHelpers.randomFeature("feature_bytes", "agg", true)),
false,
false
);
// test behavior when AD is enabled
updateClusterSettings(ADEnabledSetting.AD_ENABLED, true);
// create a detector with flatten result index disabled, shouldn't find related ingest pipeline
Response response = TestHelpers
.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
assertEquals("Create anomaly detector without flattened result index failed", RestStatus.CREATED, TestHelpers.restStatus(response));
Map<String, Object> responseMap = entityAsMap(response);
String id = (String) responseMap.get("_id");
String expectedPipelineId = String.format(Locale.ROOT, "flatten_result_index_ingest_pipeline%s", id.toLowerCase(Locale.ROOT));
String getIngestPipelineEndpoint = String.format(Locale.ROOT, "_ingest/pipeline/%s", expectedPipelineId);
ResponseException responseException = expectThrows(
ResponseException.class,
() -> TestHelpers.makeRequest(client(), "GET", getIngestPipelineEndpoint, ImmutableMap.of(), "", null)
);
int statusCode = responseException.getResponse().getStatusLine().getStatusCode();
assertEquals("Expected 404 response but got: " + statusCode, 404, statusCode);
// update the detector with flatten result index enabled, should be able to find ingest pipeline
List<Feature> features = detector.getFeatureAttributes();
AnomalyDetector newDetector = new AnomalyDetector(
id,
detector.getVersion(),
detector.getName(),
detector.getDescription(),
detector.getTimeField(),
detector.getIndices(),
features,
detector.getFilterQuery(),
detector.getInterval(),
detector.getWindowDelay(),
detector.getShingleSize(),
detector.getUiMetadata(),
detector.getSchemaVersion(),
detector.getLastUpdateTime(),
null,
detector.getUser(),
detector.getCustomResultIndexOrAlias(),
TestHelpers.randomImputationOption(features),
randomIntBetween(1, 10000),
randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2),
randomIntBetween(1, 1000),
null,
null,
null,
null,
true,
detector.getLastBreakingUIChangeTime()
);
Response updateResponse = TestHelpers
.makeRequest(
client(),
"PUT",
TestHelpers.AD_BASE_DETECTORS_URI + "/" + id + "?refresh=true",
ImmutableMap.of(),
TestHelpers.toHttpEntity(newDetector),
null
);
assertEquals("Update anomaly detector failed", RestStatus.OK, TestHelpers.restStatus(updateResponse));
Response getPipelineResponse = TestHelpers.makeRequest(client(), "GET", getIngestPipelineEndpoint, ImmutableMap.of(), "", null);
assertEquals(
"Expected 200 response but got: " + getPipelineResponse.getStatusLine().getStatusCode(),
200,
getPipelineResponse.getStatusLine().getStatusCode()
);
}

public void testCreateAnomalyDetector() throws Exception {
AnomalyDetector detector = createIndexAndGetAnomalyDetector(INDEX_NAME);
updateClusterSettings(ADEnabledSetting.AD_ENABLED, false);
Expand Down

0 comments on commit f1e21cf

Please sign in to comment.