Skip to content

Commit

Permalink
Internal: expose the indices names every action relates to if applicable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
javanna committed Jul 24, 2014
1 parent 6f31b11 commit d9ff42f
Show file tree
Hide file tree
Showing 64 changed files with 577 additions and 105 deletions.
@@ -0,0 +1,34 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.action;

import java.util.List;

/**
* Needs to be implemented by all {@link org.elasticsearch.action.ActionRequest} subclasses that are composed of
* multiple subrequests which relate to one or more indices. Allows to retrieve those subrequests.
*/
public interface CompositeIndicesRequest {

/**
* Returns the subrequests that a composite request is composed of
*/
List<? extends IndicesRequest> subRequests();
}
40 changes: 40 additions & 0 deletions src/main/java/org/elasticsearch/action/IndicesRequest.java
@@ -0,0 +1,40 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.action;

import org.elasticsearch.action.support.IndicesOptions;

/**
* Needs to be implemented by all {@link org.elasticsearch.action.ActionRequest} subclasses that relate to
* one or more indices. Allows to retrieve which indices the action relates to.
*/
public interface IndicesRequest {

/**
* Returns the array of indices that the action relates to
*/
String[] indices();

/**
* Returns the indices options used to resolve indices. They tell for instance whether a single index is
* accepted, whether an empty array will be converted to _all, and how wildcards will be expanded if needed.
*/
IndicesOptions indicesOptions();
}
Expand Up @@ -20,6 +20,8 @@
package org.elasticsearch.action.admin.cluster.health;

import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.Strings;
Expand All @@ -35,7 +37,7 @@
/**
*
*/
public class ClusterHealthRequest extends MasterNodeReadOperationRequest<ClusterHealthRequest> {
public class ClusterHealthRequest extends MasterNodeReadOperationRequest<ClusterHealthRequest> implements IndicesRequest {

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

@Override
public String[] indices() {
return indices;
}
Expand All @@ -61,6 +64,11 @@ public ClusterHealthRequest indices(String[] indices) {
return this;
}

@Override
public IndicesOptions indicesOptions() {
return IndicesOptions.lenientExpandOpen();
}

public TimeValue timeout() {
return timeout;
}
Expand Down
Expand Up @@ -217,7 +217,7 @@ private ClusterHealthResponse clusterHealth(ClusterHealthRequest request, Cluste
}
String[] concreteIndices;
try {
concreteIndices = clusterState.metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), request.indices());
concreteIndices = clusterState.metaData().concreteIndices(request.indicesOptions(), request.indices());
} catch (IndexMissingException e) {
// one of the specified indices is not there - treat it as RED.
ClusterHealthResponse response = new ClusterHealthResponse(clusterName.value(), Strings.EMPTY_ARRAY, clusterState);
Expand Down
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
import org.elasticsearch.common.Nullable;
Expand All @@ -32,7 +33,7 @@

/**
*/
public class ClusterSearchShardsRequest extends MasterNodeReadOperationRequest<ClusterSearchShardsRequest> {
public class ClusterSearchShardsRequest extends MasterNodeReadOperationRequest<ClusterSearchShardsRequest> implements IndicesRequest {
private String[] indices;
@Nullable
private String routing;
Expand Down Expand Up @@ -74,10 +75,12 @@ public ClusterSearchShardsRequest indices(String... indices) {
/**
* The indices
*/
@Override
public String[] indices() {
return indices;
}

@Override
public IndicesOptions indicesOptions() {
return indicesOptions;
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.common.Strings;
Expand All @@ -42,9 +43,9 @@
import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.Strings.EMPTY_ARRAY;
import static org.elasticsearch.common.Strings.hasLength;
import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.settings.ImmutableSettings.readSettingsFromStream;
import static org.elasticsearch.common.settings.ImmutableSettings.writeSettingsToStream;
import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;

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

private String snapshot;

Expand Down Expand Up @@ -195,6 +196,7 @@ public CreateSnapshotRequest indices(List<String> indices) {
*
* @return list of indices
*/
@Override
public String[] indices() {
return indices;
}
Expand All @@ -204,6 +206,7 @@ public String[] indices() {
*
* @return the desired behaviour regarding indices options
*/
@Override
public IndicesOptions indicesOptions() {
return indicesOptions;
}
Expand Down
Expand Up @@ -21,6 +21,8 @@

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
Expand All @@ -31,7 +33,7 @@
/**
*
*/
public class ClusterStateRequest extends MasterNodeReadOperationRequest<ClusterStateRequest> {
public class ClusterStateRequest extends MasterNodeReadOperationRequest<ClusterStateRequest> implements IndicesRequest {

private boolean routingTable = true;
private boolean nodes = true;
Expand Down Expand Up @@ -101,6 +103,7 @@ public ClusterStateRequest blocks(boolean blocks) {
return this;
}

@Override
public String[] indices() {
return indices;
}
Expand All @@ -110,6 +113,11 @@ public ClusterStateRequest indices(String... indices) {
return this;
}

@Override
public IndicesOptions indicesOptions() {
return IndicesOptions.lenientExpandOpen();
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
Expand Down
Expand Up @@ -22,7 +22,6 @@
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.TransportMasterNodeReadOperationAction;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterService;
Expand Down Expand Up @@ -99,7 +98,7 @@ protected void masterOperation(final ClusterStateRequest request, final ClusterS
}

if (request.indices().length > 0) {
String[] indices = currentState.metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), request.indices());
String[] indices = currentState.metaData().concreteIndices(request.indicesOptions(), request.indices());
for (String filteredIndex : indices) {
IndexMetaData indexMetaData = currentState.metaData().index(filteredIndex);
if (indexMetaData != null) {
Expand Down
Expand Up @@ -22,7 +22,9 @@
import com.carrotsearch.hppc.cursors.ObjectCursor;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.cluster.metadata.AliasAction;
Expand All @@ -37,18 +39,15 @@
import org.elasticsearch.index.query.FilterBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;

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

/**
* A request to add/remove aliases for one or more indices.
*/
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> {
public class IndicesAliasesRequest extends AcknowledgedRequest<IndicesAliasesRequest> implements IndicesRequest {

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

Expand Down Expand Up @@ -135,6 +134,9 @@ public AliasActions filter(String filter) {
}

public void indices(String... indices) {
if (indices == null) {
throw new ElasticsearchIllegalArgumentException("indices must not be null");
}
List<String> finalIndices = new ArrayList<>();
for (String index : indices) {
if (index != null) {
Expand Down Expand Up @@ -309,6 +311,17 @@ public ActionRequestValidationException validate() {
return validationException;
}

@Override
public String[] indices() {
List<String> indices = Lists.newArrayList();
for (AliasActions aliasActions : aliasActions()) {
if (!CollectionUtils.isEmpty(aliasActions.indices())) {
Collections.addAll(indices, aliasActions.indices);
}
}
return indices.toArray(new String[indices.size()]);
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
Expand All @@ -329,6 +342,7 @@ public void writeTo(StreamOutput out) throws IOException {
writeTimeout(out);
}

@Override
public IndicesOptions indicesOptions() {
return indicesOptions;
}
Expand Down
Expand Up @@ -20,6 +20,7 @@

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
import org.elasticsearch.common.Strings;
Expand All @@ -30,7 +31,7 @@

/**
*/
public class GetAliasesRequest extends MasterNodeReadOperationRequest<GetAliasesRequest> {
public class GetAliasesRequest extends MasterNodeReadOperationRequest<GetAliasesRequest> implements IndicesRequest {

private String[] indices = Strings.EMPTY_ARRAY;
private String[] aliases = Strings.EMPTY_ARRAY;
Expand Down Expand Up @@ -63,6 +64,7 @@ public GetAliasesRequest indicesOptions(IndicesOptions indicesOptions) {
return this;
}

@Override
public String[] indices() {
return indices;
}
Expand All @@ -71,6 +73,7 @@ public String[] aliases() {
return aliases;
}

@Override
public IndicesOptions indicesOptions() {
return indicesOptions;
}
Expand Down
Expand Up @@ -20,6 +20,8 @@

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.single.custom.SingleCustomOperationRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
Expand All @@ -34,7 +36,7 @@
* A request to analyze a text associated with a specific index. Allow to provide
* the actual analyzer name to perform the analysis with.
*/
public class AnalyzeRequest extends SingleCustomOperationRequest<AnalyzeRequest> {
public class AnalyzeRequest extends SingleCustomOperationRequest<AnalyzeRequest> implements IndicesRequest {

private String index;

Expand Down Expand Up @@ -87,6 +89,19 @@ public String index() {
return this.index;
}

@Override
public String[] indices() {
if (index == null) {
return Strings.EMPTY_ARRAY;
}
return new String[]{index};
}

@Override
public IndicesOptions indicesOptions() {
return IndicesOptions.strictSingleIndexNoExpandForbidClosed();
}

public AnalyzeRequest analyzer(String analyzer) {
this.analyzer = analyzer;
return this;
Expand Down
Expand Up @@ -89,7 +89,7 @@ protected ClusterBlockException checkGlobalBlock(ClusterState state, AnalyzeRequ
@Override
protected ClusterBlockException checkRequestBlock(ClusterState state, AnalyzeRequest request) {
if (request.index() != null) {
request.index(state.metaData().concreteSingleIndex(request.index()));
request.index(state.metaData().concreteSingleIndex(request.index(), request.indicesOptions()));
return state.blocks().indexBlockedException(ClusterBlockLevel.READ, request.index());
}
return null;
Expand Down

0 comments on commit d9ff42f

Please sign in to comment.