Skip to content

Commit

Permalink
Clear context propagation virtual field (#12397)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Oct 8, 2024
1 parent 2edd778 commit 77be7c6
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ public static PropagatedContext enter(@Advice.Argument(1) SystemMessage systemMe

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(1) SystemMessage systemMessage,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<SystemMessage, PropagatedContext> virtualField =
VirtualField.find(SystemMessage.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(
propagatedContext, throwable, virtualField, systemMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public static PropagatedContext enterDispatch(@Advice.Argument(1) Envelope envel

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitDispatch(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(1) Envelope envelope,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Envelope, PropagatedContext> virtualField =
VirtualField.find(Envelope.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, envelope);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ public static PropagatedContext enterJobSubmit(@Advice.Argument(0) ForkJoinTask<

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(0) ForkJoinTask<?> task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<ForkJoinTask<?>, PropagatedContext> virtualField =
VirtualField.find(ForkJoinTask.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ public static <T> PropagatedContext attachContextToTask(
* Clean up {@code propagatedContext} in case of any submission errors. Call this method after the
* submission method has exited.
*/
public static void cleanUpAfterSubmit(
@Nullable PropagatedContext propagatedContext, @Nullable Throwable throwable) {
public static <T> void cleanUpAfterSubmit(
@Nullable PropagatedContext propagatedContext,
@Nullable Throwable throwable,
VirtualField<T, PropagatedContext> virtualField,
T task) {
if (propagatedContext != null && throwable != null) {
/*
Note: this may potentially clear somebody else's parent span if we didn't set it
Expand All @@ -106,6 +109,8 @@ public static void cleanUpAfterSubmit(
exceptions.
*/
propagatedContext.clear();
// setting the field to null removes it from the fallback map
virtualField.set(task, null);
}
}

Expand All @@ -119,6 +124,8 @@ public static <T> void cleanPropagatedContext(
PropagatedContext propagatedContext = virtualField.get(task);
if (propagatedContext != null) {
propagatedContext.clear();
// setting the field to null removes it from the fallback map
virtualField.set(task, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static <T> Scope makePropagatedContextCurrent(
VirtualField<T, PropagatedContext> virtualField, T task) {
PropagatedContext propagatedContext = virtualField.get(task);
if (propagatedContext != null) {
// setting the field to null removes it from the fallback map
virtualField.set(task, null);
Context context = propagatedContext.getAndClear();
if (context != null) {
return context.makeCurrent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ public static PropagatedContext enterJobSubmit(

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(0) Runnable task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}

Expand All @@ -126,8 +130,12 @@ public static PropagatedContext enterJobSubmit(@Advice.Argument(0) ForkJoinTask<

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(0) ForkJoinTask<?> task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<ForkJoinTask<?>, PropagatedContext> virtualField =
VirtualField.find(ForkJoinTask.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}

Expand All @@ -148,6 +156,7 @@ public static PropagatedContext enterJobSubmit(

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Argument(0) Runnable task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable,
@Advice.Return Future<?> future) {
Expand All @@ -156,7 +165,9 @@ public static void exitJobSubmit(
VirtualField.find(Future.class, PropagatedContext.class);
virtualField.set(future, propagatedContext);
}
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}

Expand All @@ -176,6 +187,7 @@ public static PropagatedContext enterJobSubmit(@Advice.Argument(0) Callable<?> t

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Argument(0) Callable<?> task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable,
@Advice.Return Future<?> future) {
Expand All @@ -184,7 +196,9 @@ public static void exitJobSubmit(
VirtualField.find(Future.class, PropagatedContext.class);
virtualField.set(future, propagatedContext);
}
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
VirtualField<Callable<?>, PropagatedContext> virtualField =
VirtualField.find(Callable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}

Expand Down Expand Up @@ -230,7 +244,8 @@ public static void submitExit(
VirtualField<Callable<?>, PropagatedContext> virtualField =
VirtualField.find(Callable.class, PropagatedContext.class);
PropagatedContext propagatedContext = virtualField.get(task);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
ExecutorAdviceHelper.cleanUpAfterSubmit(
propagatedContext, throwable, virtualField, task);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ public static PropagatedContext enterFork(@Advice.This ForkJoinTask<?> task) {

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitFork(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.This ForkJoinTask<?> task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<ForkJoinTask<?>, PropagatedContext> virtualField =
VirtualField.find(ForkJoinTask.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public static PropagatedContext enterCallableFork(@Advice.Argument(0) Callable<?

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitCallableFork(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(0) Callable<?> task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Callable<?>, PropagatedContext> virtualField =
VirtualField.find(Callable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ public static PropagatedContext addListenerEnter(@Advice.Argument(0) Runnable ta

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void addListenerExit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(0) Runnable task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public static PropagatedContext enterJobSubmit(@Advice.Argument(0) Runnable task

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(0) Runnable task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ public static PropagatedContext onEnter(@Advice.Argument(0) Runnable call) {

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(0) Runnable call,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, call);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ public static PropagatedContext onEnter(@Advice.Argument(0) Runnable call) {

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(0) Runnable call,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, call);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ public static PropagatedContext enter(@Advice.Argument(1) SystemMessage systemMe

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(1) SystemMessage systemMessage,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<SystemMessage, PropagatedContext> virtualField =
VirtualField.find(SystemMessage.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(
propagatedContext, throwable, virtualField, systemMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public static PropagatedContext enterDispatch(@Advice.Argument(1) Envelope envel

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitDispatch(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(1) Envelope envelope,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Envelope, PropagatedContext> virtualField =
VirtualField.find(Envelope.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, envelope);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ public static PropagatedContext enterJobSubmit(@Advice.Argument(0) ForkJoinTask<

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(0) ForkJoinTask<?> task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<ForkJoinTask<?>, PropagatedContext> virtualField =
VirtualField.find(ForkJoinTask.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ public static PropagatedContext enterJobSubmit(@Advice.Argument(0) Runnable task

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(0) Runnable task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public static PropagatedContext enterJobSubmit(@Advice.Argument(1) Runnable task

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void exitJobSubmit(
@Advice.Enter PropagatedContext propagatedContext, @Advice.Thrown Throwable throwable) {
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable);
@Advice.Argument(1) Runnable task,
@Advice.Enter PropagatedContext propagatedContext,
@Advice.Thrown Throwable throwable) {
VirtualField<Runnable, PropagatedContext> virtualField =
VirtualField.find(Runnable.class, PropagatedContext.class);
ExecutorAdviceHelper.cleanUpAfterSubmit(propagatedContext, throwable, virtualField, task);
}
}
}

0 comments on commit 77be7c6

Please sign in to comment.