# BulkIngester not reliably executing afterBulk handlers

**URL:** <https://discuss.elastic.co/t/bulkingester-not-reliably-executing-afterbulk-handlers/364838>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 13, 2024, 2:48pm UTC](https://discuss.elastic.co/t/bulkingester-not-reliably-executing-afterbulk-handlers/364838 "2024-08-13T14:48:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackwinter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/blackwinter/32/11589_2.png) [@blackwinter](https://discuss.elastic.co/u/blackwinter)\
**Post date:** [August 13, 2024, 2:48pm UTC](https://discuss.elastic.co/t/bulkingester-not-reliably-executing-afterbulk-handlers/364838/1 "2024-08-13T14:48:41Z")

</div>

We're having difficulties adjusting to the new listener thread pool (see pull request [#830](https://github.com/elastic/elasticsearch-java/pull/830)) with `elasticsearch-java` version 8.15.0 on Java 17. We're relying on `afterBulk` handlers for statistics gathering and error handling.

In order to control the scheduler shutdown (instead of stopping actively executing tasks and halting the processing of waiting tasks via `shutdownNow()`), we're already passing an external scheduler to the ingester. But it appears as if there's room for our `shutdown()` to be called (i.e., no longer accepting new tasks) _before_ the remaining `afterBulk` tasks have been `submit()`ed. The termination always finishes without timing out, yet the handler is occasionally not executed.

How can we ensure an orderly shutdown here that waits for _all_ expected `afterBulk` tasks to be finished? See commit [hbz/limetrans@ca0723f](https://github.com/hbz/limetrans/commit/ca0723f) for our latest attempt where we now have to keep track of in-flight bulks via synchronous `beforeBulk` (which was reverted from async in pull request [#837](https://github.com/elastic/elasticsearch-java/pull/837)).

See also issue [#559](https://github.com/elastic/elasticsearch-java/issues/559) for a similar report; same objective, albeit with the previous non-threaded implementation (with which we didn't have any issues).

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [August 23, 2024, 8:54am UTC](https://discuss.elastic.co/t/bulkingester-not-reliably-executing-afterbulk-handlers/364838/2 "2024-08-23T08:54:49Z")

</div>

Hello and welcome.  
It's probably related to [this issue](https://github.com/elastic/elasticsearch-java/pull/867) that a user found, where the scheduler starts the shutdown before accepting the last tasks, this is more centered on the internal scheduler, but the same logic probably applies. We're working on the problem and hopefully we'll find a solution before the next patch. Thank you!

---

<div class="post-metadata">

**Author:** ![blackwinter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/blackwinter/32/11589_2.png) [@blackwinter](https://discuss.elastic.co/u/blackwinter)\
**Post date:** [September 12, 2024, 3:11pm UTC](https://discuss.elastic.co/t/bulkingester-not-reliably-executing-afterbulk-handlers/364838/3 "2024-09-12T15:11:26Z")

</div>

Unfortunately, our issue seems to persist with the 8.15.1 release. Without the countermeasures outlined above, our tests keep failing.

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [September 12, 2024, 3:55pm UTC](https://discuss.elastic.co/t/bulkingester-not-reliably-executing-afterbulk-handlers/364838/4 "2024-09-12T15:55:03Z")

</div>

Sorry, that issue took a bit more longer to test and we're just about to merge it. It will be available in 8.15.2, which should be out in a couple of weeks.

---

<div class="post-metadata">

**Author:** ![blackwinter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/blackwinter/32/11589_2.png) [@blackwinter](https://discuss.elastic.co/u/blackwinter)\
**Post date:** [October 7, 2024, 11:12am UTC](https://discuss.elastic.co/t/bulkingester-not-reliably-executing-afterbulk-handlers/364838/5 "2024-10-07T11:12:18Z")

</div>

Thanks! The issue appears to be fixed with the 8.15.2 release.

---

<div class="post-metadata">

**Author:** ![blackwinter](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/blackwinter/32/11589_2.png) [@blackwinter](https://discuss.elastic.co/u/blackwinter)\
**Post date:** [November 20, 2024, 11:31am UTC](https://discuss.elastic.co/t/bulkingester-not-reliably-executing-afterbulk-handlers/364838/6 "2024-11-20T11:31:46Z")

</div>

Unfortunately, the fix is [not included](https://github.com/elastic/elasticsearch-java/pull/867#issuecomment-2488328335) in the 8.16.0 release.
