|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.solr.cloud; |
| 18 | + |
| 19 | +import static org.hamcrest.core.IsIterableContaining.hasItems; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import org.apache.solr.client.solrj.SolrQuery; |
| 24 | +import org.apache.solr.client.solrj.request.CollectionAdminRequest; |
| 25 | +import org.apache.solr.client.solrj.request.UpdateRequest; |
| 26 | +import org.apache.solr.client.solrj.response.QueryResponse; |
| 27 | +import org.apache.solr.common.params.ShardParams; |
| 28 | +import org.apache.solr.common.util.SimpleOrderedMap; |
| 29 | +import org.hamcrest.MatcherAssert; |
| 30 | +import org.junit.BeforeClass; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +/** |
| 34 | + * Test which asserts that shards.info=true works even if several shards are down It must return one |
| 35 | + * unique key per shard. See SOLR-14892 |
| 36 | + */ |
| 37 | +public class TestShardsInfoResponse extends SolrCloudTestCase { |
| 38 | + |
| 39 | + @BeforeClass |
| 40 | + public static void setupCluster() throws Exception { |
| 41 | + configureCluster(3).addConfig("cloud-minimal", configset("cloud-minimal")).configure(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + @SuppressWarnings("unchecked") |
| 46 | + public void searchingWithShardsInfoMustNotReturnEmptyOrDuplicateKeys() throws Exception { |
| 47 | + |
| 48 | + CollectionAdminRequest.createCollection("collection", "cloud-minimal", 3, 1) |
| 49 | + .process(cluster.getSolrClient()); |
| 50 | + |
| 51 | + UpdateRequest update = new UpdateRequest(); |
| 52 | + for (int i = 0; i < 100; i++) { |
| 53 | + update.add("id", Integer.toString(i)); |
| 54 | + } |
| 55 | + update.commit(cluster.getSolrClient(), "collection"); |
| 56 | + |
| 57 | + // Stops 2 shards |
| 58 | + for (int i = 0; i < 2; i++) { |
| 59 | + cluster.waitForJettyToStop(cluster.stopJettySolrRunner(i)); |
| 60 | + } |
| 61 | + |
| 62 | + QueryResponse response = |
| 63 | + cluster |
| 64 | + .getSolrClient() |
| 65 | + .query( |
| 66 | + "collection", |
| 67 | + new SolrQuery("*:*") |
| 68 | + .setRows(1) |
| 69 | + .setParam(ShardParams.SHARDS_TOLERANT, true) |
| 70 | + .setParam(ShardParams.SHARDS_INFO, true)); |
| 71 | + assertEquals(0, response.getStatus()); |
| 72 | + assertTrue(response.getResults().getNumFound() > 0); |
| 73 | + |
| 74 | + SimpleOrderedMap<Object> shardsInfo = |
| 75 | + (SimpleOrderedMap<Object>) response.getResponse().get("shards.info"); |
| 76 | + // We verify that there are no duplicate keys in case of 2 shards in error |
| 77 | + assertEquals(3, shardsInfo.size()); |
| 78 | + |
| 79 | + Collection<String> keys = new ArrayList<>(); |
| 80 | + keys.add(shardsInfo.getName(0)); |
| 81 | + keys.add(shardsInfo.getName(1)); |
| 82 | + keys.add(shardsInfo.getName(2)); |
| 83 | + |
| 84 | + // The names of the shards in error are generated as unknown_shard_1 and unknown_shard_2 because |
| 85 | + // we could not get the real shard names. |
| 86 | + MatcherAssert.assertThat( |
| 87 | + (Iterable<String>) keys, hasItems("unknown_shard_1", "unknown_shard_2")); |
| 88 | + } |
| 89 | +} |
0 commit comments