Skip to content

Commit 4c6afcf

Browse files
committed
BWC changes and more test cases added
1 parent 7d7510c commit 4c6afcf

File tree

7 files changed

+94
-26
lines changed

7 files changed

+94
-26
lines changed

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.resolve_index/25_resolve_index_with_mode.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ setup:
1414
---
1515
"resolve index returns mode for standard and time_series indices":
1616
- requires:
17-
cluster_features: ["gte_v8.5.0"]
18-
reason: "Requires time series indexing support introduced in v8.5.0"
17+
cluster_features: ["gte_v8.5.0", "resolve_index_returns_mode"]
18+
reason: "Requires time series indexing support introduced in v8.5.0 & Node must support returning 'mode' in indices.resolve_index response"
1919

2020
# Create a standard index
2121
- do:

server/src/main/java/module-info.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@
434434
org.elasticsearch.script.ScriptFeatures,
435435
org.elasticsearch.search.retriever.RetrieversFeatures,
436436
org.elasticsearch.action.admin.cluster.stats.ClusterStatsFeatures,
437-
org.elasticsearch.ingest.IngestFeatures;
437+
org.elasticsearch.ingest.IngestFeatures,
438+
org.elasticsearch.action.admin.indices.resolve.ResolveIndexFeatures;
438439

439440
uses org.elasticsearch.plugins.internal.SettingsExtension;
440441
uses RestExtension;

server/src/main/java/org/elasticsearch/action/admin/indices/resolve/ResolveIndexAction.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,25 +183,21 @@ public static class ResolvedIndex extends ResolvedIndexAbstraction implements Wr
183183
private final String[] aliases;
184184
private final String[] attributes;
185185
private final String dataStream;
186-
private final String mode;
186+
private final IndexMode mode;
187187

188188
ResolvedIndex(StreamInput in) throws IOException {
189189
setName(in.readString());
190190
this.aliases = in.readStringArray();
191191
this.attributes = in.readStringArray();
192192
this.dataStream = in.readOptionalString();
193193
if (in.getTransportVersion().onOrAfter(TransportVersions.RESOLVE_INDEX_MODE_ADDED)) {
194-
this.mode = in.readOptionalString();
194+
this.mode = IndexMode.readFrom(in);
195195
} else {
196196
this.mode = null;
197197
}
198198
}
199199

200-
ResolvedIndex(String name, String[] aliases, String[] attributes, @Nullable String dataStream) {
201-
this(name, aliases, attributes, dataStream, null);
202-
}
203-
204-
ResolvedIndex(String name, String[] aliases, String[] attributes, @Nullable String dataStream, String mode) {
200+
ResolvedIndex(String name, String[] aliases, String[] attributes, @Nullable String dataStream, IndexMode mode) {
205201
super(name);
206202
this.aliases = aliases;
207203
this.attributes = attributes;
@@ -225,7 +221,7 @@ public String getDataStream() {
225221
return dataStream;
226222
}
227223

228-
public String getMode() {
224+
public IndexMode getMode() {
229225
return mode;
230226
}
231227

@@ -236,7 +232,7 @@ public void writeTo(StreamOutput out) throws IOException {
236232
out.writeStringArray(attributes);
237233
out.writeOptionalString(dataStream);
238234
if (out.getTransportVersion().onOrAfter(TransportVersions.RESOLVE_INDEX_MODE_ADDED)) {
239-
out.writeOptionalString(mode);
235+
IndexMode.writeTo(mode, out);
240236
}
241237
}
242238

@@ -251,8 +247,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
251247
if (Strings.isNullOrEmpty(dataStream) == false) {
252248
builder.field(DATA_STREAM_FIELD.getPreferredName(), dataStream);
253249
}
254-
if (Strings.isNullOrEmpty(mode) == false) {
255-
builder.field(MODE_FIELD.getPreferredName(), mode);
250+
if (mode != null) {
251+
builder.field(MODE_FIELD.getPreferredName(), mode.toString());
256252
}
257253
builder.endObject();
258254
return builder;
@@ -666,7 +662,7 @@ private static void enrichIndexAbstraction(
666662
aliasNames,
667663
attributes.stream().map(Enum::name).map(e -> e.toLowerCase(Locale.ROOT)).toArray(String[]::new),
668664
ia.getParentDataStream() == null ? null : ia.getParentDataStream().getName(),
669-
writeIndex.getIndexMode() == null ? IndexMode.STANDARD.toString() : writeIndex.getIndexMode().getName()
665+
writeIndex.getIndexMode() == null ? IndexMode.STANDARD : writeIndex.getIndexMode()
670666
)
671667
);
672668
}

server/src/test/java/org/elasticsearch/action/admin/indices/resolve/ResolveIndexResponseTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.Response;
1616
import org.elasticsearch.common.Strings;
1717
import org.elasticsearch.common.io.stream.Writeable;
18+
import org.elasticsearch.index.IndexMode;
1819
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
1920
import org.elasticsearch.xcontent.ConstructingObjectParser;
2021
import org.elasticsearch.xcontent.XContentParser;
@@ -28,6 +29,7 @@
2829
import static org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.ResolvedIndex.ALIASES_FIELD;
2930
import static org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.ResolvedIndex.ATTRIBUTES_FIELD;
3031
import static org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.ResolvedIndex.DATA_STREAM_FIELD;
32+
import static org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.ResolvedIndex.MODE_FIELD;
3133
import static org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.ResolvedIndexAbstraction.NAME_FIELD;
3234
import static org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.Response.DATA_STREAMS_FIELD;
3335
import static org.elasticsearch.action.admin.indices.resolve.ResolveIndexAction.Response.INDICES_FIELD;
@@ -76,8 +78,9 @@ private static ResolvedIndex createTestResolvedIndexInstance() {
7678
String[] aliases = randomStringArray(0, 5);
7779
String[] attributes = randomSubsetOf(List.of("open", "hidden", "frozen")).toArray(Strings.EMPTY_ARRAY);
7880
String dataStream = randomBoolean() ? randomAlphaOfLength(6) : null;
81+
IndexMode mode = randomFrom(IndexMode.values());
7982

80-
return new ResolvedIndex(name, aliases, attributes, dataStream);
83+
return new ResolvedIndex(name, aliases, attributes, dataStream, mode);
8184
}
8285

8386
private static ResolvedAlias createTestResolvedAliasInstance() {
@@ -109,7 +112,8 @@ static String[] randomStringArray(int minLength, int maxLength) {
109112
(String) args[0],
110113
args[1] != null ? ((List<String>) args[1]).toArray(Strings.EMPTY_ARRAY) : new String[0],
111114
((List<String>) args[2]).toArray(Strings.EMPTY_ARRAY),
112-
(String) args[3]
115+
(String) args[3],
116+
IndexMode.fromString((String) args[4])
113117
)
114118
);
115119
@SuppressWarnings("unchecked")
@@ -133,6 +137,7 @@ static String[] randomStringArray(int minLength, int maxLength) {
133137
INDEX_PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), ALIASES_FIELD);
134138
INDEX_PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), ATTRIBUTES_FIELD);
135139
INDEX_PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), DATA_STREAM_FIELD);
140+
INDEX_PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), MODE_FIELD);
136141
ALIAS_PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME_FIELD);
137142
ALIAS_PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), INDICES_FIELD);
138143
RESPONSE_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> indexFromXContent(p), INDICES_FIELD);

server/src/test/java/org/elasticsearch/action/admin/indices/resolve/ResolveIndexTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ private void validateIndices(List<ResolvedIndex> resolvedIndices, String... expe
365365
assertThat(resolvedIndex.getAliases(), is(((String[]) indexInfo[6])));
366366
assertThat(resolvedIndex.getAttributes(), is(flagsToAttributes(indexInfo)));
367367
assertThat(resolvedIndex.getDataStream(), equalTo((String) indexInfo[5]));
368-
assertThat(resolvedIndex.getMode(), equalTo(((IndexMode) indexInfo[7]).toString()));
368+
assertThat(resolvedIndex.getMode().toString(), equalTo(((IndexMode) indexInfo[7]).toString()));
369369
}
370370
}
371371

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.action.admin.indices.resolve;
11+
12+
import org.elasticsearch.common.io.stream.Writeable;
13+
import org.elasticsearch.index.IndexMode;
14+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
19+
public class ResolvedIndexSerializingTests extends AbstractWireSerializingTestCase<ResolveIndexAction.ResolvedIndex> {
20+
21+
@Override
22+
protected Writeable.Reader<ResolveIndexAction.ResolvedIndex> instanceReader() {
23+
return ResolveIndexAction.ResolvedIndex::new;
24+
}
25+
26+
@Override
27+
protected ResolveIndexAction.ResolvedIndex mutateInstance(ResolveIndexAction.ResolvedIndex instance) {
28+
String name = instance.getName();
29+
String[] aliases = instance.getAliases();
30+
String[] attributes = instance.getAttributes();
31+
String dataStream = instance.getDataStream();
32+
IndexMode mode = instance.getMode();
33+
mode = randomValueOtherThan(mode, () -> randomFrom(IndexMode.values()));
34+
return new ResolveIndexAction.ResolvedIndex(name, aliases, attributes, dataStream, mode);
35+
}
36+
37+
@Override
38+
protected ResolveIndexAction.ResolvedIndex createTestInstance() {
39+
return createTestItem();
40+
}
41+
42+
private static ResolveIndexAction.ResolvedIndex createTestItem() {
43+
// Random index name
44+
final String name = randomAlphaOfLengthBetween(5, 20);
45+
46+
// Random aliases (possibly empty)
47+
final String[] aliases = randomBoolean()
48+
? new String[0]
49+
: randomArray(0, 4, String[]::new, () -> randomAlphaOfLengthBetween(3, 15));
50+
51+
// Attributes: always one of "open"/"closed", plus optional flags
52+
final List<String> attrs = new ArrayList<>();
53+
attrs.add(randomBoolean() ? "open" : "closed");
54+
if (randomBoolean()) attrs.add("hidden");
55+
if (randomBoolean()) attrs.add("system");
56+
if (randomBoolean()) attrs.add("frozen");
57+
final String[] attributes = attrs.toArray(new String[0]);
58+
59+
final String dataStream = randomBoolean() ? randomAlphaOfLengthBetween(3, 15) : null;
60+
61+
final IndexMode mode = randomFrom(IndexMode.values());
62+
63+
return new ResolveIndexAction.ResolvedIndex(name, aliases, attributes, dataStream, mode);
64+
65+
}
66+
}

server/src/test/java/org/elasticsearch/action/admin/indices/resolve/TransportResolveClusterActionTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,30 @@ public void testHasNonClosedMatchingIndex() {
104104

105105
// as long as there is one non-closed index it should return true
106106
indices = new ArrayList<>();
107-
indices.add(new ResolveIndexAction.ResolvedIndex("foo", null, new String[] { "open" }, ".ds-foo"));
107+
indices.add(new ResolveIndexAction.ResolvedIndex("foo", null, new String[] { "open" }, ".ds-foo", null));
108108
assertThat(TransportResolveClusterAction.hasNonClosedMatchingIndex(indices), equalTo(true));
109109

110-
indices.add(new ResolveIndexAction.ResolvedIndex("bar", null, new String[] { "system" }, ".ds-bar"));
110+
indices.add(new ResolveIndexAction.ResolvedIndex("bar", null, new String[] { "system" }, ".ds-bar", null));
111111
assertThat(TransportResolveClusterAction.hasNonClosedMatchingIndex(indices), equalTo(true));
112112

113-
indices.add(new ResolveIndexAction.ResolvedIndex("baz", null, new String[] { "system", "open", "hidden" }, null));
113+
indices.add(new ResolveIndexAction.ResolvedIndex("baz", null, new String[] { "system", "open", "hidden" }, null, null));
114114
assertThat(TransportResolveClusterAction.hasNonClosedMatchingIndex(indices), equalTo(true));
115115

116-
indices.add(new ResolveIndexAction.ResolvedIndex("quux", null, new String[0], null));
116+
indices.add(new ResolveIndexAction.ResolvedIndex("quux", null, new String[0], null, null));
117117
assertThat(TransportResolveClusterAction.hasNonClosedMatchingIndex(indices), equalTo(true));
118118

119-
indices.add(new ResolveIndexAction.ResolvedIndex("wibble", null, new String[] { "system", "closed" }, null));
119+
indices.add(new ResolveIndexAction.ResolvedIndex("wibble", null, new String[] { "system", "closed" }, null, null));
120120
assertThat(TransportResolveClusterAction.hasNonClosedMatchingIndex(indices), equalTo(true));
121121

122122
// if only closed indexes are present, should return false
123123
indices.clear();
124-
indices.add(new ResolveIndexAction.ResolvedIndex("wibble", null, new String[] { "system", "closed" }, null));
124+
indices.add(new ResolveIndexAction.ResolvedIndex("wibble", null, new String[] { "system", "closed" }, null, null));
125125
assertThat(TransportResolveClusterAction.hasNonClosedMatchingIndex(indices), equalTo(false));
126-
indices.add(new ResolveIndexAction.ResolvedIndex("wobble", null, new String[] { "closed" }, null));
126+
indices.add(new ResolveIndexAction.ResolvedIndex("wobble", null, new String[] { "closed" }, null, null));
127127
assertThat(TransportResolveClusterAction.hasNonClosedMatchingIndex(indices), equalTo(false));
128128

129129
// now add a non-closed index and should return true
130-
indices.add(new ResolveIndexAction.ResolvedIndex("aaa", null, new String[] { "hidden" }, null));
130+
indices.add(new ResolveIndexAction.ResolvedIndex("aaa", null, new String[] { "hidden" }, null, null));
131131
assertThat(TransportResolveClusterAction.hasNonClosedMatchingIndex(indices), equalTo(true));
132132
}
133133
}

0 commit comments

Comments
 (0)