fdb-record-layer
fdb-record-layer copied to clipboard
The lucene index uses `.thenApplyAsync` when it shouldn't (and incorrectly)
In locations, such as https://github.com/FoundationDB/fdb-record-layer/blob/2e1b2bc723ee5973e765f7fd35c99226f2330208/fdb-record-layer-lucene/src/main/java/com/apple/foundationdb/record/lucene/directory/FDBDirectory.java#L176, the Lucene index is using .thenApplyAsync
.
In general you don't need (or want) to use thenApplyAsync(). The CompletableFuture. *Async()
will create a new future that will execute the code in the lambda, rather than executing the code in the lambda immediately when this future completes. For example:
fetchSomeNumber().thenApplyAsync(number -> number * 10);
will create an entirely new future that is enqueued on the Executor
thread, simply to do the multiplication of the number. Whereas:
fetchSomeNumber().thenApply(number -> number * 10);
Will do the calculation immediately, as soon as number
becomes available. So the former form is FAR more expensive, for very little gain. The main use of the *Async()
methods are cases where the lambda needs to do something kind of expensive (e.g. factor the number) and you don't want to block the current future chain while it happens.
In addition, in the cases where you do need to use the *Async()
methods, be sure you pass it an executor, like:
fetchSomeNumber().thenApplyAsync(number -> number * 10, context.getExecutor());
otherwise it is executed in the global thread pool, rather than the record layer thread pool