Workflow Pause on Activity Failure with callback using WorkflowThreadLocal

Slack Thread: Slack

2. What if client want to have diff callbacks (i.e. compensation activity) whenever they have to execute activity in workflow it could be same ActivityType or diff but different callback action can be provided in multiple places in workflow, since we’re store the callback in WorkflowThreadLocal what’s your suggestion to store all such callbacks in WorkflowThreadLocal or some other data structure so that when runCallback() called by workflowOutboundInterceptor just before pausing the workflow should be able to identify which callback to pick from it? Since there could be possibility that concurrent activity might be executing for a workflow. Any suggestion will be helpful. Following is the ActivityFailureCallback sample code for single callback which will be set when invoked and unset when it’s done which works fine in sequentially executing activities of workflow one by one, but what if we have concurrent activities of a workflow then will that cause any issue? WorkflowThreadLocal is workflow type level isolation not activity level not workflow instance level for storing callback, right?

public class ActivityFailureCallback {
  private static final WorkflowThreadLocal<Runnable> callbackLocal =
          WorkflowThreadLocal.withCachedInitial(() -> null);

  public static <R> R invoke(Supplier<R> activityInvocation, Runnable compensationCallback) {
    callbackLocal.set(compensationCallback);
    try {
      return activityInvocation.get();
    } finally {
      callbackLocal.set(null);
    }
  }

  /** Called by the interceptor when any activity fails. */
  protected static void runCallback() {
    Runnable cb = callbackLocal.get();
    if (cb != null) {
      cb.run();
    }
  }
}

3. Does WorkflowThreadLocal means each thread executing one workflow and it’s all activity so they can lookup in that variable without any issues with workflow level isolation? let say multiple activity executing on different workers then whichever thread they’re executing will maintain a copy of that WorkflowThreadLocal variable? but on single worker it’s always on single thread only for a workflow, right?

Thanks in advance!!