Search in sources :

Example 1 with UpdateRequestBuilder

use of org.elasticsearch.action.update.UpdateRequestBuilder in project elasticsearch by elastic.

the class UpdateIT method testConcurrentUpdateWithRetryOnConflict.

public void testConcurrentUpdateWithRetryOnConflict() throws Exception {
    final boolean useBulkApi = randomBoolean();
    createTestIndex();
    ensureGreen();
    int numberOfThreads = scaledRandomIntBetween(2, 5);
    final CountDownLatch latch = new CountDownLatch(numberOfThreads);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final int numberOfUpdatesPerThread = scaledRandomIntBetween(100, 500);
    final List<Exception> failures = new CopyOnWriteArrayList<>();
    for (int i = 0; i < numberOfThreads; i++) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    startLatch.await();
                    for (int i = 0; i < numberOfUpdatesPerThread; i++) {
                        if (i % 100 == 0) {
                            logger.debug("Client [{}] issued [{}] of [{}] requests", Thread.currentThread().getName(), i, numberOfUpdatesPerThread);
                        }
                        if (useBulkApi) {
                            UpdateRequestBuilder updateRequestBuilder = client().prepareUpdate(indexOrAlias(), "type1", Integer.toString(i)).setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).setRetryOnConflict(Integer.MAX_VALUE).setUpsert(jsonBuilder().startObject().field("field", 1).endObject());
                            client().prepareBulk().add(updateRequestBuilder).execute().actionGet();
                        } else {
                            client().prepareUpdate(indexOrAlias(), "type1", Integer.toString(i)).setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).setRetryOnConflict(Integer.MAX_VALUE).setUpsert(jsonBuilder().startObject().field("field", 1).endObject()).execute().actionGet();
                        }
                    }
                    logger.info("Client [{}] issued all [{}] requests.", Thread.currentThread().getName(), numberOfUpdatesPerThread);
                } catch (InterruptedException e) {
                    // test infrastructure kills long-running tests by interrupting them, thus we handle this case separately
                    logger.warn("Test was forcefully stopped. Client [{}] may still have outstanding requests.", Thread.currentThread().getName());
                    failures.add(e);
                    Thread.currentThread().interrupt();
                } catch (Exception e) {
                    failures.add(e);
                } finally {
                    latch.countDown();
                }
            }
        };
        Thread updater = new Thread(r);
        updater.setName("UpdateIT-Client-" + i);
        updater.start();
    }
    startLatch.countDown();
    latch.await();
    for (Throwable throwable : failures) {
        logger.info("Captured failure on concurrent update:", throwable);
    }
    assertThat(failures.size(), equalTo(0));
    for (int i = 0; i < numberOfUpdatesPerThread; i++) {
        GetResponse response = client().prepareGet("test", "type1", Integer.toString(i)).execute().actionGet();
        assertThat(response.getId(), equalTo(Integer.toString(i)));
        assertThat(response.isExists(), equalTo(true));
        assertThat(response.getVersion(), equalTo((long) numberOfThreads));
        assertThat((Integer) response.getSource().get("field"), equalTo(numberOfThreads));
    }
}
Also used : SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) GetResponse(org.elasticsearch.action.get.GetResponse) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with UpdateRequestBuilder

use of org.elasticsearch.action.update.UpdateRequestBuilder in project elasticsearch by elastic.

the class UpdateNoopIT method update.

private UpdateResponse update(Boolean detectNoop, long expectedVersion, XContentBuilder xContentBuilder) {
    UpdateRequestBuilder updateRequest = client().prepareUpdate("test", "type1", "1").setDoc(xContentBuilder).setDocAsUpsert(true).setFields("_source");
    if (detectNoop != null) {
        updateRequest.setDetectNoop(detectNoop);
    }
    UpdateResponse updateResponse = updateRequest.get();
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertEquals(expectedVersion, updateResponse.getVersion());
    return updateResponse;
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder)

Example 3 with UpdateRequestBuilder

use of org.elasticsearch.action.update.UpdateRequestBuilder in project elasticsearch by elastic.

the class MoreExpressionTests method testInvalidUpdateScript.

// test to make sure expressions are not allowed to be used as update scripts
public void testInvalidUpdateScript() throws Exception {
    try {
        createIndex("test_index");
        ensureGreen("test_index");
        indexRandom(true, client().prepareIndex("test_index", "doc", "1").setSource("text_field", "text"));
        UpdateRequestBuilder urb = client().prepareUpdate().setIndex("test_index");
        urb.setType("doc");
        urb.setId("1");
        urb.setScript(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "0", Collections.emptyMap()));
        urb.get();
        fail("Expression scripts should not be allowed to run as update scripts.");
    } catch (Exception e) {
        String message = e.getMessage();
        assertThat(message + " should have contained failed to execute", message.contains("failed to execute"), equalTo(true));
        message = e.getCause().getMessage();
        assertThat(message + " should have contained not supported", message.contains("not supported"), equalTo(true));
    }
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) CompiledScript(org.elasticsearch.script.CompiledScript) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) GeneralScriptException(org.elasticsearch.script.GeneralScriptException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException)

Example 4 with UpdateRequestBuilder

use of org.elasticsearch.action.update.UpdateRequestBuilder in project fess by codelibs.

the class EsAbstractBehavior method delegateBatchRequest.

protected <BUILDER> int[] delegateBatchRequest(final List<? extends Entity> entityList, Function<EsAbstractEntity, BUILDER> call) {
    @SuppressWarnings("unchecked") final BulkList<? extends Entity, BUILDER> bulkList = (BulkList<? extends Entity, BUILDER>) entityList;
    final RequestOptionCall<BUILDER> builderEntityCall = bulkList.getEntityCall();
    final BulkRequestBuilder bulkBuilder = client.prepareBulk();
    for (final Entity entity : entityList) {
        final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
        BUILDER builder = call.apply(esEntity);
        if (builder instanceof IndexRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((IndexRequestBuilder) builder);
        } else if (builder instanceof UpdateRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((UpdateRequestBuilder) builder);
        } else if (builder instanceof DeleteRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((DeleteRequestBuilder) builder);
        }
    }
    final RequestOptionCall<BulkRequestBuilder> builderCall = bulkList.getCall();
    if (builderCall != null) {
        builderCall.callback(bulkBuilder);
    }
    final BulkResponse response = bulkBuilder.execute().actionGet(bulkTimeout);
    final BulkItemResponse[] itemResponses = response.getItems();
    if (itemResponses.length != entityList.size()) {
        throw new IllegalStateException("Invalid response size: " + itemResponses.length + " != " + entityList.size());
    }
    final int[] results = new int[itemResponses.length];
    for (int i = 0; i < itemResponses.length; i++) {
        final BulkItemResponse itemResponse = itemResponses[i];
        final Entity entity = entityList.get(i);
        if (entity instanceof EsAbstractEntity) {
            ((EsAbstractEntity) entity).asDocMeta().id(itemResponse.getId());
        }
        results[i] = itemResponse.isFailed() ? 0 : 1;
    }
    return results;
}
Also used : DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) Entity(org.dbflute.Entity) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Example 5 with UpdateRequestBuilder

use of org.elasticsearch.action.update.UpdateRequestBuilder in project fess by codelibs.

the class EsAbstractBehavior method delegateBatchRequest.

protected <BUILDER> int[] delegateBatchRequest(final List<? extends Entity> entityList, Function<EsAbstractEntity, BUILDER> call) {
    @SuppressWarnings("unchecked") final BulkList<? extends Entity, BUILDER> bulkList = (BulkList<? extends Entity, BUILDER>) entityList;
    final RequestOptionCall<BUILDER> builderEntityCall = bulkList.getEntityCall();
    final BulkRequestBuilder bulkBuilder = client.prepareBulk();
    for (final Entity entity : entityList) {
        final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
        BUILDER builder = call.apply(esEntity);
        if (builder instanceof IndexRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((IndexRequestBuilder) builder);
        } else if (builder instanceof UpdateRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((UpdateRequestBuilder) builder);
        } else if (builder instanceof DeleteRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((DeleteRequestBuilder) builder);
        }
    }
    final RequestOptionCall<BulkRequestBuilder> builderCall = bulkList.getCall();
    if (builderCall != null) {
        builderCall.callback(bulkBuilder);
    }
    final BulkResponse response = bulkBuilder.execute().actionGet(bulkTimeout);
    final BulkItemResponse[] itemResponses = response.getItems();
    if (itemResponses.length != entityList.size()) {
        throw new IllegalStateException("Invalid response size: " + itemResponses.length + " != " + entityList.size());
    }
    final int[] results = new int[itemResponses.length];
    for (int i = 0; i < itemResponses.length; i++) {
        final BulkItemResponse itemResponse = itemResponses[i];
        final Entity entity = entityList.get(i);
        if (entity instanceof EsAbstractEntity) {
            ((EsAbstractEntity) entity).asDocMeta().id(itemResponse.getId());
        }
        results[i] = itemResponse.isFailed() ? 0 : 1;
    }
    return results;
}
Also used : DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) Entity(org.dbflute.Entity) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Aggregations

UpdateRequestBuilder (org.elasticsearch.action.update.UpdateRequestBuilder)9 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)4 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)4 Entity (org.dbflute.Entity)3 DeleteRequestBuilder (org.elasticsearch.action.delete.DeleteRequestBuilder)3 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)3 Script (org.elasticsearch.script.Script)3 IOException (java.io.IOException)2 GetResponse (org.elasticsearch.action.get.GetResponse)2 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)2 CompiledScript (org.elasticsearch.script.CompiledScript)2 TitanException (com.thinkaurelius.titan.core.TitanException)1 FileNotFoundException (java.io.FileNotFoundException)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 FessEsClientException (org.codelibs.fess.es.client.FessEsClientException)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)1 ActionRequestValidationException (org.elasticsearch.action.ActionRequestValidationException)1