-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Mid-level service client and updated high-level clients. #12696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ac6602
Mid-level service client and updated high-level clients.
gianm 462fac4
Style adjustments.
gianm 2b45c4b
For the coverage.
gianm 48d0b3c
Adjustments.
gianm cd3bb9c
Better behaviors.
gianm bd2949c
Fixes.
gianm 0fdbcdf
Merge branch 'master' into rpc-service-client
gianm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
core/src/main/java/org/apache/druid/common/guava/FutureUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.common.guava; | ||
|
|
||
| import com.google.common.util.concurrent.Futures; | ||
| import com.google.common.util.concurrent.ListenableFuture; | ||
|
|
||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.function.Function; | ||
|
|
||
| public class FutureUtils | ||
| { | ||
| /** | ||
| * Waits for a given future and returns its value, like {@code future.get()}. | ||
| * | ||
| * On InterruptedException, cancels the provided future if {@code cancelIfInterrupted}, then re-throws the | ||
| * original InterruptedException. | ||
| * | ||
| * Passes through CancellationExceptions and ExecutionExceptions as-is. | ||
| */ | ||
| public static <T> T get(final ListenableFuture<T> future, final boolean cancelIfInterrupted) | ||
| throws InterruptedException, ExecutionException | ||
| { | ||
| try { | ||
| return future.get(); | ||
| } | ||
| catch (InterruptedException e) { | ||
| if (cancelIfInterrupted) { | ||
| future.cancel(true); | ||
| } | ||
|
|
||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Waits for a given future and returns its value, like {@code future.get()}. | ||
| * | ||
| * On InterruptException, cancels the provided future if {@code cancelIfInterrupted}, and in either case, throws | ||
| * a RuntimeException that wraps the original InterruptException. | ||
| * | ||
| * Passes through CancellationExceptions as-is. | ||
| * | ||
| * Re-wraps the causes of ExecutionExceptions using RuntimeException. | ||
| */ | ||
| public static <T> T getUnchecked(final ListenableFuture<T> future, final boolean cancelIfInterrupted) | ||
| { | ||
| try { | ||
| return FutureUtils.get(future, cancelIfInterrupted); | ||
| } | ||
| catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(e); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interrupt status probably needs to be set again by calling Thread.currentThread().interrupt()
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, I added a line to set the flag here. |
||
| } | ||
| catch (ExecutionException e) { | ||
| throw new RuntimeException(e.getCause()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Like {@link Futures#transform}, but works better with lambdas due to not having overloads. | ||
| * | ||
| * One can write {@code FutureUtils.transform(future, v -> ...)} instead of | ||
| * {@code Futures.transform(future, (Function<? super T, ?>) v -> ...)} | ||
| */ | ||
| public static <T, R> ListenableFuture<R> transform(final ListenableFuture<T> future, final Function<T, R> fn) | ||
| { | ||
| return Futures.transform(future, fn::apply); | ||
| } | ||
| } | ||
172 changes: 172 additions & 0 deletions
172
core/src/test/java/org/apache/druid/common/guava/FutureUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.common.guava; | ||
|
|
||
| import com.google.common.util.concurrent.Futures; | ||
| import com.google.common.util.concurrent.SettableFuture; | ||
| import org.apache.druid.java.util.common.ISE; | ||
| import org.apache.druid.java.util.common.StringUtils; | ||
| import org.apache.druid.java.util.common.concurrent.Execs; | ||
| import org.hamcrest.CoreMatchers; | ||
| import org.hamcrest.MatcherAssert; | ||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.internal.matchers.ThrowableMessageMatcher; | ||
|
|
||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| public class FutureUtilsTest | ||
| { | ||
| private ExecutorService exec; | ||
|
|
||
| @Before | ||
| public void setUp() | ||
| { | ||
| exec = Execs.singleThreaded(StringUtils.encodeForFormat(getClass().getName()) + "-%d"); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() | ||
| { | ||
| if (exec != null) { | ||
| exec.shutdownNow(); | ||
| exec = null; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void test_get_ok() throws Exception | ||
| { | ||
| final String s = FutureUtils.get(Futures.immediateFuture("x"), true); | ||
| Assert.assertEquals("x", s); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_get_failed() | ||
| { | ||
| final ExecutionException e = Assert.assertThrows( | ||
| ExecutionException.class, | ||
| () -> FutureUtils.get(Futures.immediateFailedFuture(new ISE("oh no")), true) | ||
| ); | ||
|
|
||
| MatcherAssert.assertThat(e.getCause(), CoreMatchers.instanceOf(IllegalStateException.class)); | ||
| MatcherAssert.assertThat(e.getCause(), ThrowableMessageMatcher.hasMessage(CoreMatchers.containsString("oh no"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_getUnchecked_interrupted_cancelOnInterrupt() throws InterruptedException | ||
| { | ||
| final SettableFuture<String> neverGoingToResolve = SettableFuture.create(); | ||
| final AtomicReference<Throwable> exceptionFromOtherThread = new AtomicReference<>(); | ||
| final CountDownLatch runningLatch = new CountDownLatch(1); | ||
|
|
||
| final Future<?> execResult = exec.submit(() -> { | ||
| runningLatch.countDown(); | ||
|
|
||
| try { | ||
| FutureUtils.getUnchecked(neverGoingToResolve, true); | ||
| } | ||
| catch (Throwable t) { | ||
| exceptionFromOtherThread.set(t); | ||
| } | ||
| }); | ||
|
|
||
| runningLatch.await(); | ||
| Assert.assertTrue(execResult.cancel(true)); | ||
| exec.shutdown(); | ||
|
|
||
| Assert.assertTrue(exec.awaitTermination(1, TimeUnit.MINUTES)); | ||
| exec = null; | ||
|
|
||
| Assert.assertTrue(neverGoingToResolve.isCancelled()); | ||
|
|
||
| final Throwable e = exceptionFromOtherThread.get(); | ||
| MatcherAssert.assertThat(e, CoreMatchers.instanceOf(RuntimeException.class)); | ||
| MatcherAssert.assertThat(e.getCause(), CoreMatchers.instanceOf(InterruptedException.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_getUnchecked_interrupted_dontCancelOnInterrupt() throws InterruptedException | ||
| { | ||
| final SettableFuture<String> neverGoingToResolve = SettableFuture.create(); | ||
| final AtomicReference<Throwable> exceptionFromOtherThread = new AtomicReference<>(); | ||
| final CountDownLatch runningLatch = new CountDownLatch(1); | ||
|
|
||
| final Future<?> execResult = exec.submit(() -> { | ||
| runningLatch.countDown(); | ||
|
|
||
| try { | ||
| FutureUtils.getUnchecked(neverGoingToResolve, false); | ||
| } | ||
| catch (Throwable t) { | ||
| exceptionFromOtherThread.set(t); | ||
| } | ||
| }); | ||
|
|
||
| runningLatch.await(); | ||
| Assert.assertTrue(execResult.cancel(true)); | ||
| exec.shutdown(); | ||
|
|
||
| Assert.assertTrue(exec.awaitTermination(1, TimeUnit.MINUTES)); | ||
| exec = null; | ||
|
|
||
| Assert.assertFalse(neverGoingToResolve.isCancelled()); | ||
| Assert.assertFalse(neverGoingToResolve.isDone()); | ||
|
|
||
| final Throwable e = exceptionFromOtherThread.get(); | ||
| MatcherAssert.assertThat(e, CoreMatchers.instanceOf(RuntimeException.class)); | ||
| MatcherAssert.assertThat(e.getCause(), CoreMatchers.instanceOf(InterruptedException.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_getUnchecked_ok() | ||
| { | ||
| final String s = FutureUtils.getUnchecked(Futures.immediateFuture("x"), true); | ||
| Assert.assertEquals("x", s); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_getUnchecked_failed() | ||
| { | ||
| final RuntimeException e = Assert.assertThrows( | ||
| RuntimeException.class, | ||
| () -> FutureUtils.getUnchecked(Futures.immediateFailedFuture(new ISE("oh no")), true) | ||
| ); | ||
|
|
||
| MatcherAssert.assertThat(e.getCause(), CoreMatchers.instanceOf(IllegalStateException.class)); | ||
| MatcherAssert.assertThat(e.getCause(), ThrowableMessageMatcher.hasMessage(CoreMatchers.containsString("oh no"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void test_transform() throws Exception | ||
| { | ||
| Assert.assertEquals( | ||
| "xy", | ||
| FutureUtils.transform(Futures.immediateFuture("x"), s -> s + "y").get() | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interrupt status probably needs to be set again by calling
Thread.currentThread().interrupt()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The InterruptedException is re-thrown here, so it's ok that we don't set the flag. According to https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html it is preferred to not set the interrupt flag when throwing InterruptedException.