简体   繁体   中英

How does AWS Step Functions handle Worker/Activity race conditions?

I am trying to use Java with Spring Boot framework with AWS Step Functions as one of the example below. In here, we setup a Runnable thread in Java along with the service starts up, and register this thread as a "worker" thread which is an Activity in AWS Step Function.

Here is my question:

How does AWS Step Function handles race condition when multiple worker of the same Activity (same ARN) is registered, how does Step Function avoids more than 1 worker picks up the Task and executing on duplicated work? In the AWS Step Functions config, is there any setting we can change to prevent such race condition occur in more than 1 worker in the same AZ, same region?


    import com.amazonaws.services.stepfunctions.AWSStepFunctions;
    import com.amazonaws.services.stepfunctions.model.GetActivityTaskRequest;
    import com.amazonaws.services.stepfunctions.model.GetActivityTaskResult;

    @Component
    class MyActivity implements DisposableBean, Runnable {

      private final int WAITS_FOR_ACTIVITY_MILLISECONDS = 500;
    

      public void run() {
        while (shouldRun) {
          GetActivityTaskResult getActivityTaskResult =
              client.getActivityTask(
                  new GetActivityTaskRequest()
                      .withActivityArn(config.getActivityArns().getMyActivity()));
          String taskToken = getActivityTaskResult.getTaskToken();
          if (getActivityTaskResult.getTaskToken() != null) {
            try {

              // Get input
              JsonNode json = Jackson.jsonNodeOf(getActivityTaskResult.getInput());
              ActivityInput input =
                  gson.fromJson(json.toString(), ActivityInput.class);

              // do some work

              
              client.sendTaskSuccess(
                  new SendTaskSuccessRequest()
                      .withOutput(gson.toJson(output))
                      .withTaskToken(taskToken));
            } catch (Exception e) {
              logger.error(e.getMessage());
              e.printStackTrace();
              client.sendTaskFailure(
                  new SendTaskFailureRequest().withTaskToken(taskToken).withError(e.getMessage()));
            }
          } else {
            try {
              Thread.sleep(WAITS_FOR_ACTIVITY_MILLISECONDS);
            } catch (InterruptedException e) {
              logger.error(e.getMessage());
              e.printStackTrace();
            }
          }
        }
      }
  


      @Override
      public void destroy() {
        shouldRun = false;
      }

    }

Multiple activity workers may poll for the same type of activity. Step Functions manages the many to many relationships between multiple concurrently executing state machines and multiple activity workers polling for work.

Each time an activity worker successfully polls for work from an executing state machine in the activity task state of interest, Step Functions dispatches a unique token to the activity worker along with the JSON input to the task state.

The next activity worker polling will not be assigned this same task. When the activity worker calls the Step Functions API with ActivityTaskSuccess or ActivityTaskFailure, it returns a result and the token. Step Function uses the token to match the result to the appropriate state machine. For large workloads, you may create Auto Scaling groups of activity workers and scale your workers in response to demand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM