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;
}
}