Skip to content

Commit 60db06e

Browse files
authored
[ILM] refactor ExplainLifecycleRequest to enforce indices (#35753)
Previously, it was possible to initialize a request that does not include any indices. This results in undefined behavior when generating the URL path to send to ES. By enforcing indices to be defined, users will be more explicit about the intention.
1 parent 3bee25c commit 60db06e

File tree

6 files changed

+28
-36
lines changed

6 files changed

+28
-36
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,9 @@ static Request lifecycleManagementStatus(LifecycleManagementStatusRequest lifecy
126126
}
127127

128128
static Request explainLifecycle(ExplainLifecycleRequest explainLifecycleRequest) {
129-
String[] indices = explainLifecycleRequest.indices() == null ? Strings.EMPTY_ARRAY : explainLifecycleRequest.indices();
130129
Request request = new Request(HttpGet.METHOD_NAME,
131130
new RequestConverters.EndpointBuilder()
132-
.addCommaSeparatedPathParts(indices)
131+
.addCommaSeparatedPathParts(explainLifecycleRequest.getIndices())
133132
.addPathPartAsIs("_ilm")
134133
.addPathPartAsIs("explain")
135134
.build());

client/rest-high-level/src/main/java/org/elasticsearch/client/indexlifecycle/ExplainLifecycleRequest.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,27 @@
2222
import org.elasticsearch.action.support.IndicesOptions;
2323
import org.elasticsearch.client.TimedRequest;
2424
import org.elasticsearch.client.ValidationException;
25-
import org.elasticsearch.common.Strings;
2625

2726
import java.util.Arrays;
2827
import java.util.Objects;
2928
import java.util.Optional;
3029

3130
/**
3231
* The request object used by the Explain Lifecycle API.
33-
*
34-
* Multiple indices may be queried in the same request using the
35-
* {@link #indices(String...)} method
3632
*/
3733
public class ExplainLifecycleRequest extends TimedRequest {
3834

39-
private String[] indices = Strings.EMPTY_ARRAY;
35+
private final String[] indices;
4036
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
4137

42-
public ExplainLifecycleRequest() {
43-
super();
44-
}
45-
46-
public ExplainLifecycleRequest indices(String... indices) {
38+
public ExplainLifecycleRequest(String... indices) {
39+
if (indices.length == 0) {
40+
throw new IllegalArgumentException("Must at least specify one index to explain");
41+
}
4742
this.indices = indices;
48-
return this;
4943
}
5044

51-
public String[] indices() {
45+
public String[] getIndices() {
5246
return indices;
5347
}
5448

@@ -60,15 +54,15 @@ public ExplainLifecycleRequest indicesOptions(IndicesOptions indicesOptions) {
6054
public IndicesOptions indicesOptions() {
6155
return indicesOptions;
6256
}
63-
57+
6458
@Override
6559
public Optional<ValidationException> validate() {
6660
return Optional.empty();
6761
}
6862

6963
@Override
7064
public int hashCode() {
71-
return Objects.hash(Arrays.hashCode(indices()), indicesOptions());
65+
return Objects.hash(Arrays.hashCode(indices), indicesOptions);
7266
}
7367

7468
@Override
@@ -80,13 +74,13 @@ public boolean equals(Object obj) {
8074
return false;
8175
}
8276
ExplainLifecycleRequest other = (ExplainLifecycleRequest) obj;
83-
return Objects.deepEquals(indices(), other.indices()) &&
77+
return Objects.deepEquals(getIndices(), other.getIndices()) &&
8478
Objects.equals(indicesOptions(), other.indicesOptions());
8579
}
8680

8781
@Override
8882
public String toString() {
89-
return "ExplainLifecycleRequest [indices()=" + Arrays.toString(indices()) + ", indicesOptions()=" + indicesOptions() + "]";
83+
return "ExplainLifecycleRequest [indices()=" + Arrays.toString(indices) + ", indicesOptions()=" + indicesOptions + "]";
9084
}
9185

9286
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ public void testExplainLifecycle() throws Exception {
182182

183183
createIndex("squash", Settings.EMPTY);
184184

185-
ExplainLifecycleRequest req = new ExplainLifecycleRequest();
186-
req.indices("foo-01", "baz-01", "squash");
185+
ExplainLifecycleRequest req = new ExplainLifecycleRequest("foo-01", "baz-01", "squash");
187186
ExplainLifecycleResponse response = execute(req, highLevelClient().indexLifecycle()::explainLifecycle,
188187
highLevelClient().indexLifecycle()::explainLifecycleAsync);
189188
Map<String, IndexLifecycleExplainResponse> indexResponses = response.getIndexResponses();

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,15 @@ public void testLifecycleManagementStatus() throws Exception {
142142
}
143143

144144
public void testExplainLifecycle() throws Exception {
145-
ExplainLifecycleRequest req = new ExplainLifecycleRequest();
146-
String[] indices = rarely() ? null : randomIndicesNames(0, 10);
147-
req.indices(indices);
145+
ExplainLifecycleRequest req = new ExplainLifecycleRequest(randomIndicesNames(1, 10));
148146
Map<String, String> expectedParams = new HashMap<>();
149147
setRandomMasterTimeout(req, expectedParams);
150148
setRandomIndicesOptions(req::indicesOptions, req::indicesOptions, expectedParams);
151149

152150
Request request = IndexLifecycleRequestConverters.explainLifecycle(req);
153151
assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
154-
String idxString = Strings.arrayToCommaDelimitedString(indices);
155-
assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm/explain"));
152+
String idxString = Strings.arrayToCommaDelimitedString(req.getIndices());
153+
assertThat(request.getEndpoint(), equalTo("/" + idxString + "/" + "_ilm/explain"));
156154
assertThat(request.getParameters(), equalTo(expectedParams));
157155
}
158156

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ILMDocumentationIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ public void testRetryPolicy() throws Exception {
482482
.build());
483483
client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
484484
assertBusy(() -> assertNotNull(client.indexLifecycle()
485-
.explainLifecycle(new ExplainLifecycleRequest().indices("my_index"), RequestOptions.DEFAULT)
485+
.explainLifecycle(new ExplainLifecycleRequest("my_index"), RequestOptions.DEFAULT)
486486
.getIndexResponses().get("my_index").getFailedStep()));
487487
}
488488

client/rest-high-level/src/test/java/org/elasticsearch/client/indexlifecycle/ExplainLifecycleRequestTests.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@
2525

2626
import java.util.Arrays;
2727

28+
import static org.hamcrest.Matchers.equalTo;
29+
2830
public class ExplainLifecycleRequestTests extends ESTestCase {
2931

3032
public void testEqualsAndHashcode() {
3133
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createTestInstance(), this::copy, this::mutateInstance);
3234
}
3335

36+
public void testEmptyIndices() {
37+
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, ExplainLifecycleRequest::new);
38+
assertThat(exception.getMessage(), equalTo("Must at least specify one index to explain"));
39+
}
40+
3441
private ExplainLifecycleRequest createTestInstance() {
35-
ExplainLifecycleRequest request = new ExplainLifecycleRequest();
36-
if (randomBoolean()) {
37-
request.indices(generateRandomStringArray(20, 20, false, true));
38-
}
42+
ExplainLifecycleRequest request = new ExplainLifecycleRequest(generateRandomStringArray(20, 20, false, true));
3943
if (randomBoolean()) {
4044
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(),
4145
randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
@@ -45,11 +49,11 @@ private ExplainLifecycleRequest createTestInstance() {
4549
}
4650

4751
private ExplainLifecycleRequest mutateInstance(ExplainLifecycleRequest instance) {
48-
String[] indices = instance.indices();
52+
String[] indices = instance.getIndices();
4953
IndicesOptions indicesOptions = instance.indicesOptions();
5054
switch (between(0, 1)) {
5155
case 0:
52-
indices = randomValueOtherThanMany(i -> Arrays.equals(i, instance.indices()),
56+
indices = randomValueOtherThanMany(i -> Arrays.equals(i, instance.getIndices()),
5357
() -> generateRandomStringArray(20, 10, false, true));
5458
break;
5559
case 1:
@@ -59,15 +63,13 @@ private ExplainLifecycleRequest mutateInstance(ExplainLifecycleRequest instance)
5963
default:
6064
throw new AssertionError("Illegal randomisation branch");
6165
}
62-
ExplainLifecycleRequest newRequest = new ExplainLifecycleRequest();
63-
newRequest.indices(indices);
66+
ExplainLifecycleRequest newRequest = new ExplainLifecycleRequest(indices);
6467
newRequest.indicesOptions(indicesOptions);
6568
return newRequest;
6669
}
6770

6871
private ExplainLifecycleRequest copy(ExplainLifecycleRequest original) {
69-
ExplainLifecycleRequest copy = new ExplainLifecycleRequest();
70-
copy.indices(original.indices());
72+
ExplainLifecycleRequest copy = new ExplainLifecycleRequest(original.getIndices());
7173
copy.indicesOptions(original.indicesOptions());
7274
return copy;
7375
}

0 commit comments

Comments
 (0)