Parallel Wait State Design Pattern
Real-time software sometimes involves handling parallel interactions with multiple entities to initiate some action. Further action can be initiated only after response message has been received from all the entities. Parallel Wait state pattern handles such scenarios.
The figure below shows a typical parallel operation. Here A initiates a parallel operation by sending a request to B, C and D. B, C and D perform the operation in parallel and respond back to A.
The main intention here is to initiate similar activity on multiple entities in parallel. Since activities are initiated in parallel, the time taken to perform the task is more or less independent of the number of entities involved in the interaction. This is faster in comparison to Serial Wait State pattern where one message is sent to initiate an activity on an entity, its response is awaited and only on receipt of the response same activity is initiated on the next entity.
The main objective of this design pattern is to efficiently handle scenarios where sending of similar message to multiple entities to initiate similar activity in parallel is involved. Here, a response is expected from each entity after the entity finishes the activity. This also involves counting the received response messages and recognizing the point where collection has been completed.
Also Known As
- Simultaneous Action State Pattern
- Parallel Interaction
Main motivation to use Parallel Wait State is to speed up operations with multiple entities. If a particular operation takes a long time to perform, initiating the operation in parallel will be the most time efficient way of performing that operation. Such designs scale very well with increase in number of entities handled by the system.
The Parallel Wait State pattern can be used wherever multiple operations need to be initiated on multiple entities. This pattern can be applied when parallel operation does not cause an undue loading of the systems. This design pattern should not be used in scenarios where each operation is extremely resource intensive and parallel operation might consume too many system resources at the same time.
This pattern consists of the following event handlers:
- Parallel Message Sending Routine: This routine sends message to multiple entities in one shot. This routine also starts a timer to keep track of parallel response collection.
- Parallel Response Handler: This handler receives the response messages being collected and takes a decision when collection has been completed. The collection is assumed to have been completed, when the parallel response count matches the number of requests sent to multiple entities.
- Timeout Handler: When a timeout takes place, the parallel message sending routine calls this handler. If the parallel response count is less than the number of messages sent, timeout signals failure of response message collection. Otherwise, timeout signals a successful response message collection. Depending on requirement, the timeout handler retries by resending the command message to the entity that timed out and keeps a timer to await the response.
The key participants in this pattern are the task sending the message to multiple entities and the tasks on these entities that receive this command message, perform the desired operation and send back the response message.
The state machine implementing the parallel state pattern collaborates with the tasks on the entities where some desired operation needs to be performed. The state machine keeps a timer to keep track of the operation being performed at each entity.
Parallel state pattern can result in triggering a state transition corresponding to successful collection of response messages. If a timeout takes place before the collection of response messages has been completed, a state transition to an error handling state might be initiated.
Implementing the Parallel Wait State Pattern involves sending request messages in parallel and waiting till all responses are collected. The implementation sequence is illustrated by the following example based on digital trunk diagnostics.
In the Xenon switching system XEN Configuration Manager (XCM) needs to initiate diagnostics on all the digital trunk circuits controlled by the XEN. This is done by sending diagnostics request message to all the circuits and waiting for response from each circuit. Once the responses are received from all the circuits, XCM responds back to the initiator.
- Parallel Wait State is invoked to start diagnostics on all the digital trunks in the XEN.
- Parallel Wait State loops through the list of digital trunk circuits and sends a diagnostics request to each circuit. A timer is initiated to wait for diagnostics response. A counter is incremented to keep track of number of requests.
- The diagnostics progress in parallel and the digital trunk cards respond back on completion of diagnostics.
- The Parallel Wait State receives the responses from all the digital trunk. It decrements the counter when a response is successfully received.
- When the counter goes down to zero, all responses have been received, so the aggregate response can be sent back.
Sample Code and Usage
Here is the sample code for the digital trunk diagnostics example:
This design pattern has been used in the following cases:
- Initiating diagnostics on multiple entities
- Parallel initialization of multiple modules during system initialization.
- Polling of multiple entities in parallel