Skip to content

Commit 0a1ede2

Browse files
author
Christoph Büscher
committed
Throw IAE for runtime mappings in _field_caps for older nodes (#69722)
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 #68904 in the "_field_caps" API. Relates to #68904
1 parent f1de81a commit 0a1ede2

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesIndexRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ public void writeTo(StreamOutput out) throws IOException {
124124
}
125125
if (out.getVersion().onOrAfter(Version.V_7_12_0)) {
126126
out.writeMap(runtimeFields);
127+
} else {
128+
if (false == runtimeFields.isEmpty()) {
129+
throw new IllegalArgumentException(
130+
"Versions before 7.12.0 don't support [runtime_mappings], but trying to send _field_caps request to a node "
131+
+ "with version [" + out.getVersion()+ "]");
132+
}
127133
}
128134
}
129135

server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ public void writeTo(StreamOutput out) throws IOException {
9696
}
9797
if (out.getVersion().onOrAfter(Version.V_7_12_0)) {
9898
out.writeMap(runtimeFields);
99+
} else {
100+
if (false == runtimeFields.isEmpty()) {
101+
throw new IllegalArgumentException(
102+
"Versions before 7.12.0 don't support [runtime_mappings], but trying to send _field_caps request to a node "
103+
+ "with version [" + out.getVersion() + "]");
104+
}
99105
}
100106
}
101107

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.action.fieldcaps;
10+
11+
import org.elasticsearch.Version;
12+
import org.elasticsearch.action.OriginalIndices;
13+
import org.elasticsearch.action.support.IndicesOptions;
14+
import org.elasticsearch.test.ESTestCase;
15+
import org.elasticsearch.test.VersionUtils;
16+
17+
import static java.util.Collections.singletonMap;
18+
import static org.hamcrest.Matchers.equalTo;
19+
20+
public class FieldCapabilitiesIndexRequestTests extends ESTestCase {
21+
22+
public void testSerializingWithRuntimeFieldsBeforeSupportedThrows() {
23+
FieldCapabilitiesIndexRequest request = new FieldCapabilitiesIndexRequest(
24+
new String[] { "field" },
25+
"index",
26+
new OriginalIndices(new String[] { "original_index" }, IndicesOptions.LENIENT_EXPAND_OPEN),
27+
null,
28+
0L,
29+
singletonMap("day_of_week", singletonMap("type", "keyword"))
30+
);
31+
Version v = VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, VersionUtils.getPreviousVersion(Version.V_7_12_0));
32+
Exception e = expectThrows(
33+
IllegalArgumentException.class,
34+
() -> copyWriteable(request, writableRegistry(), FieldCapabilitiesRequest::new, v)
35+
);
36+
assertThat(e.getMessage(), equalTo("Versions before 7.12.0 don't support [runtime_mappings], but trying to send _field_caps "
37+
+ "request to a node with version [" + v + "]"));
38+
}
39+
}

server/src/test/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesRequestTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.elasticsearch.action.fieldcaps;
1010

11+
import org.elasticsearch.Version;
1112
import org.elasticsearch.action.ActionRequestValidationException;
1213
import org.elasticsearch.action.support.IndicesOptions;
1314
import org.elasticsearch.common.bytes.BytesReference;
@@ -22,6 +23,7 @@
2223
import org.elasticsearch.index.query.QueryBuilders;
2324
import org.elasticsearch.search.SearchModule;
2425
import org.elasticsearch.test.AbstractWireSerializingTestCase;
26+
import org.elasticsearch.test.VersionUtils;
2527

2628
import java.io.IOException;
2729
import java.util.ArrayList;
@@ -30,6 +32,7 @@
3032
import java.util.function.Consumer;
3133

3234
import static java.util.Collections.singletonMap;
35+
import static org.hamcrest.Matchers.equalTo;
3336

3437
public class FieldCapabilitiesRequestTests extends AbstractWireSerializingTestCase<FieldCapabilitiesRequest> {
3538

@@ -138,4 +141,16 @@ public void testValidation() {
138141
ActionRequestValidationException exception = request.validate();
139142
assertNotNull(exception);
140143
}
144+
145+
public void testSerializingWithRuntimeFieldsBeforeSupportedThrows() {
146+
FieldCapabilitiesRequest request = new FieldCapabilitiesRequest();
147+
request.runtimeFields(singletonMap("day_of_week", singletonMap("type", "keyword")));
148+
Version v = VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, VersionUtils.getPreviousVersion(Version.V_7_12_0));
149+
Exception e = expectThrows(
150+
IllegalArgumentException.class,
151+
() -> copyWriteable(request, writableRegistry(), FieldCapabilitiesRequest::new, v)
152+
);
153+
assertThat(e.getMessage(), equalTo("Versions before 7.12.0 don't support [runtime_mappings], but trying to send _field_caps "
154+
+ "request to a node with version [" + v + "]"));
155+
}
141156
}

0 commit comments

Comments
 (0)