I am writing a calculation engine (implemented as an Akka Actors) that will use a few asynchronous and concurrent actors to perform some data retrieval as well as some parts of the calculation.
When the main actor receives a message to start the calculation, it must retrieve three distinct pieces of data from the DB before performing the calculation. To do this data retrieval, I am spinning up 3 actors, one for each data retrieval.
Before I can move on to the next step I need all three data sets. However, because they are being retrieved asynchronously and concurrently, I have no way of knowing which one will be returned first.
My first instinct is to maintain a flag (boolean) in my main actor for each process. As each child actor sends back the data set the corresponding flag in my main actor is set to true, and the other two flags are checked to see if were ready to move on.
As I learn more about FSM's, I believe there could be a fit for what I am doing. However, some of my "states" could exist at once. For example, the states "WaitingForData1", "WaitingForData2", and "WaitingForData3" could/will all co-exist. That does not seem to be the right thing to do in an FSM. An alternative would be to create a state for each permeation of the other states. For example:
- State 1: Waiting for Data 1, Data 2 and Data 3
- State 2: Data 1 Ready, Waiting for Data 2 and Data 3
- State 3: Data 2 Ready, Waiting for Data 1 and Data 3
- State 4: Data 3 Ready, Waiting for Data 1 and Data 2
- State 5: Data 1 and Data 2 Ready, Waiting for Data 3
... - State N: Data 1, Data 2 and Data 3 are Ready
This seems to be a bit of overkill for what I am doing, but may in fact be the best way to implement an FSM which uses asynchronous and concurrent processes.
Any advice from you FSM experts would be greatly appreciated.