Search in sources :

Example 1 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class IndexRecoveryIT method testDisconnectsDuringRecovery.

/**
     * Tests scenario where recovery target successfully sends recovery request to source but then the channel gets closed while
     * the source is working on the recovery process.
     */
@TestLogging("_root:DEBUG,org.elasticsearch.indices.recovery:TRACE")
public void testDisconnectsDuringRecovery() throws Exception {
    boolean primaryRelocation = randomBoolean();
    final String indexName = "test";
    final Settings nodeSettings = Settings.builder().put(RecoverySettings.INDICES_RECOVERY_RETRY_DELAY_NETWORK_SETTING.getKey(), TimeValue.timeValueMillis(randomIntBetween(0, 100))).build();
    TimeValue disconnectAfterDelay = TimeValue.timeValueMillis(randomIntBetween(0, 100));
    // start a master node
    String masterNodeName = internalCluster().startMasterOnlyNode(nodeSettings);
    final String blueNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "blue").put(nodeSettings).build());
    final String redNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "red").put(nodeSettings).build());
    client().admin().indices().prepareCreate(indexName).setSettings(Settings.builder().put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + "color", "blue").put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)).get();
    List<IndexRequestBuilder> requests = new ArrayList<>();
    int numDocs = scaledRandomIntBetween(25, 250);
    for (int i = 0; i < numDocs; i++) {
        requests.add(client().prepareIndex(indexName, "type").setSource("{}", XContentType.JSON));
    }
    indexRandom(true, requests);
    ensureSearchable(indexName);
    assertHitCount(client().prepareSearch(indexName).get(), numDocs);
    MockTransportService masterTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, masterNodeName);
    MockTransportService blueMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, blueNodeName);
    MockTransportService redMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, redNodeName);
    redMockTransportService.addDelegate(blueMockTransportService, new MockTransportService.DelegateTransport(redMockTransportService.original()) {

        private final AtomicInteger count = new AtomicInteger();

        @Override
        protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
            logger.info("--> sending request {} on {}", action, connection.getNode());
            if (PeerRecoverySourceService.Actions.START_RECOVERY.equals(action) && count.incrementAndGet() == 1) {
                // ensures that it's considered as valid recovery attempt by source
                try {
                    awaitBusy(() -> client(blueNodeName).admin().cluster().prepareState().setLocal(true).get().getState().getRoutingTable().index("test").shard(0).getAllInitializingShards().isEmpty() == false);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                super.sendRequest(connection, requestId, action, request, options);
                try {
                    Thread.sleep(disconnectAfterDelay.millis());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                throw new ConnectTransportException(connection.getNode(), "DISCONNECT: simulation disconnect after successfully sending " + action + " request");
            } else {
                super.sendRequest(connection, requestId, action, request, options);
            }
        }
    });
    final AtomicBoolean finalized = new AtomicBoolean();
    blueMockTransportService.addDelegate(redMockTransportService, new MockTransportService.DelegateTransport(blueMockTransportService.original()) {

        @Override
        protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
            logger.info("--> sending request {} on {}", action, connection.getNode());
            if (action.equals(PeerRecoveryTargetService.Actions.FINALIZE)) {
                finalized.set(true);
            }
            super.sendRequest(connection, requestId, action, request, options);
        }
    });
    for (MockTransportService mockTransportService : Arrays.asList(redMockTransportService, blueMockTransportService)) {
        mockTransportService.addDelegate(masterTransportService, new MockTransportService.DelegateTransport(mockTransportService.original()) {

            @Override
            protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
                logger.info("--> sending request {} on {}", action, connection.getNode());
                if ((primaryRelocation && finalized.get()) == false) {
                    assertNotEquals(action, ShardStateAction.SHARD_FAILED_ACTION_NAME);
                }
                super.sendRequest(connection, requestId, action, request, options);
            }
        });
    }
    if (primaryRelocation) {
        logger.info("--> starting primary relocation recovery from blue to red");
        client().admin().indices().prepareUpdateSettings(indexName).setSettings(Settings.builder().put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + "color", "red")).get();
        // also waits for relocation / recovery to complete
        ensureGreen();
        // if a primary relocation fails after the source shard has been marked as relocated, both source and target are failed. If the
        // source shard is moved back to started because the target fails first, it's possible that there is a cluster state where the
        // shard is marked as started again (and ensureGreen returns), but while applying the cluster state the primary is failed and
        // will be reallocated. The cluster will thus become green, then red, then green again. Triggering a refresh here before
        // searching helps, as in contrast to search actions, refresh waits for the closed shard to be reallocated.
        client().admin().indices().prepareRefresh(indexName).get();
    } else {
        logger.info("--> starting replica recovery from blue to red");
        client().admin().indices().prepareUpdateSettings(indexName).setSettings(Settings.builder().put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + "color", "red,blue").put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1)).get();
        ensureGreen();
    }
    for (int i = 0; i < 10; i++) {
        assertHitCount(client().prepareSearch(indexName).get(), numDocs);
    }
}
Also used : TransportRequest(org.elasticsearch.transport.TransportRequest) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Example 2 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class IndexRecoveryIT method testDisconnectsWhileRecovering.

public void testDisconnectsWhileRecovering() throws Exception {
    final String indexName = "test";
    final Settings nodeSettings = Settings.builder().put(RecoverySettings.INDICES_RECOVERY_RETRY_DELAY_NETWORK_SETTING.getKey(), "100ms").put(RecoverySettings.INDICES_RECOVERY_INTERNAL_ACTION_TIMEOUT_SETTING.getKey(), "1s").put(MockFSDirectoryService.RANDOM_PREVENT_DOUBLE_WRITE_SETTING.getKey(), // restarted recoveries will delete temp files and write them again
    false).build();
    // start a master node
    internalCluster().startNode(nodeSettings);
    final String blueNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "blue").put(nodeSettings).build());
    final String redNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "red").put(nodeSettings).build());
    ClusterHealthResponse response = client().admin().cluster().prepareHealth().setWaitForNodes(">=3").get();
    assertThat(response.isTimedOut(), is(false));
    client().admin().indices().prepareCreate(indexName).setSettings(Settings.builder().put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + "color", "blue").put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)).get();
    List<IndexRequestBuilder> requests = new ArrayList<>();
    int numDocs = scaledRandomIntBetween(25, 250);
    for (int i = 0; i < numDocs; i++) {
        requests.add(client().prepareIndex(indexName, "type").setSource("{}", XContentType.JSON));
    }
    indexRandom(true, requests);
    ensureSearchable(indexName);
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
    final String blueNodeId = internalCluster().getInstance(ClusterService.class, blueNodeName).localNode().getId();
    assertFalse(stateResponse.getState().getRoutingNodes().node(blueNodeId).isEmpty());
    SearchResponse searchResponse = client().prepareSearch(indexName).get();
    assertHitCount(searchResponse, numDocs);
    String[] recoveryActions = new String[] { PeerRecoverySourceService.Actions.START_RECOVERY, PeerRecoveryTargetService.Actions.FILES_INFO, PeerRecoveryTargetService.Actions.FILE_CHUNK, PeerRecoveryTargetService.Actions.CLEAN_FILES, //RecoveryTarget.Actions.TRANSLOG_OPS, <-- may not be sent if already flushed
    PeerRecoveryTargetService.Actions.PREPARE_TRANSLOG, PeerRecoveryTargetService.Actions.FINALIZE };
    final String recoveryActionToBlock = randomFrom(recoveryActions);
    final boolean dropRequests = randomBoolean();
    logger.info("--> will {} between blue & red on [{}]", dropRequests ? "drop requests" : "break connection", recoveryActionToBlock);
    MockTransportService blueMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, blueNodeName);
    MockTransportService redMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, redNodeName);
    TransportService redTransportService = internalCluster().getInstance(TransportService.class, redNodeName);
    TransportService blueTransportService = internalCluster().getInstance(TransportService.class, blueNodeName);
    final CountDownLatch requestBlocked = new CountDownLatch(1);
    blueMockTransportService.addDelegate(redTransportService, new RecoveryActionBlocker(dropRequests, recoveryActionToBlock, blueMockTransportService.original(), requestBlocked));
    redMockTransportService.addDelegate(blueTransportService, new RecoveryActionBlocker(dropRequests, recoveryActionToBlock, redMockTransportService.original(), requestBlocked));
    logger.info("--> starting recovery from blue to red");
    client().admin().indices().prepareUpdateSettings(indexName).setSettings(Settings.builder().put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + "color", "red,blue").put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1)).get();
    requestBlocked.await();
    logger.info("--> stopping to block recovery");
    blueMockTransportService.clearAllRules();
    redMockTransportService.clearAllRules();
    ensureGreen();
    searchResponse = client(redNodeName).prepareSearch(indexName).setPreference("_local").get();
    assertHitCount(searchResponse, numDocs);
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) Settings(org.elasticsearch.common.settings.Settings)

Example 3 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class UpdateMappingIntegrationIT method testDynamicUpdates.

public void testDynamicUpdates() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), Long.MAX_VALUE)).execute().actionGet();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    int recCount = randomIntBetween(200, 600);
    int numberOfTypes = randomIntBetween(1, 5);
    List<IndexRequestBuilder> indexRequests = new ArrayList<>();
    for (int rec = 0; rec < recCount; rec++) {
        String type = "type" + (rec % numberOfTypes);
        String fieldName = "field_" + type + "_" + rec;
        indexRequests.add(client().prepareIndex("test", type, Integer.toString(rec)).setSource(fieldName, "some_value"));
    }
    indexRandom(true, indexRequests);
    logger.info("checking all the documents are there");
    RefreshResponse refreshResponse = client().admin().indices().prepareRefresh().execute().actionGet();
    assertThat(refreshResponse.getFailedShards(), equalTo(0));
    SearchResponse response = client().prepareSearch("test").setSize(0).execute().actionGet();
    assertThat(response.getHits().getTotalHits(), equalTo((long) recCount));
    logger.info("checking all the fields are in the mappings");
    for (int rec = 0; rec < recCount; rec++) {
        String type = "type" + (rec % numberOfTypes);
        String fieldName = "field_" + type + "_" + rec;
        assertConcreteMappingsOnAll("test", type, fieldName);
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class CircuitBreakerNoopIT method testNoopRequestBreaker.

public void testNoopRequestBreaker() throws Exception {
    assertAcked(prepareCreate("cb-test", 1, Settings.builder().put(SETTING_NUMBER_OF_REPLICAS, between(0, 1))));
    Client client = client();
    // index some different terms so we have some field data for loading
    int docCount = scaledRandomIntBetween(300, 1000);
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    for (long id = 0; id < docCount; id++) {
        reqs.add(client.prepareIndex("cb-test", "type", Long.toString(id)).setSource("test", id));
    }
    indexRandom(true, reqs);
    // A cardinality aggregation uses BigArrays and thus the REQUEST breaker
    client.prepareSearch("cb-test").setQuery(matchAllQuery()).addAggregation(cardinality("card").field("test")).get();
// no exception because the breaker is a noop
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) Client(org.elasticsearch.client.Client)

Example 5 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class CircuitBreakerServiceIT method testParentChecking.

/**
     * Test that a breaker correctly redistributes to a different breaker, in
     * this case, the fielddata breaker borrows space from the request breaker
     */
@TestLogging("_root:DEBUG,org.elasticsearch.action.search:TRACE")
public void testParentChecking() throws Exception {
    if (noopBreakerUsed()) {
        logger.info("--> noop breakers used, skipping test");
        return;
    }
    assertAcked(prepareCreate("cb-test", 1, Settings.builder().put(SETTING_NUMBER_OF_REPLICAS, between(0, 1))).addMapping("type", "test", "type=text,fielddata=true"));
    Client client = client();
    // index some different terms so we have some field data for loading
    int docCount = scaledRandomIntBetween(300, 1000);
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    for (long id = 0; id < docCount; id++) {
        reqs.add(client.prepareIndex("cb-test", "type", Long.toString(id)).setSource("test", "value" + id));
    }
    indexRandom(true, reqs);
    Settings resetSettings = Settings.builder().put(HierarchyCircuitBreakerService.FIELDDATA_CIRCUIT_BREAKER_LIMIT_SETTING.getKey(), "10b").put(HierarchyCircuitBreakerService.FIELDDATA_CIRCUIT_BREAKER_OVERHEAD_SETTING.getKey(), 1.0).build();
    assertAcked(client.admin().cluster().prepareUpdateSettings().setTransientSettings(resetSettings));
    // Perform a search to load field data for the "test" field
    try {
        client.prepareSearch("cb-test").setQuery(matchAllQuery()).addSort("test", SortOrder.DESC).get();
        fail("should have thrown an exception");
    } catch (Exception e) {
        String errMsg = "CircuitBreakingException[[fielddata] Data too large, data for [test] would be";
        assertThat("Exception: [" + e.toString() + "] should contain a CircuitBreakingException", e.toString(), containsString(errMsg));
        errMsg = "which is larger than the limit of [10/10b]]";
        assertThat("Exception: [" + e.toString() + "] should contain a CircuitBreakingException", e.toString(), containsString(errMsg));
    }
    // execute a search that loads field data (sorting on the "test" field)
    // again, this time it should trip the breaker
    SearchRequestBuilder searchRequest = client.prepareSearch("cb-test").setQuery(matchAllQuery()).addSort("test", SortOrder.DESC);
    String errMsg = "Data too large, data for [test] would be";
    assertFailures(searchRequest, RestStatus.INTERNAL_SERVER_ERROR, containsString(errMsg));
    errMsg = "which is larger than the limit of [10/10b]";
    assertFailures(searchRequest, RestStatus.INTERNAL_SERVER_ERROR, containsString(errMsg));
    reset();
    // Adjust settings so the parent breaker will fail, but neither the fielddata breaker nor the node request breaker will fail
    resetSettings = Settings.builder().put(HierarchyCircuitBreakerService.TOTAL_CIRCUIT_BREAKER_LIMIT_SETTING.getKey(), "500b").put(HierarchyCircuitBreakerService.FIELDDATA_CIRCUIT_BREAKER_LIMIT_SETTING.getKey(), "90%").put(HierarchyCircuitBreakerService.FIELDDATA_CIRCUIT_BREAKER_OVERHEAD_SETTING.getKey(), 1.0).build();
    client.admin().cluster().prepareUpdateSettings().setTransientSettings(resetSettings).execute().actionGet();
    // Perform a search to load field data for the "test" field
    try {
        SearchResponse searchResponse = client.prepareSearch("cb-test").setQuery(matchAllQuery()).addSort("test", SortOrder.DESC).get();
        if (searchResponse.getShardFailures().length > 0) {
            // each shard must have failed with CircuitBreakingException
            for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) {
                Throwable cause = ExceptionsHelper.unwrap(shardSearchFailure.getCause(), CircuitBreakingException.class);
                assertThat(cause, instanceOf(CircuitBreakingException.class));
                assertEquals(((CircuitBreakingException) cause).getByteLimit(), 500L);
            }
        } else {
            fail("should have thrown a CircuitBreakingException");
        }
    } catch (Exception e) {
        Throwable cause = ExceptionsHelper.unwrap(e, CircuitBreakingException.class);
        assertThat(cause, instanceOf(CircuitBreakingException.class));
        assertEquals(((CircuitBreakingException) cause).getByteLimit(), 500L);
        assertThat("Exception: [" + cause.toString() + "] should be caused by the parent circuit breaker", cause.toString(), startsWith("CircuitBreakingException[[parent] Data too large"));
    }
    reset();
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings) BreakerSettings(org.elasticsearch.indices.breaker.BreakerSettings) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Aggregations

IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)227 ArrayList (java.util.ArrayList)134 SearchResponse (org.elasticsearch.action.search.SearchResponse)125 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)48 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)45 Matchers.containsString (org.hamcrest.Matchers.containsString)38 GeoPoint (org.elasticsearch.common.geo.GeoPoint)36 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)31 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)23 Settings (org.elasticsearch.common.settings.Settings)21 IOException (java.io.IOException)19 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)19 Client (org.elasticsearch.client.Client)18 SearchHit (org.elasticsearch.search.SearchHit)17 LinkedHashMap (java.util.LinkedHashMap)16 SearchHits (org.elasticsearch.search.SearchHits)16 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)15 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)15 CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)15 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)15