Skip to content

Commit

Permalink
Deprecate the unused elasticsearch_version field of enrich policy json (
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo committed Dec 5, 2023
1 parent 714611e commit 4934d08
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 66 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/103013.yaml
@@ -0,0 +1,5 @@
pr: 103013
summary: Deprecate the unused `elasticsearch_version` field of enrich policy json
area: Ingest Node
type: enhancement
issues: []
Expand Up @@ -190,6 +190,7 @@ static TransportVersion def(int id) {
public static final TransportVersion SOURCE_IN_SINGLE_VALUE_QUERY_ADDED = def(8_557_00_0);
public static final TransportVersion MISSED_INDICES_UPDATE_EXCEPTION_ADDED = def(8_558_00_0);
public static final TransportVersion INFERENCE_SERVICE_EMBEDDING_SIZE_ADDED = def(8_559_00_0);
public static final TransportVersion ENRICH_ELASTICSEARCH_VERSION_REMOVED = def(8_560_00_0);

/*
* STOP! READ THIS FIRST! No, really,
Expand Down
Expand Up @@ -6,16 +6,18 @@
*/
package org.elasticsearch.xpack.core.enrich;

import org.elasticsearch.TransportVersions;
import org.elasticsearch.Version;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.logging.DeprecationCategory;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ObjectParser.ValueType;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentFragment;
import org.elasticsearch.xcontent.XContentBuilder;
Expand All @@ -33,6 +35,11 @@
*/
public final class EnrichPolicy implements Writeable, ToXContentFragment {

private static final String ELASTICEARCH_VERSION_DEPRECATION_MESSAGE =
"the [elasticsearch_version] field of an enrich policy has no effect and will be removed in Elasticsearch 9.0";

private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(EnrichPolicy.class);

public static final String ENRICH_INDEX_NAME_BASE = ".enrich-";
public static final String ENRICH_INDEX_PATTERN = ENRICH_INDEX_NAME_BASE + "*";

Expand All @@ -57,7 +64,7 @@ public final class EnrichPolicy implements Writeable, ToXContentFragment {
(List<String>) args[1],
(String) args[2],
(List<String>) args[3],
(Version) args[4]
(String) args[4]
)
);

Expand All @@ -74,12 +81,7 @@ private static <T> void declareCommonConstructorParsingOptions(ConstructingObjec
parser.declareStringArray(ConstructingObjectParser.constructorArg(), INDICES);
parser.declareString(ConstructingObjectParser.constructorArg(), MATCH_FIELD);
parser.declareStringArray(ConstructingObjectParser.constructorArg(), ENRICH_FIELDS);
parser.declareField(
ConstructingObjectParser.optionalConstructorArg(),
((p, c) -> Version.fromString(p.text())),
ELASTICSEARCH_VERSION,
ValueType.STRING
);
parser.declareString(ConstructingObjectParser.optionalConstructorArg(), ELASTICSEARCH_VERSION);
}

public static EnrichPolicy fromXContent(XContentParser parser) throws IOException {
Expand Down Expand Up @@ -108,37 +110,45 @@ public static EnrichPolicy fromXContent(XContentParser parser) throws IOExceptio
private final List<String> indices;
private final String matchField;
private final List<String> enrichFields;
private final Version elasticsearchVersion;

public EnrichPolicy(StreamInput in) throws IOException {
this(
in.readString(),
in.readOptionalWriteable(QuerySource::new),
in.readStringCollectionAsList(),
in.readString(),
in.readStringCollectionAsList(),
Version.readVersion(in)
);
this.type = in.readString();
this.query = in.readOptionalWriteable(QuerySource::new);
this.indices = in.readStringCollectionAsList();
this.matchField = in.readString();
this.enrichFields = in.readStringCollectionAsList();
if (in.getTransportVersion().before(TransportVersions.ENRICH_ELASTICSEARCH_VERSION_REMOVED)) {
// consume the passed-in meaningless version that old elasticsearch clusters will send
Version.readVersion(in);
}
}

public EnrichPolicy(String type, QuerySource query, List<String> indices, String matchField, List<String> enrichFields) {
this(type, query, indices, matchField, enrichFields, Version.CURRENT);
this.type = type;
this.query = query;
this.indices = indices;
this.matchField = matchField;
this.enrichFields = enrichFields;
}

public EnrichPolicy(
private EnrichPolicy(
String type,
QuerySource query,
List<String> indices,
String matchField,
List<String> enrichFields,
Version elasticsearchVersion
String elasticsearchVersion
) {
this.type = type;
this.query = query;
this.indices = indices;
this.matchField = matchField;
this.enrichFields = enrichFields;
this.elasticsearchVersion = elasticsearchVersion != null ? elasticsearchVersion : Version.CURRENT;
this(type, query, indices, matchField, enrichFields);
// for backwards compatibility reasons, it is possible to pass in an elasticsearchVersion -- that version is
// completely ignored and does nothing. we'll fix that in a future version, so send a deprecation warning.
if (elasticsearchVersion != null) {
deprecationLogger.warn(
DeprecationCategory.OTHER,
"enrich_policy_with_elasticsearch_version",
ELASTICEARCH_VERSION_DEPRECATION_MESSAGE
);
}
}

public String getType() {
Expand All @@ -161,10 +171,6 @@ public List<String> getEnrichFields() {
return enrichFields;
}

public Version getElasticsearchVersion() {
return elasticsearchVersion;
}

public static String getBaseName(String policyName) {
return ENRICH_INDEX_NAME_BASE + policyName;
}
Expand Down Expand Up @@ -202,7 +208,10 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeStringCollection(indices);
out.writeString(matchField);
out.writeStringCollection(enrichFields);
Version.writeVersion(elasticsearchVersion, out);
if (out.getTransportVersion().before(TransportVersions.ENRICH_ELASTICSEARCH_VERSION_REMOVED)) {
// emit the current version of elasticsearch for bwc serialization reasons
Version.writeVersion(Version.CURRENT, out);
}
}

@Override
Expand All @@ -222,9 +231,6 @@ private void toInnerXContent(XContentBuilder builder, Params params) throws IOEx
builder.array(INDICES.getPreferredName(), indices.toArray(new String[0]));
builder.field(MATCH_FIELD.getPreferredName(), matchField);
builder.array(ENRICH_FIELDS.getPreferredName(), enrichFields.toArray(new String[0]));
if (params.paramAsBoolean("include_version", false) && elasticsearchVersion != null) {
builder.field(ELASTICSEARCH_VERSION.getPreferredName(), elasticsearchVersion.toString());
}
}

@Override
Expand All @@ -236,13 +242,12 @@ public boolean equals(Object o) {
&& Objects.equals(query, policy.query)
&& indices.equals(policy.indices)
&& matchField.equals(policy.matchField)
&& enrichFields.equals(policy.enrichFields)
&& elasticsearchVersion.equals(policy.elasticsearchVersion);
&& enrichFields.equals(policy.enrichFields);
}

@Override
public int hashCode() {
return Objects.hash(type, query, indices, matchField, enrichFields, elasticsearchVersion);
return Objects.hash(type, query, indices, matchField, enrichFields);
}

public String toString() {
Expand Down Expand Up @@ -310,7 +315,7 @@ public static class NamedPolicy implements Writeable, ToXContentFragment {
(List<String>) args[2],
(String) args[3],
(List<String>) args[4],
(Version) args[5]
(String) args[5]
)
)
);
Expand Down
Expand Up @@ -6,7 +6,6 @@
*/
package org.elasticsearch.xpack.core.enrich.action;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
Expand Down Expand Up @@ -39,11 +38,6 @@ public static class Request extends MasterNodeRequest<PutEnrichPolicyAction.Requ

public Request(String name, EnrichPolicy policy) {
this.name = Objects.requireNonNull(name, "name cannot be null");
if (Version.CURRENT.equals(policy.getElasticsearchVersion()) == false) {
throw new IllegalArgumentException(
"Cannot set [version_created] field on enrich policy [" + name + "]. Found [" + policy.getElasticsearchVersion() + "]"
);
}
this.policy = policy;
}

Expand Down
Expand Up @@ -8,7 +8,6 @@

import org.elasticsearch.ResourceAlreadyExistsException;
import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.Version;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
Expand Down Expand Up @@ -81,21 +80,8 @@ public static void putPolicy(
);
}

final EnrichPolicy finalPolicy;
if (policy.getElasticsearchVersion() == null) {
finalPolicy = new EnrichPolicy(
policy.getType(),
policy.getQuery(),
policy.getIndices(),
policy.getMatchField(),
policy.getEnrichFields(),
Version.CURRENT
);
} else {
finalPolicy = policy;
}
updateClusterState(clusterService, handler, current -> {
for (String indexExpression : finalPolicy.getIndices()) {
for (String indexExpression : policy.getIndices()) {
// indices field in policy can contain wildcards, aliases etc.
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(
current,
Expand All @@ -110,12 +96,12 @@ public static void putPolicy(
throw new IllegalArgumentException("source index [" + concreteIndex + "] has no mapping");
}
Map<String, Object> mappingSource = mapping.getSourceAsMap();
EnrichPolicyRunner.validateMappings(name, finalPolicy, concreteIndex, mappingSource);
EnrichPolicyRunner.validateMappings(name, policy, concreteIndex, mappingSource);
}
}

final Map<String, EnrichPolicy> policies = getPolicies(current);
EnrichPolicy existing = policies.putIfAbsent(name, finalPolicy);
EnrichPolicy existing = policies.putIfAbsent(name, policy);
if (existing != null) {
throw new ResourceAlreadyExistsException("policy [{}] already exists", name);
}
Expand Down
Expand Up @@ -313,7 +313,7 @@ private void testNumberRangeMatchType(String rangeType) throws Exception {
}
);
List<String> enrichFields = List.of("zipcode");
EnrichPolicy policy = new EnrichPolicy(EnrichPolicy.RANGE_TYPE, null, List.of(sourceIndex), "range", enrichFields, null);
EnrichPolicy policy = new EnrichPolicy(EnrichPolicy.RANGE_TYPE, null, List.of(sourceIndex), "range", enrichFields);
String policyName = "test1";

final long createTime = randomNonNegativeLong();
Expand Down
@@ -1,6 +1,4 @@
---
"Test enrich crud apis":

setup:
- do:
indices.create:
index: bar
Expand All @@ -13,8 +11,9 @@
type: keyword
b:
type: keyword
- is_true: acknowledged

---
"Test enrich crud apis":
- do:
enrich.put_policy:
name: policy-crud
Expand Down Expand Up @@ -60,3 +59,28 @@
enrich.delete_policy:
name: policy-crud
- is_true: acknowledged

---
"Test using the deprecated elasticsearch_version field results in a warning":
- skip:
version: " - 8.11.99"
reason: "elasticsearch_version field deprecated in 8.12.0, to be removed in 9.0"
features: warnings

- do:
warnings:
- "the [elasticsearch_version] field of an enrich policy has no effect and will be removed in Elasticsearch 9.0"
enrich.put_policy:
name: policy-crud-warning
body:
match:
indices: ["bar*"]
match_field: baz
enrich_fields: ["a", "b"]
elasticsearch_version: "any string here is acceptable"
- is_true: acknowledged

- do:
enrich.delete_policy:
name: policy-crud-warning
- is_true: acknowledged

0 comments on commit 4934d08

Please sign in to comment.