Stop aborting of multiget requests in case of missing index

The MultiGet API stops with a IndexMissingException, if only one of all
requests tries to access a non existing index. This patch creates a
failure for this item without failing the whole request.

Closes #3267
This commit is contained in:
Alexander Reelsen 2013-07-01 09:43:43 +02:00
parent 751d4ab68e
commit 7790d2bf65
2 changed files with 61 additions and 1 deletions

View File

@ -58,9 +58,16 @@ public class TransportMultiGetAction extends TransportAction<MultiGetRequest, Mu
clusterState.blocks().globalBlockedRaiseException(ClusterBlockLevel.READ); clusterState.blocks().globalBlockedRaiseException(ClusterBlockLevel.READ);
final MultiGetItemResponse[] responses = new MultiGetItemResponse[request.items.size()];
Map<ShardId, MultiGetShardRequest> shardRequests = new HashMap<ShardId, MultiGetShardRequest>(); Map<ShardId, MultiGetShardRequest> shardRequests = new HashMap<ShardId, MultiGetShardRequest>();
for (int i = 0; i < request.items.size(); i++) { for (int i = 0; i < request.items.size(); i++) {
MultiGetRequest.Item item = request.items.get(i); MultiGetRequest.Item item = request.items.get(i);
if (!clusterState.metaData().hasConcreteIndex(item.index())) {
responses[i] = new MultiGetItemResponse(null, new MultiGetResponse.Failure(item.index(), item.type(), item.id(), "[" + item.index() + "] missing"));
continue;
}
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().concreteIndex(item.index()));
ShardId shardId = clusterService.operationRouting() ShardId shardId = clusterService.operationRouting()
@ -77,7 +84,6 @@ public class TransportMultiGetAction extends TransportAction<MultiGetRequest, Mu
shardRequest.add(i, item.type(), item.id(), item.fields()); shardRequest.add(i, item.type(), item.id(), item.fields());
} }
final MultiGetItemResponse[] responses = new MultiGetItemResponse[request.items.size()];
final AtomicInteger counter = new AtomicInteger(shardRequests.size()); final AtomicInteger counter = new AtomicInteger(shardRequests.size());
for (final MultiGetShardRequest shardRequest : shardRequests.values()) { for (final MultiGetShardRequest shardRequest : shardRequests.values()) {

View File

@ -0,0 +1,54 @@
/*
* 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.integration.mget;
import org.elasticsearch.action.get.MultiGetRequest;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.test.integration.AbstractSharedClusterTest;
import org.testng.annotations.Test;
import java.io.IOException;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class SimpleMgetTests extends AbstractSharedClusterTest {
@Test
public void testThatMgetShouldWorkWithOneIndexMissing() throws IOException {
createIndex("test");
ensureYellow();
client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject().field("foo", "bar").endObject()).setRefresh(true).execute().actionGet();
MultiGetResponse mgetResponse = client().prepareMultiGet()
.add(new MultiGetRequest.Item("test", "test", "1"))
.add(new MultiGetRequest.Item("nonExistingIndex", "test", "1"))
.execute().actionGet();
assertThat(mgetResponse.getResponses().length, is(2));
assertThat(mgetResponse.getResponses()[0].getIndex(), is("test"));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(false));
assertThat(mgetResponse.getResponses()[1].getIndex(), is("nonExistingIndex"));
assertThat(mgetResponse.getResponses()[1].isFailed(), is(true));
assertThat(mgetResponse.getResponses()[1].getFailure().getMessage(), is("[nonExistingIndex] missing"));
}
}