When data in a workflow passes through multiple states, each state receives the output of the previous state as input. Passing variables state by state complicates cross-state data passing. CloudFlow provides a global variable mechanism to simplify this process and improve efficiency.
Limits
The following table describes the limits on global variables. If the maximum value cannot meet your business requirements, submit a ticket to increase the value.
Item | Maximum value |
Length of a variable key | 128 Bytes |
Length of a variable value | 256 KiB |
Notes
$OutputinGlobalConstructorrefers to the raw output of the Task state instead of the construction result ofOutputConstructor.$GlobalinOutputConstructorrefers to the raw value before the construction made byGlobalConstructor.
Limits on context size
When CloudFlow passes data by using $Global, the outputs of InputConstructor and OutputConstructor must meet the limit that the context size cannot be greater than 64 KiB.
Race condition
Global variables provide execution-level global key-value storage space, which is readable and writable in all states throughout the execution lifecycle. When parallel logic, such as map and parallel, exists in a flow, data may be overwritten between different states. Take note of the following suggestions:
Use different keys to write global variables into different branches of Parallel states.
Do not write global variables into Map states. All iteration definitions of Map are the same. Therefore, the variable names in GlobalConstructor are the same. The same variable names written from different branches overwrite each other.
A child execution in distributed mode cannot access the global variables of the parent execution because the
Executionlevels of the child and parent executions are different.
Use global variables to pass data
Global variables provide execution-level key-value storage space. Any state in a workflow can write data to the global variable at the end of the state to pass the data to other states for consumption.
In the preceding figure, State A uses GlobalConstructor to store the data that corresponds to $Input.key_a, and comes from the upstream input to variable_a in the global variables. State A also uses GlobalConstructor to store the data corresponding to $Output.key_b in the output of State A to variable_b in the global variables. The workflow uses InputConstructor to extract values of variable_a and variable_b in the global variables and passes the values to State Z as inputs. This way, data is passed across states.
The inputs and outputs of CloudFlow workflows are used to explicitly pass data between tasks to ensure the independence and clarity of tasks. Global variables are used to maintain cross-task sharing states or global resource configurations. Inputs, outputs, and global variables are used together to achieve an efficient and flexible system design. The preceding figure compares InputConstructor, OutputConstructor, and GlobalConstructor. Description:
InputConstructor($Context,$Global),OutputConstructor($Context,$Global), andGlobalConstructor($Context,$Global)indicate that InputConstructor, OutputConstructor, and GlobalConstructor can reference$Contextand$Globalto extract information.$Global=Merge($Global,GlobalConstructor($Context,$Global))indicates that the variables constructed by the global constructor are merged with the existing variables in$Global, and then all variables are stored in$Globalto update shared global variables.$Outputis the result of the Task call. If the call is initiated in asynchronous callback mode, the value of$Outputis the result of the callback.$Globalindicates global variables, which are accessible throughout the workflow execution period.
Creation and update of global variables
Global variables are created and updated by using GlobalConstructor. When a state execution ends, GlobalConstructor is executed. The following state machine definition shows how to use GlobalConstructor.
Type: StateMachine
Name: MyWorkFlow
SpecVersion: v1
StartAt: InvokeFunction
States:
- Type: Task
Name: InvokeFunction
Action: FC:InvokeFunction
TaskMode: RequestComplete
Parameters:
resourceArn: >-
acs:fc:cn-hangzhou:123456:functions/myFunction/LATEST
invocationType: Sync
body.$: $Input
GlobalConstructor:
InvokeFunctionResultHeader.$: $Output.Header
InvokeFunctionResultBody.$: $Output.Body
End: trueThe data structure of
GlobalConstructoris Map[String]Any.InvokeFunctionResultHeader.$: $Output. HeaderandInvokeFunctionResultBody.$: $Output. Bodyindicate that the header and body of the returned function value are separately stored in the InvokeFunctionResultHeader and InvokeFunctionResultBody keys in the global variables.
Reading of global variables
CloudFlow defines the $Global keyword for reading global variables. In all scenarios that support expression construction, the global storage space of the current execution can be accessed by using $Global. States can extract the values from the global storage space by using explicit keys. The following state machine definition shows how to use $Global.
Type: StateMachine
Name: MyWorkFlow
SpecVersion: v1
StartAt: InvokeFunction1
States:
- Type: Task
Name: InvokeFunction1
Action: FC:InvokeFunction
TaskMode: RequestComplete
Parameters:
invocationType: Sync
resourceArn: acs:fc:cn-hangzhou:123456:functions/myFunction/LATEST
GlobalConstructor:
InvokeFunctionPayload.$: $Output.Body
Next: InvokeFunction
- Type: Task
Name: InvokeFunction
Action: FC:InvokeFunction
TaskMode: RequestComplete
Parameters:
resourceArn: acs:fc:cn-hangzhou:123456:functions/myFunction/LATEST
invocationType: Sync
body.$: $Global.InvokeFunctionPayload
OutputConstructor:
demo.$: $Global
End: true
When a state is executed, the
InvokeFunctionfunction executesGlobalConstructorto store the body of the return value ofInvokeFunction1in theInvokeFunctionPayloadkey in the global variables.InvokeFunctionuses the$Global.InvokeFunctionPayloadexpression inParametersto extract the value of theInvokeFunctionPayloadkey from the global storage space and passes the value as thebodyfield to theTaskstate for execution.InvokeFunctioncan use OutputConstructor to view the values in$Global.