Search in sources :

Example 26 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.

the class IndicesRequestIT method testBulk.

public void testBulk() {
    String[] bulkShardActions = new String[] { BulkAction.NAME + "[s][p]", BulkAction.NAME + "[s][r]" };
    interceptTransportActions(bulkShardActions);
    List<String> indices = new ArrayList<>();
    BulkRequest bulkRequest = new BulkRequest();
    int numIndexRequests = iterations(1, 10);
    for (int i = 0; i < numIndexRequests; i++) {
        String indexOrAlias = randomIndexOrAlias();
        bulkRequest.add(new IndexRequest(indexOrAlias, "type", "id").source(Requests.INDEX_CONTENT_TYPE, "field", "value"));
        indices.add(indexOrAlias);
    }
    int numDeleteRequests = iterations(1, 10);
    for (int i = 0; i < numDeleteRequests; i++) {
        String indexOrAlias = randomIndexOrAlias();
        bulkRequest.add(new DeleteRequest(indexOrAlias, "type", "id"));
        indices.add(indexOrAlias);
    }
    int numUpdateRequests = iterations(1, 10);
    for (int i = 0; i < numUpdateRequests; i++) {
        String indexOrAlias = randomIndexOrAlias();
        bulkRequest.add(new UpdateRequest(indexOrAlias, "type", "id").doc(Requests.INDEX_CONTENT_TYPE, "field1", "value1"));
        indices.add(indexOrAlias);
    }
    internalCluster().coordOnlyNodeClient().bulk(bulkRequest).actionGet();
    clearInterceptedActions();
    assertIndicesSubset(indices, bulkShardActions);
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ArrayList(java.util.ArrayList) IndexRequest(org.elasticsearch.action.index.IndexRequest) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 27 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.

the class UpdateIT method testStressUpdateDeleteConcurrency.

public void testStressUpdateDeleteConcurrency() throws Exception {
    //We create an index with merging disabled so that deletes don't get merged away
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(MergePolicyConfig.INDEX_MERGE_ENABLED, false)));
    ensureGreen();
    final int numberOfThreads = scaledRandomIntBetween(3, 5);
    final int numberOfIdsPerThread = scaledRandomIntBetween(3, 10);
    final int numberOfUpdatesPerId = scaledRandomIntBetween(10, 100);
    final int retryOnConflict = randomIntBetween(0, 1);
    final CountDownLatch latch = new CountDownLatch(numberOfThreads);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final List<Throwable> failures = new CopyOnWriteArrayList<>();
    final class UpdateThread extends Thread {

        final Map<Integer, Integer> failedMap = new HashMap<>();

        final int numberOfIds;

        final int updatesPerId;

        final int maxUpdateRequests = numberOfIdsPerThread * numberOfUpdatesPerId;

        final int maxDeleteRequests = numberOfIdsPerThread * numberOfUpdatesPerId;

        private final Semaphore updateRequestsOutstanding = new Semaphore(maxUpdateRequests);

        private final Semaphore deleteRequestsOutstanding = new Semaphore(maxDeleteRequests);

        UpdateThread(int numberOfIds, int updatesPerId) {
            this.numberOfIds = numberOfIds;
            this.updatesPerId = updatesPerId;
        }

        final class UpdateListener implements ActionListener<UpdateResponse> {

            int id;

            UpdateListener(int id) {
                this.id = id;
            }

            @Override
            public void onResponse(UpdateResponse updateResponse) {
                updateRequestsOutstanding.release(1);
            }

            @Override
            public void onFailure(Exception e) {
                synchronized (failedMap) {
                    incrementMapValue(id, failedMap);
                }
                updateRequestsOutstanding.release(1);
            }
        }

        final class DeleteListener implements ActionListener<DeleteResponse> {

            int id;

            DeleteListener(int id) {
                this.id = id;
            }

            @Override
            public void onResponse(DeleteResponse deleteResponse) {
                deleteRequestsOutstanding.release(1);
            }

            @Override
            public void onFailure(Exception e) {
                synchronized (failedMap) {
                    incrementMapValue(id, failedMap);
                }
                deleteRequestsOutstanding.release(1);
            }
        }

        @Override
        public void run() {
            try {
                startLatch.await();
                boolean hasWaitedForNoNode = false;
                for (int j = 0; j < numberOfIds; j++) {
                    for (int k = 0; k < numberOfUpdatesPerId; ++k) {
                        updateRequestsOutstanding.acquire();
                        try {
                            UpdateRequest ur = client().prepareUpdate("test", "type1", Integer.toString(j)).setScript(new Script(ScriptType.INLINE, "field_inc", "field", Collections.emptyMap())).setRetryOnConflict(retryOnConflict).setUpsert(jsonBuilder().startObject().field("field", 1).endObject()).request();
                            client().update(ur, new UpdateListener(j));
                        } catch (NoNodeAvailableException nne) {
                            updateRequestsOutstanding.release();
                            synchronized (failedMap) {
                                incrementMapValue(j, failedMap);
                            }
                            if (hasWaitedForNoNode) {
                                throw nne;
                            }
                            logger.warn("Got NoNodeException waiting for 1 second for things to recover.");
                            hasWaitedForNoNode = true;
                            Thread.sleep(1000);
                        }
                        try {
                            deleteRequestsOutstanding.acquire();
                            DeleteRequest dr = client().prepareDelete("test", "type1", Integer.toString(j)).request();
                            client().delete(dr, new DeleteListener(j));
                        } catch (NoNodeAvailableException nne) {
                            deleteRequestsOutstanding.release();
                            synchronized (failedMap) {
                                incrementMapValue(j, failedMap);
                            }
                            if (hasWaitedForNoNode) {
                                throw nne;
                            }
                            logger.warn("Got NoNodeException waiting for 1 second for things to recover.");
                            hasWaitedForNoNode = true;
                            //Wait for no-node to clear
                            Thread.sleep(1000);
                        }
                    }
                }
            } catch (Exception e) {
                logger.error("Something went wrong", e);
                failures.add(e);
            } finally {
                try {
                    waitForOutstandingRequests(TimeValue.timeValueSeconds(60), updateRequestsOutstanding, maxUpdateRequests, "Update");
                    waitForOutstandingRequests(TimeValue.timeValueSeconds(60), deleteRequestsOutstanding, maxDeleteRequests, "Delete");
                } catch (ElasticsearchTimeoutException ete) {
                    failures.add(ete);
                }
                latch.countDown();
            }
        }

        private void incrementMapValue(int j, Map<Integer, Integer> map) {
            if (!map.containsKey(j)) {
                map.put(j, 0);
            }
            map.put(j, map.get(j) + 1);
        }

        private void waitForOutstandingRequests(TimeValue timeOut, Semaphore requestsOutstanding, int maxRequests, String name) {
            long start = System.currentTimeMillis();
            do {
                long msRemaining = timeOut.getMillis() - (System.currentTimeMillis() - start);
                logger.info("[{}] going to try and acquire [{}] in [{}]ms [{}] available to acquire right now", name, maxRequests, msRemaining, requestsOutstanding.availablePermits());
                try {
                    requestsOutstanding.tryAcquire(maxRequests, msRemaining, TimeUnit.MILLISECONDS);
                    return;
                } catch (InterruptedException ie) {
                //Just keep swimming
                }
            } while ((System.currentTimeMillis() - start) < timeOut.getMillis());
            throw new ElasticsearchTimeoutException("Requests were still outstanding after the timeout [" + timeOut + "] for type [" + name + "]");
        }
    }
    final List<UpdateThread> threads = new ArrayList<>();
    for (int i = 0; i < numberOfThreads; i++) {
        UpdateThread ut = new UpdateThread(numberOfIdsPerThread, numberOfUpdatesPerId);
        ut.start();
        threads.add(ut);
    }
    startLatch.countDown();
    latch.await();
    for (UpdateThread ut : threads) {
        //Threads should have finished because of the latch.await
        ut.join();
    }
    //aquiring the request outstanding semaphores.
    for (Throwable throwable : failures) {
        logger.info("Captured failure on concurrent update:", throwable);
    }
    assertThat(failures.size(), equalTo(0));
    //All the previous operations should be complete or failed at this point
    for (int i = 0; i < numberOfIdsPerThread; ++i) {
        UpdateResponse ur = client().prepareUpdate("test", "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();
    }
    refresh();
    for (int i = 0; i < numberOfIdsPerThread; ++i) {
        int totalFailures = 0;
        GetResponse response = client().prepareGet("test", "type1", Integer.toString(i)).execute().actionGet();
        if (response.isExists()) {
            assertThat(response.getId(), equalTo(Integer.toString(i)));
            int expectedVersion = (numberOfThreads * numberOfUpdatesPerId * 2) + 1;
            for (UpdateThread ut : threads) {
                if (ut.failedMap.containsKey(i)) {
                    totalFailures += ut.failedMap.get(i);
                }
            }
            expectedVersion -= totalFailures;
            logger.error("Actual version [{}] Expected version [{}] Total failures [{}]", response.getVersion(), expectedVersion, totalFailures);
            assertThat(response.getVersion(), equalTo((long) expectedVersion));
            assertThat(response.getVersion() + totalFailures, equalTo((long) ((numberOfUpdatesPerId * numberOfThreads * 2) + 1)));
        }
    }
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) Matchers.containsString(org.hamcrest.Matchers.containsString) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) CountDownLatch(java.util.concurrent.CountDownLatch) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) 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) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) Map(java.util.Map) HashMap(java.util.HashMap) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 28 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch-jdbc by jprante.

the class StandardSink method delete.

@Override
public void delete(IndexableObject object) {
    if (clientAPI == null) {
        return;
    }
    if (Strings.hasLength(object.index())) {
        this.index = object.index();
    }
    if (Strings.hasLength(object.type())) {
        this.type = object.type();
    }
    if (Strings.hasLength(object.id())) {
        setId(object.id());
    }
    if (getId() == null) {
        // skip if no doc is specified to delete
        return;
    }
    DeleteRequest request = Requests.deleteRequest(this.index).type(this.type).id(getId());
    if (object.meta(ControlKeys._version.name()) != null) {
        request.versionType(VersionType.EXTERNAL).version(Long.parseLong(object.meta(ControlKeys._version.name())));
    }
    if (object.meta(ControlKeys._routing.name()) != null) {
        request.routing(object.meta(ControlKeys._routing.name()));
    }
    if (object.meta(ControlKeys._parent.name()) != null) {
        request.parent(object.meta(ControlKeys._parent.name()));
    }
    if (logger.isTraceEnabled()) {
        logger.trace("adding bulk delete action {}/{}/{}", request.index(), request.type(), request.id());
    }
    clientAPI.bulkDelete(request);
}
Also used : DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 29 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project camel by apache.

the class ElasticsearchGetSearchDeleteExistsUpdateTest method deleteRequestBody.

@Test
public void deleteRequestBody() throws Exception {
    String prefix = createPrefix();
    // given
    DeleteRequest request = new DeleteRequest(prefix + "foo").type(prefix + "bar");
    // when
    String documentId = template.requestBody("direct:index", new IndexRequest("" + prefix + "foo", "" + prefix + "bar", "" + prefix + "testId").source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class);
    DeleteResponse response = template.requestBody("direct:delete", request.id(documentId), DeleteResponse.class);
    // then
    assertThat(response, notNullValue());
    assertThat(documentId, equalTo(response.getId()));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) Test(org.junit.Test)

Example 30 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project camel by apache.

the class ElasticsearchProducer method process.

public void process(Exchange exchange) throws Exception {
    // 2. Index and type will be set by:
    // a. If the incoming body is already an action request
    // b. If the body is not an action request we will use headers if they
    // are set.
    // c. If the body is not an action request and the headers aren't set we
    // will use the configuration.
    // No error is thrown by the component in the event none of the above
    // conditions are met. The java es client
    // will throw.
    Message message = exchange.getIn();
    final ElasticsearchOperation operation = resolveOperation(exchange);
    // Set the index/type headers on the exchange if necessary. This is used
    // for type conversion.
    boolean configIndexName = false;
    String indexName = message.getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class);
    if (indexName == null) {
        message.setHeader(ElasticsearchConstants.PARAM_INDEX_NAME, configuration.getIndexName());
        configIndexName = true;
    }
    boolean configIndexType = false;
    String indexType = message.getHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, String.class);
    if (indexType == null) {
        message.setHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, configuration.getIndexType());
        configIndexType = true;
    }
    boolean configWaitForActiveShards = false;
    Integer waitForActiveShards = message.getHeader(ElasticsearchConstants.PARAM_WAIT_FOR_ACTIVE_SHARDS, Integer.class);
    if (waitForActiveShards == null) {
        message.setHeader(ElasticsearchConstants.PARAM_WAIT_FOR_ACTIVE_SHARDS, configuration.getWaitForActiveShards());
        configWaitForActiveShards = true;
    }
    if (operation == ElasticsearchOperation.INDEX) {
        IndexRequest indexRequest = message.getBody(IndexRequest.class);
        message.setBody(client.index(indexRequest).actionGet().getId());
    } else if (operation == ElasticsearchOperation.UPDATE) {
        UpdateRequest updateRequest = message.getBody(UpdateRequest.class);
        message.setBody(client.update(updateRequest).actionGet().getId());
    } else if (operation == ElasticsearchOperation.GET_BY_ID) {
        GetRequest getRequest = message.getBody(GetRequest.class);
        message.setBody(client.get(getRequest));
    } else if (operation == ElasticsearchOperation.MULTIGET) {
        MultiGetRequest multiGetRequest = message.getBody(MultiGetRequest.class);
        message.setBody(client.multiGet(multiGetRequest));
    } else if (operation == ElasticsearchOperation.BULK) {
        BulkRequest bulkRequest = message.getBody(BulkRequest.class);
        message.setBody(client.bulk(bulkRequest).actionGet());
    } else if (operation == ElasticsearchOperation.BULK_INDEX) {
        BulkRequest bulkRequest = message.getBody(BulkRequest.class);
        List<String> indexedIds = new ArrayList<String>();
        for (BulkItemResponse response : client.bulk(bulkRequest).actionGet().getItems()) {
            indexedIds.add(response.getId());
        }
        message.setBody(indexedIds);
    } else if (operation == ElasticsearchOperation.DELETE) {
        DeleteRequest deleteRequest = message.getBody(DeleteRequest.class);
        message.setBody(client.delete(deleteRequest).actionGet());
    } else if (operation == ElasticsearchOperation.EXISTS) {
        // ExistsRequest API is deprecated, using SearchRequest instead with size=0 and terminate_after=1
        SearchRequest searchRequest = new SearchRequest(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class));
        try {
            client.prepareSearch(searchRequest.indices()).setSize(0).setTerminateAfter(1).get();
            message.setBody(true);
        } catch (IndexNotFoundException e) {
            message.setBody(false);
        }
    } else if (operation == ElasticsearchOperation.SEARCH) {
        SearchRequest searchRequest = message.getBody(SearchRequest.class);
        message.setBody(client.search(searchRequest).actionGet());
    } else if (operation == ElasticsearchOperation.MULTISEARCH) {
        MultiSearchRequest multiSearchRequest = message.getBody(MultiSearchRequest.class);
        message.setBody(client.multiSearch(multiSearchRequest));
    } else if (operation == ElasticsearchOperation.DELETE_INDEX) {
        DeleteIndexRequest deleteIndexRequest = message.getBody(DeleteIndexRequest.class);
        message.setBody(client.admin().indices().delete(deleteIndexRequest).actionGet());
    } else {
        throw new IllegalArgumentException(ElasticsearchConstants.PARAM_OPERATION + " value '" + operation + "' is not supported");
    }
    // subsequent endpoint index/type with the first endpoint index/type.
    if (configIndexName) {
        message.removeHeader(ElasticsearchConstants.PARAM_INDEX_NAME);
    }
    if (configIndexType) {
        message.removeHeader(ElasticsearchConstants.PARAM_INDEX_TYPE);
    }
    if (configWaitForActiveShards) {
        message.removeHeader(ElasticsearchConstants.PARAM_WAIT_FOR_ACTIVE_SHARDS);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) Message(org.apache.camel.Message) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) GetRequest(org.elasticsearch.action.get.GetRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) ArrayList(java.util.ArrayList) List(java.util.List) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Aggregations

DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)33 IndexRequest (org.elasticsearch.action.index.IndexRequest)25 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)18 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)8 IOException (java.io.IOException)7 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)7 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)7 ArrayList (java.util.ArrayList)5 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 GetRequest (org.elasticsearch.action.get.GetRequest)4 BytesReference (org.elasticsearch.common.bytes.BytesReference)4 XContentType (org.elasticsearch.common.xcontent.XContentType)4 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)3 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)3 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)3 TitanException (com.thinkaurelius.titan.core.TitanException)2 FileNotFoundException (java.io.FileNotFoundException)2