This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Prevent creating detector with duplicate name. Issue:#118 #134
Merged
yizheliu-amazon
merged 3 commits into
opendistro-for-elasticsearch:master
from
yizheliu-amazon:forbid-duplicate
May 22, 2020
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
import org.elasticsearch.rest.RestResponse; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.rest.action.RestResponseListener; | ||
import org.elasticsearch.search.SearchHit; | ||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
|
||
import java.io.IOException; | ||
|
@@ -221,6 +222,46 @@ private void onSearchAdInputIndicesResponse(SearchResponse response, String dete | |
+ Arrays.toString(anomalyDetector.getIndices().toArray(new String[0])); | ||
logger.error(errorMsg); | ||
onFailure(new IllegalArgumentException(errorMsg)); | ||
} else { | ||
checkADNameExists(detectorId); | ||
} | ||
} | ||
|
||
private void checkADNameExists(String detectorId) throws IOException { | ||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() | ||
// src/main/resources/mappings/anomaly-detectors.json#L14 | ||
.query(QueryBuilders.termQuery("name.keyword", anomalyDetector.getName())) | ||
.timeout(requestTimeout); | ||
SearchRequest searchRequest = new SearchRequest(ANOMALY_DETECTORS_INDEX).source(searchSourceBuilder); | ||
|
||
client | ||
.search( | ||
searchRequest, | ||
ActionListener | ||
.wrap( | ||
searchResponse -> onSearchADNameResponse(searchResponse, detectorId, anomalyDetector.getName()), | ||
exception -> onFailure(exception) | ||
) | ||
); | ||
} | ||
|
||
private void onSearchADNameResponse(SearchResponse response, String detectorId, String name) throws IOException { | ||
boolean hasDuplicateName = false; | ||
String existingDetectorId = null; | ||
if (response.getHits().getTotalHits().value > 0) { | ||
for (SearchHit hit : response.getHits()) { | ||
if (!hit.getId().equals(detectorId)) { | ||
hasDuplicateName = true; | ||
existingDetectorId = hit.getId(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (hasDuplicateName) { | ||
String errorMsg = String.format("Cannot create anomaly detector with name[%s] used by detectorId %s", name, existingDetectorId); | ||
logger.error(errorMsg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor. i would use a warning for this case since it's a harmless user error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure. will do |
||
onFailure(new IllegalArgumentException(errorMsg)); | ||
} else { | ||
indexAnomalyDetector(detectorId); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor. can this query also include a condition like not this detector id to utilize es search instead of additional postprocessing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a good point. will change to use must_not to exclude this detector id