Skip to content

Commit c72a4cb

Browse files
committed
Upgrade Elasticsearch 8.x to version 8.15.0. [DOX-412]
Incompatible change in elastic/elasticsearch-java#830. (Totally meaningless release notes entry: "Fixed bug in BulkIngester") https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.15/release-highlights.html
1 parent 6763b88 commit c72a4cb

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ gradle.rootProject {
1414
'commons-cli' : '1.3.1',
1515
'commons-io' : '2.7',
1616
'elasticsearch2' : '2.2.1',
17-
'elasticsearch8' : '8.14.1',
17+
'elasticsearch8' : '8.15.0',
1818
'htsjdk' : '4.0.1',
1919
'jackson' : '2.13.4.2',
2020
'jdk' : '17',

src/main/java/hbz/limetrans/ElasticsearchClientV8.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
import java.util.List;
4646
import java.util.Map;
4747
import java.util.Set;
48+
import java.util.concurrent.CountDownLatch;
49+
import java.util.concurrent.Executors;
50+
import java.util.concurrent.ScheduledExecutorService;
51+
import java.util.concurrent.TimeUnit;
52+
import java.util.concurrent.atomic.LongAdder;
4853
import java.util.function.BiConsumer;
4954
import java.util.function.Consumer;
5055
import java.util.function.Function;
@@ -63,7 +68,9 @@ public class ElasticsearchClientV8 extends ElasticsearchClient { // checkstyle-d
6368
private final long mBulkSizeValue;
6469

6570
private BulkIngester<Void> mBulkIngester;
71+
private ElasticsearchBulkListener mBulkListener;
6672
private RestClientTransport mTransport;
73+
private ScheduledExecutorService mBulkScheduler;
6774
private co.elastic.clients.elasticsearch.ElasticsearchClient mClient;
6875

6976
public ElasticsearchClientV8(final Settings aSettings) {
@@ -75,6 +82,8 @@ public ElasticsearchClientV8(final Settings aSettings) {
7582
@Override
7683
public void reset() {
7784
mBulkIngester = null;
85+
mBulkListener = null;
86+
mBulkScheduler = null;
7887

7988
super.reset();
8089
}
@@ -269,9 +278,19 @@ private void addBulk(final Function<BulkOperation.Builder, ObjectBuilder<BulkOpe
269278

270279
@Override
271280
protected void createBulk(final int aBulkActions, final int aBulkRequests) {
281+
mBulkScheduler = Executors.newScheduledThreadPool(aBulkRequests + 1, r -> {
282+
final Thread t = Executors.defaultThreadFactory().newThread(r);
283+
t.setName("bulk-ingester-executor#" + t.getId());
284+
t.setDaemon(true);
285+
return t;
286+
});
287+
288+
mBulkListener = new ElasticsearchBulkListener(this);
289+
272290
mBulkIngester = BulkIngester.of(b -> b
273291
.client(mClient)
274-
.listener(new ElasticsearchBulkListener(this))
292+
.listener(mBulkListener)
293+
.scheduler(mBulkScheduler)
275294
.maxOperations(aBulkActions)
276295
.maxSize(mBulkSizeValue)
277296
.maxConcurrentRequests(aBulkRequests)
@@ -287,10 +306,23 @@ protected boolean isBulkClosed() {
287306
protected boolean closeBulk() throws InterruptedException {
288307
try {
289308
mBulkIngester.close();
309+
310+
if (!mBulkListener.awaitTermination(1, TimeUnit.MINUTES)) {
311+
getLogger().warn("Some bulk listener tasks still pending");
312+
}
313+
314+
mBulkScheduler.shutdown();
315+
316+
if (!mBulkScheduler.awaitTermination(1, TimeUnit.MINUTES)) {
317+
getLogger().warn("Some bulk scheduler tasks still pending");
318+
}
319+
290320
return mBulkIngester.pendingRequests() == 0;
291321
}
292322
finally {
293323
mBulkIngester = null;
324+
mBulkListener = null;
325+
mBulkScheduler = null;
294326
}
295327
}
296328

@@ -349,19 +381,24 @@ private interface IOFunction<T, R> {
349381

350382
private static class ElasticsearchBulkListener implements BulkListener<Void> {
351383

384+
private static final int PENDING_TASKS_WAIT = 100;
385+
352386
private final ElasticsearchClient mClient;
387+
private final LongAdder mBulkHandlers = new LongAdder();
353388

354389
private ElasticsearchBulkListener(final ElasticsearchClient aClient) {
355390
mClient = aClient;
356391
}
357392

358393
@Override
359394
public void beforeBulk(final long aId, final BulkRequest aRequest, final List<Void> aContexts) {
395+
mBulkHandlers.increment();
360396
mClient.beforeBulk(aId, aRequest.operations().size(), -1);
361397
}
362398

363399
@Override
364400
public void afterBulk(final long aId, final BulkRequest aRequest, final List<Void> aContexts, final BulkResponse aResponse) {
401+
mBulkHandlers.decrement();
365402
mClient.afterBulk(aId, aResponse.took(), (d, s, f) -> aResponse.items().forEach(r -> {
366403
if (r.operationType() == OperationType.Delete) {
367404
d.run();
@@ -381,9 +418,29 @@ public void afterBulk(final long aId, final BulkRequest aRequest, final List<Voi
381418

382419
@Override
383420
public void afterBulk(final long aId, final BulkRequest aRequest, final List<Void> aContexts, final Throwable aThrowable) {
421+
mBulkHandlers.decrement();
384422
mClient.afterBulk(aId, aThrowable);
385423
}
386424

425+
public boolean awaitTermination(final long aTimeout, final TimeUnit aUnit) throws InterruptedException {
426+
final CountDownLatch latch = new CountDownLatch(1);
427+
428+
new Thread(() -> {
429+
while (mBulkHandlers.sum() != 0) {
430+
try {
431+
Thread.sleep(PENDING_TASKS_WAIT);
432+
}
433+
catch (final InterruptedException e) {
434+
throw new RuntimeException(e);
435+
}
436+
}
437+
438+
latch.countDown();
439+
}).start();
440+
441+
return latch.await(aTimeout, aUnit);
442+
}
443+
387444
}
388445

389446
}

0 commit comments

Comments
 (0)