X

Antony Reynolds' Blog

  • February 14, 2005

Hidden Sequences in BPEL

Antony Reynolds
Senior Director Integration Strategy

Gotchas with Parallel Execution



I just had a phone call from one of my team - "Help I loaded a BPEL process into BPEL Designer and it shows a series of sequential steps as parallel activities, why?". Lets look at some similar BPEL code and find out what is happening.



A Parallel Mystery



The picture below shows a sample flow.

Image of four parallel sequences, each with an assign and a wait

We would expect all four assignments to be executed in parallel and all four paths to complete at about the same time.

All the paths are similar to the following code.




<sequence name="flow-sequence-4">

<assign name="D">

<copy>

<from expression="ora:getCurrentTime("HH:mm:ss")"/>

<to variable="timestampD"/>

</copy>

</assign>

<wait name="wait-1" for="'PT2S'"/>

</sequence>


This code copies the current time into a variable and then waits for two seconds before completing. When I execute the above sequence however I get the following values in my variables



<li/>timestampA = 22:49:00

<li/>timestampB = 22:49:02

<li/>timestampC = 22:49:04

<li/>timestampD = 22:49:00

Instead of them all executing in parallel, only A and D have executed in parallel, whilst C seems to have executed in sequence with B which has executed in sequence with A.



The Mysteries of "link" Uncovered



All is explained by looking at the following snippet of code for paths A and B.




<sequence name="flow-sequence-1">
<source linkName="AtoB"/>

<assign name="A">

<copy>

<from expression="ora:getCurrentTime("HH:mm:ss")"/>

<to variable="timestampA"/>

</copy>

</assign>

<wait name="wait-1" for="'PT2S'"/>

</sequence>

<sequence name="flow-sequence-2">
<target linkName="AtoB"/>

<source linkName="BtoC"/>

<assign name="B">

<copy>

<from expression="ora:getCurrentTime("HH:mm:ss")"/>

<to variable="timestampB"/>

</copy>

</assign>

<wait name="wait-1" for="'PT2S'"/>

</sequence>


Note the source tag in flow-sequence-1. This says that there is a link between this tag and another tag in the same scope. The other tag cannot start executing until this tag has completed. Note also the target tag in flow-sequence-2. This says that this tag cannot start until another tag (sequence 1 in this example) has completed. We need to declare the link names in the scope in which they will be used. Hence in the flow tag we find the following declarations.




<flow name="flow-1">

<links>

<link name="AtoB"/>

<link name="BtoC"/>

</links>


The link tag allows us to declare dependencies between tasks in a flow. In the above example they force an ordering on the parallel execution, because C depends on B which depends on A. D doesn't depend on any other task so A and D execute in parallel whilst A, B and C execute in that sequence.



But Why?



So why go to all this trouble when we could have just had a sequence construct for A,B,C and put in parallel with D by using a 2-way flow instead of the misleading 4-way that we showed. Well to start with, we can now impose ordering requirements whilst allowing the maximum of parallel execution. The dependencies are resolved and enforced by the BPEL runtime, freeing us to think about the dependencies rather than the mechanism of ordering them



What is to stop me creating a flow that has A depend on B, B depend on C, and C depend on A? Well, this creates a cyclic graph and can't be executed. BPEL specifically excludes any dependency graphs that are cyclic, all dependencies must form acyclic graphs.



Gotcha



So next time you are looking at your BPEL code, make sure that you don't get caught out by link elements enforcing an unexpected ordering when you thought there was parallel execution. Now I need to phone a colleague....



References



The ultimate source on BPEL syntax and semantics is found in the BPEL spec. This document contains a detailed description of links and how they affect BPEL processing. Enjoy your reading.



Sample Code



A have uploaded a sample BPEL file and corresponding WSDL document so you can try this at home. Try altering the source and target links to change the dependency graph, or move a target to be in a wait statement and see what effect that has. Enjoy!

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.