Unified MetaData#concreteIndices methods into a single method that accepts indices (or aliases) and indices options

Added new internal flag to IndicesOptions that tells whether aliases can be resolved to multiple indices or not.

Cut over to new metaData#concreteIndices(IndicesOptions, String...) for all the api previously using MetaData#concreteIndices(String[], IndicesOptions) and removed old method, deprecation is not needed as it doesn't break client code.

Introduced constants for flags in IndicesOptions for more readability

Renamed MetaData#concreteIndex to concreteSingleIndex, left method as a shortcut although it calls the common concreteIndices that accepts IndicesOptions and multipleIndices
This commit is contained in:
javanna 2014-05-14 14:17:45 +02:00 committed by Luca Cavanna
parent 1f28cd0ba8
commit 7548b2edb7
50 changed files with 485 additions and 213 deletions

View File

@ -140,7 +140,7 @@ public class TransportClusterHealthAction extends TransportMasterNodeReadOperati
} }
if (request.indices().length > 0) { if (request.indices().length > 0) {
try { try {
clusterState.metaData().concreteIndices(request.indices(), IndicesOptions.strictExpand()); clusterState.metaData().concreteIndices(IndicesOptions.strictExpand(), request.indices());
waitForCounter++; waitForCounter++;
} catch (IndexMissingException e) { } catch (IndexMissingException e) {
response.status = ClusterHealthStatus.RED; // no indices, make sure its RED response.status = ClusterHealthStatus.RED; // no indices, make sure its RED
@ -221,7 +221,7 @@ public class TransportClusterHealthAction extends TransportMasterNodeReadOperati
} }
String[] concreteIndices; String[] concreteIndices;
try { try {
concreteIndices = clusterState.metaData().concreteIndices(request.indices(), IndicesOptions.lenientExpandOpen()); concreteIndices = clusterState.metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), request.indices());
} catch (IndexMissingException e) { } catch (IndexMissingException e) {
// one of the specified indices is not there - treat it as RED. // one of the specified indices is not there - treat it as RED.
ClusterHealthResponse response = new ClusterHealthResponse(clusterName.value(), Strings.EMPTY_ARRAY, clusterState); ClusterHealthResponse response = new ClusterHealthResponse(clusterName.value(), Strings.EMPTY_ARRAY, clusterState);

View File

@ -71,7 +71,7 @@ public class TransportClusterSearchShardsAction extends TransportMasterNodeReadO
@Override @Override
protected void masterOperation(final ClusterSearchShardsRequest request, final ClusterState state, final ActionListener<ClusterSearchShardsResponse> listener) throws ElasticsearchException { protected void masterOperation(final ClusterSearchShardsRequest request, final ClusterState state, final ActionListener<ClusterSearchShardsResponse> listener) throws ElasticsearchException {
ClusterState clusterState = clusterService.state(); ClusterState clusterState = clusterService.state();
String[] concreteIndices = clusterState.metaData().concreteIndices(request.indices(), request.indicesOptions()); String[] concreteIndices = clusterState.metaData().concreteIndices(request.indicesOptions(), request.indices());
Map<String, Set<String>> routingMap = clusterState.metaData().resolveSearchRouting(request.routing(), request.indices()); Map<String, Set<String>> routingMap = clusterState.metaData().resolveSearchRouting(request.routing(), request.indices());
Set<String> nodeIds = newHashSet(); Set<String> nodeIds = newHashSet();
GroupShardsIterator groupShardsIterator = clusterService.operationRouting().searchShards(clusterState, request.indices(), concreteIndices, routingMap, request.preference()); GroupShardsIterator groupShardsIterator = clusterService.operationRouting().searchShards(clusterState, request.indices(), concreteIndices, routingMap, request.preference());

View File

@ -103,7 +103,7 @@ public class TransportClusterStateAction extends TransportMasterNodeReadOperatio
} }
if (request.indices().length > 0) { if (request.indices().length > 0) {
String[] indices = currentState.metaData().concreteIndices(request.indices(), IndicesOptions.lenientExpandOpen()); String[] indices = currentState.metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), request.indices());
for (String filteredIndex : indices) { for (String filteredIndex : indices) {
IndexMetaData indexMetaData = currentState.metaData().index(filteredIndex); IndexMetaData indexMetaData = currentState.metaData().index(filteredIndex);
if (indexMetaData != null) { if (indexMetaData != null) {

View File

@ -99,7 +99,7 @@ public class TransportIndicesAliasesAction extends TransportMasterNodeOperationA
Set<String> aliases = new HashSet<>(); Set<String> aliases = new HashSet<>();
for (AliasActions action : actions) { for (AliasActions action : actions) {
//expand indices //expand indices
String[] concreteIndices = state.metaData().concreteIndices(action.indices(), request.indicesOptions()); String[] concreteIndices = state.metaData().concreteIndices(request.indicesOptions(), action.indices());
//collect the aliases //collect the aliases
for (String alias : action.aliases()) { for (String alias : action.aliases()) {
aliases.add(alias); aliases.add(alias);

View File

@ -61,7 +61,7 @@ public class TransportAliasesExistAction extends TransportMasterNodeReadOperatio
@Override @Override
protected void masterOperation(GetAliasesRequest request, ClusterState state, ActionListener<AliasesExistResponse> listener) throws ElasticsearchException { protected void masterOperation(GetAliasesRequest request, ClusterState state, ActionListener<AliasesExistResponse> listener) throws ElasticsearchException {
String[] concreteIndices = state.metaData().concreteIndices(request.indices(), request.indicesOptions()); String[] concreteIndices = state.metaData().concreteIndices(request.indicesOptions(), request.indices());
request.indices(concreteIndices); request.indices(concreteIndices);
boolean result = state.metaData().hasAliases(request.aliases(), request.indices()); boolean result = state.metaData().hasAliases(request.aliases(), request.indices());

View File

@ -64,7 +64,7 @@ public class TransportGetAliasesAction extends TransportMasterNodeReadOperationA
@Override @Override
protected void masterOperation(GetAliasesRequest request, ClusterState state, ActionListener<GetAliasesResponse> listener) throws ElasticsearchException { protected void masterOperation(GetAliasesRequest request, ClusterState state, ActionListener<GetAliasesResponse> listener) throws ElasticsearchException {
String[] concreteIndices = state.metaData().concreteIndices(request.indices(), request.indicesOptions()); String[] concreteIndices = state.metaData().concreteIndices(request.indicesOptions(), request.indices());
request.indices(concreteIndices); request.indices(concreteIndices);
@SuppressWarnings("unchecked") // ImmutableList to List results incompatible type @SuppressWarnings("unchecked") // ImmutableList to List results incompatible type

View File

@ -93,7 +93,7 @@ public class TransportAnalyzeAction extends TransportSingleCustomOperationAction
@Override @Override
protected ClusterBlockException checkRequestBlock(ClusterState state, AnalyzeRequest request) { protected ClusterBlockException checkRequestBlock(ClusterState state, AnalyzeRequest request) {
if (request.index() != null) { if (request.index() != null) {
request.index(state.metaData().concreteIndex(request.index())); request.index(state.metaData().concreteSingleIndex(request.index()));
return state.blocks().indexBlockedException(ClusterBlockLevel.READ, request.index()); return state.blocks().indexBlockedException(ClusterBlockLevel.READ, request.index());
} }
return null; return null;

View File

@ -86,7 +86,7 @@ public class TransportCloseIndexAction extends TransportMasterNodeOperationActio
@Override @Override
protected void masterOperation(final CloseIndexRequest request, final ClusterState state, final ActionListener<CloseIndexResponse> listener) throws ElasticsearchException { protected void masterOperation(final CloseIndexRequest request, final ClusterState state, final ActionListener<CloseIndexResponse> listener) throws ElasticsearchException {
request.indices(state.metaData().concreteIndices(request.indices(), request.indicesOptions())); request.indices(state.metaData().concreteIndices(request.indicesOptions(), request.indices()));
CloseIndexClusterStateUpdateRequest updateRequest = new CloseIndexClusterStateUpdateRequest() CloseIndexClusterStateUpdateRequest updateRequest = new CloseIndexClusterStateUpdateRequest()
.ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout()) .ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
.indices(request.indices()); .indices(request.indices());

View File

@ -85,7 +85,7 @@ public class TransportDeleteIndexAction extends TransportMasterNodeOperationActi
@Override @Override
protected void masterOperation(final DeleteIndexRequest request, final ClusterState state, final ActionListener<DeleteIndexResponse> listener) throws ElasticsearchException { protected void masterOperation(final DeleteIndexRequest request, final ClusterState state, final ActionListener<DeleteIndexResponse> listener) throws ElasticsearchException {
request.indices(state.metaData().concreteIndices(request.indices(), request.indicesOptions())); request.indices(state.metaData().concreteIndices(request.indicesOptions(), request.indices()));
if (request.indices().length == 0) { if (request.indices().length == 0) {
listener.onResponse(new DeleteIndexResponse(true)); listener.onResponse(new DeleteIndexResponse(true));
return; return;

View File

@ -81,7 +81,7 @@ public class TransportIndicesExistsAction extends TransportMasterNodeReadOperati
boolean exists; boolean exists;
try { try {
// Similar as the previous behaviour, but now also aliases and wildcards are supported. // Similar as the previous behaviour, but now also aliases and wildcards are supported.
clusterService.state().metaData().concreteIndices(request.indices(), request.indicesOptions()); clusterService.state().metaData().concreteIndices(request.indicesOptions(), request.indices());
exists = true; exists = true;
} catch (IndexMissingException e) { } catch (IndexMissingException e) {
exists = false; exists = false;

View File

@ -71,7 +71,7 @@ public class TransportTypesExistsAction extends TransportMasterNodeReadOperation
@Override @Override
protected void masterOperation(final TypesExistsRequest request, final ClusterState state, final ActionListener<TypesExistsResponse> listener) throws ElasticsearchException { protected void masterOperation(final TypesExistsRequest request, final ClusterState state, final ActionListener<TypesExistsResponse> listener) throws ElasticsearchException {
String[] concreteIndices = state.metaData().concreteIndices(request.indices(), request.indicesOptions()); String[] concreteIndices = state.metaData().concreteIndices(request.indicesOptions(), request.indices());
if (concreteIndices.length == 0) { if (concreteIndices.length == 0) {
listener.onResponse(new TypesExistsResponse(false)); listener.onResponse(new TypesExistsResponse(false));
return; return;

View File

@ -116,7 +116,7 @@ public class TransportDeleteMappingAction extends TransportMasterNodeOperationAc
@Override @Override
protected void masterOperation(final DeleteMappingRequest request, final ClusterState state, final ActionListener<DeleteMappingResponse> listener) throws ElasticsearchException { protected void masterOperation(final DeleteMappingRequest request, final ClusterState state, final ActionListener<DeleteMappingResponse> listener) throws ElasticsearchException {
request.indices(state.metaData().concreteIndices(request.indices(), request.indicesOptions())); request.indices(state.metaData().concreteIndices(request.indicesOptions(), request.indices()));
flushAction.execute(Requests.flushRequest(request.indices()), new ActionListener<FlushResponse>() { flushAction.execute(Requests.flushRequest(request.indices()), new ActionListener<FlushResponse>() {
@Override @Override
public void onResponse(FlushResponse flushResponse) { public void onResponse(FlushResponse flushResponse) {

View File

@ -55,7 +55,7 @@ public class TransportGetFieldMappingsAction extends TransportAction<GetFieldMap
@Override @Override
protected void doExecute(GetFieldMappingsRequest request, final ActionListener<GetFieldMappingsResponse> listener) { protected void doExecute(GetFieldMappingsRequest request, final ActionListener<GetFieldMappingsResponse> listener) {
ClusterState clusterState = clusterService.state(); ClusterState clusterState = clusterService.state();
String[] concreteIndices = clusterState.metaData().concreteIndices(request.indices(), request.indicesOptions()); String[] concreteIndices = clusterState.metaData().concreteIndices(request.indicesOptions(), request.indices());
final AtomicInteger indexCounter = new AtomicInteger(); final AtomicInteger indexCounter = new AtomicInteger();
final AtomicInteger completionCounter = new AtomicInteger(concreteIndices.length); final AtomicInteger completionCounter = new AtomicInteger(concreteIndices.length);
final AtomicReferenceArray<Object> indexResponses = new AtomicReferenceArray<>(concreteIndices.length); final AtomicReferenceArray<Object> indexResponses = new AtomicReferenceArray<>(concreteIndices.length);

View File

@ -71,7 +71,7 @@ public class TransportPutMappingAction extends TransportMasterNodeOperationActio
@Override @Override
protected void doExecute(PutMappingRequest request, ActionListener<PutMappingResponse> listener) { protected void doExecute(PutMappingRequest request, ActionListener<PutMappingResponse> listener) {
request.indices(clusterService.state().metaData().concreteIndices(request.indices(), request.indicesOptions())); request.indices(clusterService.state().metaData().concreteIndices(request.indicesOptions(), request.indices()));
super.doExecute(request, listener); super.doExecute(request, listener);
} }

View File

@ -86,7 +86,7 @@ public class TransportOpenIndexAction extends TransportMasterNodeOperationAction
@Override @Override
protected void masterOperation(final OpenIndexRequest request, final ClusterState state, final ActionListener<OpenIndexResponse> listener) throws ElasticsearchException { protected void masterOperation(final OpenIndexRequest request, final ClusterState state, final ActionListener<OpenIndexResponse> listener) throws ElasticsearchException {
request.indices(state.metaData().concreteIndices(request.indices(), request.indicesOptions())); request.indices(state.metaData().concreteIndices(request.indicesOptions(), request.indices()));
OpenIndexClusterStateUpdateRequest updateRequest = new OpenIndexClusterStateUpdateRequest() OpenIndexClusterStateUpdateRequest updateRequest = new OpenIndexClusterStateUpdateRequest()
.ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout()) .ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout())
.indices(request.indices()); .indices(request.indices());

View File

@ -73,7 +73,7 @@ public class TransportGetSettingsAction extends TransportMasterNodeReadOperation
@Override @Override
protected void masterOperation(GetSettingsRequest request, ClusterState state, ActionListener<GetSettingsResponse> listener) throws ElasticsearchException { protected void masterOperation(GetSettingsRequest request, ClusterState state, ActionListener<GetSettingsResponse> listener) throws ElasticsearchException {
request.indices(state.metaData().concreteIndices(request.indices(), request.indicesOptions())); request.indices(state.metaData().concreteIndices(request.indicesOptions(), request.indices()));
ImmutableOpenMap.Builder<String, Settings> indexToSettingsBuilder = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, Settings> indexToSettingsBuilder = ImmutableOpenMap.builder();
for (String concreteIndex : request.indices()) { for (String concreteIndex : request.indices()) {
IndexMetaData indexMetaData = state.getMetaData().index(concreteIndex); IndexMetaData indexMetaData = state.getMetaData().index(concreteIndex);

View File

@ -69,7 +69,7 @@ public class TransportUpdateSettingsAction extends TransportMasterNodeOperationA
@Override @Override
protected void doExecute(UpdateSettingsRequest request, ActionListener<UpdateSettingsResponse> listener) { protected void doExecute(UpdateSettingsRequest request, ActionListener<UpdateSettingsResponse> listener) {
request.indices(clusterService.state().metaData().concreteIndices(request.indices(), request.indicesOptions())); request.indices(clusterService.state().metaData().concreteIndices(request.indicesOptions(), request.indices()));
super.doExecute(request, listener); super.doExecute(request, listener);
} }

View File

@ -79,7 +79,7 @@ public class TransportDeleteWarmerAction extends TransportMasterNodeOperationAct
@Override @Override
protected void doExecute(DeleteWarmerRequest request, ActionListener<DeleteWarmerResponse> listener) { protected void doExecute(DeleteWarmerRequest request, ActionListener<DeleteWarmerResponse> listener) {
// update to concrete indices // update to concrete indices
request.indices(clusterService.state().metaData().concreteIndices(request.indices(), request.indicesOptions())); request.indices(clusterService.state().metaData().concreteIndices(request.indicesOptions(), request.indices()));
super.doExecute(request, listener); super.doExecute(request, listener);
} }

View File

@ -83,7 +83,7 @@ public class TransportPutWarmerAction extends TransportMasterNodeOperationAction
@Override @Override
protected ClusterBlockException checkBlock(PutWarmerRequest request, ClusterState state) { protected ClusterBlockException checkBlock(PutWarmerRequest request, ClusterState state) {
String[] concreteIndices = clusterService.state().metaData().concreteIndices(request.searchRequest().indices(), request.searchRequest().indicesOptions()); String[] concreteIndices = clusterService.state().metaData().concreteIndices(request.searchRequest().indicesOptions(), request.searchRequest().indices());
return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA, concreteIndices); return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA, concreteIndices);
} }
@ -134,7 +134,7 @@ public class TransportPutWarmerAction extends TransportMasterNodeOperationAction
@Override @Override
public ClusterState execute(ClusterState currentState) { public ClusterState execute(ClusterState currentState) {
MetaData metaData = currentState.metaData(); MetaData metaData = currentState.metaData();
String[] concreteIndices = metaData.concreteIndices(request.searchRequest().indices(), request.searchRequest().indicesOptions()); String[] concreteIndices = metaData.concreteIndices(request.searchRequest().indicesOptions(), request.searchRequest().indices());
BytesReference source = null; BytesReference source = null;
if (request.searchRequest().source() != null && request.searchRequest().source().length() > 0) { if (request.searchRequest().source() != null && request.searchRequest().source().length() > 0) {

View File

@ -202,7 +202,7 @@ public class TransportBulkAction extends TransportAction<BulkRequest, BulkRespon
if (request instanceof IndexRequest) { if (request instanceof IndexRequest) {
IndexRequest indexRequest = (IndexRequest) request; IndexRequest indexRequest = (IndexRequest) request;
String aliasOrIndex = indexRequest.index(); String aliasOrIndex = indexRequest.index();
indexRequest.index(clusterState.metaData().concreteIndex(indexRequest.index())); indexRequest.index(clusterState.metaData().concreteSingleIndex(indexRequest.index()));
MappingMetaData mappingMd = null; MappingMetaData mappingMd = null;
if (metaData.hasIndex(indexRequest.index())) { if (metaData.hasIndex(indexRequest.index())) {
@ -220,11 +220,11 @@ public class TransportBulkAction extends TransportAction<BulkRequest, BulkRespon
} else if (request instanceof DeleteRequest) { } else if (request instanceof DeleteRequest) {
DeleteRequest deleteRequest = (DeleteRequest) request; DeleteRequest deleteRequest = (DeleteRequest) request;
deleteRequest.routing(clusterState.metaData().resolveIndexRouting(deleteRequest.routing(), deleteRequest.index())); deleteRequest.routing(clusterState.metaData().resolveIndexRouting(deleteRequest.routing(), deleteRequest.index()));
deleteRequest.index(clusterState.metaData().concreteIndex(deleteRequest.index())); deleteRequest.index(clusterState.metaData().concreteSingleIndex(deleteRequest.index()));
} else if (request instanceof UpdateRequest) { } else if (request instanceof UpdateRequest) {
UpdateRequest updateRequest = (UpdateRequest) request; UpdateRequest updateRequest = (UpdateRequest) request;
updateRequest.routing(clusterState.metaData().resolveIndexRouting(updateRequest.routing(), updateRequest.index())); updateRequest.routing(clusterState.metaData().resolveIndexRouting(updateRequest.routing(), updateRequest.index()));
updateRequest.index(clusterState.metaData().concreteIndex(updateRequest.index())); updateRequest.index(clusterState.metaData().concreteSingleIndex(updateRequest.index()));
} }
} }

View File

@ -103,7 +103,7 @@ public class TransportDeleteAction extends TransportShardReplicationOperationAct
@Override @Override
protected boolean resolveRequest(final ClusterState state, final DeleteRequest request, final ActionListener<DeleteResponse> listener) { protected boolean resolveRequest(final ClusterState state, final DeleteRequest request, final ActionListener<DeleteResponse> listener) {
request.routing(state.metaData().resolveIndexRouting(request.routing(), request.index())); request.routing(state.metaData().resolveIndexRouting(request.routing(), request.index()));
request.index(state.metaData().concreteIndex(request.index())); request.index(state.metaData().concreteSingleIndex(request.index()));
if (state.metaData().hasIndex(request.index())) { if (state.metaData().hasIndex(request.index())) {
// check if routing is required, if so, do a broadcast delete // check if routing is required, if so, do a broadcast delete
MappingMetaData mappingMd = state.metaData().index(request.index()).mappingOrDefault(request.type()); MappingMetaData mappingMd = state.metaData().index(request.index()).mappingOrDefault(request.type());

View File

@ -98,9 +98,9 @@ public class TransportExplainAction extends TransportShardSingleOperationAction<
@Override @Override
protected void resolveRequest(ClusterState state, ExplainRequest request) { protected void resolveRequest(ClusterState state, ExplainRequest request) {
String concreteIndex = state.metaData().concreteIndex(request.index()); String concreteIndex = state.metaData().concreteSingleIndex(request.index());
request.filteringAlias(state.metaData().filteringAliases(concreteIndex, request.index())); request.filteringAlias(state.metaData().filteringAliases(concreteIndex, request.index()));
request.index(state.metaData().concreteIndex(request.index())); request.index(state.metaData().concreteSingleIndex(request.index()));
// Fail fast on the node that received the request. // Fail fast on the node that received the request.
if (request.routing() == null && state.getMetaData().routingRequired(request.index(), request.type())) { if (request.routing() == null && state.getMetaData().routingRequired(request.index(), request.type())) {

View File

@ -89,7 +89,7 @@ public class TransportGetAction extends TransportShardSingleOperationAction<GetR
} }
// update the routing (request#index here is possibly an alias) // update the routing (request#index here is possibly an alias)
request.routing(state.metaData().resolveIndexRouting(request.routing(), request.index())); request.routing(state.metaData().resolveIndexRouting(request.routing(), request.index()));
request.index(state.metaData().concreteIndex(request.index())); request.index(state.metaData().concreteSingleIndex(request.index()));
// Fail fast on the node that received the request. // Fail fast on the node that received the request.
if (request.routing() == null && state.getMetaData().routingRequired(request.index(), request.type())) { if (request.routing() == null && state.getMetaData().routingRequired(request.index(), request.type())) {

View File

@ -74,7 +74,7 @@ public class TransportMultiGetAction extends TransportAction<MultiGetRequest, Mu
} }
item.routing(clusterState.metaData().resolveIndexRouting(item.routing(), item.index())); item.routing(clusterState.metaData().resolveIndexRouting(item.routing(), item.index()));
item.index(clusterState.metaData().concreteIndex(item.index())); item.index(clusterState.metaData().concreteSingleIndex(item.index()));
ShardId shardId = clusterService.operationRouting() ShardId shardId = clusterService.operationRouting()
.getShards(clusterState, item.index(), item.type(), item.id(), item.routing(), null).shardId(); .getShards(clusterState, item.index(), item.type(), item.id(), item.routing(), null).shardId();
MultiGetShardRequest shardRequest = shardRequests.get(shardId); MultiGetShardRequest shardRequest = shardRequests.get(shardId);

View File

@ -112,7 +112,7 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi
protected boolean resolveRequest(ClusterState state, IndexRequest request, ActionListener<IndexResponse> indexResponseActionListener) { protected boolean resolveRequest(ClusterState state, IndexRequest request, ActionListener<IndexResponse> indexResponseActionListener) {
MetaData metaData = clusterService.state().metaData(); MetaData metaData = clusterService.state().metaData();
String aliasOrIndex = request.index(); String aliasOrIndex = request.index();
request.index(metaData.concreteIndex(request.index())); request.index(metaData.concreteSingleIndex(request.index()));
MappingMetaData mappingMd = null; MappingMetaData mappingMd = null;
if (metaData.hasIndex(request.index())) { if (metaData.hasIndex(request.index())) {
mappingMd = metaData.index(request.index()).mappingOrDefault(request.type()); mappingMd = metaData.index(request.index()).mappingOrDefault(request.type());

View File

@ -92,7 +92,7 @@ public class TransportMoreLikeThisAction extends TransportAction<MoreLikeThisReq
// update to actual index name // update to actual index name
ClusterState clusterState = clusterService.state(); ClusterState clusterState = clusterService.state();
// update to the concrete index // update to the concrete index
final String concreteIndex = clusterState.metaData().concreteIndex(request.index()); final String concreteIndex = clusterState.metaData().concreteSingleIndex(request.index());
Iterable<MutableShardRouting> routingNode = clusterState.getRoutingNodes().routingNodeIter(clusterService.localNode().getId()); Iterable<MutableShardRouting> routingNode = clusterState.getRoutingNodes().routingNodeIter(clusterService.localNode().getId());
if (routingNode == null) { if (routingNode == null) {

View File

@ -166,7 +166,7 @@ public class TransportMultiPercolateAction extends TransportAction<MultiPercolat
PercolateRequest percolateRequest = (PercolateRequest) element; PercolateRequest percolateRequest = (PercolateRequest) element;
String[] concreteIndices; String[] concreteIndices;
try { try {
concreteIndices = clusterState.metaData().concreteIndices(percolateRequest.indices(), percolateRequest.indicesOptions()); concreteIndices = clusterState.metaData().concreteIndices(percolateRequest.indicesOptions(), percolateRequest.indices());
} catch (IndexMissingException e) { } catch (IndexMissingException e) {
reducedResponses.set(slot, e); reducedResponses.set(slot, e);
responsesByItemAndShard.set(slot, new AtomicReferenceArray(0)); responsesByItemAndShard.set(slot, new AtomicReferenceArray(0));

View File

@ -80,7 +80,7 @@ public class TransportSearchAction extends TransportAction<SearchRequest, Search
if (optimizeSingleShard && searchRequest.searchType() != SCAN && searchRequest.searchType() != COUNT) { if (optimizeSingleShard && searchRequest.searchType() != SCAN && searchRequest.searchType() != COUNT) {
try { try {
ClusterState clusterState = clusterService.state(); ClusterState clusterState = clusterService.state();
String[] concreteIndices = clusterState.metaData().concreteIndices(searchRequest.indices(), searchRequest.indicesOptions()); String[] concreteIndices = clusterState.metaData().concreteIndices(searchRequest.indicesOptions(), searchRequest.indices());
Map<String, Set<String>> routingMap = clusterState.metaData().resolveSearchRouting(searchRequest.routing(), searchRequest.indices()); Map<String, Set<String>> routingMap = clusterState.metaData().resolveSearchRouting(searchRequest.routing(), searchRequest.indices());
int shardCount = clusterService.operationRouting().searchShardsCount(clusterState, searchRequest.indices(), concreteIndices, routingMap, searchRequest.preference()); int shardCount = clusterService.operationRouting().searchShardsCount(clusterState, searchRequest.indices(), concreteIndices, routingMap, searchRequest.preference());
if (shardCount == 1) { if (shardCount == 1) {

View File

@ -110,7 +110,7 @@ public abstract class TransportSearchTypeAction extends TransportAction<SearchRe
clusterState.blocks().globalBlockedRaiseException(ClusterBlockLevel.READ); clusterState.blocks().globalBlockedRaiseException(ClusterBlockLevel.READ);
String[] concreteIndices = clusterState.metaData().concreteIndices(request.indices(), request.indicesOptions()); String[] concreteIndices = clusterState.metaData().concreteIndices(request.indicesOptions(), request.indices());
for (String index : concreteIndices) { for (String index : concreteIndices) {
clusterState.blocks().indexBlockedRaiseException(ClusterBlockLevel.READ, index); clusterState.blocks().indexBlockedRaiseException(ClusterBlockLevel.READ, index);

View File

@ -19,6 +19,7 @@
package org.elasticsearch.action.support; package org.elasticsearch.action.support;
import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.Version;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
@ -34,8 +35,14 @@ public class IndicesOptions {
private static final IndicesOptions[] VALUES; private static final IndicesOptions[] VALUES;
private static final byte IGNORE_UNAVAILABLE = 1;
private static final byte ALLOW_NO_INDICES = 2;
private static final byte EXPAND_WILDCARDS_OPEN = 4;
private static final byte EXPAND_WILDCARDS_CLOSED = 8;
private static final byte FORBID_ALIASES_TO_MULTIPLE_INDICES = 16;
static { static {
byte max = 1 << 4; byte max = 1 << 5;
VALUES = new IndicesOptions[max]; VALUES = new IndicesOptions[max];
for (byte id = 0; id < max; id++) { for (byte id = 0; id < max; id++) {
VALUES[id] = new IndicesOptions(id); VALUES[id] = new IndicesOptions(id);
@ -52,7 +59,7 @@ public class IndicesOptions {
* @return Whether specified concrete indices should be ignored when unavailable (missing or closed) * @return Whether specified concrete indices should be ignored when unavailable (missing or closed)
*/ */
public boolean ignoreUnavailable() { public boolean ignoreUnavailable() {
return (id & 1) != 0; return (id & IGNORE_UNAVAILABLE) != 0;
} }
/** /**
@ -60,28 +67,45 @@ public class IndicesOptions {
* The `_all` string or empty list of indices count as wildcard expressions too. * The `_all` string or empty list of indices count as wildcard expressions too.
*/ */
public boolean allowNoIndices() { public boolean allowNoIndices() {
return (id & 2) != 0; return (id & ALLOW_NO_INDICES) != 0;
} }
/** /**
* @return Whether wildcard expressions should get expanded to open indices * @return Whether wildcard expressions should get expanded to open indices
*/ */
public boolean expandWildcardsOpen() { public boolean expandWildcardsOpen() {
return (id & 4) != 0; return (id & EXPAND_WILDCARDS_OPEN) != 0;
} }
/** /**
* @return Whether wildcard expressions should get expanded to closed indices * @return Whether wildcard expressions should get expanded to closed indices
*/ */
public boolean expandWildcardsClosed() { public boolean expandWildcardsClosed() {
return (id & 8) != 0; return (id & EXPAND_WILDCARDS_CLOSED) != 0;
}
/**
* @return whether aliases pointing to multiple indices are allowed
*/
public boolean allowAliasesToMultipleIndices() {
//true is default here, for bw comp we keep the first 16 values
//in the array same as before + the default value for the new flag
return (id & FORBID_ALIASES_TO_MULTIPLE_INDICES) == 0;
} }
public void writeIndicesOptions(StreamOutput out) throws IOException { public void writeIndicesOptions(StreamOutput out) throws IOException {
out.write(id); if (allowAliasesToMultipleIndices() || out.getVersion().onOrAfter(Version.V_1_2_0)) {
out.write(id);
} else {
//if we are talking to a node that doesn't support the newly added flag (allowAliasesToMultipleIndices)
//flip to 0 all the bits starting from the 5th
out.write(id & 0xf);
}
} }
public static IndicesOptions readIndicesOptions(StreamInput in) throws IOException { public static IndicesOptions readIndicesOptions(StreamInput in) throws IOException {
//if we read from a node that doesn't support the newly added flag (allowAliasesToMultipleIndices)
//we just receive the old corresponding value with the new flag set to true (default)
byte id = in.readByte(); byte id = in.readByte();
if (id >= VALUES.length) { if (id >= VALUES.length) {
throw new ElasticsearchIllegalArgumentException("No valid missing index type id: " + id); throw new ElasticsearchIllegalArgumentException("No valid missing index type id: " + id);
@ -90,7 +114,11 @@ public class IndicesOptions {
} }
public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices) { public static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices) {
byte id = toByte(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices); return fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, true);
}
static IndicesOptions fromOptions(boolean ignoreUnavailable, boolean allowNoIndices, boolean expandToOpenIndices, boolean expandToClosedIndices, boolean allowAliasesToMultipleIndices) {
byte id = toByte(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices);
return VALUES[id]; return VALUES[id];
} }
@ -117,6 +145,7 @@ public class IndicesOptions {
} }
} }
//note that allowAliasesToMultipleIndices is not exposed, always true (only for internal use)
return fromOptions( return fromOptions(
toBool(sIgnoreUnavailable, defaultSettings.ignoreUnavailable()), toBool(sIgnoreUnavailable, defaultSettings.ignoreUnavailable()),
toBool(sAllowNoIndices, defaultSettings.allowNoIndices()), toBool(sAllowNoIndices, defaultSettings.allowNoIndices()),
@ -133,7 +162,6 @@ public class IndicesOptions {
return VALUES[6]; return VALUES[6];
} }
/** /**
* @return indices option that requires every specified index to exist, expands wildcards to both open and closed * @return indices option that requires every specified index to exist, expands wildcards to both open and closed
* indices and allows that no indices are resolved from wildcard expressions (not returning an error). * indices and allows that no indices are resolved from wildcard expressions (not returning an error).
@ -142,6 +170,14 @@ public class IndicesOptions {
return VALUES[14]; return VALUES[14];
} }
/**
* @return indices option that requires each specified index or alias to exist, doesn't expand wildcards and
* throws error if any of the aliases resolves to multiple indices
*/
public static IndicesOptions strictSingleIndexNoExpand() {
return VALUES[FORBID_ALIASES_TO_MULTIPLE_INDICES];
}
/** /**
* @return indices options that ignores unavailable indices, expands wildcards only to open indices and * @return indices options that ignores unavailable indices, expands wildcards only to open indices and
* allows that no indices are resolved from wildcard expressions (not returning an error). * allows that no indices are resolved from wildcard expressions (not returning an error).
@ -150,19 +186,24 @@ public class IndicesOptions {
return VALUES[7]; return VALUES[7];
} }
private static byte toByte(boolean ignoreUnavailable, boolean allowNoIndices, boolean wildcardExpandToOpen, boolean wildcardExpandToClosed) { private static byte toByte(boolean ignoreUnavailable, boolean allowNoIndices, boolean wildcardExpandToOpen, boolean wildcardExpandToClosed, boolean allowAliasesToMultipleIndices) {
byte id = 0; byte id = 0;
if (ignoreUnavailable) { if (ignoreUnavailable) {
id |= 1; id |= IGNORE_UNAVAILABLE;
} }
if (allowNoIndices) { if (allowNoIndices) {
id |= 2; id |= ALLOW_NO_INDICES;
} }
if (wildcardExpandToOpen) { if (wildcardExpandToOpen) {
id |= 4; id |= EXPAND_WILDCARDS_OPEN;
} }
if (wildcardExpandToClosed) { if (wildcardExpandToClosed) {
id |= 8; id |= EXPAND_WILDCARDS_CLOSED;
}
//true is default here, for bw comp we keep the first 16 values
//in the array same as before + the default value for the new flag
if (!allowAliasesToMultipleIndices) {
id |= FORBID_ALIASES_TO_MULTIPLE_INDICES;
} }
return id; return id;
} }
@ -173,5 +214,4 @@ public class IndicesOptions {
} }
return !(sValue.equals("false") || sValue.equals("0") || sValue.equals("off")); return !(sValue.equals("false") || sValue.equals("0") || sValue.equals("off"));
} }
} }

View File

@ -117,7 +117,7 @@ public abstract class TransportBroadcastOperationAction<Request extends Broadcas
throw blockException; throw blockException;
} }
// update to concrete indices // update to concrete indices
String[] concreteIndices = clusterState.metaData().concreteIndices(request.indices(), request.indicesOptions()); String[] concreteIndices = clusterState.metaData().concreteIndices(request.indicesOptions(), request.indices());
blockException = checkRequestBlock(clusterState, request, concreteIndices); blockException = checkRequestBlock(clusterState, request, concreteIndices);
if (blockException != null) { if (blockException != null) {
throw blockException; throw blockException;

View File

@ -44,7 +44,7 @@ public abstract class TransportClusterInfoAction<Request extends ClusterInfoRequ
@Override @Override
protected final void masterOperation(final Request request, final ClusterState state, final ActionListener<Response> listener) throws ElasticsearchException { protected final void masterOperation(final Request request, final ClusterState state, final ActionListener<Response> listener) throws ElasticsearchException {
String[] concreteIndices = state.metaData().concreteIndices(request.indices(), request.indicesOptions()); String[] concreteIndices = state.metaData().concreteIndices(request.indicesOptions(), request.indices());
request.indices(concreteIndices); request.indices(concreteIndices);
doMasterOperation(request, state, listener); doMasterOperation(request, state, listener);
} }

View File

@ -70,7 +70,7 @@ public abstract class TransportIndexReplicationOperationAction<Request extends I
throw blockException; throw blockException;
} }
// update to concrete index // update to concrete index
request.index(clusterState.metaData().concreteIndex(request.index())); request.index(clusterState.metaData().concreteSingleIndex(request.index()));
blockException = checkRequestBlock(clusterState, request); blockException = checkRequestBlock(clusterState, request);
if (blockException != null) { if (blockException != null) {
throw blockException; throw blockException;

View File

@ -74,7 +74,7 @@ public abstract class TransportIndicesReplicationOperationAction<Request extends
throw blockException; throw blockException;
} }
// get actual indices // get actual indices
String[] concreteIndices = clusterState.metaData().concreteIndices(request.indices(), request.indicesOptions()); String[] concreteIndices = clusterState.metaData().concreteIndices(request.indicesOptions(), request.indices());
blockException = checkRequestBlock(clusterState, request, concreteIndices); blockException = checkRequestBlock(clusterState, request, concreteIndices);
if (blockException != null) { if (blockException != null) {
throw blockException; throw blockException;

View File

@ -138,7 +138,7 @@ public abstract class TransportShardReplicationOperationAction<Request extends S
* means a different execution, then return false here to indicate not to continue and execute this request. * means a different execution, then return false here to indicate not to continue and execute this request.
*/ */
protected boolean resolveRequest(ClusterState state, Request request, ActionListener<Response> listener) { protected boolean resolveRequest(ClusterState state, Request request, ActionListener<Response> listener) {
request.index(state.metaData().concreteIndex(request.index())); request.index(state.metaData().concreteSingleIndex(request.index()));
return true; return true;
} }

View File

@ -89,7 +89,7 @@ public abstract class TransportInstanceSingleOperationAction<Request extends Ins
* means a different execution, then return false here to indicate not to continue and execute this request. * means a different execution, then return false here to indicate not to continue and execute this request.
*/ */
protected boolean resolveRequest(ClusterState state, Request request, ActionListener<Response> listener) { protected boolean resolveRequest(ClusterState state, Request request, ActionListener<Response> listener) {
request.index(state.metaData().concreteIndex(request.index())); request.index(state.metaData().concreteSingleIndex(request.index()));
return true; return true;
} }

View File

@ -89,7 +89,7 @@ public abstract class TransportShardSingleOperationAction<Request extends Single
protected abstract ClusterBlockException checkRequestBlock(ClusterState state, Request request); protected abstract ClusterBlockException checkRequestBlock(ClusterState state, Request request);
protected void resolveRequest(ClusterState state, Request request) { protected void resolveRequest(ClusterState state, Request request) {
request.index(state.metaData().concreteIndex(request.index())); request.index(state.metaData().concreteSingleIndex(request.index()));
} }
protected abstract ShardIterator shards(ClusterState state, Request request) throws ElasticsearchException; protected abstract ShardIterator shards(ClusterState state, Request request) throws ElasticsearchException;

View File

@ -76,7 +76,7 @@ public class TransportMultiTermVectorsAction extends TransportAction<MultiTermVe
termVectorRequest.type(), termVectorRequest.id(), "routing is required, but hasn't been specified"))); termVectorRequest.type(), termVectorRequest.id(), "routing is required, but hasn't been specified")));
continue; continue;
} }
termVectorRequest.index(clusterState.metaData().concreteIndex(termVectorRequest.index())); termVectorRequest.index(clusterState.metaData().concreteSingleIndex(termVectorRequest.index()));
ShardId shardId = clusterService ShardId shardId = clusterService
.operationRouting() .operationRouting()
.getShards(clusterState, termVectorRequest.index(), termVectorRequest.type(), termVectorRequest.id(), .getShards(clusterState, termVectorRequest.index(), termVectorRequest.type(), termVectorRequest.id(),

View File

@ -80,7 +80,7 @@ public class TransportSingleShardTermVectorAction extends TransportShardSingleOp
protected void resolveRequest(ClusterState state, TermVectorRequest request) { protected void resolveRequest(ClusterState state, TermVectorRequest request) {
// update the routing (request#index here is possibly an alias) // update the routing (request#index here is possibly an alias)
request.routing(state.metaData().resolveIndexRouting(request.routing(), request.index())); request.routing(state.metaData().resolveIndexRouting(request.routing(), request.index()));
request.index(state.metaData().concreteIndex(request.index())); request.index(state.metaData().concreteSingleIndex(request.index()));
// Fail fast on the node that received the request. // Fail fast on the node that received the request.
if (request.routing() == null && state.getMetaData().routingRequired(request.index(), request.type())) { if (request.routing() == null && state.getMetaData().routingRequired(request.index(), request.type())) {

View File

@ -121,7 +121,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio
MetaData metaData = clusterService.state().metaData(); MetaData metaData = clusterService.state().metaData();
String aliasOrIndex = request.index(); String aliasOrIndex = request.index();
request.routing((metaData.resolveIndexRouting(request.routing(), aliasOrIndex))); request.routing((metaData.resolveIndexRouting(request.routing(), aliasOrIndex)));
request.index(metaData.concreteIndex(request.index())); request.index(metaData.concreteSingleIndex(request.index()));
// Fail fast on the node that received the request, rather than failing when translating on the index or delete request. // Fail fast on the node that received the request, rather than failing when translating on the index or delete request.
if (request.routing() == null && state.getMetaData().routingRequired(request.index(), request.type())) { if (request.routing() == null && state.getMetaData().routingRequired(request.index(), request.type())) {

View File

@ -624,45 +624,44 @@ public class MetaData implements Iterable<IndexMetaData> {
} }
/** /**
* Translates the provided indices (possibly aliased) into actual indices. * Translates the provided indices or aliases, eventually containing wildcard expressions, into actual indices.
* @param indicesOptions how the aliases or indices need to be resolved to concrete indices
* @param aliasesOrIndices the aliases or indices to be resolved to concrete indices
* @return the obtained concrete indices
* @throws IndexMissingException if one of the aliases or indices is missing and the provided indices options
* don't allow such a case, or if the final result of the indices resolution is no indices and the indices options
* don't allow such a case.
* @throws ElasticsearchIllegalArgumentException if one of the aliases resolve to multiple indices and the provided
* indices options don't allow such a case.
*/ */
public String[] concreteIndices(String[] aliasesOrIndices, IndicesOptions indicesOptions) throws IndexMissingException { public String[] concreteIndices(IndicesOptions indicesOptions, String... aliasesOrIndices) throws IndexMissingException, ElasticsearchIllegalArgumentException {
if (isAllIndices(aliasesOrIndices)) {
String[] concreteIndices; if (indicesOptions.expandWildcardsOpen() || indicesOptions.expandWildcardsClosed()) {
if (indicesOptions.expandWildcardsOpen() && indicesOptions.expandWildcardsClosed()) { if (isAllIndices(aliasesOrIndices)) {
concreteIndices = concreteAllIndices(); String[] concreteIndices;
} else if (indicesOptions.expandWildcardsOpen()) { if (indicesOptions.expandWildcardsOpen() && indicesOptions.expandWildcardsClosed()) {
concreteIndices = concreteAllOpenIndices(); concreteIndices = concreteAllIndices();
} else if (indicesOptions.expandWildcardsClosed()) { } else if (indicesOptions.expandWildcardsOpen()) {
concreteIndices = concreteAllClosedIndices(); concreteIndices = concreteAllOpenIndices();
} else { } else {
assert false : "Shouldn't end up here"; concreteIndices = concreteAllClosedIndices();
concreteIndices = Strings.EMPTY_ARRAY; }
if (!indicesOptions.allowNoIndices() && concreteIndices.length == 0) {
throw new IndexMissingException(new Index("_all"));
}
return concreteIndices;
} }
if (!indicesOptions.allowNoIndices() && concreteIndices.length == 0) { aliasesOrIndices = convertFromWildcards(aliasesOrIndices, indicesOptions);
throw new IndexMissingException(new Index("_all"));
}
return concreteIndices;
} }
aliasesOrIndices = convertFromWildcards(aliasesOrIndices, indicesOptions);
// optimize for single element index (common case) // optimize for single element index (common case)
if (aliasesOrIndices.length == 1) { if (aliasesOrIndices.length == 1) {
String aliasOrIndex = aliasesOrIndices[0]; return concreteIndices(aliasesOrIndices[0], indicesOptions.allowNoIndices(), indicesOptions.allowAliasesToMultipleIndices());
// if a direct index name, just return the array provided
if (this.indices.containsKey(aliasOrIndex)) {
return aliasesOrIndices;
}
String[] actualLst = aliasAndIndexToIndexMap.getOrDefault(aliasOrIndex, Strings.EMPTY_ARRAY);
if (actualLst.length == 0 && !indicesOptions.allowNoIndices()) {
throw new IndexMissingException(new Index(aliasOrIndex));
} else {
return actualLst;
}
} }
// check if its a possible aliased index, if not, just return the // check if its a possible aliased index, if not, just return the passed array
// passed array
boolean possiblyAliased = false; boolean possiblyAliased = false;
for (String index : aliasesOrIndices) { for (String index : aliasesOrIndices) {
if (!this.indices.containsKey(index)) { if (!this.indices.containsKey(index)) {
@ -675,17 +674,9 @@ public class MetaData implements Iterable<IndexMetaData> {
} }
Set<String> actualIndices = new HashSet<>(); Set<String> actualIndices = new HashSet<>();
for (String index : aliasesOrIndices) { for (String aliasOrIndex : aliasesOrIndices) {
String[] actualLst = aliasAndIndexToIndexMap.get(index); String[] indices = concreteIndices(aliasOrIndex, indicesOptions.ignoreUnavailable(), indicesOptions.allowAliasesToMultipleIndices());
if (actualLst == null) { Collections.addAll(actualIndices, indices);
if (!indicesOptions.ignoreUnavailable()) {
throw new IndexMissingException(new Index(index));
}
} else {
for (String x : actualLst) {
actualIndices.add(x);
}
}
} }
if (!indicesOptions.allowNoIndices() && actualIndices.isEmpty()) { if (!indicesOptions.allowNoIndices() && actualIndices.isEmpty()) {
@ -694,20 +685,26 @@ public class MetaData implements Iterable<IndexMetaData> {
return actualIndices.toArray(new String[actualIndices.size()]); return actualIndices.toArray(new String[actualIndices.size()]);
} }
public String concreteIndex(String index) throws IndexMissingException, ElasticsearchIllegalArgumentException { public String concreteSingleIndex(String indexOrAlias) throws IndexMissingException, ElasticsearchIllegalArgumentException {
String[] indices = concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), indexOrAlias);
assert indices.length == 1 : "expected an exception to be thrown otherwise";
return indices[0];
}
private String[] concreteIndices(String aliasOrIndex, boolean allowNoIndices, boolean allowMultipleIndices) throws IndexMissingException, ElasticsearchIllegalArgumentException {
// a quick check, if this is an actual index, if so, return it // a quick check, if this is an actual index, if so, return it
if (indices.containsKey(index)) { if (indices.containsKey(aliasOrIndex)) {
return index; return new String[]{aliasOrIndex};
} }
// not an actual index, fetch from an alias // not an actual index, fetch from an alias
String[] lst = aliasAndIndexToIndexMap.get(index); String[] indices = aliasAndIndexToIndexMap.getOrDefault(aliasOrIndex, Strings.EMPTY_ARRAY);
if (lst == null) { if (indices.length == 0 && !allowNoIndices) {
throw new IndexMissingException(new Index(index)); throw new IndexMissingException(new Index(aliasOrIndex));
} }
if (lst.length > 1) { if (indices.length > 1 && !allowMultipleIndices) {
throw new ElasticsearchIllegalArgumentException("Alias [" + index + "] has more than one indices associated with it [" + Arrays.toString(lst) + "], can't execute a single index op"); throw new ElasticsearchIllegalArgumentException("Alias [" + aliasOrIndex + "] has more than one indices associated with it [" + Arrays.toString(indices) + "], can't execute a single index op");
} }
return lst[0]; return indices;
} }
/** /**
@ -781,7 +778,7 @@ public class MetaData implements Iterable<IndexMetaData> {
} else if (indicesOptions.expandWildcardsClosed()) { } else if (indicesOptions.expandWildcardsClosed()) {
indices = concreteAllClosedIndices(); indices = concreteAllClosedIndices();
} else { } else {
assert false : "Shouldn't end up here"; assert false : "convertFromWildcards shouldn't get called if wildcards expansion is disabled";
indices = Strings.EMPTY_ARRAY; indices = Strings.EMPTY_ARRAY;
} }
boolean found = false; boolean found = false;

View File

@ -229,7 +229,7 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements
@Override @Override
public ClusterState execute(ClusterState currentState) { public ClusterState execute(ClusterState currentState) {
String[] actualIndices = currentState.metaData().concreteIndices(request.indices(), IndicesOptions.strictExpand()); String[] actualIndices = currentState.metaData().concreteIndices(IndicesOptions.strictExpand(), request.indices());
RoutingTable.Builder routingTableBuilder = RoutingTable.builder(currentState.routingTable()); RoutingTable.Builder routingTableBuilder = RoutingTable.builder(currentState.routingTable());
MetaData.Builder metaDataBuilder = MetaData.builder(currentState.metaData()); MetaData.Builder metaDataBuilder = MetaData.builder(currentState.metaData());

View File

@ -146,7 +146,7 @@ public class IndicesFilterParser implements FilterParser {
} }
protected boolean matchesIndices(String currentIndex, String... indices) { protected boolean matchesIndices(String currentIndex, String... indices) {
final String[] concreteIndices = clusterService.state().metaData().concreteIndices(indices, IndicesOptions.lenientExpandOpen()); final String[] concreteIndices = clusterService.state().metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), indices);
for (String index : concreteIndices) { for (String index : concreteIndices) {
if (Regex.simpleMatch(index, currentIndex)) { if (Regex.simpleMatch(index, currentIndex)) {
return true; return true;

View File

@ -147,7 +147,7 @@ public class IndicesQueryParser implements QueryParser {
} }
protected boolean matchesIndices(String currentIndex, String... indices) { protected boolean matchesIndices(String currentIndex, String... indices) {
final String[] concreteIndices = clusterService.state().metaData().concreteIndices(indices, IndicesOptions.lenientExpandOpen()); final String[] concreteIndices = clusterService.state().metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), indices);
for (String index : concreteIndices) { for (String index : concreteIndices) {
if (Regex.simpleMatch(index, currentIndex)) { if (Regex.simpleMatch(index, currentIndex)) {
return true; return true;

View File

@ -72,7 +72,7 @@ public class RestIndicesAction extends AbstractCatAction {
client.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) { client.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {
@Override @Override
public void processResponse(final ClusterStateResponse clusterStateResponse) { public void processResponse(final ClusterStateResponse clusterStateResponse) {
final String[] concreteIndices = clusterStateResponse.getState().metaData().concreteIndices(indices, IndicesOptions.lenientExpandOpen()); final String[] concreteIndices = clusterStateResponse.getState().metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), indices);
ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest(concreteIndices); ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest(concreteIndices);
clusterHealthRequest.local(request.paramAsBoolean("local", clusterHealthRequest.local())); clusterHealthRequest.local(request.paramAsBoolean("local", clusterHealthRequest.local()));
client.admin().cluster().health(clusterHealthRequest, new RestActionListener<ClusterHealthResponse>(channel) { client.admin().cluster().health(clusterHealthRequest, new RestActionListener<ClusterHealthResponse>(channel) {

View File

@ -164,7 +164,7 @@ public class SnapshotsService extends AbstractComponent implements ClusterStateL
SnapshotMetaData snapshots = metaData.custom(SnapshotMetaData.TYPE); SnapshotMetaData snapshots = metaData.custom(SnapshotMetaData.TYPE);
if (snapshots == null || snapshots.entries().isEmpty()) { if (snapshots == null || snapshots.entries().isEmpty()) {
// Store newSnapshot here to be processed in clusterStateProcessed // Store newSnapshot here to be processed in clusterStateProcessed
ImmutableList<String> indices = ImmutableList.copyOf(metaData.concreteIndices(request.indices(), request.indicesOptions())); ImmutableList<String> indices = ImmutableList.copyOf(metaData.concreteIndices(request.indicesOptions(), request.indices()));
logger.trace("[{}][{}] creating snapshot for indices [{}]", request.repository(), request.name(), indices); logger.trace("[{}][{}] creating snapshot for indices [{}]", request.repository(), request.name(), indices);
newSnapshot = new SnapshotMetaData.Entry(snapshotId, request.includeGlobalState(), State.INIT, indices, null); newSnapshot = new SnapshotMetaData.Entry(snapshotId, request.includeGlobalState(), State.INIT, indices, null);
snapshots = new SnapshotMetaData(newSnapshot); snapshots = new SnapshotMetaData(newSnapshot);

View File

@ -0,0 +1,79 @@
/*
* 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.support;
import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.BytesStreamInput;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
public class IndicesOptionsTests extends ElasticsearchTestCase {
@Test
public void testSerialization() throws Exception {
int iterations = randomIntBetween(5, 20);
for (int i = 0; i < iterations; i++) {
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
BytesStreamOutput output = new BytesStreamOutput();
Version outputVersion = randomVersion();
output.setVersion(outputVersion);
indicesOptions.writeIndicesOptions(output);
BytesStreamInput bytesStreamInput = new BytesStreamInput(output.bytes());
bytesStreamInput.setVersion(randomVersion());
IndicesOptions indicesOptions2 = IndicesOptions.readIndicesOptions(bytesStreamInput);
assertThat(indicesOptions2.ignoreUnavailable(), equalTo(indicesOptions.ignoreUnavailable()));
assertThat(indicesOptions2.allowNoIndices(), equalTo(indicesOptions.allowNoIndices()));
assertThat(indicesOptions2.expandWildcardsOpen(), equalTo(indicesOptions.expandWildcardsOpen()));
assertThat(indicesOptions2.expandWildcardsClosed(), equalTo(indicesOptions.expandWildcardsClosed()));
if (outputVersion.onOrAfter(Version.V_1_2_0)) {
assertThat(indicesOptions2.allowAliasesToMultipleIndices(), equalTo(indicesOptions.allowAliasesToMultipleIndices()));
} else {
//default value (true) if the node version doesn't support the allowAliasesToMultipleIndices flag
assertThat(indicesOptions2.allowAliasesToMultipleIndices(), equalTo(true));
}
}
}
@Test
public void testFromOptions() {
int iterations = randomIntBetween(5, 20);
for (int i = 0; i < iterations; i++) {
boolean ignoreUnavailable = randomBoolean();
boolean allowNoIndices = randomBoolean();
boolean expandToOpenIndices = randomBoolean();
boolean expandToClosedIndices = randomBoolean();
boolean allowAliasesToMultipleIndices = randomBoolean();
IndicesOptions indicesOptions = IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, expandToOpenIndices, expandToClosedIndices, allowAliasesToMultipleIndices);
assertThat(indicesOptions.ignoreUnavailable(), equalTo(ignoreUnavailable));
assertThat(indicesOptions.allowNoIndices(), equalTo(allowNoIndices));
assertThat(indicesOptions.expandWildcardsOpen(), equalTo(expandToOpenIndices));
assertThat(indicesOptions.expandWildcardsClosed(), equalTo(expandToClosedIndices));
assertThat(indicesOptions.allowAliasesToMultipleIndices(), equalTo(allowAliasesToMultipleIndices));
}
}
}

View File

@ -189,7 +189,7 @@ public class ClusterHealthResponsesTests extends ElasticsearchTestCase {
routingTable.add(indexRoutingTable); routingTable.add(indexRoutingTable);
} }
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).routingTable(routingTable).build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).routingTable(routingTable).build();
ClusterHealthResponse clusterHealth = new ClusterHealthResponse("bla", clusterState.metaData().concreteIndices(null, IndicesOptions.strictExpand()), clusterState); ClusterHealthResponse clusterHealth = new ClusterHealthResponse("bla", clusterState.metaData().concreteIndices(IndicesOptions.strictExpand(), (String[])null), clusterState);
logger.info("cluster status: {}, expected {}", clusterHealth.getStatus(), counter.status()); logger.info("cluster status: {}, expected {}", clusterHealth.getStatus(), counter.status());
assertClusterHealth(clusterHealth, counter); assertClusterHealth(clusterHealth, counter);
@ -210,7 +210,7 @@ public class ClusterHealthResponsesTests extends ElasticsearchTestCase {
metaData.put(indexMetaData, true); metaData.put(indexMetaData, true);
routingTable.add(indexRoutingTable); routingTable.add(indexRoutingTable);
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).routingTable(routingTable).build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).routingTable(routingTable).build();
ClusterHealthResponse clusterHealth = new ClusterHealthResponse("bla", clusterState.metaData().concreteIndices(null, IndicesOptions.strictExpand()), clusterState); ClusterHealthResponse clusterHealth = new ClusterHealthResponse("bla", clusterState.metaData().concreteIndices(IndicesOptions.strictExpand(), (String[])null), clusterState);
// currently we have no cluster level validation failures as index validation issues are reported per index. // currently we have no cluster level validation failures as index validation issues are reported per index.
assertThat(clusterHealth.getValidationFailures(), Matchers.hasSize(0)); assertThat(clusterHealth.getValidationFailures(), Matchers.hasSize(0));
} }

View File

@ -20,6 +20,7 @@
package org.elasticsearch.cluster.metadata; package org.elasticsearch.cluster.metadata;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
@ -28,9 +29,7 @@ import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test; import org.junit.Test;
import static com.google.common.collect.Sets.newHashSet; import static com.google.common.collect.Sets.newHashSet;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder; import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.equalTo;
/** /**
*/ */
@ -39,8 +38,8 @@ public class MetaDataTests extends ElasticsearchTestCase {
@Test @Test
public void testIndexOptions_strict() { public void testIndexOptions_strict() {
MetaData.Builder mdBuilder = MetaData.builder() MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("foo")) .put(indexBuilder("foo").putAlias(AliasMetaData.builder("foofoobar")))
.put(indexBuilder("foobar")) .put(indexBuilder("foobar").putAlias(AliasMetaData.builder("foofoobar")))
.put(indexBuilder("foofoo-closed").state(IndexMetaData.State.CLOSE)) .put(indexBuilder("foofoo-closed").state(IndexMetaData.State.CLOSE))
.put(indexBuilder("foofoo").putAlias(AliasMetaData.builder("barbaz"))); .put(indexBuilder("foofoo").putAlias(AliasMetaData.builder("barbaz")));
MetaData md = mdBuilder.build(); MetaData md = mdBuilder.build();
@ -48,61 +47,79 @@ public class MetaDataTests extends ElasticsearchTestCase {
IndicesOptions[] indicesOptions = new IndicesOptions[]{ IndicesOptions.strictExpandOpen(), IndicesOptions.strictExpand()}; IndicesOptions[] indicesOptions = new IndicesOptions[]{ IndicesOptions.strictExpandOpen(), IndicesOptions.strictExpand()};
for (IndicesOptions options : indicesOptions) { for (IndicesOptions options : indicesOptions) {
String[] results = md.concreteIndices(new String[]{"foo"}, options); String[] results = md.concreteIndices(options, "foo");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("foo", results[0]); assertEquals("foo", results[0]);
try { try {
md.concreteIndices(new String[]{"bar"}, options); md.concreteIndices(options, "bar");
fail(); fail();
} catch (IndexMissingException e) {} } catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("bar"));
}
results = md.concreteIndices(new String[]{"foofoo", "foobar"}, options); results = md.concreteIndices(options, "foofoo", "foobar");
assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foofoo", "foobar"));
results = md.concreteIndices(options, "foofoobar");
assertEquals(2, results.length);
assertEquals("foo", results[0]);
assertEquals("foobar", results[1]);
try {
md.concreteIndices(options, "bar");
fail();
} catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("bar"));
}
try {
md.concreteIndices(options, "foo", "bar");
fail();
} catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("bar"));
}
results = md.concreteIndices(options, "barbaz", "foobar");
assertEquals(2, results.length); assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foofoo", "foobar")); assertThat(results, arrayContainingInAnyOrder("foofoo", "foobar"));
try { try {
md.concreteIndices(new String[]{"foo", "bar"}, options); md.concreteIndices(options, "barbaz", "bar");
fail(); fail();
} catch (IndexMissingException e) {} } catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("bar"));
}
results = md.concreteIndices(new String[]{"barbaz", "foobar"}, options); results = md.concreteIndices(options, "baz*");
assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foofoo", "foobar"));
try {
md.concreteIndices(new String[]{"barbaz", "bar"}, options);
fail();
} catch (IndexMissingException e) {}
results = md.concreteIndices(new String[]{"baz*"}, options);
assertThat(results, emptyArray()); assertThat(results, emptyArray());
results = md.concreteIndices(new String[]{"foo", "baz*"}, options); results = md.concreteIndices(options, "foo", "baz*");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("foo", results[0]); assertEquals("foo", results[0]);
} }
String[] results = md.concreteIndices(Strings.EMPTY_ARRAY, IndicesOptions.strictExpandOpen()); String[] results = md.concreteIndices(IndicesOptions.strictExpandOpen(), Strings.EMPTY_ARRAY);
assertEquals(3, results.length); assertEquals(3, results.length);
results = md.concreteIndices(Strings.EMPTY_ARRAY, IndicesOptions.strictExpand()); results = md.concreteIndices(IndicesOptions.strictExpand(), Strings.EMPTY_ARRAY);
assertEquals(4, results.length); assertEquals(4, results.length);
results = md.concreteIndices(new String[]{"foofoo*"}, IndicesOptions.strictExpandOpen()); results = md.concreteIndices(IndicesOptions.strictExpandOpen(), "foofoo*");
assertEquals(1, results.length); assertEquals(3, results.length);
assertEquals("foofoo", results[0]); assertThat(results, arrayContainingInAnyOrder("foo", "foobar", "foofoo"));
results = md.concreteIndices(new String[]{"foofoo*"}, IndicesOptions.strictExpand()); results = md.concreteIndices(IndicesOptions.strictExpand(), "foofoo*");
assertEquals(2, results.length); assertEquals(4, results.length);
assertThat(results, arrayContainingInAnyOrder("foofoo", "foofoo-closed")); assertThat(results, arrayContainingInAnyOrder("foo", "foobar", "foofoo", "foofoo-closed"));
} }
@Test @Test
public void testIndexOptions_lenient() { public void testIndexOptions_lenient() {
MetaData.Builder mdBuilder = MetaData.builder() MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("foo")) .put(indexBuilder("foo").putAlias(AliasMetaData.builder("foofoobar")))
.put(indexBuilder("foobar")) .put(indexBuilder("foobar").putAlias(AliasMetaData.builder("foofoobar")))
.put(indexBuilder("foofoo-closed").state(IndexMetaData.State.CLOSE)) .put(indexBuilder("foofoo-closed").state(IndexMetaData.State.CLOSE))
.put(indexBuilder("foofoo").putAlias(AliasMetaData.builder("barbaz"))); .put(indexBuilder("foofoo").putAlias(AliasMetaData.builder("barbaz")));
MetaData md = mdBuilder.build(); MetaData md = mdBuilder.build();
@ -111,50 +128,55 @@ public class MetaDataTests extends ElasticsearchTestCase {
IndicesOptions[] indicesOptions = new IndicesOptions[]{ IndicesOptions.lenientExpandOpen(), lenientExpand}; IndicesOptions[] indicesOptions = new IndicesOptions[]{ IndicesOptions.lenientExpandOpen(), lenientExpand};
for (IndicesOptions options : indicesOptions) { for (IndicesOptions options : indicesOptions) {
String[] results = md.concreteIndices(new String[]{"foo"}, options); String[] results = md.concreteIndices(options, "foo");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("foo", results[0]); assertEquals("foo", results[0]);
results = md.concreteIndices(new String[]{"bar"}, options); results = md.concreteIndices(options, "bar");
assertThat(results, emptyArray()); assertThat(results, emptyArray());
results = md.concreteIndices(new String[]{"foofoo", "foobar"}, options); results = md.concreteIndices(options, "foofoo", "foobar");
assertEquals(2, results.length); assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foofoo", "foobar")); assertThat(results, arrayContainingInAnyOrder("foofoo", "foobar"));
results = md.concreteIndices(new String[]{"foo", "bar"}, options); results = md.concreteIndices(options, "foofoobar");
assertEquals(2, results.length);
assertEquals("foo", results[0]);
assertEquals("foobar", results[1]);
results = md.concreteIndices(options, "foo", "bar");
assertEquals(1, results.length); assertEquals(1, results.length);
assertThat(results, arrayContainingInAnyOrder("foo")); assertThat(results, arrayContainingInAnyOrder("foo"));
results = md.concreteIndices(new String[]{"barbaz", "foobar"}, options); results = md.concreteIndices(options, "barbaz", "foobar");
assertEquals(2, results.length); assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foofoo", "foobar")); assertThat(results, arrayContainingInAnyOrder("foofoo", "foobar"));
results = md.concreteIndices(new String[]{"barbaz", "bar"}, options); results = md.concreteIndices(options, "barbaz", "bar");
assertEquals(1, results.length); assertEquals(1, results.length);
assertThat(results, arrayContainingInAnyOrder("foofoo")); assertThat(results, arrayContainingInAnyOrder("foofoo"));
results = md.concreteIndices(new String[]{"baz*"}, options); results = md.concreteIndices(options, "baz*");
assertThat(results, emptyArray()); assertThat(results, emptyArray());
results = md.concreteIndices(new String[]{"foo", "baz*"}, options); results = md.concreteIndices(options, "foo", "baz*");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("foo", results[0]); assertEquals("foo", results[0]);
} }
String[] results = md.concreteIndices(Strings.EMPTY_ARRAY, IndicesOptions.lenientExpandOpen()); String[] results = md.concreteIndices(IndicesOptions.lenientExpandOpen(), Strings.EMPTY_ARRAY);
assertEquals(3, results.length); assertEquals(3, results.length);
results = md.concreteIndices(Strings.EMPTY_ARRAY, lenientExpand); results = md.concreteIndices(lenientExpand, Strings.EMPTY_ARRAY);
assertEquals(4, results.length); assertEquals(4, results.length);
results = md.concreteIndices(new String[]{"foofoo*"}, IndicesOptions.lenientExpandOpen()); results = md.concreteIndices(IndicesOptions.lenientExpandOpen(), "foofoo*");
assertEquals(1, results.length); assertEquals(3, results.length);
assertEquals("foofoo", results[0]); assertThat(results, arrayContainingInAnyOrder("foo", "foobar", "foofoo"));
results = md.concreteIndices(new String[]{"foofoo*"}, lenientExpand); results = md.concreteIndices(lenientExpand, "foofoo*");
assertEquals(2, results.length); assertEquals(4, results.length);
assertThat(results, arrayContainingInAnyOrder("foofoo", "foofoo-closed")); assertThat(results, arrayContainingInAnyOrder("foo", "foobar", "foofoo", "foofoo-closed"));
} }
@Test @Test
@ -171,36 +193,36 @@ public class MetaDataTests extends ElasticsearchTestCase {
IndicesOptions[] indicesOptions = new IndicesOptions[]{expandOpen, expand}; IndicesOptions[] indicesOptions = new IndicesOptions[]{expandOpen, expand};
for (IndicesOptions options : indicesOptions) { for (IndicesOptions options : indicesOptions) {
String[] results = md.concreteIndices(new String[]{"foo"}, options); String[] results = md.concreteIndices(options, "foo");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("foo", results[0]); assertEquals("foo", results[0]);
try { try {
md.concreteIndices(new String[]{"bar"}, options); md.concreteIndices(options, "bar");
fail(); fail();
} catch(IndexMissingException e) { } catch(IndexMissingException e) {
assertThat(e.index().name(), equalTo("bar")); assertThat(e.index().name(), equalTo("bar"));
} }
try { try {
md.concreteIndices(new String[]{"baz*"}, options); md.concreteIndices(options, "baz*");
fail(); fail();
} catch (IndexMissingException e) { } catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("baz*")); assertThat(e.index().name(), equalTo("baz*"));
} }
try { try {
md.concreteIndices(new String[]{"foo", "baz*"}, options); md.concreteIndices(options, "foo", "baz*");
fail(); fail();
} catch (IndexMissingException e) { } catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("baz*")); assertThat(e.index().name(), equalTo("baz*"));
} }
} }
String[] results = md.concreteIndices(Strings.EMPTY_ARRAY, expandOpen); String[] results = md.concreteIndices(expandOpen, Strings.EMPTY_ARRAY);
assertEquals(3, results.length); assertEquals(3, results.length);
results = md.concreteIndices(Strings.EMPTY_ARRAY, expand); results = md.concreteIndices(expand, Strings.EMPTY_ARRAY);
assertEquals(4, results.length); assertEquals(4, results.length);
} }
@ -214,59 +236,193 @@ public class MetaDataTests extends ElasticsearchTestCase {
// Only closed // Only closed
IndicesOptions options = IndicesOptions.fromOptions(false, true, false, true); IndicesOptions options = IndicesOptions.fromOptions(false, true, false, true);
String[] results = md.concreteIndices(Strings.EMPTY_ARRAY, options); String[] results = md.concreteIndices(options, Strings.EMPTY_ARRAY);
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("foo", results[0]); assertEquals("foo", results[0]);
results = md.concreteIndices(new String[]{"foo*"}, options); results = md.concreteIndices(options, "foo*");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("foo", results[0]); assertEquals("foo", results[0]);
// no wildcards, so wildcard expansion don't apply // no wildcards, so wildcard expansion don't apply
results = md.concreteIndices(new String[]{"bar"}, options); results = md.concreteIndices(options, "bar");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("bar", results[0]); assertEquals("bar", results[0]);
// Only open // Only open
options = IndicesOptions.fromOptions(false, true, true, false); options = IndicesOptions.fromOptions(false, true, true, false);
results = md.concreteIndices(Strings.EMPTY_ARRAY, options); results = md.concreteIndices(options, Strings.EMPTY_ARRAY);
assertEquals(2, results.length); assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("bar", "foobar")); assertThat(results, arrayContainingInAnyOrder("bar", "foobar"));
results = md.concreteIndices(new String[]{"foo*"}, options); results = md.concreteIndices(options, "foo*");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("foobar", results[0]); assertEquals("foobar", results[0]);
results = md.concreteIndices(new String[]{"bar"}, options); results = md.concreteIndices(options, "bar");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("bar", results[0]); assertEquals("bar", results[0]);
// Open and closed // Open and closed
options = IndicesOptions.fromOptions(false, true, true, true); options = IndicesOptions.fromOptions(false, true, true, true);
results = md.concreteIndices(Strings.EMPTY_ARRAY, options); results = md.concreteIndices(options, Strings.EMPTY_ARRAY);
assertEquals(3, results.length); assertEquals(3, results.length);
assertThat(results, arrayContainingInAnyOrder("bar", "foobar", "foo")); assertThat(results, arrayContainingInAnyOrder("bar", "foobar", "foo"));
results = md.concreteIndices(new String[]{"foo*"}, options); results = md.concreteIndices(options, "foo*");
assertEquals(2, results.length); assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foobar", "foo")); assertThat(results, arrayContainingInAnyOrder("foobar", "foo"));
results = md.concreteIndices(new String[]{"bar"}, options); results = md.concreteIndices(options, "bar");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("bar", results[0]); assertEquals("bar", results[0]);
results = md.concreteIndices(new String[]{"-foo*"}, options); results = md.concreteIndices(options, "-foo*");
assertEquals(1, results.length); assertEquals(1, results.length);
assertEquals("bar", results[0]); assertEquals("bar", results[0]);
results = md.concreteIndices(new String[]{"-*"}, options); results = md.concreteIndices(options, "-*");
assertEquals(0, results.length); assertEquals(0, results.length);
options = IndicesOptions.fromOptions(false, false, true, true); options = IndicesOptions.fromOptions(false, false, true, true);
try { try {
md.concreteIndices(new String[]{"-*"}, options); md.concreteIndices(options, "-*");
fail(); fail();
} catch (IndexMissingException e) {} } catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("[-*]"));
}
}
@Test
public void testIndexOptions_noExpandWildcards() {
MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("foo").putAlias(AliasMetaData.builder("foofoobar")))
.put(indexBuilder("foobar").putAlias(AliasMetaData.builder("foofoobar")))
.put(indexBuilder("foofoo-closed").state(IndexMetaData.State.CLOSE))
.put(indexBuilder("foofoo").putAlias(AliasMetaData.builder("barbaz")));
MetaData md = mdBuilder.build();
//ignore unavailable and allow no indices
{
IndicesOptions noExpandLenient = IndicesOptions.fromOptions(true, true, false, false);
String[] results = md.concreteIndices(noExpandLenient, "baz*");
assertThat(results, emptyArray());
results = md.concreteIndices(noExpandLenient, "foo", "baz*");
assertEquals(1, results.length);
assertEquals("foo", results[0]);
results = md.concreteIndices(noExpandLenient, "foofoobar");
assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foo", "foobar"));
}
//ignore unavailable but don't allow no indices
{
IndicesOptions noExpandDisallowEmpty = IndicesOptions.fromOptions(true, false, false, false);
try {
md.concreteIndices(noExpandDisallowEmpty, "baz*");
fail();
} catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("baz*"));
}
String[] results = md.concreteIndices(noExpandDisallowEmpty, "foo", "baz*");
assertEquals(1, results.length);
assertEquals("foo", results[0]);
results = md.concreteIndices(noExpandDisallowEmpty, "foofoobar");
assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foo", "foobar"));
}
//error on unavailable but allow no indices
{
IndicesOptions noExpandErrorUnavailable = IndicesOptions.fromOptions(false, true, false, false);
String[] results = md.concreteIndices(noExpandErrorUnavailable, "baz*");
assertThat(results, emptyArray());
try {
md.concreteIndices(noExpandErrorUnavailable, "foo", "baz*");
fail();
} catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("baz*"));
}
results = md.concreteIndices(noExpandErrorUnavailable, "foofoobar");
assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foo", "foobar"));
}
//error on both unavailable and no indices
{
IndicesOptions noExpandStrict = IndicesOptions.fromOptions(false, false, false, false);
try {
md.concreteIndices(noExpandStrict, "baz*");
fail();
} catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("baz*"));
}
try {
md.concreteIndices(noExpandStrict, "foo", "baz*");
fail();
} catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("baz*"));
}
String[] results = md.concreteIndices(noExpandStrict, "foofoobar");
assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foo", "foobar"));
}
}
@Test
public void testIndexOptions_singleIndexNoExpandWildcards() {
MetaData.Builder mdBuilder = MetaData.builder()
.put(indexBuilder("foo").putAlias(AliasMetaData.builder("foofoobar")))
.put(indexBuilder("foobar").putAlias(AliasMetaData.builder("foofoobar")))
.put(indexBuilder("foofoo-closed").state(IndexMetaData.State.CLOSE))
.put(indexBuilder("foofoo").putAlias(AliasMetaData.builder("barbaz")));
MetaData md = mdBuilder.build();
//error on both unavailable and no indices + every alias needs to expand to a single index
try {
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), "baz*");
fail();
} catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("baz*"));
}
try {
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), "foo", "baz*");
fail();
} catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("baz*"));
}
try {
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), "foofoobar");
fail();
} catch(ElasticsearchIllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Alias [foofoobar] has more than one indices associated with it"));
}
try {
md.concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), "foo", "foofoobar");
fail();
} catch(ElasticsearchIllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Alias [foofoobar] has more than one indices associated with it"));
}
String[] results = md.concreteIndices(IndicesOptions.strictSingleIndexNoExpand(), "foo", "barbaz");
assertEquals(2, results.length);
assertThat(results, arrayContainingInAnyOrder("foo", "foofoo"));
} }
@Test @Test
@ -274,34 +430,40 @@ public class MetaDataTests extends ElasticsearchTestCase {
MetaData md = MetaData.builder().build(); MetaData md = MetaData.builder().build();
IndicesOptions options = IndicesOptions.strictExpandOpen(); IndicesOptions options = IndicesOptions.strictExpandOpen();
String[] results = md.concreteIndices(Strings.EMPTY_ARRAY, options); String[] results = md.concreteIndices(options, Strings.EMPTY_ARRAY);
assertThat(results, emptyArray()); assertThat(results, emptyArray());
try { try {
md.concreteIndices(new String[]{"foo"}, options); md.concreteIndices(options, "foo");
fail(); fail();
} catch (IndexMissingException e) {} } catch (IndexMissingException e) {
results = md.concreteIndices(new String[]{"foo*"}, options); assertThat(e.index().name(), equalTo("foo"));
}
results = md.concreteIndices(options, "foo*");
assertThat(results, emptyArray()); assertThat(results, emptyArray());
try { try {
md.concreteIndices(new String[]{"foo*", "bar"}, options); md.concreteIndices(options, "foo*", "bar");
fail(); fail();
} catch (IndexMissingException e) {} } catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("bar"));
}
options = IndicesOptions.lenientExpandOpen(); options = IndicesOptions.lenientExpandOpen();
results = md.concreteIndices(Strings.EMPTY_ARRAY, options); results = md.concreteIndices(options, Strings.EMPTY_ARRAY);
assertThat(results, emptyArray()); assertThat(results, emptyArray());
results = md.concreteIndices(new String[]{"foo"}, options); results = md.concreteIndices(options, "foo");
assertThat(results, emptyArray()); assertThat(results, emptyArray());
results = md.concreteIndices(new String[]{"foo*"}, options); results = md.concreteIndices(options, "foo*");
assertThat(results, emptyArray()); assertThat(results, emptyArray());
results = md.concreteIndices(new String[]{"foo*", "bar"}, options); results = md.concreteIndices(options, "foo*", "bar");
assertThat(results, emptyArray()); assertThat(results, emptyArray());
options = IndicesOptions.fromOptions(true, false, true, false); options = IndicesOptions.fromOptions(true, false, true, false);
try { try {
md.concreteIndices(Strings.EMPTY_ARRAY, options); md.concreteIndices(options, Strings.EMPTY_ARRAY);
} catch (IndexMissingException e) {} } catch (IndexMissingException e) {
assertThat(e.index().name(), equalTo("_all"));
}
} }
@Test @Test
@ -345,7 +507,7 @@ public class MetaDataTests extends ElasticsearchTestCase {
.put(indexBuilder("testXXX")) .put(indexBuilder("testXXX"))
.put(indexBuilder("kuku")); .put(indexBuilder("kuku"));
MetaData md = mdBuilder.build(); MetaData md = mdBuilder.build();
md.concreteIndices(new String[]{"testZZZ"}, IndicesOptions.strictExpandOpen()); md.concreteIndices(IndicesOptions.strictExpandOpen(), "testZZZ");
} }
@Test @Test
@ -354,7 +516,7 @@ public class MetaDataTests extends ElasticsearchTestCase {
.put(indexBuilder("testXXX")) .put(indexBuilder("testXXX"))
.put(indexBuilder("kuku")); .put(indexBuilder("kuku"));
MetaData md = mdBuilder.build(); MetaData md = mdBuilder.build();
assertThat(newHashSet(md.concreteIndices(new String[]{"testXXX", "testZZZ"}, IndicesOptions.lenientExpandOpen())), equalTo(newHashSet("testXXX"))); assertThat(newHashSet(md.concreteIndices(IndicesOptions.lenientExpandOpen(), "testXXX", "testZZZ")), equalTo(newHashSet("testXXX")));
} }
@Test(expected = IndexMissingException.class) @Test(expected = IndexMissingException.class)
@ -363,7 +525,7 @@ public class MetaDataTests extends ElasticsearchTestCase {
.put(indexBuilder("testXXX")) .put(indexBuilder("testXXX"))
.put(indexBuilder("kuku")); .put(indexBuilder("kuku"));
MetaData md = mdBuilder.build(); MetaData md = mdBuilder.build();
assertThat(newHashSet(md.concreteIndices(new String[]{"testMo", "testMahdy"}, IndicesOptions.strictExpandOpen())), equalTo(newHashSet("testXXX"))); assertThat(newHashSet(md.concreteIndices(IndicesOptions.strictExpandOpen(), "testMo", "testMahdy")), equalTo(newHashSet("testXXX")));
} }
@Test @Test
@ -372,7 +534,7 @@ public class MetaDataTests extends ElasticsearchTestCase {
.put(indexBuilder("testXXX")) .put(indexBuilder("testXXX"))
.put(indexBuilder("kuku")); .put(indexBuilder("kuku"));
MetaData md = mdBuilder.build(); MetaData md = mdBuilder.build();
assertThat(newHashSet(md.concreteIndices(new String[]{}, IndicesOptions.lenientExpandOpen())), equalTo(Sets.<String>newHashSet("kuku", "testXXX"))); assertThat(newHashSet(md.concreteIndices(IndicesOptions.lenientExpandOpen(), new String[]{})), equalTo(Sets.newHashSet("kuku", "testXXX")));
} }
@Test @Test
@ -451,18 +613,15 @@ public class MetaDataTests extends ElasticsearchTestCase {
public void testIsPatternMatchingAllIndices_explicitList() throws Exception { public void testIsPatternMatchingAllIndices_explicitList() throws Exception {
//even though it does identify all indices, it's not a pattern but just an explicit list of them //even though it does identify all indices, it's not a pattern but just an explicit list of them
String[] concreteIndices = new String[]{"index1", "index2", "index3"}; String[] concreteIndices = new String[]{"index1", "index2", "index3"};
String[] indicesOrAliases = concreteIndices; MetaData metaData = metaDataBuilder(concreteIndices);
String[] allConcreteIndices = concreteIndices; assertThat(metaData.isPatternMatchingAllIndices(concreteIndices, concreteIndices), equalTo(false));
MetaData metaData = metaDataBuilder(allConcreteIndices);
assertThat(metaData.isPatternMatchingAllIndices(indicesOrAliases, concreteIndices), equalTo(false));
} }
@Test @Test
public void testIsPatternMatchingAllIndices_onlyWildcard() throws Exception { public void testIsPatternMatchingAllIndices_onlyWildcard() throws Exception {
String[] indicesOrAliases = new String[]{"*"}; String[] indicesOrAliases = new String[]{"*"};
String[] concreteIndices = new String[]{"index1", "index2", "index3"}; String[] concreteIndices = new String[]{"index1", "index2", "index3"};
String[] allConcreteIndices = concreteIndices; MetaData metaData = metaDataBuilder(concreteIndices);
MetaData metaData = metaDataBuilder(allConcreteIndices);
assertThat(metaData.isPatternMatchingAllIndices(indicesOrAliases, concreteIndices), equalTo(true)); assertThat(metaData.isPatternMatchingAllIndices(indicesOrAliases, concreteIndices), equalTo(true));
} }
@ -470,8 +629,7 @@ public class MetaDataTests extends ElasticsearchTestCase {
public void testIsPatternMatchingAllIndices_matchingTrailingWildcard() throws Exception { public void testIsPatternMatchingAllIndices_matchingTrailingWildcard() throws Exception {
String[] indicesOrAliases = new String[]{"index*"}; String[] indicesOrAliases = new String[]{"index*"};
String[] concreteIndices = new String[]{"index1", "index2", "index3"}; String[] concreteIndices = new String[]{"index1", "index2", "index3"};
String[] allConcreteIndices = concreteIndices; MetaData metaData = metaDataBuilder(concreteIndices);
MetaData metaData = metaDataBuilder(allConcreteIndices);
assertThat(metaData.isPatternMatchingAllIndices(indicesOrAliases, concreteIndices), equalTo(true)); assertThat(metaData.isPatternMatchingAllIndices(indicesOrAliases, concreteIndices), equalTo(true));
} }
@ -488,8 +646,7 @@ public class MetaDataTests extends ElasticsearchTestCase {
public void testIsPatternMatchingAllIndices_matchingSingleExclusion() throws Exception { public void testIsPatternMatchingAllIndices_matchingSingleExclusion() throws Exception {
String[] indicesOrAliases = new String[]{"-index1", "+index1"}; String[] indicesOrAliases = new String[]{"-index1", "+index1"};
String[] concreteIndices = new String[]{"index1", "index2", "index3"}; String[] concreteIndices = new String[]{"index1", "index2", "index3"};
String[] allConcreteIndices = concreteIndices; MetaData metaData = metaDataBuilder(concreteIndices);
MetaData metaData = metaDataBuilder(allConcreteIndices);
assertThat(metaData.isPatternMatchingAllIndices(indicesOrAliases, concreteIndices), equalTo(true)); assertThat(metaData.isPatternMatchingAllIndices(indicesOrAliases, concreteIndices), equalTo(true));
} }
@ -506,8 +663,7 @@ public class MetaDataTests extends ElasticsearchTestCase {
public void testIsPatternMatchingAllIndices_matchingTrailingWildcardAndExclusion() throws Exception { public void testIsPatternMatchingAllIndices_matchingTrailingWildcardAndExclusion() throws Exception {
String[] indicesOrAliases = new String[]{"index*", "-index1", "+index1"}; String[] indicesOrAliases = new String[]{"index*", "-index1", "+index1"};
String[] concreteIndices = new String[]{"index1", "index2", "index3"}; String[] concreteIndices = new String[]{"index1", "index2", "index3"};
String[] allConcreteIndices = concreteIndices; MetaData metaData = metaDataBuilder(concreteIndices);
MetaData metaData = metaDataBuilder(allConcreteIndices);
assertThat(metaData.isPatternMatchingAllIndices(indicesOrAliases, concreteIndices), equalTo(true)); assertThat(metaData.isPatternMatchingAllIndices(indicesOrAliases, concreteIndices), equalTo(true));
} }

View File

@ -66,7 +66,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcke
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
public class IndicesOptionsTests extends ElasticsearchIntegrationTest { public class IndicesOptionsIntegrationTests extends ElasticsearchIntegrationTest {
@Test @Test
public void testSpecifiedIndexUnavailable() throws Exception { public void testSpecifiedIndexUnavailable() throws Exception {