Skip to content

Commit b0a74a4

Browse files
authored
Create fewer objects when an ILM policy is not used (elastic#140705)
This PR is trying to reduce the object creation by the [`GET _ilm/policy`](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-ilm-get-lifecycle) when the policy is not used. To achieve that we perform the following 2 changes: - We introduce a constant empty instance which is returned when the policy is not used - We use null instead of empty lists to reduce so the response will not have unnecessary fields. These fields are not documented yet, and from the code it looks like they are optional, another PR will come to document them. Instead of: ``` "in_use_by": { "data_streams": [], "indices": [], "composable_templates": [] } ``` It will only return the fields in use: ``` "in_use_by": {} # or "in_use_by": { "composable_templates": ["some_template"] } ```
1 parent b410ba7 commit b0a74a4

File tree

4 files changed

+36
-38
lines changed

4 files changed

+36
-38
lines changed

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package org.elasticsearch.cluster.metadata;
1111

1212
import org.elasticsearch.common.Strings;
13-
import org.elasticsearch.common.io.stream.StreamInput;
1413
import org.elasticsearch.common.io.stream.StreamOutput;
1514
import org.elasticsearch.common.io.stream.Writeable;
1615
import org.elasticsearch.core.Nullable;
@@ -28,6 +27,8 @@
2827
*/
2928
public class ItemUsage implements Writeable, ToXContentObject {
3029

30+
public static final ItemUsage EMPTY = new ItemUsage(null, null, null);
31+
3132
private final Set<String> indices;
3233
private final Set<String> dataStreams;
3334
private final Set<String> composableTemplates;
@@ -46,24 +47,6 @@ public ItemUsage(
4647
this.composableTemplates = composableTemplates == null ? null : new HashSet<>(composableTemplates);
4748
}
4849

49-
public ItemUsage(StreamInput in) throws IOException {
50-
if (in.readBoolean()) {
51-
this.indices = in.readCollectionAsSet(StreamInput::readString);
52-
} else {
53-
this.indices = null;
54-
}
55-
if (in.readBoolean()) {
56-
this.dataStreams = in.readCollectionAsSet(StreamInput::readString);
57-
} else {
58-
this.dataStreams = null;
59-
}
60-
if (in.readBoolean()) {
61-
this.composableTemplates = in.readCollectionAsSet(StreamInput::readString);
62-
} else {
63-
this.composableTemplates = null;
64-
}
65-
}
66-
6750
public Set<String> getIndices() {
6851
return indices;
6952
}
@@ -79,15 +62,9 @@ public Set<String> getComposableTemplates() {
7962
@Override
8063
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
8164
builder.startObject();
82-
if (this.indices != null) {
83-
builder.stringListField("indices", this.indices);
84-
}
85-
if (this.dataStreams != null) {
86-
builder.stringListField("data_streams", this.dataStreams);
87-
}
88-
if (this.composableTemplates != null) {
89-
builder.stringListField("composable_templates", this.composableTemplates);
90-
}
65+
builder.stringListField("indices", this.indices != null ? this.indices : Set.of());
66+
builder.stringListField("data_streams", this.dataStreams != null ? this.dataStreams : Set.of());
67+
builder.stringListField("composable_templates", this.composableTemplates != null ? this.composableTemplates : Set.of());
9168
builder.endObject();
9269
return builder;
9370
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
package org.elasticsearch.cluster.metadata;
1111

1212
import org.elasticsearch.TransportVersion;
13+
import org.elasticsearch.common.Strings;
1314
import org.elasticsearch.core.Nullable;
1415
import org.elasticsearch.test.AbstractWireTestCase;
16+
import org.elasticsearch.xcontent.ToXContent;
17+
import org.elasticsearch.xcontent.XContentBuilder;
18+
import org.elasticsearch.xcontent.XContentType;
1519

1620
import java.io.IOException;
1721
import java.util.List;
1822

23+
import static org.hamcrest.Matchers.containsString;
24+
1925
public class ItemUsageTests extends AbstractWireTestCase<ItemUsage> {
2026

2127
public static ItemUsage randomUsage() {
@@ -45,4 +51,16 @@ protected ItemUsage copyInstance(ItemUsage instance, TransportVersion version) t
4551
protected ItemUsage mutateInstance(ItemUsage instance) {
4652
return null;// TODO implement https://github.com/elastic/elasticsearch/issues/25929
4753
}
54+
55+
// We test this in a unit test and not a yml test because it is not required by API,
56+
// but it has been existing behavior that we chose to preserve.
57+
public void testEmptyInstanceXContent() throws IOException {
58+
try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
59+
builder.humanReadable(true);
60+
String json = Strings.toString(ItemUsage.EMPTY.toXContent(builder, ToXContent.EMPTY_PARAMS));
61+
assertThat(json, containsString("\"indices\":[]"));
62+
assertThat(json, containsString("\"data_streams\":[]"));
63+
assertThat(json, containsString("\"composable_templates\":[]"));
64+
}
65+
}
4866
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUsageCalculator.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ public LifecyclePolicyUsageCalculator(
107107
* Retrieves the pre-calculated indices, data streams, and composable templates that use the given policy.
108108
*/
109109
public ItemUsage retrieveCalculatedUsage(String policyName) {
110-
return new ItemUsage(
111-
policyToIndices.getOrDefault(policyName, List.of()),
112-
policyToDataStreams.getOrDefault(policyName, List.of()),
113-
policyToTemplates.getOrDefault(policyName, List.of())
114-
);
110+
List<String> indices = policyToIndices.get(policyName);
111+
List<String> dataStreams = policyToDataStreams.get(policyName);
112+
List<String> composableTemplates = policyToTemplates.get(policyName);
113+
if (indices == null && dataStreams == null && composableTemplates == null) {
114+
return ItemUsage.EMPTY;
115+
}
116+
return new ItemUsage(indices, dataStreams, composableTemplates);
115117
}
116118

117119
private boolean doesPolicyMatchAnyName(String policyName, List<String> names) {

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUsageCalculatorTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Map;
2828

2929
import static org.hamcrest.Matchers.equalTo;
30+
import static org.hamcrest.Matchers.sameInstance;
3031

3132
public class LifecyclePolicyUsageCalculatorTests extends ESTestCase {
3233

@@ -46,7 +47,7 @@ public void testGetUsageNonExistentPolicy() {
4647
final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).build();
4748
assertThat(
4849
new LifecyclePolicyUsageCalculator(iner, project, List.of("mypolicy")).retrieveCalculatedUsage("mypolicy"),
49-
equalTo(new ItemUsage(List.of(), List.of(), List.of()))
50+
sameInstance(ItemUsage.EMPTY)
5051
);
5152
}
5253

@@ -64,7 +65,7 @@ public void testGetUsageUnusedPolicy() {
6465

6566
assertThat(
6667
new LifecyclePolicyUsageCalculator(iner, project, List.of("mypolicy")).retrieveCalculatedUsage("mypolicy"),
67-
equalTo(new ItemUsage(List.of(), List.of(), List.of()))
68+
sameInstance(ItemUsage.EMPTY)
6869
);
6970
}
7071

@@ -86,7 +87,7 @@ public void testGetUsagePolicyUsedByIndex() {
8687

8788
assertThat(
8889
new LifecyclePolicyUsageCalculator(iner, project, List.of("mypolicy")).retrieveCalculatedUsage("mypolicy"),
89-
equalTo(new ItemUsage(List.of("myindex"), List.of(), List.of()))
90+
equalTo(new ItemUsage(List.of("myindex"), null, null))
9091
);
9192
}
9293

@@ -123,7 +124,7 @@ public void testGetUsagePolicyUsedByIndexAndTemplate() {
123124

124125
assertThat(
125126
new LifecyclePolicyUsageCalculator(iner, project, List.of("mypolicy")).retrieveCalculatedUsage("mypolicy"),
126-
equalTo(new ItemUsage(List.of("myindex"), List.of(), List.of("mytemplate")))
127+
equalTo(new ItemUsage(List.of("myindex"), null, List.of("mytemplate")))
127128
);
128129
}
129130

@@ -215,7 +216,7 @@ public void testGetUsagePolicyNotUsedByDataStreamDueToOverride() {
215216
// Test where policy exists and is used by an index, datastream, and template
216217
assertThat(
217218
new LifecyclePolicyUsageCalculator(iner, project, List.of("mypolicy")).retrieveCalculatedUsage("mypolicy"),
218-
equalTo(new ItemUsage(List.of("myindex"), List.of(), List.of("mytemplate")))
219+
equalTo(new ItemUsage(List.of("myindex"), null, List.of("mytemplate")))
219220
);
220221
}
221222
}

0 commit comments

Comments
 (0)