You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@druid.apache.org by Roman Leventov <le...@apache.org> on 2019/02/26 23:04:12 UTC

Uninterruptibles and Futures.getUnchecked()

I've recently discovered two utilities in Guava that are very useful in
combating InterruptedExceptions that contaminate business logic of code:

 - Uninterruptibles:
https://google.github.io/guava/releases/21.0/api/docs/com/google/common/util/concurrent/Uninterruptibles.html.
It has methods that substitute typical interruptible blocking code with a
loop that ignores an InterruptedException, as recommended in the "Java
Concurrency in Practice" book. It's recommended to try to resort to this
utility when you don't expect interruption or don't want to deal with it
and do something like wrapping into a RuntimeException or logging to get
rid of it.
 Futures.getUnchecked():
https://google.github.io/guava/releases/21.0/api/docs/com/google/common/util/concurrent/Futures.html#getUnchecked-java.util.concurrent.Future-
similar to Uninterruptibles, but additionally "washes" a Future.get() call
off ExecutionException and a CancellationException.

Re: Uninterruptibles and Futures.getUnchecked()

Posted by Roman Leventov <le...@gmail.com>.
There are about 90 usages of Future.get() (both forms) in the codebase. I
didn't check any of them (except just one - see below), but I suspect that
in a lot of those places Futures.getUnchecked() could have been called to
clarify or even improve the logic.

That one occurrence that I've run into was
https://github.com/apache/incubator-druid/blob/a0afd7931d542fdd7149f0b02c0007e3e1fa8c65/server/src/main/java/org/apache/druid/server/coordinator/CostBalancerStrategy.java#L234-L246
- I thought that particular way to handle exceptions from a Future is bad,
started to look for alternatives, and stumbled upon Futures.getUnchecked().

There are zero usages of Uninterruptibles and Futures.getUnchecked() in the
repository so far (Uninterruptibles will first appear here:
https://github.com/apache/incubator-druid/pull/7038/files#diff-95b2c7998c679f4ebc744597fc699fc5R491),
I suspect, because nobody among Druid developers is aware of those tools
(as well as I wasn't aware of them 2 weeks ago.) So I wrote this message to
raise the developers' awareness about such tools at their hand if they need
them instead of writing next `catch (InterruptedException e) { throw new
RuntimeException(e); }`

I think this is a good idea to write such knowledge-sharing messages and
would like to see them coming from other people too.

On Thu, 28 Feb 2019 at 20:07, Gian Merlino <gi...@apache.org> wrote:

> Have you got sections in mind in Druid code that would be improved by using
> these?
>
> On Tue, Feb 26, 2019 at 3:04 PM Roman Leventov <le...@apache.org>
> wrote:
>
> > I've recently discovered two utilities in Guava that are very useful in
> > combating InterruptedExceptions that contaminate business logic of code:
> >
> >  - Uninterruptibles:
> >
> >
> https://google.github.io/guava/releases/21.0/api/docs/com/google/common/util/concurrent/Uninterruptibles.html
> > .
> > It has methods that substitute typical interruptible blocking code with a
> > loop that ignores an InterruptedException, as recommended in the "Java
> > Concurrency in Practice" book. It's recommended to try to resort to this
> > utility when you don't expect interruption or don't want to deal with it
> > and do something like wrapping into a RuntimeException or logging to get
> > rid of it.
> >  Futures.getUnchecked():
> >
> >
> https://google.github.io/guava/releases/21.0/api/docs/com/google/common/util/concurrent/Futures.html#getUnchecked-java.util.concurrent.Future-
> > similar to Uninterruptibles, but additionally "washes" a Future.get()
> call
> > off ExecutionException and a CancellationException.
> >
>

Re: Uninterruptibles and Futures.getUnchecked()

Posted by Gian Merlino <gi...@apache.org>.
Have you got sections in mind in Druid code that would be improved by using
these?

On Tue, Feb 26, 2019 at 3:04 PM Roman Leventov <le...@apache.org> wrote:

> I've recently discovered two utilities in Guava that are very useful in
> combating InterruptedExceptions that contaminate business logic of code:
>
>  - Uninterruptibles:
>
> https://google.github.io/guava/releases/21.0/api/docs/com/google/common/util/concurrent/Uninterruptibles.html
> .
> It has methods that substitute typical interruptible blocking code with a
> loop that ignores an InterruptedException, as recommended in the "Java
> Concurrency in Practice" book. It's recommended to try to resort to this
> utility when you don't expect interruption or don't want to deal with it
> and do something like wrapping into a RuntimeException or logging to get
> rid of it.
>  Futures.getUnchecked():
>
> https://google.github.io/guava/releases/21.0/api/docs/com/google/common/util/concurrent/Futures.html#getUnchecked-java.util.concurrent.Future-
> similar to Uninterruptibles, but additionally "washes" a Future.get() call
> off ExecutionException and a CancellationException.
>