Skip to content

Commit 7cafce9

Browse files
committed
some testing
1 parent fc597e7 commit 7cafce9

File tree

7 files changed

+174
-29
lines changed

7 files changed

+174
-29
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
setup:
2+
- skip:
3+
features: allowed_warnings
4+
5+
---
6+
"Test single data stream":
7+
- requires:
8+
cluster_features: [ "logs_stream" ]
9+
reason: requires setting 'logs_stream' to get or set data stream settings
10+
- do:
11+
allowed_warnings:
12+
- "index template [my-template] has index patterns [my-data-stream-*] matching patterns from existing older templates [global] with patterns (global => [*]); this template [my-template] will take precedence during new index creation"
13+
indices.put_index_template:
14+
name: my-template
15+
body:
16+
index_patterns: [ my-data-stream-* ]
17+
data_stream: { }
18+
template:
19+
settings:
20+
number_of_replicas: 0
21+
lifecycle.name: my-policy
22+
23+
- do:
24+
indices.create_data_stream:
25+
name: my-data-stream-1
26+
27+
- do:
28+
cluster.health:
29+
index: "my-data-stream-1"
30+
wait_for_status: green
31+
32+
- do:
33+
indices.get_data_stream_mappings:
34+
name: my-data-stream-1
35+
- match: { data_streams.0.name: my-data-stream-1 }
36+
- match: { data_streams.0.mappings: {} }
37+
- match: { data_streams.0.effective_mappings: {} }
38+
39+
- do:
40+
indices.get_data_stream:
41+
name: my-data-stream-1
42+
- match: { data_streams.0.name: my-data-stream-1 }
43+
- match: { data_streams.0.mappings: {} }
44+
- match: { data_streams.0.effective_mappings: null }
45+
46+
- do:
47+
indices.put_data_stream_mappings:
48+
name: my-data-stream-1
49+
body:
50+
properties:
51+
name:
52+
type: keyword
53+
fields:
54+
english:
55+
type: text
56+
- match: { data_streams.0.name: my-data-stream-1 }
57+
- match: { data_streams.0.applied_to_data_stream: true }
58+
- match: { data_streams.0.mappings.properties.name.type: "keyword" }
59+
- match: { data_streams.0.effective_mappings.properties.name.type: "keyword" }
60+
61+
- do:
62+
indices.rollover:
63+
alias: "my-data-stream-1"
64+
65+
- do:
66+
cluster.health:
67+
index: "my-data-stream-1"
68+
wait_for_status: green
69+
70+
- do:
71+
indices.get_data_stream_mappings:
72+
name: my-data-stream-1
73+
- match: { data_streams.0.name: my-data-stream-1 }
74+
- match: { data_streams.0.mappings.properties.name.type: "keyword" }
75+
- match: { data_streams.0.effective_mappings.properties.name.type: "keyword" }
76+
77+
- do:
78+
indices.get_data_stream:
79+
name: my-data-stream-1
80+
- match: { data_streams.0.name: my-data-stream-1 }
81+
- match: { data_streams.0.mappings.properties.name.type: "keyword" }
82+
- match: { data_streams.0.effective_mappings: null }
83+
84+
- do:
85+
indices.get_data_stream:
86+
name: my-data-stream-1
87+
- set: { data_streams.0.indices.0.index_name: idx0name }
88+
89+
- do:
90+
indices.get_mapping:
91+
index: my-data-stream-1
92+
- match: { .$idx0name.mappings.properties.name.type: "keyword" }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"indices.put_data_stream_mappings":{
3+
"documentation":{
4+
"url":"https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html",
5+
"description":"Updates a data stream's mappings"
6+
},
7+
"stability":"stable",
8+
"visibility": "feature_flag",
9+
"feature_flag": "logs_stream",
10+
"headers":{
11+
"accept": [ "application/json"]
12+
},
13+
"url":{
14+
"paths":[
15+
{
16+
"path":"/_data_stream/{name}/_mappings",
17+
"methods":[
18+
"PUT"
19+
],
20+
"parts":{
21+
"name":{
22+
"type":"string",
23+
"description":"The name of the data stream or data stream pattern"
24+
}
25+
}
26+
}
27+
]
28+
},
29+
"params":{
30+
"dry_run":{
31+
"type":"boolean",
32+
"description":"Perform a dry run but do not actually change any mappings",
33+
"default":false
34+
},
35+
"timeout":{
36+
"type":"time",
37+
"description":"Specify timeout for acknowledging the cluster state update"
38+
},
39+
"master_timeout":{
40+
"type":"time",
41+
"description":"Specify timeout for connection to master"
42+
}
43+
},
44+
"body":{
45+
"description":"The data stream mappings to be updated",
46+
"required":true
47+
}
48+
}
49+
}

server/src/main/java/org/elasticsearch/action/datastreams/GetDataStreamAction.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -425,20 +425,14 @@ public XContentBuilder toXContent(
425425
builder.startObject(SETTINGS_FIELD.getPreferredName());
426426
dataStream.getSettings().toXContent(builder, params);
427427
builder.endObject();
428-
}
429-
430-
builder.startObject(SETTINGS_FIELD.getPreferredName());
431-
dataStream.getSettings().toXContent(builder, params);
432-
builder.endObject();
433428

434-
Map<String, Object> uncompressedMapping = XContentHelper.convertToMap(
435-
dataStream.getMappings().uncompressed(),
436-
true,
437-
XContentType.JSON
438-
).v2();
439-
if (uncompressedMapping.isEmpty() == false) {
440429
builder.field(MAPPINGS_FIELD.getPreferredName());
441-
builder.map(uncompressedMapping);
430+
Map<String, Object> uncompressedMappings = XContentHelper.convertToMap(
431+
dataStream.getMappings().uncompressed(),
432+
true,
433+
XContentType.JSON
434+
).v2();
435+
builder.map(uncompressedMappings);
442436
}
443437

444438
builder.startObject(DataStream.FAILURE_STORE_FIELD.getPreferredName());

server/src/main/java/org/elasticsearch/action/datastreams/GetDataStreamMappingsAction.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,15 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
123123
builder.startObject();
124124
builder.field("name", dataStreamName);
125125
Map<String, Object> uncompressedMappings = XContentHelper.convertToMap(mappings.uncompressed(), true, XContentType.JSON).v2();
126-
if (uncompressedMappings.isEmpty() == false) {
127-
builder.field("mappings");
128-
builder.map(uncompressedMappings);
129-
}
126+
builder.field("mappings");
127+
builder.map(uncompressedMappings);
130128
Map<String, Object> uncompressedEffectiveMappings = XContentHelper.convertToMap(
131129
effectiveMappings.uncompressed(),
132130
true,
133131
XContentType.JSON
134132
).v2();
135-
if (uncompressedEffectiveMappings.isEmpty() == false) {
136-
builder.field("effective_mappings");
137-
builder.map(uncompressedEffectiveMappings);
138-
}
133+
builder.field("effective_mappings");
134+
builder.map(uncompressedEffectiveMappings);
139135
builder.endObject();
140136
return builder;
141137
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ public final class DataStream implements SimpleDiffable<DataStream>, ToXContentO
9494
public static final String FAILURE_STORE_PREFIX = ".fs-";
9595
public static final DateFormatter DATE_FORMATTER = DateFormatter.forPattern("uuuu.MM.dd");
9696
public static final String TIMESTAMP_FIELD_NAME = "@timestamp";
97+
98+
public static final CompressedXContent EMPTY_MAPPINGS;
99+
static {
100+
try {
101+
EMPTY_MAPPINGS = new CompressedXContent(Map.of());
102+
} catch (IOException e) {
103+
throw new RuntimeException(e);
104+
}
105+
}
106+
97107
// Timeseries indices' leaf readers should be sorted by desc order of their timestamp field, as it allows search time optimizations
98108
public static final Comparator<LeafReader> TIMESERIES_LEAF_READERS_SORTER = Comparator.comparingLong((LeafReader r) -> {
99109
try {
@@ -300,7 +310,7 @@ public static DataStream read(StreamInput in) throws IOException {
300310
if (in.getTransportVersion().onOrAfter(TransportVersions.MAPPINGS_IN_DATA_STREAMS)) {
301311
mappings = CompressedXContent.readCompressedString(in);
302312
} else {
303-
mappings = Mapping.EMPTY.toCompressedXContent();
313+
mappings = EMPTY_MAPPINGS;
304314
}
305315
return new DataStream(
306316
name,
@@ -409,7 +419,12 @@ public Settings getEffectiveSettings(ProjectMetadata projectMetadata) {
409419
}
410420

411421
public CompressedXContent getEffectiveMappings(ProjectMetadata projectMetadata) throws IOException {
412-
return getMatchingIndexTemplate(projectMetadata).mergeMappings(mappings).template().mappings();
422+
CompressedXContent mergedMappings = getMatchingIndexTemplate(projectMetadata).mergeMappings(mappings).template().mappings();
423+
if (mergedMappings == null) {
424+
return EMPTY_MAPPINGS;
425+
} else {
426+
return mergedMappings;
427+
}
413428
}
414429

415430
private ComposableIndexTemplate getMatchingIndexTemplate(ProjectMetadata projectMetadata) {
@@ -1413,7 +1428,7 @@ public void writeTo(StreamOutput out) throws IOException {
14131428
(Long) args[2],
14141429
(Map<String, Object>) args[3],
14151430
args[17] == null ? Settings.EMPTY : (Settings) args[17],
1416-
args[18] == null ? Mapping.EMPTY.toCompressedXContent() : (CompressedXContent) args[18],
1431+
args[18] == null ? EMPTY_MAPPINGS : (CompressedXContent) args[18],
14171432
args[4] != null && (boolean) args[4],
14181433
args[5] != null && (boolean) args[5],
14191434
args[6] != null && (boolean) args[6],
@@ -1919,7 +1934,7 @@ public static class Builder {
19191934
@Nullable
19201935
private Map<String, Object> metadata = null;
19211936
private Settings settings = Settings.EMPTY;
1922-
private CompressedXContent mappings = Mapping.EMPTY.toCompressedXContent();
1937+
private CompressedXContent mappings = EMPTY_MAPPINGS;
19231938
private boolean hidden = false;
19241939
private boolean replicated = false;
19251940
private boolean system = false;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.elasticsearch.index.Index;
3333
import org.elasticsearch.index.IndexMode;
3434
import org.elasticsearch.index.mapper.DataStreamTimestampFieldMapper;
35-
import org.elasticsearch.index.mapper.Mapping;
3635
import org.elasticsearch.index.mapper.MappingLookup;
3736
import org.elasticsearch.index.mapper.MetadataFieldMapper;
3837
import org.elasticsearch.indices.SystemDataStreamDescriptor;
@@ -333,7 +332,7 @@ static ClusterState createDataStream(
333332
initialGeneration,
334333
template.metadata() != null ? Map.copyOf(template.metadata()) : null,
335334
Settings.EMPTY,
336-
Mapping.EMPTY.toCompressedXContent(),
335+
DataStream.EMPTY_MAPPINGS,
337336
hidden,
338337
false,
339338
isSystem,

server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,7 +2562,7 @@ public void testGetEffectiveIndexTemplateNoMatchingTemplate() {
25622562
assertThrows(IllegalArgumentException.class, () -> dataStream.getEffectiveIndexTemplate(projectMetadataBuilder.build()));
25632563
}
25642564

2565-
public void testGetEffectiveIndexTemplateTemplateSettingsOnly() {
2565+
public void testGetEffectiveIndexTemplateTemplateSettingsOnly() throws IOException {
25662566
// We only have settings from the template, so the effective template will just be the original template
25672567
DataStream dataStream = createDataStream(Settings.EMPTY);
25682568
Settings templateSettings = randomSettings();
@@ -2577,7 +2577,7 @@ public void testGetEffectiveIndexTemplateTemplateSettingsOnly() {
25772577
assertThat(dataStream.getEffectiveIndexTemplate(projectMetadataBuilder.build()), equalTo(indexTemplate));
25782578
}
25792579

2580-
public void testGetEffectiveIndexTemplateDataStreamSettingsOnly() {
2580+
public void testGetEffectiveIndexTemplateDataStreamSettingsOnly() throws IOException {
25812581
// We only have settings from the data stream, so we expect to get only those back in the effective template
25822582
Settings dataStreamSettings = randomSettings();
25832583
DataStream dataStream = createDataStream(dataStreamSettings);
@@ -2600,7 +2600,7 @@ public void testGetEffectiveIndexTemplateDataStreamSettingsOnly() {
26002600
assertThat(dataStream.getEffectiveIndexTemplate(projectMetadataBuilder.build()), equalTo(expectedEffectiveTemplate));
26012601
}
26022602

2603-
public void testGetEffectiveIndexTemplate() {
2603+
public void testGetEffectiveIndexTemplate() throws IOException {
26042604
// Here we have settings from both the template and the data stream, so we expect the data stream settings to take precedence
26052605
Settings dataStreamSettings = Settings.builder()
26062606
.put("index.setting1", "dataStreamValue")

0 commit comments

Comments
 (0)