Skip to content

Commit 117055d

Browse files
authored
Get index includes parent data stream for backing indices (#56022)
1 parent 410b03e commit 117055d

File tree

11 files changed

+215
-52
lines changed

11 files changed

+215
-52
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexResponse.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ public class GetIndexResponse {
4747
private Map<String, List<AliasMetadata>> aliases;
4848
private Map<String, Settings> settings;
4949
private Map<String, Settings> defaultSettings;
50+
private Map<String, String> dataStreams;
5051
private String[] indices;
5152

5253
GetIndexResponse(String[] indices,
5354
Map<String, MappingMetadata> mappings,
5455
Map<String, List<AliasMetadata>> aliases,
5556
Map<String, Settings> settings,
56-
Map<String, Settings> defaultSettings) {
57+
Map<String, Settings> defaultSettings,
58+
Map<String, String> dataStreams) {
5759
this.indices = indices;
5860
// to have deterministic order
5961
Arrays.sort(indices);
@@ -69,6 +71,9 @@ public class GetIndexResponse {
6971
if (defaultSettings != null) {
7072
this.defaultSettings = defaultSettings;
7173
}
74+
if (dataStreams != null) {
75+
this.dataStreams = dataStreams;
76+
}
7277
}
7378

7479
public String[] getIndices() {
@@ -99,6 +104,10 @@ public Map<String, Settings> getSettings() {
99104
return settings;
100105
}
101106

107+
public Map<String, String> getDataStreams() {
108+
return dataStreams;
109+
}
110+
102111
/**
103112
* Returns the string value for the specified index and setting. If the includeDefaults flag was not set or set to
104113
* false on the {@link GetIndexRequest}, this method will only return a value where the setting was explicitly set
@@ -142,6 +151,7 @@ private static IndexEntry parseIndexEntry(XContentParser parser) throws IOExcept
142151
MappingMetadata indexMappings = null;
143152
Settings indexSettings = null;
144153
Settings indexDefaultSettings = null;
154+
String dataStream = null;
145155
// We start at START_OBJECT since fromXContent ensures that
146156
while (parser.nextToken() != Token.END_OBJECT) {
147157
ensureExpectedToken(Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation);
@@ -163,11 +173,16 @@ private static IndexEntry parseIndexEntry(XContentParser parser) throws IOExcept
163173
default:
164174
parser.skipChildren();
165175
}
176+
} else if (parser.currentToken() == Token.VALUE_STRING) {
177+
if (parser.currentName().equals("data_stream")) {
178+
dataStream = parser.text();
179+
}
180+
parser.skipChildren();
166181
} else if (parser.currentToken() == Token.START_ARRAY) {
167182
parser.skipChildren();
168183
}
169184
}
170-
return new IndexEntry(indexAliases, indexMappings, indexSettings, indexDefaultSettings);
185+
return new IndexEntry(indexAliases, indexMappings, indexSettings, indexDefaultSettings, dataStream);
171186
}
172187

173188
// This is just an internal container to make stuff easier for returning
@@ -176,11 +191,14 @@ private static class IndexEntry {
176191
MappingMetadata indexMappings;
177192
Settings indexSettings = Settings.EMPTY;
178193
Settings indexDefaultSettings = Settings.EMPTY;
179-
IndexEntry(List<AliasMetadata> indexAliases, MappingMetadata indexMappings, Settings indexSettings, Settings indexDefaultSettings) {
194+
String dataStream;
195+
IndexEntry(List<AliasMetadata> indexAliases, MappingMetadata indexMappings, Settings indexSettings, Settings indexDefaultSettings,
196+
String dataStream) {
180197
if (indexAliases != null) this.indexAliases = indexAliases;
181198
if (indexMappings != null) this.indexMappings = indexMappings;
182199
if (indexSettings != null) this.indexSettings = indexSettings;
183200
if (indexDefaultSettings != null) this.indexDefaultSettings = indexDefaultSettings;
201+
if (dataStream != null) this.dataStream = dataStream;
184202
}
185203
}
186204

@@ -189,6 +207,7 @@ public static GetIndexResponse fromXContent(XContentParser parser) throws IOExce
189207
Map<String, MappingMetadata> mappings = new HashMap<>();
190208
Map<String, Settings> settings = new HashMap<>();
191209
Map<String, Settings> defaultSettings = new HashMap<>();
210+
Map<String, String> dataStreams = new HashMap<>();
192211
List<String> indices = new ArrayList<>();
193212

194213
if (parser.currentToken() == null) {
@@ -211,12 +230,15 @@ public static GetIndexResponse fromXContent(XContentParser parser) throws IOExce
211230
if (indexEntry.indexDefaultSettings.isEmpty() == false) {
212231
defaultSettings.put(indexName, indexEntry.indexDefaultSettings);
213232
}
233+
if (indexEntry.dataStream != null) {
234+
dataStreams.put(indexName, indexEntry.dataStream);
235+
}
214236
} else if (parser.currentToken() == Token.START_ARRAY) {
215237
parser.skipChildren();
216238
} else {
217239
parser.nextToken();
218240
}
219241
}
220-
return new GetIndexResponse(indices.toArray(new String[0]), mappings, aliases, settings, defaultSettings);
242+
return new GetIndexResponse(indices.toArray(new String[0]), mappings, aliases, settings, defaultSettings, dataStreams);
221243
}
222244
}

client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetIndexResponseTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Comparator;
3939
import java.util.HashMap;
4040
import java.util.List;
41+
import java.util.Locale;
4142
import java.util.Map;
4243
import java.util.Objects;
4344

@@ -51,6 +52,7 @@ protected org.elasticsearch.action.admin.indices.get.GetIndexResponse createServ
5152
ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
5253
ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
5354
ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
55+
ImmutableOpenMap.Builder<String, String> dataStreams = ImmutableOpenMap.builder();
5456
IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS;
5557
boolean includeDefaults = randomBoolean();
5658
for (String index: indices) {
@@ -71,9 +73,13 @@ protected org.elasticsearch.action.admin.indices.get.GetIndexResponse createServ
7173
if (includeDefaults) {
7274
defaultSettings.put(index, indexScopedSettings.diff(settings.get(index), Settings.EMPTY));
7375
}
76+
77+
if (randomBoolean()) {
78+
dataStreams.put(index, randomAlphaOfLength(5).toLowerCase(Locale.ROOT));
79+
}
7480
}
7581
return new org.elasticsearch.action.admin.indices.get.GetIndexResponse(indices,
76-
mappings.build(), aliases.build(), settings.build(), defaultSettings.build());
82+
mappings.build(), aliases.build(), settings.build(), defaultSettings.build(), dataStreams.build());
7783
}
7884

7985
@Override
@@ -89,6 +95,7 @@ protected void assertInstances(org.elasticsearch.action.admin.indices.get.GetInd
8995
assertMapEquals(serverTestInstance.getSettings(), clientInstance.getSettings());
9096
assertMapEquals(serverTestInstance.defaultSettings(), clientInstance.getDefaultSettings());
9197
assertMapEquals(serverTestInstance.getAliases(), clientInstance.getAliases());
98+
assertMapEquals(serverTestInstance.getDataStreams(), clientInstance.getDataStreams());
9299
}
93100

94101
private static MappingMetadata createMappingsForIndex() {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
"Get backing indices for data stream":
3+
- skip:
4+
version: " - 7.99.99"
5+
reason: "enable in 7.8+ after backporting"
6+
7+
- do:
8+
indices.create_data_stream:
9+
name: data-stream1
10+
body:
11+
timestamp_field: "@timestamp"
12+
- is_true: acknowledged
13+
14+
- do:
15+
indices.create:
16+
index: test_index
17+
body:
18+
settings:
19+
number_of_shards: 1
20+
number_of_replicas: 1
21+
22+
- do:
23+
indices.get:
24+
index: ['data-stream1-000001', 'test_index']
25+
26+
- is_true: data-stream1-000001.settings
27+
- is_true: data-stream1-000001.data_stream
28+
- match: { data-stream1-000001.data_stream: data-stream1 }
29+
- is_true: test_index.settings
30+
- is_false: test_index.data_stream
31+
32+
- do:
33+
indices.delete_data_stream:
34+
name: data-stream1
35+
- is_true: acknowledged

server/src/main/java/org/elasticsearch/action/admin/indices/get/GetIndexResponse.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ public class GetIndexResponse extends ActionResponse implements ToXContentObject
4949
private ImmutableOpenMap<String, List<AliasMetadata>> aliases = ImmutableOpenMap.of();
5050
private ImmutableOpenMap<String, Settings> settings = ImmutableOpenMap.of();
5151
private ImmutableOpenMap<String, Settings> defaultSettings = ImmutableOpenMap.of();
52+
private ImmutableOpenMap<String, String> dataStreams = ImmutableOpenMap.of();
5253
private String[] indices;
5354

5455
public GetIndexResponse(String[] indices,
5556
ImmutableOpenMap<String, MappingMetadata> mappings,
5657
ImmutableOpenMap<String, List<AliasMetadata>> aliases,
5758
ImmutableOpenMap<String, Settings> settings,
58-
ImmutableOpenMap<String, Settings> defaultSettings) {
59+
ImmutableOpenMap<String, Settings> defaultSettings,
60+
ImmutableOpenMap<String, String> dataStreams) {
5961
this.indices = indices;
6062
// to have deterministic order
6163
Arrays.sort(indices);
@@ -71,6 +73,9 @@ public GetIndexResponse(String[] indices,
7173
if (defaultSettings != null) {
7274
this.defaultSettings = defaultSettings;
7375
}
76+
if (dataStreams != null) {
77+
this.dataStreams = dataStreams;
78+
}
7479
}
7580

7681
GetIndexResponse(StreamInput in) throws IOException {
@@ -126,6 +131,15 @@ public GetIndexResponse(String[] indices,
126131
defaultSettingsMapBuilder.put(in.readString(), Settings.readSettingsFromStream(in));
127132
}
128133
defaultSettings = defaultSettingsMapBuilder.build();
134+
135+
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
136+
ImmutableOpenMap.Builder<String, String> dataStreamsMapBuilder = ImmutableOpenMap.builder();
137+
int dataStreamsSize = in.readVInt();
138+
for (int i = 0; i < dataStreamsSize; i++) {
139+
dataStreamsMapBuilder.put(in.readString(), in.readOptionalString());
140+
}
141+
dataStreams = dataStreamsMapBuilder.build();
142+
}
129143
}
130144

131145
public String[] indices() {
@@ -156,6 +170,14 @@ public ImmutableOpenMap<String, Settings> settings() {
156170
return settings;
157171
}
158172

173+
public ImmutableOpenMap<String, String> dataStreams() {
174+
return dataStreams;
175+
}
176+
177+
public ImmutableOpenMap<String, String> getDataStreams() {
178+
return dataStreams();
179+
}
180+
159181
/**
160182
* If the originating {@link GetIndexRequest} object was configured to include
161183
* defaults, this will contain a mapping of index name to {@link Settings} objects.
@@ -233,6 +255,13 @@ public void writeTo(StreamOutput out) throws IOException {
233255
out.writeString(indexEntry.key);
234256
Settings.writeSettingsToStream(indexEntry.value, out);
235257
}
258+
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
259+
out.writeVInt(dataStreams.size());
260+
for (ObjectObjectCursor<String, String> indexEntry : dataStreams) {
261+
out.writeString(indexEntry.key);
262+
out.writeOptionalString(indexEntry.value);
263+
}
264+
}
236265
}
237266

238267
@Override
@@ -271,6 +300,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
271300
defaultIndexSettings.toXContent(builder, params);
272301
builder.endObject();
273302
}
303+
304+
String dataStream = dataStreams.get(index);
305+
if (dataStream != null) {
306+
builder.field("data_stream", dataStream);
307+
}
274308
}
275309
builder.endObject();
276310
}
@@ -293,7 +327,8 @@ public boolean equals(Object o) {
293327
Objects.equals(aliases, that.aliases) &&
294328
Objects.equals(mappings, that.mappings) &&
295329
Objects.equals(settings, that.settings) &&
296-
Objects.equals(defaultSettings, that.defaultSettings);
330+
Objects.equals(defaultSettings, that.defaultSettings) &&
331+
Objects.equals(dataStreams, that.dataStreams);
297332
}
298333

299334
@Override
@@ -304,7 +339,8 @@ public int hashCode() {
304339
aliases,
305340
mappings,
306341
settings,
307-
defaultSettings
342+
defaultSettings,
343+
dataStreams
308344
);
309345
}
310346
}

server/src/main/java/org/elasticsearch/action/admin/indices/get/TransportGetIndexAction.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
import java.io.IOException;
4646
import java.util.List;
47+
import java.util.stream.Collectors;
48+
import java.util.stream.StreamSupport;
4749

4850
/**
4951
* Get index action.
@@ -90,6 +92,9 @@ protected void doMasterOperation(final GetIndexRequest request, String[] concret
9092
ImmutableOpenMap<String, List<AliasMetadata>> aliasesResult = ImmutableOpenMap.of();
9193
ImmutableOpenMap<String, Settings> settings = ImmutableOpenMap.of();
9294
ImmutableOpenMap<String, Settings> defaultSettings = ImmutableOpenMap.of();
95+
ImmutableOpenMap<String, String> dataStreams = ImmutableOpenMap.<String, String>builder()
96+
.putAll(StreamSupport.stream(state.metadata().findDataStreams(concreteIndices).spliterator(), false)
97+
.collect(Collectors.toMap(k -> k.key, v -> v.value.getName()))).build();
9398
Feature[] features = request.features();
9499
boolean doneAliases = false;
95100
boolean doneMappings = false;
@@ -140,7 +145,7 @@ protected void doMasterOperation(final GetIndexRequest request, String[] concret
140145
}
141146
}
142147
listener.onResponse(
143-
new GetIndexResponse(concreteIndices, mappingsResult, aliasesResult, settings, defaultSettings)
148+
new GetIndexResponse(concreteIndices, mappingsResult, aliasesResult, settings, defaultSettings, dataStreams)
144149
);
145150
}
146151
}

server/src/main/java/org/elasticsearch/cluster/metadata/Metadata.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,24 @@ public ImmutableOpenMap<String, MappingMetadata> findMappings(String[] concreteI
401401
return indexMapBuilder.build();
402402
}
403403

404+
/**
405+
* Finds the parent data streams, if any, for the specified concrete indices.
406+
*/
407+
public ImmutableOpenMap<String, IndexAbstraction.DataStream> findDataStreams(String[] concreteIndices) {
408+
assert concreteIndices != null;
409+
final ImmutableOpenMap.Builder<String, IndexAbstraction.DataStream> builder = ImmutableOpenMap.builder();
410+
final SortedMap<String, IndexAbstraction> lookup = getIndicesLookup();
411+
for (String indexName : concreteIndices) {
412+
IndexAbstraction index = lookup.get(indexName);
413+
assert index != null;
414+
assert index.getType() == IndexAbstraction.Type.CONCRETE_INDEX;
415+
if (index.getParentDataStream() != null) {
416+
builder.put(indexName, index.getParentDataStream());
417+
}
418+
}
419+
return builder.build();
420+
}
421+
404422
@SuppressWarnings("unchecked")
405423
private static MappingMetadata filterFields(MappingMetadata mappingMetadata, Predicate<String> fieldPredicate) {
406424
if (mappingMetadata == null) {

server/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexResponseTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Collections;
3636
import java.util.Comparator;
3737
import java.util.List;
38+
import java.util.Locale;
3839

3940
public class GetIndexResponseTests extends AbstractWireSerializingTestCase<GetIndexResponse> {
4041

@@ -50,6 +51,7 @@ protected GetIndexResponse createTestInstance() {
5051
ImmutableOpenMap.Builder<String, List<AliasMetadata>> aliases = ImmutableOpenMap.builder();
5152
ImmutableOpenMap.Builder<String, Settings> settings = ImmutableOpenMap.builder();
5253
ImmutableOpenMap.Builder<String, Settings> defaultSettings = ImmutableOpenMap.builder();
54+
ImmutableOpenMap.Builder<String, String> dataStreams = ImmutableOpenMap.builder();
5355
IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS;
5456
boolean includeDefaults = randomBoolean();
5557
for (String index: indices) {
@@ -70,9 +72,13 @@ protected GetIndexResponse createTestInstance() {
7072
if (includeDefaults) {
7173
defaultSettings.put(index, indexScopedSettings.diff(settings.get(index), Settings.EMPTY));
7274
}
75+
76+
if (randomBoolean()) {
77+
dataStreams.put(index, randomAlphaOfLength(5).toLowerCase(Locale.ROOT));
78+
}
7379
}
7480
return new GetIndexResponse(
75-
indices, mappings.build(), aliases.build(), settings.build(), defaultSettings.build()
81+
indices, mappings.build(), aliases.build(), settings.build(), defaultSettings.build(), dataStreams.build()
7682
);
7783
}
7884
}

0 commit comments

Comments
 (0)