Search in sources :

Example 51 with ElasticsearchException

use of org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class IndexWithShadowReplicasIT method testPrimaryRelocationWhereRecoveryFails.

public void testPrimaryRelocationWhereRecoveryFails() throws Exception {
    Path dataPath = createTempDir();
    Settings nodeSettings = Settings.builder().put("node.add_lock_id_to_custom_path", false).put(Environment.PATH_SHARED_DATA_SETTING.getKey(), dataPath).build();
    String node1 = internalCluster().startNode(nodeSettings);
    final String IDX = "test";
    Settings idxSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).put(IndexMetaData.SETTING_DATA_PATH, dataPath.toAbsolutePath().toString()).put(IndexMetaData.SETTING_SHADOW_REPLICAS, true).put(IndexMetaData.SETTING_SHARED_FILESYSTEM, true).build();
    prepareCreate(IDX).setSettings(idxSettings).addMapping("doc", "foo", "type=text").get();
    // Node1 has the primary, now node2 has the replica
    String node2 = internalCluster().startNode(nodeSettings);
    ensureGreen(IDX);
    flushAndRefresh(IDX);
    String node3 = internalCluster().startNode(nodeSettings);
    final AtomicInteger counter = new AtomicInteger(0);
    final CountDownLatch started = new CountDownLatch(1);
    final int numPhase1Docs = scaledRandomIntBetween(25, 200);
    final int numPhase2Docs = scaledRandomIntBetween(25, 200);
    final int numPhase3Docs = scaledRandomIntBetween(25, 200);
    final CountDownLatch phase1finished = new CountDownLatch(1);
    final CountDownLatch phase2finished = new CountDownLatch(1);
    final CountDownLatch phase3finished = new CountDownLatch(1);
    final AtomicBoolean keepFailing = new AtomicBoolean(true);
    MockTransportService mockTransportService = ((MockTransportService) internalCluster().getInstance(TransportService.class, node1));
    mockTransportService.addDelegate(internalCluster().getInstance(TransportService.class, node3), new MockTransportService.DelegateTransport(mockTransportService.original()) {

        @Override
        protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
            if (keepFailing.get() && action.equals(PeerRecoveryTargetService.Actions.TRANSLOG_OPS)) {
                logger.info("--> failing translog ops");
                throw new ElasticsearchException("failing on purpose");
            }
            super.sendRequest(connection, requestId, action, request, options);
        }
    });
    Thread thread = new Thread() {

        @Override
        public void run() {
            started.countDown();
            while (counter.get() < (numPhase1Docs + numPhase2Docs + numPhase3Docs)) {
                final IndexResponse indexResponse = client().prepareIndex(IDX, "doc", Integer.toString(counter.incrementAndGet())).setSource("foo", "bar").get();
                assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
                final int docCount = counter.get();
                if (docCount == numPhase1Docs) {
                    phase1finished.countDown();
                } else if (docCount == (numPhase1Docs + numPhase2Docs)) {
                    phase2finished.countDown();
                }
            }
            logger.info("--> stopping indexing thread");
            phase3finished.countDown();
        }
    };
    thread.start();
    started.await();
    // wait for a certain number of documents to be indexed
    phase1finished.await();
    logger.info("--> excluding {} from allocation", node1);
    // now prevent primary from being allocated on node 1 move to node_3
    Settings build = Settings.builder().put("index.routing.allocation.exclude._name", node1).build();
    client().admin().indices().prepareUpdateSettings(IDX).setSettings(build).execute().actionGet();
    // wait for more documents to be indexed post-recovery, also waits for
    // indexing thread to stop
    phase2finished.await();
    // stop failing
    keepFailing.set(false);
    // wait for more docs to be indexed
    phase3finished.await();
    ensureGreen(IDX);
    thread.join();
    logger.info("--> performing query");
    flushAndRefresh();
    SearchResponse resp = client().prepareSearch(IDX).setQuery(matchAllQuery()).get();
    assertHitCount(resp, counter.get());
}
Also used : Path(java.nio.file.Path) TransportRequest(org.elasticsearch.transport.TransportRequest) MockTransportService(org.elasticsearch.test.transport.MockTransportService) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) CountDownLatch(java.util.concurrent.CountDownLatch) SearchResponse(org.elasticsearch.action.search.SearchResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) IndexResponse(org.elasticsearch.action.index.IndexResponse) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Settings(org.elasticsearch.common.settings.Settings)

Example 52 with ElasticsearchException

use of org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class TokenCountFieldMapperIntegrationIT method assertSearchReturns.

private void assertSearchReturns(SearchResponse result, String... ids) {
    assertThat(result.getHits().getTotalHits(), equalTo((long) ids.length));
    assertThat(result.getHits().getHits().length, equalTo(ids.length));
    List<String> foundIds = new ArrayList<>();
    for (SearchHit hit : result.getHits()) {
        foundIds.add(hit.getId());
    }
    assertThat(foundIds, containsInAnyOrder(ids));
    for (SearchHit hit : result.getHits()) {
        String id = hit.getId();
        if (id.equals("single")) {
            assertSearchHit(hit, 4);
        } else if (id.equals("bulk1")) {
            assertSearchHit(hit, 3);
        } else if (id.equals("bulk2")) {
            assertSearchHit(hit, 5);
        } else if (id.equals("multi")) {
            assertSearchHit(hit, 2, 7);
        } else if (id.equals("multibulk1")) {
            assertSearchHit(hit, 1, 8);
        } else if (id.equals("multibulk2")) {
            assertSearchHit(hit, 6, 10);
        } else {
            throw new ElasticsearchException("Unexpected response!");
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 53 with ElasticsearchException

use of org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class MoreLikeThisQueryBuilderTests method randomArtificialDoc.

private XContentBuilder randomArtificialDoc() {
    XContentBuilder doc;
    try {
        doc = XContentFactory.jsonBuilder().startObject();
        for (String field : randomFields) {
            doc.field(field, randomAsciiOfLength(10));
        }
        doc.endObject();
    } catch (IOException e) {
        throw new ElasticsearchException("Unable to generate random artificial doc!");
    }
    return doc;
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 54 with ElasticsearchException

use of org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class GeoShapeQueryBuilderTests method executeGet.

@Override
protected GetResponse executeGet(GetRequest getRequest) {
    assertThat(indexedShapeToReturn, notNullValue());
    assertThat(indexedShapeId, notNullValue());
    assertThat(indexedShapeType, notNullValue());
    assertThat(getRequest.id(), equalTo(indexedShapeId));
    assertThat(getRequest.type(), equalTo(indexedShapeType));
    String expectedShapeIndex = indexedShapeIndex == null ? GeoShapeQueryBuilder.DEFAULT_SHAPE_INDEX_NAME : indexedShapeIndex;
    assertThat(getRequest.index(), equalTo(expectedShapeIndex));
    String expectedShapePath = indexedShapePath == null ? GeoShapeQueryBuilder.DEFAULT_SHAPE_FIELD_NAME : indexedShapePath;
    String json;
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        builder.field(expectedShapePath, indexedShapeToReturn);
        builder.endObject();
        json = builder.string();
    } catch (IOException ex) {
        throw new ElasticsearchException("boom", ex);
    }
    return new GetResponse(new GetResult(indexedShapeIndex, indexedShapeType, indexedShapeId, 0, true, new BytesArray(json), null));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) GetResult(org.elasticsearch.index.get.GetResult) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 55 with ElasticsearchException

use of org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class TermsQueryBuilderTests method executeGet.

@Override
public GetResponse executeGet(GetRequest getRequest) {
    String json;
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        builder.array(termsPath, randomTerms.toArray(new Object[randomTerms.size()]));
        builder.endObject();
        json = builder.string();
    } catch (IOException ex) {
        throw new ElasticsearchException("boom", ex);
    }
    return new GetResponse(new GetResult(getRequest.index(), getRequest.type(), getRequest.id(), 0, true, new BytesArray(json), null));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) GetResult(org.elasticsearch.index.get.GetResult) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

ElasticsearchException (org.elasticsearch.ElasticsearchException)309 IOException (java.io.IOException)127 Settings (org.elasticsearch.common.settings.Settings)32 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)30 HashMap (java.util.HashMap)29 ClusterState (org.elasticsearch.cluster.ClusterState)29 ArrayList (java.util.ArrayList)28 Matchers.containsString (org.hamcrest.Matchers.containsString)25 List (java.util.List)20 Map (java.util.Map)20 AtomicReference (java.util.concurrent.atomic.AtomicReference)20 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)18 XContentParser (org.elasticsearch.common.xcontent.XContentParser)17 Path (java.nio.file.Path)16 Test (org.junit.Test)16 ActionListener (org.elasticsearch.action.ActionListener)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Version (org.elasticsearch.Version)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)13