Skip to content

Commit 0b58a53

Browse files
authored
Adding the ability to unset data stream settings (elastic#129677)
1 parent 870d581 commit 0b58a53

File tree

3 files changed

+114
-5
lines changed

3 files changed

+114
-5
lines changed

modules/data-streams/src/main/java/org/elasticsearch/datastreams/action/TransportUpdateDataStreamSettingsAction.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected void masterOperation(
131131
new UpdateDataStreamSettingsAction.DataStreamSettingsResponse(
132132
dataStreamName,
133133
false,
134-
e.getMessage(),
134+
Strings.hasText(e.getMessage()) ? e.getMessage() : e.toString(),
135135
EMPTY,
136136
EMPTY,
137137
UpdateDataStreamSettingsAction.DataStreamSettingsResponse.IndicesSettingsResult.EMPTY
@@ -222,9 +222,12 @@ private void updateSettingsOnIndices(
222222
Map<String, Object> settingsToApply = new HashMap<>();
223223
List<String> appliedToDataStreamOnly = new ArrayList<>();
224224
List<String> appliedToDataStreamAndBackingIndices = new ArrayList<>();
225+
Settings effectiveSettings = dataStream.getEffectiveSettings(
226+
clusterService.state().projectState(projectResolver.getProjectId()).metadata()
227+
);
225228
for (String settingName : requestSettings.keySet()) {
226229
if (APPLY_TO_BACKING_INDICES.contains(settingName)) {
227-
settingsToApply.put(settingName, requestSettings.get(settingName));
230+
settingsToApply.put(settingName, effectiveSettings.get(settingName));
228231
appliedToDataStreamAndBackingIndices.add(settingName);
229232
} else if (APPLY_TO_DATA_STREAM_ONLY.contains(settingName)) {
230233
appliedToDataStreamOnly.add(settingName);
@@ -242,9 +245,7 @@ private void updateSettingsOnIndices(
242245
true,
243246
null,
244247
settingsFilter.filter(dataStream.getSettings()),
245-
settingsFilter.filter(
246-
dataStream.getEffectiveSettings(clusterService.state().projectState(projectResolver.getProjectId()).metadata())
247-
),
248+
settingsFilter.filter(effectiveSettings),
248249
new UpdateDataStreamSettingsAction.DataStreamSettingsResponse.IndicesSettingsResult(
249250
appliedToDataStreamOnly,
250251
appliedToDataStreamAndBackingIndices,

modules/data-streams/src/yamlRestTest/resources/rest-api-spec/test/data_stream/240_data_stream_settings.yml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,102 @@ setup:
299299
- match: { .$idx0name.settings.index.lifecycle.name: "my-policy" }
300300
- match: { .$idx1name.settings.index.number_of_shards: "1" }
301301
- match: { .$idx1name.settings.index.lifecycle.name: "my-policy" }
302+
303+
---
304+
"Test null out settings":
305+
- requires:
306+
cluster_features: [ "logs_stream" ]
307+
reason: requires setting 'logs_stream' to get or set data stream settings
308+
- do:
309+
allowed_warnings:
310+
- "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"
311+
indices.put_index_template:
312+
name: my-template
313+
body:
314+
index_patterns: [ my-data-stream-* ]
315+
data_stream: { }
316+
template:
317+
settings:
318+
number_of_replicas: 0
319+
lifecycle.name: my-policy
320+
321+
- do:
322+
indices.create_data_stream:
323+
name: my-data-stream-1
324+
325+
- do:
326+
cluster.health:
327+
index: "my-data-stream-1"
328+
wait_for_status: green
329+
330+
331+
332+
- do:
333+
indices.get_data_stream:
334+
name: my-data-stream-1
335+
- match: { data_streams.0.name: my-data-stream-1 }
336+
- match: { data_streams.0.settings: {} }
337+
- match: { data_streams.0.effective_settings: null }
338+
339+
- do:
340+
indices.put_data_stream_settings:
341+
name: my-data-stream-1
342+
body:
343+
index:
344+
number_of_shards: 2
345+
lifecycle:
346+
name: my-new-policy
347+
prefer_ilm: true
348+
- match: { data_streams.0.name: my-data-stream-1 }
349+
- match: { data_streams.0.applied_to_data_stream: true }
350+
- match: { data_streams.0.index_settings_results.applied_to_data_stream_only: [index.number_of_shards]}
351+
- length: { data_streams.0.index_settings_results.applied_to_data_stream_and_backing_indices: 2 }
352+
- match: { data_streams.0.settings.index.number_of_shards: "2" }
353+
- match: { data_streams.0.settings.index.lifecycle.name: "my-new-policy" }
354+
- match: { data_streams.0.settings.index.lifecycle.prefer_ilm: "true" }
355+
- match: { data_streams.0.effective_settings.index.number_of_shards: "2" }
356+
- match: { data_streams.0.effective_settings.index.number_of_replicas: "0" }
357+
- match: { data_streams.0.effective_settings.index.lifecycle.name: "my-new-policy" }
358+
- match: { data_streams.0.effective_settings.index.lifecycle.prefer_ilm: "true" }
359+
360+
- do:
361+
indices.put_data_stream_settings:
362+
name: my-data-stream-1
363+
body:
364+
index:
365+
number_of_shards: null
366+
lifecycle:
367+
name: null
368+
- match: { data_streams.0.name: my-data-stream-1 }
369+
- match: { data_streams.0.applied_to_data_stream: true }
370+
- match: { data_streams.0.index_settings_results.applied_to_data_stream_only: [index.number_of_shards]}
371+
- length: { data_streams.0.index_settings_results.applied_to_data_stream_and_backing_indices: 1 }
372+
- match: { data_streams.0.settings.index.number_of_shards: null }
373+
- match: { data_streams.0.settings.index.lifecycle.name: null }
374+
- match: { data_streams.0.settings.index.lifecycle.prefer_ilm: "true" }
375+
- match: { data_streams.0.effective_settings.index.number_of_shards: null }
376+
- match: { data_streams.0.effective_settings.index.number_of_replicas: "0" }
377+
- match: { data_streams.0.effective_settings.index.lifecycle.name: "my-policy" }
378+
- match: { data_streams.0.effective_settings.index.lifecycle.prefer_ilm: "true" }
379+
380+
- do:
381+
indices.get_data_stream_settings:
382+
name: my-data-stream-1
383+
- match: { data_streams.0.name: my-data-stream-1 }
384+
- match: { data_streams.0.settings.index.lifecycle.name: null }
385+
- match: { data_streams.0.settings.index.lifecycle.prefer_ilm: "true" }
386+
- match: { data_streams.0.effective_settings.index.number_of_shards: null }
387+
- match: { data_streams.0.effective_settings.index.number_of_replicas: "0" }
388+
- match: { data_streams.0.effective_settings.index.lifecycle.name: "my-policy" }
389+
390+
- do:
391+
indices.get_data_stream:
392+
name: my-data-stream-1
393+
- set: { data_streams.0.indices.0.index_name: idx0name }
394+
395+
- do:
396+
indices.get_settings:
397+
index: my-data-stream-1
398+
- match: { .$idx0name.settings.index.number_of_shards: "1" }
399+
- match: { .$idx0name.settings.index.lifecycle.name: "my-policy" }
400+
- match: { .$idx0name.settings.index.lifecycle.prefer_ilm: "true" }

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,15 @@ private DataStream createDataStreamForUpdatedDataStreamSettings(
456456

457457
Template.Builder templateBuilder = Template.builder();
458458
Settings.Builder mergedSettingsBuilder = Settings.builder().put(existingSettings).put(settingsOverrides);
459+
/*
460+
* A null value for a setting override means that we remove it from the data stream, and let the value from the template (if any)
461+
* be used.
462+
*/
463+
settingsOverrides.keySet().forEach(key -> {
464+
if (mergedSettingsBuilder.get(key) == null) {
465+
mergedSettingsBuilder.remove(key);
466+
}
467+
});
459468
Settings mergedSettings = mergedSettingsBuilder.build();
460469

461470
final ComposableIndexTemplate template = lookupTemplateForDataStream(dataStreamName, projectMetadata);

0 commit comments

Comments
 (0)