/*
 * 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.search.basic;

import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.junit.annotations.TestLogging;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;

/**
 * This test basically verifies that search with a single shard active (cause we indexed to it) and other
 * shards possibly not active at all (cause they haven't allocated) will still work.
 */
@TestLogging("_root:DEBUG")
public class SearchWhileCreatingIndexIT extends ESIntegTestCase {
    public void testIndexCausesIndexCreation() throws Exception {
        searchWhileCreatingIndex(false, 1); // 1 replica in our default...
    }

    public void testNoReplicas() throws Exception {
        searchWhileCreatingIndex(true, 0);
    }

    public void testOneReplica() throws Exception {
        searchWhileCreatingIndex(true, 1);
    }

    public void testTwoReplicas() throws Exception {
        searchWhileCreatingIndex(true, 2);
    }

    private void searchWhileCreatingIndex(boolean createIndex, int numberOfReplicas) throws Exception {

        // TODO: randomize the wait for active shards value on index creation and ensure the appropriate
        // number of data nodes are started for the randomized active shard count value
        String id = randomAlphaOfLength(5);
        // we will go the primary or the replica, but in a
        // randomized re-creatable manner
        int counter = 0;
        String preference = randomAlphaOfLength(5);

        logger.info("running iteration for id {}, preference {}", id, preference);

        if (createIndex) {
            createIndex("test");
        }
        client().prepareIndex("test", "type1", id).setSource("field", "test").execute().actionGet();
        RefreshResponse refreshResponse = client().admin().indices().prepareRefresh("test").execute().actionGet();
        assertThat(refreshResponse.getSuccessfulShards(), greaterThanOrEqualTo(1)); // at least one shard should be successful when refreshing

        logger.info("using preference {}", preference);
        // we want to make sure that while recovery happens, and a replica gets recovered, its properly refreshed
        ClusterHealthStatus status = client().admin().cluster().prepareHealth("test").get().getStatus();

        while (status != ClusterHealthStatus.GREEN) {
            // first, verify that search on the primary search works
            for (IndexShardRoutingTable shardRoutingTable : clusterService().state().routingTable().index("test")) {
                String primaryNode = shardRoutingTable.primaryShard().currentNodeId();
                SearchResponse searchResponse = client().prepareSearch("test")
                    .setPreference("_only_nodes:" + primaryNode)
                    .setQuery(QueryBuilders.termQuery("field", "test"))
                    .execute().actionGet();
                assertHitCount(searchResponse, 1);
                break;
            }
            Client client = client();
            SearchResponse searchResponse = client.prepareSearch("test").setPreference(preference + Integer.toString(counter++)).setQuery(QueryBuilders.termQuery("field", "test")).execute().actionGet();
            if (searchResponse.getHits().getTotalHits() != 1) {
                refresh();
                SearchResponse searchResponseAfterRefresh = client.prepareSearch("test").setPreference(preference).setQuery(QueryBuilders.termQuery("field", "test")).execute().actionGet();
                logger.info("hits count mismatch on any shard search failed, post explicit refresh hits are {}", searchResponseAfterRefresh.getHits().getTotalHits());
                ensureGreen();
                SearchResponse searchResponseAfterGreen = client.prepareSearch("test").setPreference(preference).setQuery(QueryBuilders.termQuery("field", "test")).execute().actionGet();
                logger.info("hits count mismatch on any shard search failed, post explicit wait for green hits are {}", searchResponseAfterGreen.getHits().getTotalHits());
                assertHitCount(searchResponse, 1);
            }
            assertHitCount(searchResponse, 1);
            status = client().admin().cluster().prepareHealth("test").get().getStatus();
            internalCluster().ensureAtLeastNumDataNodes(numberOfReplicas + 1);
        }

        for (String node : internalCluster().nodesInclude("test")) {
            SearchResponse searchResponse = client().prepareSearch("test")
                .setPreference("_prefer_nodes:" + node)
                .setQuery(QueryBuilders.termQuery("field", "test")).execute().actionGet();
            assertHitCount(searchResponse, 1);
        }
        cluster().wipeIndices("test");
    }
}

@@ -1055,73 +1055,8 @@
 us;%0A
-import org.elasticsearch.cluster.routing.IndexShardRoutingTable;%0A
 impo
@@ -3414,17 +3414,17 @@
 tatus();
-%0A
+;
 %0A       
@@ -3515,22 +3515,14 @@
 rch 
-on the primary
+normal
  sea
@@ -3547,364 +3547,70 @@
     
-for (IndexShardRoutingTable shardRoutingTable : clusterService().state().routingTable().index(%22test%22)) %7B%0A                String primaryNode = shardRoutingTable.primaryShard().currentNodeId();%0A                SearchResponse searchResponse = client().prepareSearch(%22test%22)%0A                    .setPreference(%22_only_nodes:%22 + primaryNode)%0A                    
+SearchResponse searchResponse = client().prepareSearch(%22test%22)
 .set
@@ -3660,29 +3660,8 @@
 t%22))
-%0A                    
 .exe
@@ -3672,36 +3672,32 @@
 ().actionGet();%0A
-    
             asse
@@ -3731,45 +3731,8 @@
 1);%0A
-                break;%0A            %7D%0A
     
@@ -3773,39 +3773,24 @@
             
-SearchResponse 
 searchRespon
@@ -5083,357 +5083,8 @@
    %7D
-%0A%0A        for (String node : internalCluster().nodesInclude(%22test%22)) %7B%0A            SearchResponse searchResponse = client().prepareSearch(%22test%22)%0A                .setPreference(%22_prefer_nodes:%22 + node)%0A                .setQuery(QueryBuilders.termQuery(%22field%22, %22test%22)).execute().actionGet();%0A            assertHitCount(searchResponse, 1);%0A        %7D
 %0A   

remove usage of _only_nodes