public static void suspendIfNecessary(Procedure<?> proc,
    Consumer<CompletableFuture<Void>> setFuture, CompletableFuture<Void> future,
    MasterProcedureEnv env, Runnable actionAfterDone)
    throws IOException, ProcedureSuspendedException {
    MutableBoolean completed = new MutableBoolean(false);
    Thread currentThread = Thread.currentThread();
    // This is for testing. In ProcedureTestingUtility, we will restart a ProcedureExecutor and
    // reuse it, for performance, so we need to make sure that all the procedure have been stopped.
    // But here, the callback of this future is not executed in a PEWorker, so in ProcedureExecutor
    // we have no way to stop it. So here, we will get the asyncTaskExecutor first, in the PEWorker
    // thread, where the ProcedureExecutor should have not been stopped yet, then when calling the
    // callback, if the ProcedureExecutor have already been stopped and restarted, the
    // asyncTaskExecutor will also be shutdown so we can not add anything back to the scheduler.
    ExecutorService asyncTaskExecutor = env.getAsyncTaskExecutor();
    FutureUtils.addListener(future, (r, e) -> {
      if (Thread.currentThread() == currentThread) {
        LOG.debug("The future has completed while adding callback, give up suspending procedure {}",
          proc);
        // this means the future has already been completed, as we call the callback directly while
        // calling addListener, so here we just set completed to true without doing anything
        completed.setTrue();
        return;
      }
      LOG.debug("Going to wake up procedure {} because future has completed", proc);
      // This callback may be called inside netty's event loop, so we should not block it for a long
      // time. The worker executor will hold the execution lock while executing the procedure, and
      // we may persist the procedure state inside the lock, which is a time consuming operation.
      // And what makes things worse is that, we persist procedure state to master local region,
      // where the AsyncFSWAL implementation will use the same netty's event loop for dealing with
      // I/O, which could even cause dead lock.
      asyncTaskExecutor.execute(() -> wakeUp(proc, env));
    });
    if (completed.getValue()) {
      FutureUtils.get(future);
      actionAfterDone.run();
    } else {
      // suspend the procedure
      setFuture.accept(future);
      proc.skipPersistence();
      suspend(proc);
    }
  }
A helper class for switching procedure out(yielding) while it is doing some time consuming
operation, such as updating meta, where we can get a {@link CompletableFuture} about the
operation.
hbase-server/src/main/java/org/apache/hadoop/hbase/procedure2/ProcedureFutureUtil.java