Skip to content

Commit 5ea782c

Browse files
seanzatzdevelasticsearchmachine
andauthored
small fix - delete sample config action request to only accept one index (elastic#137067)
* fix delete sample config action request to only accept one index * [CI] Auto commit changes from spotless --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent ef86723 commit 5ea782c

File tree

2 files changed

+18
-55
lines changed

2 files changed

+18
-55
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/sampling/DeleteSampleConfigurationAction.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
import org.elasticsearch.action.support.IndicesOptions;
1515
import org.elasticsearch.action.support.master.AcknowledgedRequest;
1616
import org.elasticsearch.action.support.master.AcknowledgedResponse;
17-
import org.elasticsearch.common.Strings;
1817
import org.elasticsearch.common.io.stream.StreamInput;
1918
import org.elasticsearch.common.io.stream.StreamOutput;
2019
import org.elasticsearch.core.Nullable;
2120
import org.elasticsearch.core.TimeValue;
2221

2322
import java.io.IOException;
24-
import java.util.Arrays;
2523
import java.util.Objects;
2624

2725
/**
@@ -73,7 +71,7 @@ private DeleteSampleConfigurationAction() {
7371
*/
7472
public static class Request extends AcknowledgedRequest<Request> implements IndicesRequest.Replaceable {
7573

76-
private String[] indices = Strings.EMPTY_ARRAY;
74+
private String index;
7775

7876
/**
7977
* Constructs a new request with specified timeouts.
@@ -93,7 +91,7 @@ public Request(@Nullable TimeValue masterNodeTimeout, @Nullable TimeValue ackTim
9391
*/
9492
public Request(StreamInput in) throws IOException {
9593
super(in);
96-
this.indices = in.readStringArray();
94+
this.index = in.readString();
9795
}
9896

9997
/**
@@ -109,7 +107,7 @@ public Request(StreamInput in) throws IOException {
109107
@Override
110108
public void writeTo(StreamOutput out) throws IOException {
111109
super.writeTo(out);
112-
out.writeStringArray(indices);
110+
out.writeString(index);
113111
}
114112

115113
/**
@@ -124,23 +122,25 @@ public void writeTo(StreamOutput out) throws IOException {
124122
*/
125123
@Override
126124
public String[] indices() {
127-
return indices;
125+
return new String[] { index };
128126
}
129127

130128
/**
131-
* Sets the array of index names or patterns for this request.
129+
* Sets the array of index name for this request.
132130
* <p>
133-
* Specifies which indices and/or data streams should have their sampling
134-
* configurations deleted. Supports individual index names, comma-separated
135-
* lists, and wildcard patterns.
131+
* Specifies which index and/or data stream should have their sampling
132+
* configurations deleted. Supports individual index names
136133
* </p>
137134
*
138135
* @param indices the index names or patterns to target, null will be converted to empty array
139136
* @return this request instance for method chaining
140137
*/
141138
@Override
142139
public IndicesRequest indices(String... indices) {
143-
this.indices = indices == null ? Strings.EMPTY_ARRAY : indices;
140+
if (indices == null || indices.length != 1) {
141+
throw new IllegalArgumentException("[indices] must contain only one index");
142+
}
143+
this.index = indices[0];
144144
return this;
145145
}
146146

@@ -185,7 +185,7 @@ public boolean equals(Object o) {
185185
if (this == o) return true;
186186
if (o == null || getClass() != o.getClass()) return false;
187187
Request request = (Request) o;
188-
return Arrays.equals(indices, request.indices)
188+
return Objects.equals(index, request.index)
189189
&& Objects.equals(this.masterNodeTimeout(), request.masterNodeTimeout())
190190
&& Objects.equals(this.ackTimeout(), request.ackTimeout());
191191
}
@@ -201,7 +201,7 @@ public boolean equals(Object o) {
201201
*/
202202
@Override
203203
public int hashCode() {
204-
return Objects.hash(Arrays.hashCode(indices), masterNodeTimeout(), ackTimeout());
204+
return Objects.hash(index, masterNodeTimeout(), ackTimeout());
205205
}
206206
}
207207
}

server/src/test/java/org/elasticsearch/action/admin/indices/sampling/TransportDeleteSampleConfigurationActionTests.java

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -325,58 +325,21 @@ public void testActionConfiguration() {
325325
}
326326

327327
/**
328-
* Tests that multiple indices in request causes IndexNotFoundException when resolved.
328+
* Tests that multiple indices in request causes IllegalArgumentException.
329329
* The action should only accept single index operations.
330330
*/
331-
public void testMasterOperationMultipleIndices() throws Exception {
331+
public void testMasterOperationMultipleIndices() {
332332
// Setup test data
333-
ProjectId projectId = randomProjectIdOrDefault();
334333
String indexName1 = randomIdentifier();
335334
String indexName2 = randomIdentifier();
336335
TimeValue masterNodeTimeout = randomTimeValue(100, 200);
337336
TimeValue ackTimeout = randomTimeValue(100, 200);
338337

339338
DeleteSampleConfigurationAction.Request request = new DeleteSampleConfigurationAction.Request(masterNodeTimeout, ackTimeout);
340-
request.indices(indexName1, indexName2);
341-
342-
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).build();
343-
Task task = mock(Task.class);
344-
345-
// Setup mocks - IndexNameExpressionResolver should throw for multiple indices
346-
IndexNotFoundException multipleIndicesException = new IndexNotFoundException("Multiple indices not supported");
347-
when(indexNameExpressionResolver.concreteIndexNames(clusterState, request)).thenThrow(multipleIndicesException);
348-
349-
// Test execution with CountDownLatch to handle async operation
350-
CountDownLatch latch = new CountDownLatch(1);
351-
AtomicReference<AcknowledgedResponse> responseRef = new AtomicReference<>();
352-
AtomicReference<Exception> exceptionRef = new AtomicReference<>();
353339

354-
ActionListener<AcknowledgedResponse> testListener = new ActionListener<AcknowledgedResponse>() {
355-
@Override
356-
public void onResponse(AcknowledgedResponse response) {
357-
responseRef.set(response);
358-
latch.countDown();
359-
}
360-
361-
@Override
362-
public void onFailure(Exception e) {
363-
exceptionRef.set(e);
364-
latch.countDown();
365-
}
366-
};
367-
368-
action.masterOperation(task, request, clusterState, testListener);
369-
370-
// Wait for async operation to complete
371-
assertTrue("Operation should complete within timeout", latch.await(5, TimeUnit.SECONDS));
372-
373-
// Verify results
374-
assertThat(responseRef.get(), nullValue());
375-
assertThat(exceptionRef.get(), not(nullValue()));
376-
assertThat(exceptionRef.get(), sameInstance(multipleIndicesException));
377-
378-
// Verify interactions
379-
verify(indexNameExpressionResolver).concreteIndexNames(clusterState, request);
340+
// Verify that setting multiple indices throws IllegalArgumentException
341+
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> request.indices(indexName1, indexName2));
342+
assertThat(exception.getMessage(), equalTo("[indices] must contain only one index"));
380343
}
381344

382345
/**

0 commit comments

Comments
 (0)