3

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.

garyKeorkunian
  • 421
  • 4
  • 7
  • Since it seems that the main actor doesn't care which order the data sets are completed, I think there should at most these states: "waiting for 3 data sets", "waiting for 2 data sets", waiting for 1 data set", "all data sets ready to go". I'm not familiar with Akka so what I'm about to say might not apply, but if I were doing this in C, I'd just have a "waiting for data sets to be ready" state that counts the completion events (but doesn't change to a new state), and when they're all ready move to a state that acts on them. – Michael Burr May 14 '12 at 19:07
  • Sorry, not a full-fledged answer (in the very sense of the word): the only thing missing in the current FSM implementation is support for nested state machines. Then you could have three of them handling the three queries and have the overall machine respond to the event that all three of them reached their final state. That would be a nice project for someone with a bit of spare time … – Roland Kuhn May 21 '12 at 07:03
  • 1
    This question is old, however I stumbled across it looking up FSM info. The scenario where you're making 3 unrelated database calls in no particular order or sequence, could probably be more simply executed using an actor as a correlation point, and using futures to execute the query's. Then fold each response into a common class, or upon the list of futures being processed, pipe the result back to the actor and handle any calculation as an event post data retrieval. FSM may be a little much for said scenario. – byte-crunch Feb 18 '17 at 08:17

0 Answers0