Skip to content

Commit d9ff42f

Browse files
committed
Internal: expose the indices names every action relates to if applicable
Added two new interfaces: 1) IndicesRequest that allows to retrieve the indices the request relates to in a generic manner, together with the indices options that tell how they are going to get resolved and expanded 2) CompositeIndicesRequest for compound requests that hold multiple indices request like MultiSearchRequest, MultiGetRequest, MultiTermVectorsRequest, BulkRequest, BenchmarkRequest, PercolateRequest, MultiPercolateRequest and MoreLikeThisRequest Taken the chance to streamline the indices options and add them to every request where it makes sense (although they can't be changed from the outside), rather than leaving them implicit in the related TransportAction when indices get expanded (tipycally MetaData#concreteIndices or MetaData#concreteSingleIndex). Added IndicesOptions parameter to MetaData#concreteSingleIndex to make sure it is taken from the request, where the information belongs, instead of hardcoded within MetaData. The concreteSingleIndex method remains but it's just a utility method that returns a single index instead of an array and complains otherwise. Also made sure NPE is never thrown when setting indices(null) to IndicesAliasesRequest, similar to what SearchRequest does. Closes #6933
1 parent 6f31b11 commit d9ff42f

File tree

64 files changed

+577
-105
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+577
-105
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.action;
21+
22+
import java.util.List;
23+
24+
/**
25+
* Needs to be implemented by all {@link org.elasticsearch.action.ActionRequest} subclasses that are composed of
26+
* multiple subrequests which relate to one or more indices. Allows to retrieve those subrequests.
27+
*/
28+
public interface CompositeIndicesRequest {
29+
30+
/**
31+
* Returns the subrequests that a composite request is composed of
32+
*/
33+
List<? extends IndicesRequest> subRequests();
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.action;
21+
22+
import org.elasticsearch.action.support.IndicesOptions;
23+
24+
/**
25+
* Needs to be implemented by all {@link org.elasticsearch.action.ActionRequest} subclasses that relate to
26+
* one or more indices. Allows to retrieve which indices the action relates to.
27+
*/
28+
public interface IndicesRequest {
29+
30+
/**
31+
* Returns the array of indices that the action relates to
32+
*/
33+
String[] indices();
34+
35+
/**
36+
* Returns the indices options used to resolve indices. They tell for instance whether a single index is
37+
* accepted, whether an empty array will be converted to _all, and how wildcards will be expanded if needed.
38+
*/
39+
IndicesOptions indicesOptions();
40+
}

src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthRequest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package org.elasticsearch.action.admin.cluster.health;
2121

2222
import org.elasticsearch.action.ActionRequestValidationException;
23+
import org.elasticsearch.action.IndicesRequest;
24+
import org.elasticsearch.action.support.IndicesOptions;
2325
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
2426
import org.elasticsearch.common.Priority;
2527
import org.elasticsearch.common.Strings;
@@ -35,7 +37,7 @@
3537
/**
3638
*
3739
*/
38-
public class ClusterHealthRequest extends MasterNodeReadOperationRequest<ClusterHealthRequest> {
40+
public class ClusterHealthRequest extends MasterNodeReadOperationRequest<ClusterHealthRequest> implements IndicesRequest {
3941

4042
private String[] indices;
4143
private TimeValue timeout = new TimeValue(30, TimeUnit.SECONDS);
@@ -52,6 +54,7 @@ public ClusterHealthRequest(String... indices) {
5254
this.indices = indices;
5355
}
5456

57+
@Override
5558
public String[] indices() {
5659
return indices;
5760
}
@@ -61,6 +64,11 @@ public ClusterHealthRequest indices(String[] indices) {
6164
return this;
6265
}
6366

67+
@Override
68+
public IndicesOptions indicesOptions() {
69+
return IndicesOptions.lenientExpandOpen();
70+
}
71+
6472
public TimeValue timeout() {
6573
return timeout;
6674
}

src/main/java/org/elasticsearch/action/admin/cluster/health/TransportClusterHealthAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private ClusterHealthResponse clusterHealth(ClusterHealthRequest request, Cluste
217217
}
218218
String[] concreteIndices;
219219
try {
220-
concreteIndices = clusterState.metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), request.indices());
220+
concreteIndices = clusterState.metaData().concreteIndices(request.indicesOptions(), request.indices());
221221
} catch (IndexMissingException e) {
222222
// one of the specified indices is not there - treat it as RED.
223223
ClusterHealthResponse response = new ClusterHealthResponse(clusterName.value(), Strings.EMPTY_ARRAY, clusterState);

src/main/java/org/elasticsearch/action/admin/cluster/shards/ClusterSearchShardsRequest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.ElasticsearchIllegalArgumentException;
2323
import org.elasticsearch.action.ActionRequestValidationException;
24+
import org.elasticsearch.action.IndicesRequest;
2425
import org.elasticsearch.action.support.IndicesOptions;
2526
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
2627
import org.elasticsearch.common.Nullable;
@@ -32,7 +33,7 @@
3233

3334
/**
3435
*/
35-
public class ClusterSearchShardsRequest extends MasterNodeReadOperationRequest<ClusterSearchShardsRequest> {
36+
public class ClusterSearchShardsRequest extends MasterNodeReadOperationRequest<ClusterSearchShardsRequest> implements IndicesRequest {
3637
private String[] indices;
3738
@Nullable
3839
private String routing;
@@ -74,10 +75,12 @@ public ClusterSearchShardsRequest indices(String... indices) {
7475
/**
7576
* The indices
7677
*/
78+
@Override
7779
public String[] indices() {
7880
return indices;
7981
}
8082

83+
@Override
8184
public IndicesOptions indicesOptions() {
8285
return indicesOptions;
8386
}

src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.ElasticsearchGenerationException;
2323
import org.elasticsearch.ElasticsearchIllegalArgumentException;
2424
import org.elasticsearch.action.ActionRequestValidationException;
25+
import org.elasticsearch.action.IndicesRequest;
2526
import org.elasticsearch.action.support.IndicesOptions;
2627
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
2728
import org.elasticsearch.common.Strings;
@@ -42,9 +43,9 @@
4243
import static org.elasticsearch.action.ValidateActions.addValidationError;
4344
import static org.elasticsearch.common.Strings.EMPTY_ARRAY;
4445
import static org.elasticsearch.common.Strings.hasLength;
46+
import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS;
4547
import static org.elasticsearch.common.settings.ImmutableSettings.readSettingsFromStream;
4648
import static org.elasticsearch.common.settings.ImmutableSettings.writeSettingsToStream;
47-
import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS;
4849
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
4950

5051
/**
@@ -61,7 +62,7 @@
6162
* <li>must not contain invalid file name characters {@link org.elasticsearch.common.Strings#INVALID_FILENAME_CHARS} </li>
6263
* </ul>
6364
*/
64-
public class CreateSnapshotRequest extends MasterNodeOperationRequest<CreateSnapshotRequest> {
65+
public class CreateSnapshotRequest extends MasterNodeOperationRequest<CreateSnapshotRequest> implements IndicesRequest {
6566

6667
private String snapshot;
6768

@@ -195,6 +196,7 @@ public CreateSnapshotRequest indices(List<String> indices) {
195196
*
196197
* @return list of indices
197198
*/
199+
@Override
198200
public String[] indices() {
199201
return indices;
200202
}
@@ -204,6 +206,7 @@ public String[] indices() {
204206
*
205207
* @return the desired behaviour regarding indices options
206208
*/
209+
@Override
207210
public IndicesOptions indicesOptions() {
208211
return indicesOptions;
209212
}

src/main/java/org/elasticsearch/action/admin/cluster/state/ClusterStateRequest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.elasticsearch.Version;
2323
import org.elasticsearch.action.ActionRequestValidationException;
24+
import org.elasticsearch.action.IndicesRequest;
25+
import org.elasticsearch.action.support.IndicesOptions;
2426
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
2527
import org.elasticsearch.common.Strings;
2628
import org.elasticsearch.common.io.stream.StreamInput;
@@ -31,7 +33,7 @@
3133
/**
3234
*
3335
*/
34-
public class ClusterStateRequest extends MasterNodeReadOperationRequest<ClusterStateRequest> {
36+
public class ClusterStateRequest extends MasterNodeReadOperationRequest<ClusterStateRequest> implements IndicesRequest {
3537

3638
private boolean routingTable = true;
3739
private boolean nodes = true;
@@ -101,6 +103,7 @@ public ClusterStateRequest blocks(boolean blocks) {
101103
return this;
102104
}
103105

106+
@Override
104107
public String[] indices() {
105108
return indices;
106109
}
@@ -110,6 +113,11 @@ public ClusterStateRequest indices(String... indices) {
110113
return this;
111114
}
112115

116+
@Override
117+
public IndicesOptions indicesOptions() {
118+
return IndicesOptions.lenientExpandOpen();
119+
}
120+
113121
@Override
114122
public void readFrom(StreamInput in) throws IOException {
115123
super.readFrom(in);

src/main/java/org/elasticsearch/action/admin/cluster/state/TransportClusterStateAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.ElasticsearchException;
2323
import org.elasticsearch.action.ActionListener;
2424
import org.elasticsearch.action.support.ActionFilters;
25-
import org.elasticsearch.action.support.IndicesOptions;
2625
import org.elasticsearch.action.support.master.TransportMasterNodeReadOperationAction;
2726
import org.elasticsearch.cluster.ClusterName;
2827
import org.elasticsearch.cluster.ClusterService;
@@ -99,7 +98,7 @@ protected void masterOperation(final ClusterStateRequest request, final ClusterS
9998
}
10099

101100
if (request.indices().length > 0) {
102-
String[] indices = currentState.metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), request.indices());
101+
String[] indices = currentState.metaData().concreteIndices(request.indicesOptions(), request.indices());
103102
for (String filteredIndex : indices) {
104103
IndexMetaData indexMetaData = currentState.metaData().index(filteredIndex);
105104
if (indexMetaData != null) {

src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import com.carrotsearch.hppc.cursors.ObjectCursor;
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.collect.Lists;
25+
import org.elasticsearch.ElasticsearchIllegalArgumentException;
2526
import org.elasticsearch.action.ActionRequestValidationException;
27+
import org.elasticsearch.action.IndicesRequest;
2628
import org.elasticsearch.action.support.IndicesOptions;
2729
import org.elasticsearch.action.support.master.AcknowledgedRequest;
2830
import org.elasticsearch.cluster.metadata.AliasAction;
@@ -37,18 +39,15 @@
3739
import org.elasticsearch.index.query.FilterBuilder;
3840

3941
import java.io.IOException;
40-
import java.util.ArrayList;
41-
import java.util.List;
42-
import java.util.Locale;
43-
import java.util.Map;
42+
import java.util.*;
4443

4544
import static org.elasticsearch.action.ValidateActions.addValidationError;
4645
import static org.elasticsearch.cluster.metadata.AliasAction.readAliasAction;
4746

4847
/**
4948
* A request to add/remove aliases for one or more indices.
5049
*/
51-
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> {
50+
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements IndicesRequest {
5251

5352
private List<AliasActions> allAliasActions = Lists.newArrayList();
5453

@@ -135,6 +134,9 @@ public AliasActions filter(String filter) {
135134
}
136135

137136
public void indices(String... indices) {
137+
if (indices == null) {
138+
throw new ElasticsearchIllegalArgumentException("indices must not be null");
139+
}
138140
List<String> finalIndices = new ArrayList<>();
139141
for (String index : indices) {
140142
if (index != null) {
@@ -309,6 +311,17 @@ public ActionRequestValidationException validate() {
309311
return validationException;
310312
}
311313

314+
@Override
315+
public String[] indices() {
316+
List<String> indices = Lists.newArrayList();
317+
for (AliasActions aliasActions : aliasActions()) {
318+
if (!CollectionUtils.isEmpty(aliasActions.indices())) {
319+
Collections.addAll(indices, aliasActions.indices);
320+
}
321+
}
322+
return indices.toArray(new String[indices.size()]);
323+
}
324+
312325
@Override
313326
public void readFrom(StreamInput in) throws IOException {
314327
super.readFrom(in);
@@ -329,6 +342,7 @@ public void writeTo(StreamOutput out) throws IOException {
329342
writeTimeout(out);
330343
}
331344

345+
@Override
332346
public IndicesOptions indicesOptions() {
333347
return indicesOptions;
334348
}

src/main/java/org/elasticsearch/action/admin/indices/alias/get/GetAliasesRequest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.elasticsearch.Version;
2222
import org.elasticsearch.action.ActionRequestValidationException;
23+
import org.elasticsearch.action.IndicesRequest;
2324
import org.elasticsearch.action.support.IndicesOptions;
2425
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
2526
import org.elasticsearch.common.Strings;
@@ -30,7 +31,7 @@
3031

3132
/**
3233
*/
33-
public class GetAliasesRequest extends MasterNodeReadOperationRequest<GetAliasesRequest> {
34+
public class GetAliasesRequest extends MasterNodeReadOperationRequest<GetAliasesRequest> implements IndicesRequest {
3435

3536
private String[] indices = Strings.EMPTY_ARRAY;
3637
private String[] aliases = Strings.EMPTY_ARRAY;
@@ -63,6 +64,7 @@ public GetAliasesRequest indicesOptions(IndicesOptions indicesOptions) {
6364
return this;
6465
}
6566

67+
@Override
6668
public String[] indices() {
6769
return indices;
6870
}
@@ -71,6 +73,7 @@ public String[] aliases() {
7173
return aliases;
7274
}
7375

76+
@Override
7477
public IndicesOptions indicesOptions() {
7578
return indicesOptions;
7679
}

src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeRequest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.elasticsearch.Version;
2222
import org.elasticsearch.action.ActionRequestValidationException;
23+
import org.elasticsearch.action.IndicesRequest;
24+
import org.elasticsearch.action.support.IndicesOptions;
2325
import org.elasticsearch.action.support.single.custom.SingleCustomOperationRequest;
2426
import org.elasticsearch.common.Nullable;
2527
import org.elasticsearch.common.Strings;
@@ -34,7 +36,7 @@
3436
* A request to analyze a text associated with a specific index. Allow to provide
3537
* the actual analyzer name to perform the analysis with.
3638
*/
37-
public class AnalyzeRequest extends SingleCustomOperationRequest<AnalyzeRequest> {
39+
public class AnalyzeRequest extends SingleCustomOperationRequest<AnalyzeRequest> implements IndicesRequest {
3840

3941
private String index;
4042

@@ -87,6 +89,19 @@ public String index() {
8789
return this.index;
8890
}
8991

92+
@Override
93+
public String[] indices() {
94+
if (index == null) {
95+
return Strings.EMPTY_ARRAY;
96+
}
97+
return new String[]{index};
98+
}
99+
100+
@Override
101+
public IndicesOptions indicesOptions() {
102+
return IndicesOptions.strictSingleIndexNoExpandForbidClosed();
103+
}
104+
90105
public AnalyzeRequest analyzer(String analyzer) {
91106
this.analyzer = analyzer;
92107
return this;

src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected ClusterBlockException checkGlobalBlock(ClusterState state, AnalyzeRequ
8989
@Override
9090
protected ClusterBlockException checkRequestBlock(ClusterState state, AnalyzeRequest request) {
9191
if (request.index() != null) {
92-
request.index(state.metaData().concreteSingleIndex(request.index()));
92+
request.index(state.metaData().concreteSingleIndex(request.index(), request.indicesOptions()));
9393
return state.blocks().indexBlockedException(ClusterBlockLevel.READ, request.index());
9494
}
9595
return null;

0 commit comments

Comments
 (0)