Allow GetAliasRequest to retrieve all aliases

Results in less data being sent over the wire, as the Cat API does not
need to have the whole cluster state.

Also added matchers for hasKey() for immutable open map (I think we should
add more of those to have map style assertions).

Closes #4455
This commit is contained in:
Alexander Reelsen 2014-01-02 12:06:29 +01:00
parent 1ede9a5730
commit 040719f337
6 changed files with 125 additions and 30 deletions

View File

@ -78,11 +78,7 @@ public class GetAliasesRequest extends MasterNodeOperationRequest<GetAliasesRequ
@Override
public ActionRequestValidationException validate() {
if (aliases.length == 0) {
return addValidationError("No alias specified", null);
} else {
return null;
}
return null;
}
@Override

View File

@ -274,6 +274,7 @@ public class MetaData implements Iterable<IndexMetaData> {
return ImmutableOpenMap.of();
}
boolean matchAllAliases = aliases.length == 0;
ImmutableOpenMap.Builder<String, ImmutableList<AliasMetaData>> mapBuilder = ImmutableOpenMap.builder();
Iterable<String> intersection = HppcMaps.intersection(ObjectOpenHashSet.from(concreteIndices), indices.keys());
for (String index : intersection) {
@ -281,7 +282,7 @@ public class MetaData implements Iterable<IndexMetaData> {
List<AliasMetaData> filteredValues = Lists.newArrayList();
for (ObjectCursor<AliasMetaData> cursor : indexMetaData.getAliases().values()) {
AliasMetaData value = cursor.value;
if (Regex.simpleMatch(aliases, value.alias())) {
if (matchAllAliases || Regex.simpleMatch(aliases, value.alias())) {
filteredValues.add(value);
}
}

View File

@ -20,13 +20,12 @@ package org.elasticsearch.rest.action.cat;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.Table;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestChannel;
@ -36,7 +35,7 @@ import org.elasticsearch.rest.XContentThrowableRestResponse;
import org.elasticsearch.rest.action.support.RestTable;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import static org.elasticsearch.rest.RestRequest.Method.GET;
@ -55,16 +54,13 @@ public class RestAliasAction extends AbstractCatAction {
@Override
void doRequest(final RestRequest request, final RestChannel channel) {
final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
clusterStateRequest.filterMetaData(true);
clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
clusterStateRequest.filterAll().filterMetaData(false);
client.admin().cluster().state(clusterStateRequest, new ActionListener<ClusterStateResponse>() {
final GetAliasesRequest getAliasesRequest = request.hasParam("alias") ?
new GetAliasesRequest(request.param("alias")) :
new GetAliasesRequest();
client.admin().indices().getAliases(getAliasesRequest, new ActionListener<GetAliasesResponse>() {
@Override
public void onResponse(ClusterStateResponse response) {
public void onResponse(GetAliasesResponse response) {
try {
Table tab = buildTable(request, response);
channel.sendResponse(RestTable.buildResponse(tab, request, channel));
@ -86,8 +82,8 @@ public class RestAliasAction extends AbstractCatAction {
@Override
void documentation(StringBuilder sb) {
sb.append("/_cat_alias");
sb.append("/_cat_alias/{alias}");
sb.append("/_cat/aliases");
sb.append("/_cat/aliases/{alias}");
}
@Override
@ -103,19 +99,14 @@ public class RestAliasAction extends AbstractCatAction {
return table;
}
private Table buildTable(RestRequest request, ClusterStateResponse response) {
private Table buildTable(RestRequest request, GetAliasesResponse response) {
Table table = getTableWithHeader(request);
for (ObjectObjectCursor<String, ImmutableOpenMap<String, AliasMetaData>> cursor : response.getState().getMetaData().aliases()) {
String aliasName = cursor.key;
Iterator<ObjectObjectCursor<String,AliasMetaData>> iterator = cursor.value.iterator();
while (iterator.hasNext()) {
ObjectObjectCursor<String, AliasMetaData> iteratorCursor = iterator.next();
String indexName = iteratorCursor.key;
AliasMetaData aliasMetaData = iteratorCursor.value;
for (ObjectObjectCursor<String, List<AliasMetaData>> cursor : response.getAliases()) {
String indexName = cursor.key;
for (AliasMetaData aliasMetaData : cursor.value) {
table.startRow();
table.addCell(aliasName);
table.addCell(aliasMetaData.alias());
table.addCell(indexName);
table.addCell(aliasMetaData.filteringRequired() ? "*" : "-");
String indexRouting = Strings.hasLength(aliasMetaData.indexRouting()) ? aliasMetaData.indexRouting() : "-";

View File

@ -52,6 +52,7 @@ import static com.google.common.collect.Sets.newHashSet;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.index.query.FilterBuilders.termFilter;
import static org.elasticsearch.test.hamcrest.CollectionAssertions.hasKey;
import static org.hamcrest.Matchers.*;
/**
@ -828,6 +829,20 @@ public class IndexAliasesTests extends ElasticsearchIntegrationTest {
}
}
@Test
public void testGetAllAliasesWorks() {
createIndex("index1");
createIndex("index2");
ensureYellow();
admin().indices().prepareAliases().addAlias("index1", "alias1").addAlias("index2", "alias2").get();
GetAliasesResponse response = admin().indices().prepareGetAliases().get();
assertThat(response.getAliases(), hasKey("index1"));
assertThat(response.getAliases(), hasKey("index1"));
}
private void assertHits(SearchHits hits, String... ids) {
assertThat(hits.totalHits(), equalTo((long) ids.length));
Set<String> hitIds = newHashSet();

View File

@ -0,0 +1,33 @@
/*
* Licensed to ElasticSearch and Shay Banon 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.test.hamcrest;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.hamcrest.Matcher;
/**
* Assertions for easier handling of our custom collections,
* for example ImmutableOpenMap
*/
public class CollectionAssertions {
public static Matcher<ImmutableOpenMap> hasKey(final String key) {
return new CollectionMatchers.ImmutableOpenMapHasKeyMatcher(key);
}
}

View File

@ -0,0 +1,59 @@
/*
* Licensed to ElasticSearch and Shay Banon 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.test.hamcrest;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
/**
* Matchers for easier handling of our custom collections,
* for example ImmutableOpenMap
*/
public class CollectionMatchers {
public static class ImmutableOpenMapHasKeyMatcher extends TypeSafeMatcher<ImmutableOpenMap> {
private final String key;
public ImmutableOpenMapHasKeyMatcher(String key) {
this.key = key;
}
@Override
protected boolean matchesSafely(ImmutableOpenMap item) {
return item.containsKey(key);
}
@Override
public void describeMismatchSafely(final ImmutableOpenMap map, final Description mismatchDescription) {
if (map.size() == 0) {
mismatchDescription.appendText("was empty");
} else {
mismatchDescription.appendText(" was ").appendValue(map);
}
}
@Override
public void describeTo(Description description) {
description.appendText("ImmutableOpenMap should contain key " + key);
}
}
}