Skip to content
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

[improvement] Add support for tracing-aware Guava FutureCallbacks. #77

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tracing/src/main/java/com/palantir/tracing/Tracers.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package com.palantir.tracing;

import com.google.common.util.concurrent.FutureCallback;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadLocalRandom;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

/** Utility methods for making {@link ExecutorService} and {@link Runnable} instances tracing-aware. */
public final class Tracers {
Expand Down Expand Up @@ -133,6 +135,11 @@ public static Runnable wrap(Runnable delegate) {
return new TracingAwareRunnable(Optional.empty(), delegate);
}

/** Like {@link #wrap(String, Callable)}, but for Guava's FutureCallback. */
public static <V> FutureCallback<V> wrap(String operation, FutureCallback<V> delegate) {
return new TracingAwareFutureCallback<>(operation, delegate);
}

/**
* Like {@link #wrap(Runnable)}, but using the given {@link String operation} is used to create a span for the
* execution.
Expand Down Expand Up @@ -352,6 +359,37 @@ public void run() {
}
}

/**
* Wrap a given guava future callback such that its execution operated with the {@link Trace thread-local Trace} of
* the thread the constructs the {@link TracingAwareFutureCallback} instance rather than the thread that executes
* the callback.
*/
private static class TracingAwareFutureCallback<V> implements FutureCallback<V> {
private final FutureCallback<V> delegate;
private DeferredTracer deferredTracer;

TracingAwareFutureCallback(String operation, FutureCallback<V> delegate) {
this.delegate = delegate;
this.deferredTracer = new DeferredTracer(operation);
}

@Override
public void onSuccess(@NullableDecl V result) {
deferredTracer.withTrace(() -> {
delegate.onSuccess(result);
return null;
});
}

@Override
public void onFailure(Throwable throwable) {
deferredTracer.withTrace(() -> {
delegate.onFailure(throwable);
return null;
});
}
}

public interface ThrowingCallable<T, E extends Throwable> {
T call() throws E;
}
Expand Down
220 changes: 220 additions & 0 deletions tracing/src/test/java/com/palantir/tracing/TracersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,27 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.google.common.collect.Lists;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.palantir.tracing.api.OpenSpan;
import com.palantir.tracing.api.Span;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -500,6 +509,150 @@ public void testWrapRunnableWithAlternateTraceId_traceStateRestoredToCleared() {
assertThat(Tracer.hasTraceId()).isFalse();
}

@Test
public void testWrappingFutureCallback_futureCallbackTraceIsIsolated_success() throws Exception {
CompletionAwareFutureCallback<Void> futureCallback = createFutureCallbackWithFunction(
() -> Tracer.startSpan("inside"));

ListeningExecutorService listeningExecutorService =
MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
ListenableFuture<Void> future = listeningExecutorService.submit(() -> null);

Tracer.startSpan("outside");
FutureCallback<Void> callback = Tracers.wrap("callback", futureCallback);
// Using direct executor to use same thread to verify callback doesn't modify thread state
Futures.addCallback(future, callback, MoreExecutors.directExecutor());

assertThat(futureCallback.waitForCompletion(10, TimeUnit.SECONDS)).isTrue();
assertThat(futureCallback.wasSuccess()).isTrue();
future.get();

assertThat(Tracer.completeSpan().get().getOperation()).isEqualTo("outside");
}

@Test
public void testWrappingFutureCallback_futureCallbackTraceIsIsolated_failure() throws Exception {
CompletionAwareFutureCallback<Void> futureCallback = createFutureCallbackWithFunction(
() -> Tracer.startSpan("inside"));

ListeningExecutorService listeningExecutorService =
MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
ListenableFuture<Void> future = listeningExecutorService.submit(() -> {
throw new IllegalStateException();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use separate test cases for these


Tracer.startSpan("outside");
FutureCallback<Void> callback = Tracers.wrap("callback", futureCallback);
// Using direct executor to use same thread to verify callback doesn't modify thread state
Futures.addCallback(future, callback, MoreExecutors.directExecutor());

assertThat(futureCallback.waitForCompletion(10, TimeUnit.SECONDS)).isTrue();
assertThat(futureCallback.wasSuccess()).isFalse();
assertThatThrownBy(future::get).isInstanceOf(ExecutionException.class);

assertThat(Tracer.completeSpan().get().getOperation()).isEqualTo("outside");
}

@Test
public void testWrappingFutureCallback_traceStateShowsCorrectlyParentedNewOperation_success_sameThread()
throws Exception {
AtomicReference<Span> span = new AtomicReference<>();
CompletionAwareFutureCallback<Void> futureCallback = createFutureCallbackWithFunction(
() -> span.set(Tracer.completeSpan().get()));

ListeningExecutorService listeningExecutorService =
MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
ListenableFuture<Void> future = listeningExecutorService.submit(() -> null);

OpenSpan beforeSpan = Tracer.startSpan("before-construction");
FutureCallback<Void> callback = Tracers.wrap("callback", futureCallback);
Tracer.startSpan("after-construction");
Futures.addCallback(future, callback, MoreExecutors.directExecutor());

assertThat(futureCallback.waitForCompletion(10, TimeUnit.SECONDS)).isTrue();
assertThat(futureCallback.wasSuccess()).isTrue();
future.get();

assertThat(span.get().getOperation()).isEqualTo("callback");
assertThat(span.get().getParentSpanId().get()).isEqualTo(beforeSpan.getSpanId());
}

@Test
public void testWrappingFutureCallback_traceStateShowsCorrectlyParentedNewOperation_success_differentThread()
throws Exception {
AtomicReference<Span> span = new AtomicReference<>();
CompletionAwareFutureCallback<Void> futureCallback = createFutureCallbackWithFunction(
() -> span.set(Tracer.completeSpan().get()));

ListeningExecutorService listeningExecutorService =
MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
ListenableFuture<Void> future = listeningExecutorService.submit(() -> null);

OpenSpan beforeSpan = Tracer.startSpan("before-construction");
FutureCallback<Void> callback = Tracers.wrap("callback", futureCallback);
Tracer.startSpan("after-construction");
Futures.addCallback(future, callback, Executors.newSingleThreadExecutor());

assertThat(futureCallback.waitForCompletion(10, TimeUnit.SECONDS)).isTrue();
assertThat(futureCallback.wasSuccess()).isTrue();
future.get();

assertThat(span.get().getOperation()).isEqualTo("callback");
assertThat(span.get().getParentSpanId().get()).isEqualTo(beforeSpan.getSpanId());
}

@Test
public void testWrappingFutureCallback_traceStateShowsCorrectlyParentedNewOperation_failure_sameThread()
throws Exception {
AtomicReference<Span> span = new AtomicReference<>();
CompletionAwareFutureCallback<Void> futureCallback = createFutureCallbackWithFunction(
() -> span.set(Tracer.completeSpan().get()));

ListeningExecutorService listeningExecutorService =
MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
ListenableFuture<Void> future = listeningExecutorService.submit(() -> {
throw new IllegalStateException();
});

OpenSpan beforeSpan = Tracer.startSpan("before-construction");
FutureCallback<Void> callback = Tracers.wrap("callback", futureCallback);
Tracer.startSpan("after-construction");
Futures.addCallback(future, callback, MoreExecutors.directExecutor());

assertThat(futureCallback.waitForCompletion(10, TimeUnit.SECONDS)).isTrue();
assertThat(futureCallback.wasSuccess()).isFalse();
assertThatThrownBy(future::get).isInstanceOf(ExecutionException.class);

assertThat(span.get().getOperation()).isEqualTo("callback");
assertThat(span.get().getParentSpanId().get()).isEqualTo(beforeSpan.getSpanId());
}

@Test
public void testWrappingFutureCallback_traceStateShowsCorrectlyParentedNewOperation_failure_differentThread()
throws Exception {
AtomicReference<Span> span = new AtomicReference<>();
CompletionAwareFutureCallback<Void> futureCallback = createFutureCallbackWithFunction(
() -> span.set(Tracer.completeSpan().get()));

ListeningExecutorService listeningExecutorService =
MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
ListenableFuture<Void> future = listeningExecutorService.submit(() -> {
throw new IllegalStateException();
});

OpenSpan beforeSpan = Tracer.startSpan("before-construction");
FutureCallback<Void> callback = Tracers.wrap("callback", futureCallback);
Tracer.startSpan("after-construction");
Futures.addCallback(future, callback, Executors.newSingleThreadExecutor());

assertThat(futureCallback.waitForCompletion(10, TimeUnit.SECONDS)).isTrue();
assertThat(futureCallback.wasSuccess()).isFalse();
assertThatThrownBy(future::get).isInstanceOf(ExecutionException.class);

assertThat(span.get().getOperation()).isEqualTo("callback");
assertThat(span.get().getParentSpanId().get()).isEqualTo(beforeSpan.getSpanId());
}

@Test
public void testTraceIdGeneration() throws Exception {
assertThat(Tracers.randomId()).hasSize(16); // fails with p=1/16 if generated string is not padded
Expand Down Expand Up @@ -615,4 +768,71 @@ private static List<OpenSpan> getCurrentTrace() {
return Lists.reverse(spans);
}).orElse(Collections.emptyList());
}

private static <V> CompletionAwareFutureCallback<V> createFutureCallbackWithFunction(Runnable runnable) {
return new CompletionAwareFutureCallback<>(new FutureCallback<V>() {
@Override
public void onSuccess(@NullableDecl V result) {
runnable.run();
}

@Override
public void onFailure(Throwable throwable) {
runnable.run();
}
});
}

/**
* Defines an interface for providing information on completion of execution and the ability to wait on completion
* for use with tests involving FutureCallback.
*/
private interface CompletionAware {
boolean waitForCompletion(long timeout, TimeUnit unit) throws InterruptedException;
boolean wasSuccess();
}

/**
* There is no guarantee when a FutureCallback will actually run. In order to verify state from inside the
* FutureCallback, a CountDownLatch is used to track when the FutureCallback has finished executing and to provide
* a condition to wait on.
*/
private static final class CompletionAwareFutureCallback<V> implements FutureCallback<V>, CompletionAware {
private final CountDownLatch latch = new CountDownLatch(1);
private final FutureCallback<V> delegate;
private AtomicReference<Boolean> success = new AtomicReference<>(Boolean.FALSE);

CompletionAwareFutureCallback(FutureCallback<V> delegate) {
this.delegate = delegate;
}

@Override
public void onSuccess(@NullableDecl V result) {
try {
success.set(Boolean.TRUE);
delegate.onSuccess(result);
} finally {
latch.countDown();
}
}

@Override
public void onFailure(Throwable throwable) {
try {
delegate.onFailure(throwable);
} finally {
latch.countDown();
}
}

@Override
public boolean waitForCompletion(long timeout, TimeUnit unit) throws InterruptedException {
return latch.await(timeout, unit);
}

@Override
public boolean wasSuccess() {
return success.get();
}
}
}