【问题标题】:How to create async stacktraces?如何创建异步堆栈跟踪?
【发布时间】:2018-03-14 04:06:49
【问题描述】:

更新:最新版本的 Intellij IDEA implements 正是我正在寻找的。问题是如何在 IDE 之外实现这一点(这样我就可以将异步堆栈跟踪转储到日志文件中),理想情况下不使用检测代理。


自从我将应用程序从同步模型转换为异步模型后,我一直在调试失败。

当我使用同步 API 时,我总是在异常堆栈跟踪中找到我的类,因此如果出现问题,我知道从哪里开始查找。使用异步 API,我得到的堆栈跟踪没有引用我的类,也没有指示哪个请求触发了失败。

我会给你一个具体的例子,但我对这类问题的一般解决方案感兴趣。

具体例子

我使用 Jersey 发出 HTTP 请求:

new Client().target("http://test.com/").request().rx().get(JsonNode.class);

其中rx() 表示请求应该异步进行,直接返回CompletionStage<JsonNode> 而不是JsonNode。如果这个调用失败,我会得到这个堆栈跟踪:

javax.ws.rs.ForbiddenException: HTTP 403 Authentication Failed
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1083)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:883)
    at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$1(JerseyInvocation.java:767)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:229)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:414)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:765)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:456)
    at org.glassfish.jersey.client.JerseyCompletionStageRxInvoker.lambda$method$1(JerseyCompletionStageRxInvoker.java:70)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)

注意:

  • 堆栈跟踪未引用用户代码。
  • 异常消息不包含有关触发错误的 HTTP 请求的上下文信息(HTTP 方法、URI 等)。

因此,我无法将异常追溯到其源头。

为什么会这样

如果你深入挖掘,你会发现Jersey is invoking

CompletableFuture.supplyAsync(() -> getSyncInvoker().method(name, entity, responseType))

用于rx() 调用。因为供应商是由 Jersey 构建的,所以没有引用回用户代码。

我尝试过的

I tried filing a bug report 针对 Jetty 提出不相关的异步示例,随后以安全为由拒绝。

相反,我一直在添加上下文信息,如下所示:

makeHttpRequest().exceptionally(e ->
{
    throw new RuntimeException(e);
});

意思是,我在代码中的每个 HTTP 请求之后手动添加 exceptionally()。 Jersey 抛出的任何异常都包含在引用我的代码的辅助异常中。生成的堆栈跟踪如下所示:

java.lang.RuntimeException: javax.ws.rs.ForbiddenException: HTTP 403 Authentication Failed
    at my.user.code.Testcase.lambda$null$1(Testcase.java:25)
    at java.util.concurrent.CompletableFuture.uniExceptionally(CompletableFuture.java:870)
    ... 6 common frames omitted
Caused by: javax.ws.rs.ForbiddenException: HTTP 403 Authentication Failed
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1083)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:883)
    at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$1(JerseyInvocation.java:767)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:229)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:414)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:765)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:456)
    at org.glassfish.jersey.client.JerseyCompletionStageRxInvoker.lambda$method$1(JerseyCompletionStageRxInvoker.java:70)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
    ... 3 common frames omitted

我不喜欢这种方法,因为它容易出错并降低代码的可读性。如果我在某些 HTTP 请求中错误地忽略了这一点,我最终会得到一个模糊的堆栈跟踪并花费大量时间来跟踪它。

此外,如果我想在实用程序类后面隐藏这个技巧,那么我必须在 CompletionStage 之外实例化一个异常;否则,实用程序类将显示在堆栈跟踪中,而不是实际的调用站点。在 CompletionStage 之外实例化异常非常昂贵,因为即使异步调用没有抛出异常,这段代码也会运行。

我的问题

是否有一种可靠且易于维护的方法来将上下文信息添加到异步调用中?

或者,是否有一种有效的方法可以在没有此上下文信息的情况下将堆栈跟踪追溯到其来源?

【问题讨论】:

  • 像往常一样的好问题,我已经列出了一个关于异步的演示文稿,他们说最难的事情是调试并在需要时获取异常......
  • Spring的RestTemplate有方法setErrorHandler(ResponseErrorHandler),那个ResponseErrorHandler接收http响应。我认为泽西岛有类似的东西,至少谷歌请求“泽西错误处理”提供了许多网络参考。
  • @AlexeiKaigorodov 正如我在问题顶部附近解释的那样,我正在寻找一个通用解决方案。我给出的具体例子是作为更普遍问题的一个具体例子。此外,我认为错误处理程序无法在异步抛出异常时知道调用站点。
  • 假设在CompletableFutures 和CompletionStages 链中的错误信息返回给异步调用者。这是一个约定。如果不是,则某些阶段行为不端,只是将其丢弃,这是一个错误。由于 Jersey 维护人员拒绝了您的错误报告,因此您必须自己找到这个地方。然后你可以修复它并提交一个拉取请求。

标签: java debugging asynchronous completable-future


【解决方案1】:

鉴于这个问题已经将近一个月没有收到任何答案,我将发布我迄今为止找到的最佳解决方案:

DebugCompletableFuture.java

/**
 * A {@link CompletableFuture} that eases debugging.
 *
 * @param <T> the type of value returned by the future
 */
public final class DebugCompletableFuture<T> extends CompletableFuture<T>
{
    private static RunMode RUN_MODE = RunMode.DEBUG;
    private static final Set<String> CLASS_PREFIXES_TO_REMOVE = ImmutableSet.of(DebugCompletableFuture.class.getName(),
        CompletableFuture.class.getName(), ThreadPoolExecutor.class.getName());
    private static final Set<Class<? extends Throwable>> EXCEPTIONS_TO_UNWRAP = ImmutableSet.of(AsynchronousException.class,
        CompletionException.class, ExecutionException.class);
    private final CompletableFuture<T> delegate;
    private final AsynchronousException asyncStacktrace;

    /**
     * @param delegate the stage to delegate to
     * @throws NullPointerException if any of the arguments are null
     */
    private DebugCompletableFuture(CompletableFuture<T> delegate)
    {
        requireThat("delegate", delegate).isNotNull();
        this.delegate = delegate;
        this.asyncStacktrace = new AsynchronousException();
        delegate.whenComplete((value, exception) ->
        {
            if (exception == null)
            {
                super.complete(value);
                return;
            }
            exception = Exceptions.unwrap(exception, EXCEPTIONS_TO_UNWRAP);
            asyncStacktrace.initCause(exception);
            filterStacktrace(asyncStacktrace, element ->
            {
                String className = element.getClassName();
                for (String prefix : CLASS_PREFIXES_TO_REMOVE)
                    if (className.startsWith(prefix))
                        return true;
                return false;
            });
            Set<String> newMethods = getMethodsInStacktrace(asyncStacktrace);
            if (!newMethods.isEmpty())
            {
                Set<String> oldMethods = getMethodsInStacktrace(exception);
                newMethods.removeAll(oldMethods);
                if (!newMethods.isEmpty())
                {
                    // The async stacktrace introduces something new
                    super.completeExceptionally(asyncStacktrace);
                    return;
                }
            }
            super.completeExceptionally(exception);
        });
    }

    /**
     * @param exception an exception
     * @return the methods referenced by the stacktrace
     * @throws NullPointerException if {@code exception} is null
     */
    private Set<String> getMethodsInStacktrace(Throwable exception)
    {
        requireThat("exception", exception).isNotNull();
        Set<String> result = new HashSet<>();
        for (StackTraceElement element : exception.getStackTrace())
            result.add(element.getClassName() + "." + element.getMethodName());
        for (Throwable suppressed : exception.getSuppressed())
            result.addAll(getMethodsInStacktrace(suppressed));
        return result;
    }

    /**
     * @param <T2>     the type returned by the delegate
     * @param delegate the stage to delegate to
     * @return if {@code RUN_MODE == DEBUG} returns an instance that wraps {@code delegate}; otherwise, returns {@code delegate}
     * unchanged
     * @throws NullPointerException if any of the arguments are null
     */
    public static <T2> CompletableFuture<T2> wrap(CompletableFuture<T2> delegate)
    {
        if (RUN_MODE != RunMode.DEBUG)
            return delegate;
        return new DebugCompletableFuture<>(delegate);
    }

    /**
     * Removes stack trace elements that match a filter. The exception and its descendants are processed recursively.
     * <p>
     * This method can be used to remove lines that hold little value for the end user (such as the implementation of utility functions).
     *
     * @param exception     the exception to process
     * @param elementFilter returns true if the current stack trace element should be removed
     */
    private void filterStacktrace(Throwable exception, Predicate<StackTraceElement> elementFilter)
    {
        Throwable cause = exception.getCause();
        if (cause != null)
            filterStacktrace(cause, elementFilter);
        for (Throwable suppressed : exception.getSuppressed())
            filterStacktrace(suppressed, elementFilter);
        StackTraceElement[] elements = exception.getStackTrace();
        List<StackTraceElement> keep = new ArrayList<>(elements.length);
        for (StackTraceElement element : elements)
        {
            if (!elementFilter.test(element))
                keep.add(element);
        }
        exception.setStackTrace(keep.toArray(new StackTraceElement[0]));
    }

    @Override
    public <U> CompletableFuture<U> thenApply(Function<? super T, ? extends U> fn)
    {
        return wrap(super.thenApply(fn));
    }

    @Override
    public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn)
    {
        return wrap(super.thenApplyAsync(fn));
    }

    @Override
    public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor)
    {
        return wrap(super.thenApplyAsync(fn, executor));
    }

    @Override
    public CompletableFuture<Void> thenAccept(Consumer<? super T> action)
    {
        return wrap(super.thenAccept(action));
    }

    @Override
    public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)
    {
        return wrap(super.thenAcceptAsync(action));
    }

    @Override
    public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)
    {
        return wrap(super.thenAcceptAsync(action, executor));
    }

    @Override
    public CompletableFuture<Void> thenRun(Runnable action)
    {
        return wrap(super.thenRun(action));
    }

    @Override
    public CompletableFuture<Void> thenRunAsync(Runnable action)
    {
        return wrap(super.thenRunAsync(action));
    }

    @Override
    public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)
    {
        return wrap(super.thenRunAsync(action, executor));
    }

    @Override
    public <U, V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other,
                                                   BiFunction<? super T, ? super U, ? extends V> fn)
    {
        return wrap(super.thenCombine(other, fn));
    }

    @Override
    public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,
                                                        BiFunction<? super T, ? super U, ? extends V> fn)
    {
        return wrap(super.thenCombineAsync(other, fn));
    }

    @Override
    public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,
                                                        BiFunction<? super T, ? super U, ? extends V> fn,
                                                        Executor executor)
    {
        return wrap(super.thenCombineAsync(other, fn, executor));
    }

    @Override
    public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other,
                                                      BiConsumer<? super T, ? super U> action)
    {
        return wrap(super.thenAcceptBoth(other, action));
    }

    @Override
    public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,
                                                           BiConsumer<? super T, ? super U> action)
    {
        return wrap(super.thenAcceptBothAsync(other, action));
    }

    @Override
    public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,
                                                           BiConsumer<? super T, ? super U> action,
                                                           Executor executor)
    {
        return wrap(super.thenAcceptBothAsync(other, action, executor));
    }

    @Override
    public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action)
    {
        return wrap(super.runAfterBoth(other, action));
    }

    @Override
    public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action)
    {
        return wrap(super.runAfterBothAsync(other, action));
    }

    @Override
    public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor)
    {
        return wrap(super.runAfterBothAsync(other, action, executor));
    }

    @Override
    public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn)
    {
        return wrap(super.applyToEither(other, fn));
    }

    @Override
    public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn)
    {
        return wrap(super.applyToEitherAsync(other, fn));
    }

    @Override
    public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn,
                                                       Executor executor)
    {
        return wrap(super.applyToEitherAsync(other, fn, executor));
    }

    @Override
    public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)
    {
        return wrap(super.acceptEither(other, action));
    }

    @Override
    public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)
    {
        return wrap(super.acceptEitherAsync(other, action));
    }

    @Override
    public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action,
                                                     Executor executor)
    {
        return wrap(super.acceptEitherAsync(other, action, executor));
    }

    @Override
    public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action)
    {
        return wrap(super.runAfterEither(other, action));
    }

    @Override
    public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action)
    {
        return wrap(super.runAfterEitherAsync(other, action));
    }

    @Override
    public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor)
    {
        return wrap(super.runAfterEitherAsync(other, action, executor));
    }

    @Override
    public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)
    {
        return wrap(super.thenCompose(fn));
    }

    @Override
    public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn)
    {
        return wrap(super.thenComposeAsync(fn));
    }

    @Override
    public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn,
                                                     Executor executor)
    {
        return wrap(super.thenComposeAsync(fn, executor));
    }

    @Override
    public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn)
    {
        return wrap(super.exceptionally(fn));
    }

    @Override
    public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action)
    {
        return wrap(super.whenComplete(action));
    }

    @Override
    public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action)
    {
        return wrap(super.whenCompleteAsync(action));
    }

    @Override
    public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action,
                                                  Executor executor)
    {
        return wrap(super.whenCompleteAsync(action, executor));
    }

    @Override
    public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn)
    {
        return wrap(super.handle(fn));
    }

    @Override
    public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn)
    {
        return wrap(super.handleAsync(fn));
    }

    @Override
    public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn,
                                                Executor executor)
    {
        return wrap(super.handleAsync(fn, executor));
    }

    @Override
    public boolean complete(T value)
    {
        return delegate.complete(value);
    }

    @Override
    public boolean completeExceptionally(Throwable ex)
    {
        return delegate.completeExceptionally(ex);
    }
}

RunMode.java

/**
 * Operational modes.
 */
public enum RunMode
{
    /**
     * Optimized for debugging problems (extra runtime checks, logging of the program state).
     */
    DEBUG,
    /**
     * Optimized for maximum performance.
     */
    RELEASE
}

AsynchronousException.java

/**
 * Thrown when an asynchronous operation fails. The stacktrace indicates who triggered the operation.
 */
public final class AsynchronousException extends RuntimeException
{
    private static final long serialVersionUID = 0L;

    public AsynchronousException()
    {
    }
}

用法:

DebugCompletableFuture.wrap(CompletableFuture.supplyAsync(this::expensiveOperation));

好处:您将获得相对干净的异步堆栈跟踪。

缺点:每次创建未来时都构建一个新的AsynchronousException 非常昂贵。具体来说,如果您要生成大量期货,则会在堆上产生大量垃圾,并且 GC 开销会变得很明显。

我仍然希望有人能提出更好的方法。

【讨论】:

  • 在类似的情况下,我使用addSuppressed() 来存储上下文信息,以便保留原始异常,但您会在堆栈跟踪中看到Suppressed:。这避免了在您的代码中处理AsynchrounousException。理想情况下,您会在执行程序中实现它,这样您就不必将它放在任何地方。顺便说一句,您的AsynchronousException 中的the JVM omit the stacktrace 使用此解决方案不是很快吗?
  • @DidierL 看来我每天都学到新东西 :) 直到今天我才听说 JVM 优化堆栈跟踪。谢谢你。我也喜欢你把这段代码移到ExecutorService.submit() 的想法。一旦有机会,我会用这种方法更新这个答案。
  • @DidierL 事实证明,无法按照您的建议在执行程序中实现这一点。 CompletableFuture 调用 Executor.execute(Runnable) 并捕获它提供的 Runnable 内的所有异常。执行器永远不会看到任何异常,因此它无法添加所需的上下文。
  • 啊,是的,我想这是有道理的……看来CompletableFuture API 有改进的余地……
  • 好主意——我希望这不是必需的,但你努力找出答案是件好事!
【解决方案2】:

这可能是由于JVM update 发现堆栈已耗尽发出相同的日志,因此开始忽略它。

解决方案是使用-XX:-OmitStackTraceInFastThrow 标志来防止JVM优化内置异常堆栈跟踪。

【讨论】:

  • 不错的尝试,但不行。这个问题与一遍又一遍地抛出相同的异常无关。您生成的任何异步任务都会发生这种情况。
猜你喜欢
  • 2021-02-25
  • 2018-02-21
  • 2011-01-05
  • 1970-01-01
  • 2017-08-06
  • 2010-09-13
  • 2019-11-05
  • 2015-05-14
相关资源
最近更新 更多