# The latch in BulkRequestHandler.execute is unnecessary

**URL:** <https://discuss.elastic.co/t/the-latch-in-bulkrequesthandler-execute-is-unnecessary/296570>\
**Category:** Elasticsearch\
**Created:** [February 8, 2022, 9:45am UTC](https://discuss.elastic.co/t/the-latch-in-bulkrequesthandler-execute-is-unnecessary/296570 "2022-02-08T09:45:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![asce0705](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asce0705/32/81317_2.png) [@asce0705](https://discuss.elastic.co/u/asce0705)\
**Post date:** [February 8, 2022, 9:45am UTC](https://discuss.elastic.co/t/the-latch-in-bulkrequesthandler-execute-is-unnecessary/296570/1 "2022-02-08T09:45:15Z")

</div>

The latch in BulkRequestHandler is added before to ensure that the new handler executes a bulk request and awaits for the completion if the BulkProcessor was configured with a concurrentRequests setting of 0. The related issue is [Simplify BulkProcessor handling and retry logic](https://github.com/elastic/elasticsearch/pull/24051).  
However, the only code that currently calls into this code, which is blocked by `synchronized`  
methods, is removed by [this change](https://github.com/elastic/elasticsearch/pull/41451).  
The latch is now unnecessary in this handler. Shall we consider removing this logic and simplify the execute method?

```auto
    public void execute(BulkRequest bulkRequest, long executionId) {
        ......
        semaphore.acquire();
        toRelease = semaphore::release;
        CountDownLatch latch = new CountDownLatch(1);
        retry.withBackoff(consumer, bulkRequest, ActionListener.runAfter(new ActionListener<BulkResponse>() {
            @Override
            public void onResponse(BulkResponse response) {
                listener.afterBulk(executionId, bulkRequest, response);
            }
    
            @Override
            public void onFailure(Exception e) {
                listener.afterBulk(executionId, bulkRequest, e);
            }
        }, () -> {
            semaphore.release();
            latch.countDown();
        }));
        bulkRequestSetupSuccessful = true;
        if (concurrentRequests == 0) {
            latch.await();
        }
        ......
    }

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 8, 2022, 9:45am UTC](https://discuss.elastic.co/t/the-latch-in-bulkrequesthandler-execute-is-unnecessary/296570/2 "2022-03-08T09:45:19Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
