Skip to content

Commit 324af0a

Browse files
committed
always waiting for listener to be done before closing
1 parent ac086ff commit 324af0a

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

java-client/src/main/java/co/elastic/clients/elasticsearch/_helpers/bulk/BulkIngester.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public class BulkIngester<Context> implements AutoCloseable {
7676
private final FnCondition addCondition = new FnCondition(lock, this::canAddOperation);
7777
private final FnCondition sendRequestCondition = new FnCondition(lock, this::canSendRequest);
7878
private final FnCondition closeCondition = new FnCondition(lock, this::closedAndFlushed);
79+
private AtomicInteger listenerInProgressCount = new AtomicInteger();
7980

8081
private static class RequestExecution<Context> {
8182
public final long id;
@@ -235,7 +236,7 @@ private boolean canAddOperation() {
235236
}
236237

237238
private boolean closedAndFlushed() {
238-
return isClosed && operations.isEmpty() && requestsInFlightCount == 0;
239+
return isClosed && operations.isEmpty() && requestsInFlightCount == 0 && listenerInProgressCount.get() == 0;
239240
}
240241

241242
//----- Ingester logic
@@ -314,14 +315,32 @@ public void flush() {
314315
if (resp != null) {
315316
// Success
316317
if (listener != null) {
317-
scheduler.submit(() -> listener.afterBulk(exec.id, exec.request,
318-
exec.contexts, resp));
318+
listenerInProgressCount.incrementAndGet();
319+
scheduler.submit(() -> {
320+
try {
321+
listener.afterBulk(exec.id, exec.request, exec.contexts, resp);
322+
}
323+
finally {
324+
if(listenerInProgressCount.decrementAndGet() == 0){
325+
closeCondition.signalIfReady();
326+
}
327+
}
328+
});
319329
}
320330
} else {
321331
// Failure
322332
if (listener != null) {
323-
scheduler.submit(() -> listener.afterBulk(exec.id, exec.request,
324-
exec.contexts, thr));
333+
listenerInProgressCount.incrementAndGet();
334+
scheduler.submit(() -> {
335+
try {
336+
listener.afterBulk(exec.id, exec.request, exec.contexts, thr);
337+
}
338+
finally {
339+
if(listenerInProgressCount.decrementAndGet() == 0){
340+
closeCondition.signalIfReady();
341+
}
342+
}
343+
});
325344
}
326345
}
327346

java-client/src/test/java/co/elastic/clients/elasticsearch/_helpers/bulk/BulkIngesterTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ public void basicTestNoFlush() throws Exception {
105105
multiThreadTest(10, 3, 5, 100, true);
106106
}
107107

108+
@Test
109+
public void basicTestNoFlushWithInternalScheduler() throws Exception {
110+
// Will have nothing to flush on close.
111+
multiThreadTest(10, 3, 5, 100, false);
112+
}
113+
108114
private void multiThreadTest(int maxOperations, int maxRequests, int numThreads, int numOperations,
109115
boolean externalScheduler) throws Exception {
110116

0 commit comments

Comments
 (0)