Skip to content

Commit

Permalink
[7.x][ML] Reuse SourceDestValidator for data frame analytics (elastic…
Browse files Browse the repository at this point in the history
…#50841)

This commit removes validation logic of source and dest indices
for data frame analytics and replaces it with using the common
`SourceDestValidator` class which is already used by transforms.
This way the validations and their messages become consistent
while we reduce code.

This means that where these validations fail the error messages
will be slightly different for data frame analytics.

Backport of elastic#50841
  • Loading branch information
dimitris-athanasiou committed Jan 10, 2020
1 parent 7e68989 commit 998fb27
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 385 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public final class SourceDestValidator {

// messages
public static final String SOURCE_INDEX_MISSING = "Source index [{0}] does not exist";
public static final String SOURCE_LOWERCASE = "Source index [{0}] must be lowercase";
public static final String DEST_IN_SOURCE = "Destination index [{0}] is included in source expression [{1}]";
public static final String DEST_LOWERCASE = "Destination index [{0}] must be lowercase";
public static final String NEEDS_REMOTE_CLUSTER_SEARCH = "Source index is configured with a remote index pattern(s) [{0}]"
Expand All @@ -62,6 +61,7 @@ public final class SourceDestValidator {
+ "alias [{0}], at least a [{1}] license is required, found license [{2}]";
public static final String REMOTE_CLUSTER_LICENSE_INACTIVE = "License check failed for remote cluster "
+ "alias [{0}], license is not active";
public static final String REMOTE_SOURCE_INDICES_NOT_SUPPORTED = "remote source indices are not supported";

// workaround for 7.x: remoteClusterAliases does not throw
private static final ClusterNameExpressionResolver clusterNameExpressionResolver = new ClusterNameExpressionResolver();
Expand Down Expand Up @@ -222,7 +222,7 @@ private void resolveLocalAndRemoteSource() {
}
}

interface SourceDestValidation {
public interface SourceDestValidation {
void validate(Context context, ActionListener<Context> listener);
}

Expand All @@ -234,18 +234,7 @@ interface SourceDestValidation {
public static final SourceDestValidation REMOTE_SOURCE_VALIDATION = new RemoteSourceEnabledAndRemoteLicenseValidation();
public static final SourceDestValidation DESTINATION_IN_SOURCE_VALIDATION = new DestinationInSourceValidation();
public static final SourceDestValidation DESTINATION_SINGLE_INDEX_VALIDATION = new DestinationSingleIndexValidation();

// set of default validation collections, if you want to automatically benefit from new validators, use those
public static final List<SourceDestValidation> PREVIEW_VALIDATIONS = Arrays.asList(SOURCE_MISSING_VALIDATION, REMOTE_SOURCE_VALIDATION);

public static final List<SourceDestValidation> ALL_VALIDATIONS = Arrays.asList(
SOURCE_MISSING_VALIDATION,
REMOTE_SOURCE_VALIDATION,
DESTINATION_IN_SOURCE_VALIDATION,
DESTINATION_SINGLE_INDEX_VALIDATION
);

public static final List<SourceDestValidation> NON_DEFERABLE_VALIDATIONS = Arrays.asList(DESTINATION_SINGLE_INDEX_VALIDATION);
public static final SourceDestValidation REMOTE_SOURCE_NOT_SUPPORTED_VALIDATION = new RemoteSourceNotSupportedValidation();

/**
* Create a new Source Dest Validator
Expand Down Expand Up @@ -305,10 +294,11 @@ public void validate(
}
}, listener::onFailure);

// We traverse the validations in reverse order as we chain the listeners from back to front
for (int i = validations.size() - 1; i >= 0; i--) {
final SourceDestValidation validation = validations.get(i);
SourceDestValidation validation = validations.get(i);
final ActionListener<Context> previousValidationListener = validationListener;
validationListener = ActionListener.wrap(c -> { validation.validate(c, previousValidationListener); }, listener::onFailure);
validationListener = ActionListener.wrap(c -> validation.validate(c, previousValidationListener), listener::onFailure);
}

validationListener.onResponse(context);
Expand Down Expand Up @@ -433,13 +423,13 @@ public void validate(Context context, ActionListener<Context> listener) {
return;
}

if (context.resolvedSource.contains(destIndex)) {
if (context.resolveSource().contains(destIndex)) {
context.addValidationError(DEST_IN_SOURCE, destIndex, Strings.arrayToCommaDelimitedString(context.getSource()));
listener.onResponse(context);
return;
}

if (context.resolvedSource.contains(context.resolveDest())) {
if (context.resolveSource().contains(context.resolveDest())) {
context.addValidationError(
DEST_IN_SOURCE,
context.resolveDest(),
Expand All @@ -460,6 +450,17 @@ public void validate(Context context, ActionListener<Context> listener) {
}
}

static class RemoteSourceNotSupportedValidation implements SourceDestValidation {

@Override
public void validate(Context context, ActionListener<Context> listener) {
if (context.resolveRemoteSource().isEmpty() == false) {
context.addValidationError(REMOTE_SOURCE_INDICES_NOT_SUPPORTED);
}
listener.onResponse(context);
}
}

private static String getMessage(String message, Object... args) {
return new MessageFormat(message, Locale.ROOT).format(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.xpack.core.common.validation.SourceDestValidator;
import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig;
import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsSource;
import org.elasticsearch.xpack.core.ml.job.messages.Messages;
import org.elasticsearch.xpack.core.ml.utils.MlStrings;

import java.io.IOException;
import java.util.Objects;
Expand Down Expand Up @@ -90,14 +92,29 @@ public DataFrameAnalyticsConfig getConfig() {
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException error = null;
error = checkConfigIdIsValid(config, error);
error = SourceDestValidator.validateRequest(error, config.getDest().getIndex());
error = checkNoIncludedAnalyzedFieldsAreExcludedBySourceFiltering(config, error);
return error;
}

private ActionRequestValidationException checkConfigIdIsValid(DataFrameAnalyticsConfig config,
ActionRequestValidationException error) {
if (MlStrings.isValidId(config.getId()) == false) {
error = ValidateActions.addValidationError(Messages.getMessage(Messages.INVALID_ID, DataFrameAnalyticsConfig.ID,
config.getId()), error);
}
if (!MlStrings.hasValidLengthForId(config.getId())) {
error = ValidateActions.addValidationError(Messages.getMessage(Messages.ID_TOO_LONG, DataFrameAnalyticsConfig.ID,
config.getId(), MlStrings.ID_LENGTH_LIMIT), error);
}
return error;
}

private ActionRequestValidationException checkNoIncludedAnalyzedFieldsAreExcludedBySourceFiltering(
DataFrameAnalyticsConfig config, ActionRequestValidationException error) {
if (config.getAnalyzedFields() == null) {
return null;
return error;
}
for (String analyzedInclude : config.getAnalyzedFields().includes()) {
if (config.getSource().isFieldExcluded(analyzedInclude)) {
Expand All @@ -107,7 +124,7 @@ private ActionRequestValidationException checkNoIncludedAnalyzedFieldsAreExclude
+ DataFrameAnalyticsSource._SOURCE.getPreferredName() + "]", error);
}
}
return null;
return error;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.indices.InvalidIndexNameException;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

import static org.elasticsearch.cluster.metadata.MetaDataCreateIndexService.validateIndexOrAliasName;

public class DataFrameAnalyticsDest implements Writeable, ToXContentObject {

public static final ParseField INDEX = new ParseField("index");
Expand Down Expand Up @@ -94,13 +90,4 @@ public String getIndex() {
public String getResultsField() {
return resultsField;
}

public void validate() {
if (index != null) {
validateIndexOrAliasName(index, InvalidIndexNameException::new);
if (index.toLowerCase(Locale.ROOT).equals(index) == false) {
throw new InvalidIndexNameException(index, "dest.index must be lowercase");
}
}
}
}
Loading

0 comments on commit 998fb27

Please sign in to comment.