Search in sources :

Example 6 with RefreshResponse

use of org.elasticsearch.action.admin.indices.refresh.RefreshResponse in project elasticsearch by elastic.

the class SimpleRecoveryIT method testSimpleRecovery.

public void testSimpleRecovery() throws Exception {
    assertAcked(prepareCreate("test", 1).execute().actionGet());
    NumShards numShards = getNumShards("test");
    client().index(indexRequest("test").type("type1").id("1").source(source("1", "test"), XContentType.JSON)).actionGet();
    FlushResponse flushResponse = client().admin().indices().flush(flushRequest("test")).actionGet();
    assertThat(flushResponse.getTotalShards(), equalTo(numShards.totalNumShards));
    assertThat(flushResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
    assertThat(flushResponse.getFailedShards(), equalTo(0));
    client().index(indexRequest("test").type("type1").id("2").source(source("2", "test"), XContentType.JSON)).actionGet();
    RefreshResponse refreshResponse = client().admin().indices().refresh(refreshRequest("test")).actionGet();
    assertThat(refreshResponse.getTotalShards(), equalTo(numShards.totalNumShards));
    assertThat(refreshResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
    assertThat(refreshResponse.getFailedShards(), equalTo(0));
    allowNodes("test", 2);
    logger.info("Running Cluster Health");
    ensureGreen();
    GetResponse getResult;
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("1").operationThreaded(false)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("1").operationThreaded(false)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
    }
    // now start another one so we move some primaries
    allowNodes("test", 3);
    Thread.sleep(200);
    logger.info("Running Cluster Health");
    ensureGreen();
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
    }
}
Also used : FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 7 with RefreshResponse

use of org.elasticsearch.action.admin.indices.refresh.RefreshResponse in project elasticsearch by elastic.

the class DocumentActionsIT method testBulk.

public void testBulk() throws Exception {
    createIndex();
    NumShards numShards = getNumShards(getConcreteIndexName());
    logger.info("-> running Cluster Health");
    ensureGreen();
    BulkResponse bulkResponse = client().prepareBulk().add(client().prepareIndex().setIndex("test").setType("type1").setId("1").setSource(source("1", "test"))).add(client().prepareIndex().setIndex("test").setType("type1").setId("2").setSource(source("2", "test")).setCreate(true)).add(client().prepareIndex().setIndex("test").setType("type1").setSource(source("3", "test"))).add(client().prepareDelete().setIndex("test").setType("type1").setId("1")).add(// failure
    client().prepareIndex().setIndex("test").setType("type1").setSource("{ xxx }", XContentType.JSON)).execute().actionGet();
    assertThat(bulkResponse.hasFailures(), equalTo(true));
    assertThat(bulkResponse.getItems().length, equalTo(5));
    assertThat(bulkResponse.getItems()[0].isFailed(), equalTo(false));
    assertThat(bulkResponse.getItems()[0].getOpType(), equalTo(OpType.INDEX));
    assertThat(bulkResponse.getItems()[0].getIndex(), equalTo(getConcreteIndexName()));
    assertThat(bulkResponse.getItems()[0].getType(), equalTo("type1"));
    assertThat(bulkResponse.getItems()[0].getId(), equalTo("1"));
    assertThat(bulkResponse.getItems()[1].isFailed(), equalTo(false));
    assertThat(bulkResponse.getItems()[1].getOpType(), equalTo(OpType.CREATE));
    assertThat(bulkResponse.getItems()[1].getIndex(), equalTo(getConcreteIndexName()));
    assertThat(bulkResponse.getItems()[1].getType(), equalTo("type1"));
    assertThat(bulkResponse.getItems()[1].getId(), equalTo("2"));
    assertThat(bulkResponse.getItems()[2].isFailed(), equalTo(false));
    assertThat(bulkResponse.getItems()[2].getOpType(), equalTo(OpType.INDEX));
    assertThat(bulkResponse.getItems()[2].getIndex(), equalTo(getConcreteIndexName()));
    assertThat(bulkResponse.getItems()[2].getType(), equalTo("type1"));
    String generatedId3 = bulkResponse.getItems()[2].getId();
    assertThat(bulkResponse.getItems()[3].isFailed(), equalTo(false));
    assertThat(bulkResponse.getItems()[3].getOpType(), equalTo(OpType.DELETE));
    assertThat(bulkResponse.getItems()[3].getIndex(), equalTo(getConcreteIndexName()));
    assertThat(bulkResponse.getItems()[3].getType(), equalTo("type1"));
    assertThat(bulkResponse.getItems()[3].getId(), equalTo("1"));
    assertThat(bulkResponse.getItems()[4].isFailed(), equalTo(true));
    assertThat(bulkResponse.getItems()[4].getOpType(), equalTo(OpType.INDEX));
    assertThat(bulkResponse.getItems()[4].getIndex(), equalTo(getConcreteIndexName()));
    assertThat(bulkResponse.getItems()[4].getType(), equalTo("type1"));
    waitForRelocation(ClusterHealthStatus.GREEN);
    RefreshResponse refreshResponse = client().admin().indices().prepareRefresh("test").execute().actionGet();
    assertNoFailures(refreshResponse);
    assertThat(refreshResponse.getSuccessfulShards(), equalTo(numShards.totalNumShards));
    for (int i = 0; i < 5; i++) {
        GetResponse getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
        assertThat("cycle #" + i, getResult.isExists(), equalTo(false));
        getResult = client().get(getRequest("test").type("type1").id("2")).actionGet();
        assertThat("cycle #" + i, getResult.getSourceAsString(), equalTo(source("2", "test").string()));
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
        getResult = client().get(getRequest("test").type("type1").id(generatedId3)).actionGet();
        assertThat("cycle #" + i, getResult.getSourceAsString(), equalTo(source("3", "test").string()));
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
    }
}
Also used : RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 8 with RefreshResponse

use of org.elasticsearch.action.admin.indices.refresh.RefreshResponse in project elasticsearch by elastic.

the class FieldStatsProviderRefreshTests method refreshIndex.

private void refreshIndex() {
    RefreshResponse refreshResponse = client().admin().indices().prepareRefresh("index").get();
    assertThat(refreshResponse.getSuccessfulShards(), equalTo(refreshResponse.getSuccessfulShards()));
}
Also used : RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse)

Example 9 with RefreshResponse

use of org.elasticsearch.action.admin.indices.refresh.RefreshResponse in project elasticsearch by elastic.

the class RandomExceptionCircuitBreakerIT method testBreakerWithRandomExceptions.

public void testBreakerWithRandomExceptions() throws IOException, InterruptedException, ExecutionException {
    for (NodeStats node : client().admin().cluster().prepareNodesStats().clear().setBreaker(true).execute().actionGet().getNodes()) {
        assertThat("Breaker is not set to 0", node.getBreaker().getStats(CircuitBreaker.FIELDDATA).getEstimated(), equalTo(0L));
    }
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("test-str").field("type", "keyword").field("doc_values", randomBoolean()).endObject().startObject("test-num").field("type", randomFrom(Arrays.asList("float", "long", "double", "short", "integer"))).endObject().endObject().endObject().endObject().string();
    final double topLevelRate;
    final double lowLevelRate;
    if (frequently()) {
        if (randomBoolean()) {
            if (randomBoolean()) {
                lowLevelRate = 1.0 / between(2, 10);
                topLevelRate = 0.0d;
            } else {
                topLevelRate = 1.0 / between(2, 10);
                lowLevelRate = 0.0d;
            }
        } else {
            lowLevelRate = 1.0 / between(2, 10);
            topLevelRate = 1.0 / between(2, 10);
        }
    } else {
        // rarely no exception
        topLevelRate = 0d;
        lowLevelRate = 0d;
    }
    Settings.Builder settings = Settings.builder().put(indexSettings()).put(EXCEPTION_TOP_LEVEL_RATIO_KEY, topLevelRate).put(EXCEPTION_LOW_LEVEL_RATIO_KEY, lowLevelRate).put(MockEngineSupport.WRAP_READER_RATIO.getKey(), 1.0d);
    logger.info("creating index: [test] using settings: [{}]", settings.build().getAsMap());
    CreateIndexResponse response = client().admin().indices().prepareCreate("test").setSettings(settings).addMapping("type", mapping, XContentType.JSON).execute().actionGet();
    final int numDocs;
    if (response.isShardsAcked() == false) {
        /* some seeds just won't let you create the index at all and we enter a ping-pong mode
             * trying one node after another etc. that is ok but we need to make sure we don't wait
             * forever when indexing documents so we set numDocs = 1 and expect all shards to fail
             * when we search below.*/
        if (response.isAcknowledged()) {
            logger.info("Index creation timed out waiting for primaries to start - only index one doc and expect searches to fail");
        } else {
            logger.info("Index creation failed - only index one doc and expect searches to fail");
        }
        numDocs = 1;
    } else {
        numDocs = between(10, 100);
    }
    for (int i = 0; i < numDocs; i++) {
        try {
            client().prepareIndex("test", "type", "" + i).setTimeout(TimeValue.timeValueSeconds(1)).setSource("test-str", randomUnicodeOfLengthBetween(5, 25), "test-num", i).get();
        } catch (ElasticsearchException ex) {
        }
    }
    logger.info("Start Refresh");
    // don't assert on failures here
    RefreshResponse refreshResponse = client().admin().indices().prepareRefresh("test").execute().get();
    final boolean refreshFailed = refreshResponse.getShardFailures().length != 0 || refreshResponse.getFailedShards() != 0;
    logger.info("Refresh failed: [{}] numShardsFailed: [{}], shardFailuresLength: [{}], successfulShards: [{}], totalShards: [{}] ", refreshFailed, refreshResponse.getFailedShards(), refreshResponse.getShardFailures().length, refreshResponse.getSuccessfulShards(), refreshResponse.getTotalShards());
    final int numSearches = scaledRandomIntBetween(50, 150);
    NodesStatsResponse resp = client().admin().cluster().prepareNodesStats().clear().setBreaker(true).execute().actionGet();
    for (NodeStats stats : resp.getNodes()) {
        assertThat("Breaker is set to 0", stats.getBreaker().getStats(CircuitBreaker.FIELDDATA).getEstimated(), equalTo(0L));
    }
    for (int i = 0; i < numSearches; i++) {
        SearchRequestBuilder searchRequestBuilder = client().prepareSearch().setQuery(QueryBuilders.matchAllQuery());
        if (random().nextBoolean()) {
            searchRequestBuilder.addSort("test-str", SortOrder.ASC);
        }
        searchRequestBuilder.addSort("test-num", SortOrder.ASC);
        boolean success = false;
        try {
            // Sort by the string and numeric fields, to load them into field data
            searchRequestBuilder.get();
            success = true;
        } catch (SearchPhaseExecutionException ex) {
            logger.info("expected SearchPhaseException: [{}]", ex.getMessage());
        }
        if (frequently()) {
            // Now, clear the cache and check that the circuit breaker has been
            // successfully set back to zero. If there is a bug in the circuit
            // breaker adjustment code, it should show up here by the breaker
            // estimate being either positive or negative.
            // make sure all shards are there - there could be shards that are still starting up.
            ensureGreen("test");
            assertAllSuccessful(client().admin().indices().prepareClearCache("test").setFieldDataCache(true).execute().actionGet());
            // Since .cleanUp() is no longer called on cache clear, we need to call it on each node manually
            for (String node : internalCluster().getNodeNames()) {
                final IndicesFieldDataCache fdCache = internalCluster().getInstance(IndicesService.class, node).getIndicesFieldDataCache();
                // Clean up the cache, ensuring that entries' listeners have been called
                fdCache.getCache().refresh();
            }
            NodesStatsResponse nodeStats = client().admin().cluster().prepareNodesStats().clear().setBreaker(true).execute().actionGet();
            for (NodeStats stats : nodeStats.getNodes()) {
                assertThat("Breaker reset to 0 last search success: " + success + " mapping: " + mapping, stats.getBreaker().getStats(CircuitBreaker.FIELDDATA).getEstimated(), equalTo(0L));
            }
        }
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) IndicesService(org.elasticsearch.indices.IndicesService) ElasticsearchException(org.elasticsearch.ElasticsearchException) NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) IndicesFieldDataCache(org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) Settings(org.elasticsearch.common.settings.Settings)

Example 10 with RefreshResponse

use of org.elasticsearch.action.admin.indices.refresh.RefreshResponse in project elasticsearch by elastic.

the class SearchWhileCreatingIndexIT method searchWhileCreatingIndex.

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 = randomAsciiOfLength(5);
    // we will go the primary or the replica, but in a
    // randomized re-creatable manner
    int counter = 0;
    String preference = randomAsciiOfLength(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();
    // at least one shard should be successful when refreshing
    assertThat(refreshResponse.getSuccessfulShards(), greaterThanOrEqualTo(1));
    logger.info("using preference {}", preference);
    // we want to make sure that while recovery happens, and a replica gets recovered, its properly refreshed
    ClusterHealthStatus status = ClusterHealthStatus.RED;
    while (status != ClusterHealthStatus.GREEN) {
        // first, verify that search on the primary search works
        SearchResponse searchResponse = client().prepareSearch("test").setPreference("_primary").setQuery(QueryBuilders.termQuery("field", "test")).execute().actionGet();
        assertHitCount(searchResponse, 1);
        Client client = client();
        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);
    }
    cluster().wipeIndices("test");
}
Also used : ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) Client(org.elasticsearch.client.Client) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

RefreshResponse (org.elasticsearch.action.admin.indices.refresh.RefreshResponse)15 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 GetResponse (org.elasticsearch.action.get.GetResponse)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)3 Settings (org.elasticsearch.common.settings.Settings)3 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)2 FlushResponse (org.elasticsearch.action.admin.indices.flush.FlushResponse)2 RefreshRequest (org.elasticsearch.action.admin.indices.refresh.RefreshRequest)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)1 ShardOperationFailedException (org.elasticsearch.action.ShardOperationFailedException)1 NodeStats (org.elasticsearch.action.admin.cluster.node.stats.NodeStats)1 NodesStatsResponse (org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse)1 ClearIndicesCacheResponse (org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse)1 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)1 ForceMergeResponse (org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse)1 RefreshRequestBuilder (org.elasticsearch.action.admin.indices.refresh.RefreshRequestBuilder)1