-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE][ML] Stregthen source dest validations for DF analytics #43399
Merged
dimitris-athanasiou
merged 4 commits into
elastic:feature-ml-data-frame-analytics
from
dimitris-athanasiou:stregthen-source-dest-validations-for-df-analytics
Jun 24, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f421ec7
[FEATURE][ML] Stregthen source dest validations for DF analytics
dimitris-athanasiou f9dfd65
Fix failing tests
dimitris-athanasiou 268a083
Add test with dest index being a wildcard pattern
dimitris-athanasiou 7fe9410
Fix test failure in security suite
dimitris-athanasiou 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
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
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
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
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
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
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
65 changes: 65 additions & 0 deletions
65
x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/SourceDestValidator.java
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.ml.dataframe; | ||
|
||
import org.elasticsearch.action.support.IndicesOptions; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.regex.Regex; | ||
import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; | ||
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class SourceDestValidator { | ||
|
||
private final ClusterState clusterState; | ||
private final IndexNameExpressionResolver indexNameExpressionResolver; | ||
|
||
public SourceDestValidator(ClusterState clusterState, IndexNameExpressionResolver indexNameExpressionResolver) { | ||
this.clusterState = Objects.requireNonNull(clusterState); | ||
this.indexNameExpressionResolver = Objects.requireNonNull(indexNameExpressionResolver); | ||
} | ||
|
||
public void check(DataFrameAnalyticsConfig config) { | ||
String sourceIndex = config.getSource().getIndex(); | ||
String destIndex = config.getDest().getIndex(); | ||
|
||
String[] sourceExpressions = Strings.tokenizeToStringArray(sourceIndex, ","); | ||
|
||
for (String sourceExpression : sourceExpressions) { | ||
if (Regex.simpleMatch(sourceExpression, destIndex)) { | ||
throw ExceptionsHelper.badRequestException("Destination index [{}] must not be included in source index [{}]", | ||
destIndex, sourceExpression); | ||
} | ||
} | ||
|
||
Set<String> concreteSourceIndexNames = new HashSet<>(Arrays.asList(indexNameExpressionResolver.concreteIndexNames(clusterState, | ||
IndicesOptions.lenientExpandOpen(), sourceExpressions))); | ||
|
||
if (concreteSourceIndexNames.isEmpty()) { | ||
throw ExceptionsHelper.badRequestException("No index matches source index [{}]", sourceIndex); | ||
} | ||
|
||
final String[] concreteDestIndexNames = indexNameExpressionResolver.concreteIndexNames(clusterState, | ||
IndicesOptions.lenientExpandOpen(), destIndex); | ||
|
||
if (concreteDestIndexNames.length > 1) { | ||
// In case it is an alias, it may match multiple indices | ||
throw ExceptionsHelper.badRequestException("Destination index [{}] should match a single index; matches {}", destIndex, | ||
Arrays.toString(concreteDestIndexNames)); | ||
} | ||
if (concreteDestIndexNames.length == 1 && concreteSourceIndexNames.contains(concreteDestIndexNames[0])) { | ||
// In case the dest index is an alias, we need to check the concrete index is not matched by source | ||
throw ExceptionsHelper.badRequestException("Destination index [{}], which is an alias for [{}], " + | ||
"must not be included in source index [{}]", destIndex, concreteDestIndexNames[0], sourceIndex); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
I suppose it is just a Java thing, but I have trouble with creating an object with state, but an internal method is only called once. Seems like a static
check(ClusterState clusterState, IndexNameExpressionResolver indexNameExpressionResolver, DataFrameAnalyticsConfig config)
is fewer lines of code and less state to break.Of course, I am hypocritical with this :). I often make these classes myself.
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.
I know what you mean. I insist going for objects though because I think they lend themselves better for expansion. On the other hand, when one sees a static method it discourages refactoring to make an object. Of course, only time will tell. My view is that when paradigms are not clearly better or worse than alternatives, we have to try them out and wait for empirical evidence to reward us or slap us in the face :-)