Parallelism in SonataFlow

This document describes how you can run parallel tasks in SonataFlow.

The testing procedure described in this document is based on the serverless-workflow-service-calls-quarkus example application in GitHub repository.

SonataFlow serializes the execution of parallel tasks. Therefore, the word "parallel" does not indicate simultaneous execution, but it means that there is no logical dependency between the execution of branches. An inactive branch can start or resume the execution of a task without waiting for an active branch to be completed, in case the latter suspends the execution (for example, wait for an event reception).

The serverless-workflow-service-calls-quarkus example application is a workflow calculates the double and the half of a given number.

Creating a parallel workflow

You can create a workflow, which performs a series of parallel tasks.

You can define the "completionType": "atLeast" to run only some branches in parallel workflow, instead of defining "completionType": "allOf". When you define "completionType": "atLeast", you also need to define the minimum number of branches that you want to run by defining the "numCompleted": property.

Prerequisites
Procedure
  1. Create a workflow file named as parallel.sw.json under the src/main/resources/ directory.

  2. Add the following content to the parallel.sw.json file:

    Example content for parallel.sw.json file
    {
      "id": "parallel",
      "version": "1.0",
      "specVersion": "0.8",
      "name": "Welcome to the Parallel dimension",
      "description": "Testing parallelism",
      "start": "Parallel",
      "functions": [
        {
          "name": "half",
          "type": "expression",
          "operation": "{half:.number/2}"
        },
        {
          "name": "double",
          "type": "expression",
          "operation": "{double:.number*2}"
        }
      ],
      "states": [
        {
          "name": "Parallel",
          "type": "parallel",
          "branches": [
            {
              "name": "branchA",
              "actions": [
                {
                  "functionRef": "half"
                }
              ]
            },
            {
              "name": "branchB",
              "actions": [
                {
                  "functionRef": "double"
                }
              ]
            }
          ],
          "numCompleted": "2",
          "completionType": "atLeast",
          "end": true
        }
      ]
    }

    == Running a parallel workflow

    After you create a workflow that performs a series of parallel tasks, you can run the workflow.

    Prerequisites

    You can start the workflow application in development mode by using the following command:

mvn quarkus:dev
Procedure
  1. To run the created parallel workflow, send a request to the /parallel endpoint as shown in the following example request:

Example request
curl -X 'POST' \
  'http://localhost:8080/parallel' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{"number":2}'
Example response
{"id":"358f97ba-f0f9-4f25-86cc-4b35e85c2406","workflowdata":{"half":"1","double":4}}

Found an issue?

If you find an issue or any misleading information, please feel free to report it here. We really appreciate it!