Skip to content

Commit b131f9a

Browse files
committed
add IT
Signed-off-by: Jackie Han <hnyng@amazon.com>
1 parent 352075b commit b131f9a

File tree

4 files changed

+90
-8
lines changed

4 files changed

+90
-8
lines changed

src/main/java/org/opensearch/ad/transport/ADResultBulkTransportAction.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.IOException;
1919
import java.util.List;
20+
import java.util.Locale;
2021

2122
import org.apache.logging.log4j.LogManager;
2223
import org.apache.logging.log4j.Logger;
@@ -135,7 +136,7 @@ private boolean shouldAddResult(float indexingPressurePercent, AnomalyResult res
135136
}
136137

137138
private void addToFlattenedIndexIfExists(BulkRequest bulkRequest, AnomalyResult result, String resultIndex) {
138-
String flattenedResultIndexName = resultIndex + "_flattened_" + result.getDetectorId().toLowerCase();
139+
String flattenedResultIndexName = resultIndex + "_flattened_" + result.getDetectorId().toLowerCase(Locale.ROOT);
139140
if (clusterService.state().metadata().hasIndex(flattenedResultIndexName)) {
140141
addResult(bulkRequest, result, flattenedResultIndexName);
141142
}

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ protected void validateTimeField(boolean indexingDryRun, ActionListener<T> liste
417417
* If {@code true}, the operation performs validation without creating/updating the configuration.
418418
* If {@code false}, the configuration is created or updated.
419419
* @param listener the {@link ActionListener} to handle the response or failure of the operation.
420-
* @throws IOException if an I/O error occurs during the operation.
421420
*
422421
* <p><b>Behavior:</b></p>
423422
* <ul>
@@ -467,8 +466,8 @@ private void handlePostRequest(boolean indexingDryRun, ActionListener<T> listene
467466
if (shouldHandleFlattening(indexingDryRun, createConfigResponse)) {
468467
IndexAnomalyDetectorResponse response = (IndexAnomalyDetectorResponse) createConfigResponse;
469468
String detectorId = response.getId();
470-
String indexName = config.getCustomResultIndexOrAlias() + "_flattened_" + detectorId.toLowerCase();
471-
String pipelineId = "anomaly_detection_ingest_pipeline_" + detectorId.toLowerCase();
469+
String indexName = config.getCustomResultIndexOrAlias() + "_flattened_" + detectorId.toLowerCase(Locale.ROOT);
470+
String pipelineId = "anomaly_detection_ingest_pipeline_" + detectorId.toLowerCase(Locale.ROOT);
472471

473472
timeSeriesIndices
474473
.initFlattenedResultIndex(
@@ -495,8 +494,8 @@ private boolean shouldHandleFlattening(boolean indexingDryRun, Object createConf
495494
}
496495

497496
protected void setupIngestPipeline(String detectorId, ActionListener<T> listener) {
498-
String indexName = config.getCustomResultIndexOrAlias() + "_flattened_" + detectorId.toLowerCase();
499-
String pipelineId = "anomaly_detection_ingest_pipeline_" + detectorId.toLowerCase();
497+
String indexName = config.getCustomResultIndexOrAlias() + "_flattened_" + detectorId.toLowerCase(Locale.ROOT);
498+
String pipelineId = "anomaly_detection_ingest_pipeline_" + detectorId.toLowerCase(Locale.ROOT);
500499

501500
try {
502501
BytesReference pipelineSource = createPipelineDefinition(indexName);

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

+50-2
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,20 @@ private AnomalyDetector createIndexAndGetAnomalyDetector(String indexName, List<
122122
}
123123

124124
private AnomalyDetector createIndexAndGetAnomalyDetector(String indexName, List<Feature> features, boolean useDateNanos)
125-
throws IOException {
125+
throws IOException {
126+
return createIndexAndGetAnomalyDetector(indexName, features, useDateNanos, false);
127+
}
128+
129+
private AnomalyDetector createIndexAndGetAnomalyDetector(String indexName, List<Feature> features, boolean useDateNanos,
130+
boolean useFlattenResultIndex) throws IOException {
126131
TestHelpers.createIndexWithTimeField(client(), indexName, TIME_FIELD, useDateNanos);
127132
String testIndexData = "{\"keyword-field\": \"field-1\", \"ip-field\": \"1.2.3.4\", \"timestamp\": 1}";
128133
TestHelpers.ingestDataToIndex(client(), indexName, TestHelpers.toHttpEntity(testIndexData));
129-
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(TIME_FIELD, indexName, features);
134+
135+
AnomalyDetector detector = useFlattenResultIndex
136+
? TestHelpers.randomAnomalyDetectorWithFlattenResultIndex(TIME_FIELD, indexName, features)
137+
: TestHelpers.randomAnomalyDetector(TIME_FIELD, indexName, features);
138+
130139
return detector;
131140
}
132141

@@ -180,6 +189,45 @@ public void testCreateAnomalyDetectorWithDuplicateName() throws Exception {
180189
);
181190
}
182191

192+
public void testCreateAnomalyDetectorWithFlattenedResultIndex() throws Exception {
193+
AnomalyDetector detector = createIndexAndGetAnomalyDetector(INDEX_NAME,
194+
ImmutableList.of(TestHelpers.randomFeature(true)), false, true);
195+
196+
// test behavior when AD is disabled
197+
updateClusterSettings(ADEnabledSetting.AD_ENABLED, false);
198+
Exception ex = expectThrows(
199+
ResponseException.class,
200+
() -> TestHelpers
201+
.makeRequest(
202+
client(),
203+
"POST",
204+
TestHelpers.AD_BASE_DETECTORS_URI,
205+
ImmutableMap.of(),
206+
TestHelpers.toHttpEntity(detector),
207+
null
208+
)
209+
);
210+
assertThat(ex.getMessage(), containsString(ADCommonMessages.DISABLED_ERR_MSG));
211+
212+
// test behavior when AD is enabled
213+
updateClusterSettings(ADEnabledSetting.AD_ENABLED, true);
214+
Response response = TestHelpers
215+
.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
216+
assertEquals("Create anomaly detector with flattened result index failed", RestStatus.CREATED, TestHelpers.restStatus(response));
217+
Map<String, Object> responseMap = entityAsMap(response);
218+
String id = (String) responseMap.get("_id");
219+
int version = (int) responseMap.get("_version");
220+
assertNotEquals("response is missing Id", AnomalyDetector.NO_ID, id);
221+
assertTrue("incorrect version", version > 0);
222+
// ensure the flattened result index was created
223+
String expectedFlattenedIndex = String.format(
224+
"opensearch-ad-plugin-result-test_flattened_%s",
225+
id.toLowerCase(Locale.ROOT)
226+
);
227+
boolean indexExists = indexExists(expectedFlattenedIndex);
228+
assertTrue(indexExists);
229+
}
230+
183231
public void testCreateAnomalyDetector() throws Exception {
184232
AnomalyDetector detector = createIndexAndGetAnomalyDetector(INDEX_NAME);
185233
updateClusterSettings(ADEnabledSetting.AD_ENABLED, false);

src/test/java/org/opensearch/timeseries/TestHelpers.java

+34
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,40 @@ public static AnomalyDetector randomAnomalyDetector(String timefield, String ind
511511
);
512512
}
513513

514+
public static AnomalyDetector randomAnomalyDetectorWithFlattenResultIndex(String timefield, String indexName, List<Feature> features) throws IOException {
515+
return new AnomalyDetector(
516+
randomAlphaOfLength(10),
517+
randomLong(),
518+
randomAlphaOfLength(20),
519+
randomAlphaOfLength(30),
520+
timefield,
521+
ImmutableList.of(indexName.toLowerCase(Locale.ROOT)),
522+
features,
523+
randomQuery(),
524+
randomIntervalTimeConfiguration(),
525+
randomIntervalTimeConfiguration(),
526+
randomIntBetween(1, TimeSeriesSettings.MAX_SHINGLE_SIZE),
527+
null,
528+
randomInt(),
529+
Instant.now(),
530+
null,
531+
randomUser(),
532+
ADCommonName.CUSTOM_RESULT_INDEX_PREFIX + "test",
533+
TestHelpers.randomImputationOption(features),
534+
// timeDecay (reverse of recencyEmphasis) should be less than 1.
535+
// so we start with 2.
536+
randomIntBetween(2, 10000),
537+
randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2),
538+
randomIntBetween(1, 1000),
539+
null,
540+
null,
541+
null,
542+
null,
543+
true,
544+
Instant.now()
545+
);
546+
}
547+
514548
public static AnomalyDetector randomAnomalyDetectorWithEmptyFeature() throws IOException {
515549
return new AnomalyDetector(
516550
randomAlphaOfLength(10),

0 commit comments

Comments
 (0)