Skip to content

Commit

Permalink
Throw IAE for runtime mappings in _field_caps for older nodes
Browse files Browse the repository at this point in the history
Typically we silently ignore nodes below a certain version that don't support a
specific feature when serializing requests between nodes. With runtime fields we
chose to throw errors instead when one of the nodes a search request is sent to
has a version that does not support the runtime fields feature. This change adds
the same behaviour for support of the "runtime_mappings" section introduced in elastic#68904
in the "_field_caps" API.

Relates to elastic#68904
  • Loading branch information
Christoph Büscher committed Mar 1, 2021
1 parent 50926fd commit afe9f01
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public void writeTo(StreamOutput out) throws IOException {
}
if (out.getVersion().onOrAfter(Version.V_7_12_0)) {
out.writeMap(runtimeFields);
} else {
if (false == runtimeFields.isEmpty()) {
throw new IllegalArgumentException(
"Versions before 7.12.0 don't support [runtime_mappings], but trying to send _field_caps request to a node "
+ "with version [" + out.getVersion()+ "]");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public void writeTo(StreamOutput out) throws IOException {
}
if (out.getVersion().onOrAfter(Version.V_7_12_0)) {
out.writeMap(runtimeFields);
} else {
if (false == runtimeFields.isEmpty()) {
throw new IllegalArgumentException(
"Versions before 7.12.0 don't support [runtime_mappings], but trying to send _field_caps request to a node "
+ "with version [" + out.getVersion() + "]");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.action.fieldcaps;

import org.elasticsearch.Version;
import org.elasticsearch.action.OriginalIndices;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;

import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.equalTo;

public class FieldCapabilitiesIndexRequestTests extends ESTestCase {

public void testSerializingWithRuntimeFieldsBeforeSupportedThrows() {
FieldCapabilitiesIndexRequest request = new FieldCapabilitiesIndexRequest(
new String[] { "field" },
"index",
new OriginalIndices(new String[] { "original_index" }, IndicesOptions.LENIENT_EXPAND_OPEN),
null,
0L,
singletonMap("day_of_week", singletonMap("type", "keyword"))
);
Version v = VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, VersionUtils.getPreviousVersion(Version.V_7_12_0));
Exception e = expectThrows(
IllegalArgumentException.class,
() -> copyWriteable(request, writableRegistry(), FieldCapabilitiesRequest::new, v)
);
assertThat(e.getMessage(), equalTo("Versions before 7.12.0 don't support [runtime_mappings], but trying to send _field_caps "
+ "request to a node with version [" + v + "]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.elasticsearch.action.fieldcaps;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.bytes.BytesReference;
Expand All @@ -22,6 +23,7 @@
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchModule;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
import org.elasticsearch.test.VersionUtils;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -30,6 +32,7 @@
import java.util.function.Consumer;

import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.equalTo;

public class FieldCapabilitiesRequestTests extends AbstractWireSerializingTestCase<FieldCapabilitiesRequest> {

Expand Down Expand Up @@ -138,4 +141,16 @@ public void testValidation() {
ActionRequestValidationException exception = request.validate();
assertNotNull(exception);
}

public void testSerializingWithRuntimeFieldsBeforeSupportedThrows() {
FieldCapabilitiesRequest request = new FieldCapabilitiesRequest();
request.runtimeFields(singletonMap("day_of_week", singletonMap("type", "keyword")));
Version v = VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, VersionUtils.getPreviousVersion(Version.V_7_12_0));
Exception e = expectThrows(
IllegalArgumentException.class,
() -> copyWriteable(request, writableRegistry(), FieldCapabilitiesRequest::new, v)
);
assertThat(e.getMessage(), equalTo("Versions before 7.12.0 don't support [runtime_mappings], but trying to send _field_caps request "
+ "to a node with version [" + v + "]"));
}
}

0 comments on commit afe9f01

Please sign in to comment.