Search in sources :

Example 16 with IndexResponse

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

the class IndexLevelReplicationTests method testInheritMaxValidAutoIDTimestampOnRecovery.

public void testInheritMaxValidAutoIDTimestampOnRecovery() throws Exception {
    try (ReplicationGroup shards = createGroup(0)) {
        shards.startAll();
        final IndexRequest indexRequest = new IndexRequest(index.getName(), "type").source("{}", XContentType.JSON);
        // force an update of the timestamp
        indexRequest.onRetry();
        final IndexResponse response = shards.index(indexRequest);
        assertEquals(DocWriteResponse.Result.CREATED, response.getResult());
        if (randomBoolean()) {
            // lets check if that also happens if no translog record is replicated
            shards.flush();
        }
        IndexShard replica = shards.addReplica();
        shards.recoverReplica(replica);
        SegmentsStats segmentsStats = replica.segmentStats(false);
        SegmentsStats primarySegmentStats = shards.getPrimary().segmentStats(false);
        assertNotEquals(IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, primarySegmentStats.getMaxUnsafeAutoIdTimestamp());
        assertEquals(primarySegmentStats.getMaxUnsafeAutoIdTimestamp(), segmentsStats.getMaxUnsafeAutoIdTimestamp());
        assertNotEquals(Long.MAX_VALUE, segmentsStats.getMaxUnsafeAutoIdTimestamp());
    }
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) IndexShard(org.elasticsearch.index.shard.IndexShard) IndexRequest(org.elasticsearch.action.index.IndexRequest) SegmentsStats(org.elasticsearch.index.engine.SegmentsStats)

Example 17 with IndexResponse

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

the class RecoveryDuringReplicationTests method testRecoveryAfterPrimaryPromotion.

@TestLogging("org.elasticsearch.index.shard:TRACE,org.elasticsearch.indices.recovery:TRACE")
public void testRecoveryAfterPrimaryPromotion() throws Exception {
    try (ReplicationGroup shards = createGroup(2)) {
        shards.startAll();
        int totalDocs = shards.indexDocs(randomInt(10));
        int committedDocs = 0;
        if (randomBoolean()) {
            shards.flush();
            committedDocs = totalDocs;
        }
        // we need some indexing to happen to transfer local checkpoint information to the primary
        // so it can update the global checkpoint and communicate to replicas
        boolean expectSeqNoRecovery = totalDocs > 0;
        final IndexShard oldPrimary = shards.getPrimary();
        final IndexShard newPrimary = shards.getReplicas().get(0);
        final IndexShard replica = shards.getReplicas().get(1);
        if (randomBoolean()) {
            // simulate docs that were inflight when primary failed, these will be rolled back
            final int rollbackDocs = randomIntBetween(1, 5);
            logger.info("--> indexing {} rollback docs", rollbackDocs);
            for (int i = 0; i < rollbackDocs; i++) {
                final IndexRequest indexRequest = new IndexRequest(index.getName(), "type", "rollback_" + i).source("{}", XContentType.JSON);
                final IndexResponse primaryResponse = indexOnPrimary(indexRequest, oldPrimary);
                indexOnReplica(primaryResponse, indexRequest, replica);
            }
            if (randomBoolean()) {
                oldPrimary.flush(new FlushRequest(index.getName()));
                expectSeqNoRecovery = false;
            }
        }
        shards.promoteReplicaToPrimary(newPrimary);
        // index some more
        totalDocs += shards.indexDocs(randomIntBetween(0, 5));
        oldPrimary.close("demoted", false);
        oldPrimary.store().close();
        IndexShard newReplica = shards.addReplicaWithExistingPath(oldPrimary.shardPath(), oldPrimary.routingEntry().currentNodeId());
        shards.recoverReplica(newReplica);
        if (expectSeqNoRecovery) {
            assertThat(newReplica.recoveryState().getIndex().fileDetails(), empty());
            assertThat(newReplica.recoveryState().getTranslog().recoveredOperations(), equalTo(totalDocs - committedDocs));
        } else {
            assertThat(newReplica.recoveryState().getIndex().fileDetails(), not(empty()));
            assertThat(newReplica.recoveryState().getTranslog().recoveredOperations(), equalTo(totalDocs - committedDocs));
        }
        shards.removeReplica(replica);
        replica.close("resync", false);
        replica.store().close();
        newReplica = shards.addReplicaWithExistingPath(replica.shardPath(), replica.routingEntry().currentNodeId());
        shards.recoverReplica(newReplica);
        shards.assertAllEqual(totalDocs);
    }
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) IndexShard(org.elasticsearch.index.shard.IndexShard) IndexRequest(org.elasticsearch.action.index.IndexRequest) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Example 18 with IndexResponse

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

the class IndexActionIT method testCreatedFlagWithFlush.

public void testCreatedFlagWithFlush() throws Exception {
    createIndex("test");
    ensureGreen();
    IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").execute().actionGet();
    assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
    client().prepareDelete("test", "type", "1").execute().actionGet();
    flush();
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_2").execute().actionGet();
    assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 19 with IndexResponse

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

the class IndexActionIT method testCreatedFlag.

public void testCreatedFlag() throws Exception {
    createIndex("test");
    ensureGreen();
    IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").execute().actionGet();
    assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_2").execute().actionGet();
    assertEquals(DocWriteResponse.Result.UPDATED, indexResponse.getResult());
    client().prepareDelete("test", "type", "1").execute().actionGet();
    indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_2").execute().actionGet();
    assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 20 with IndexResponse

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

the class IngestClientIT method testBulkWithIngestFailures.

public void testBulkWithIngestFailures() throws Exception {
    createIndex("index");
    BytesReference source = jsonBuilder().startObject().field("description", "my_pipeline").startArray("processors").startObject().startObject("test").endObject().endObject().endArray().endObject().bytes();
    PutPipelineRequest putPipelineRequest = new PutPipelineRequest("_id", source, XContentType.JSON);
    client().admin().cluster().putPipeline(putPipelineRequest).get();
    int numRequests = scaledRandomIntBetween(32, 128);
    BulkRequest bulkRequest = new BulkRequest();
    for (int i = 0; i < numRequests; i++) {
        IndexRequest indexRequest = new IndexRequest("index", "type", Integer.toString(i)).setPipeline("_id");
        indexRequest.source(Requests.INDEX_CONTENT_TYPE, "field", "value", "fail", i % 2 == 0);
        bulkRequest.add(indexRequest);
    }
    BulkResponse response = client().bulk(bulkRequest).actionGet();
    assertThat(response.getItems().length, equalTo(bulkRequest.requests().size()));
    for (int i = 0; i < bulkRequest.requests().size(); i++) {
        BulkItemResponse itemResponse = response.getItems()[i];
        if (i % 2 == 0) {
            BulkItemResponse.Failure failure = itemResponse.getFailure();
            ElasticsearchException compoundProcessorException = (ElasticsearchException) failure.getCause();
            assertThat(compoundProcessorException.getRootCause().getMessage(), equalTo("test processor failed"));
        } else {
            IndexResponse indexResponse = itemResponse.getResponse();
            assertThat("Expected a successful response but found failure [" + itemResponse.getFailure() + "].", itemResponse.isFailed(), is(false));
            assertThat(indexResponse, notNullValue());
            assertThat(indexResponse.getId(), equalTo(Integer.toString(i)));
            assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexResponse(org.elasticsearch.action.index.IndexResponse) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) PutPipelineRequest(org.elasticsearch.action.ingest.PutPipelineRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Aggregations

IndexResponse (org.elasticsearch.action.index.IndexResponse)76 Test (org.junit.Test)15 SearchResponse (org.elasticsearch.action.search.SearchResponse)13 IOException (java.io.IOException)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)10 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)10 IndexRequest (org.elasticsearch.action.index.IndexRequest)10 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ArrayList (java.util.ArrayList)5 ExecutionException (java.util.concurrent.ExecutionException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 GetResponse (org.elasticsearch.action.get.GetResponse)4 Settings (org.elasticsearch.common.settings.Settings)4 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)3