Search in sources :

Example 6 with DocWriteResponse

use of org.elasticsearch.action.DocWriteResponse in project elasticsearch by elastic.

the class TransportShardBulkActionTests method testUpdateReplicaRequestWithSuccess.

public void testUpdateReplicaRequestWithSuccess() throws Exception {
    DocWriteRequest writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "field", "value");
    BulkItemRequest replicaRequest = new BulkItemRequest(0, writeRequest);
    boolean created = randomBoolean();
    Translog.Location resultLocation = new Translog.Location(42, 42, 42);
    Engine.IndexResult indexResult = new FakeResult(1, 1, created, resultLocation);
    DocWriteResponse indexResponse = new IndexResponse(shardId, "index", "id", 1, 1, created);
    BulkItemResultHolder goodResults = new BulkItemResultHolder(indexResponse, indexResult, replicaRequest);
    Translog.Location originalLocation = new Translog.Location(21, 21, 21);
    BulkItemRequest[] items = new BulkItemRequest[0];
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    Translog.Location newLocation = TransportShardBulkAction.updateReplicaRequest(goodResults, DocWriteRequest.OpType.INDEX, originalLocation, bulkShardRequest);
    BulkItemResponse primaryResponse = replicaRequest.getPrimaryResponse();
    // Check that the translog is successfully advanced
    assertThat(newLocation, equalTo(resultLocation));
    // Since this was not a conflict failure, the primary response
    // should be filled out with the failure information
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
    DocWriteResponse response = primaryResponse.getResponse();
    assertThat(response.status(), equalTo(created ? RestStatus.CREATED : RestStatus.OK));
}
Also used : DocWriteResponse(org.elasticsearch.action.DocWriteResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) Translog(org.elasticsearch.index.translog.Translog) IndexResponse(org.elasticsearch.action.index.IndexResponse) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Engine(org.elasticsearch.index.engine.Engine)

Example 7 with DocWriteResponse

use of org.elasticsearch.action.DocWriteResponse in project yacy_grid_mcp by yacy.

the class ElasticsearchClient method writeMapBulk.

/**
 * bulk message write
 * @param jsonMapList
 *            a list of json documents to be indexed
 * @param indexName
 *            the name of the index
 * @param typeName
 *            the type of the index
 * @return a list with error messages.
 *            The key is the id of the document, the value is an error string.
 *            The method was only successful if this list is empty.
 *            This must be a list, because keys may appear several times.
 */
public BulkWriteResult writeMapBulk(final String indexName, final List<BulkEntry> jsonMapList) {
    long start = System.currentTimeMillis();
    BulkRequestBuilder bulkRequest = elasticsearchClient.prepareBulk();
    for (BulkEntry be : jsonMapList) {
        if (be.id == null)
            continue;
        bulkRequest.add(elasticsearchClient.prepareIndex(indexName, be.type, be.id).setSource(be.jsonMap).setVersion(1).setCreate(// enforces OpType.INDEX
        false).setVersionType(VersionType.EXTERNAL_GTE));
    }
    BulkResponse bulkResponse = bulkRequest.get();
    BulkWriteResult result = new BulkWriteResult();
    for (BulkItemResponse r : bulkResponse.getItems()) {
        String id = r.getId();
        DocWriteResponse response = r.getResponse();
        if (response.getResult() == DocWriteResponse.Result.CREATED)
            result.created.add(id);
        String err = r.getFailureMessage();
        if (err != null) {
            result.errors.put(id, err);
        }
    }
    long duration = Math.max(1, System.currentTimeMillis() - start);
    long regulator = 0;
    int created = result.created.size();
    long ops = created * 1000 / duration;
    if (duration > throttling_time_threshold && ops < throttling_ops_threshold) {
        regulator = (long) (throttling_factor * duration);
        try {
            Thread.sleep(regulator);
        } catch (InterruptedException e) {
        }
    }
    Data.logger.info("elastic write bulk to index " + indexName + ": " + jsonMapList.size() + " entries, " + result.created.size() + " created, " + result.errors.size() + " errors, " + duration + " ms" + (regulator == 0 ? "" : ", throttled with " + regulator + " ms") + ", " + ops + " objects/second");
    return result;
}
Also used : DocWriteResponse(org.elasticsearch.action.DocWriteResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Example 8 with DocWriteResponse

use of org.elasticsearch.action.DocWriteResponse in project metron by apache.

the class ElasticsearchBulkDocumentWriterTest method setupElasticsearchToSucceed.

private void setupElasticsearchToSucceed() throws IOException {
    final String documentId = UUID.randomUUID().toString();
    final boolean isFailed = false;
    final int itemID = 0;
    // the write response will contain what is used as the document ID
    DocWriteResponse writeResponse = mock(DocWriteResponse.class);
    when(writeResponse.getId()).thenReturn(documentId);
    // define the item level response
    BulkItemResponse itemResponse = mock(BulkItemResponse.class);
    when(itemResponse.isFailed()).thenReturn(isFailed);
    when(itemResponse.getItemId()).thenReturn(itemID);
    when(itemResponse.getResponse()).thenReturn(writeResponse);
    List<BulkItemResponse> itemsResponses = Collections.singletonList(itemResponse);
    // define the bulk response to indicate success
    BulkResponse response = mock(BulkResponse.class);
    when(response.iterator()).thenReturn(itemsResponses.iterator());
    when(response.hasFailures()).thenReturn(isFailed);
    // have the client return the mock response
    when(highLevelClient.bulk(any(BulkRequest.class))).thenReturn(response);
}
Also used : DocWriteResponse(org.elasticsearch.action.DocWriteResponse) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse)

Aggregations

DocWriteResponse (org.elasticsearch.action.DocWriteResponse)8 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)4 Engine (org.elasticsearch.index.engine.Engine)3 Translog (org.elasticsearch.index.translog.Translog)3 IOException (java.io.IOException)2 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)2 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)2 IndexResponse (org.elasticsearch.action.index.IndexResponse)2 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)2 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)2 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)2 LongSupplier (java.util.function.LongSupplier)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)1 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)1 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)1 ShardInfo (org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)1 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)1