'Akka Finite State Machine and how to protocol Behaviors.unhandled?

I have a question about Behaviors.unhandled, I know that Akka sends the unhandled message to the Dead Letter and with the following configuration it also logs it

akka {
 loglevel = "DEBUG"
 actor {
   debug {
    # enable DEBUG logging of unhandled messages
   unhandled = on
   }
  }
}

This is before disputed in the following issue Behaviours.unhandled and argued for typical way that Akka works, this is acceptable and should also only logged in DEBUG level.

At this point my but comes, Finite State Machine is another layer over Akka and in Finite State Machine concepts this situation relates to undesigned¿handled transition and as the Original Poster of the Issue claims is a bug which is really important in the iterative approach of designing Finite State Machines.

A typical FSM configuration will llok like the following for Akka FSM,

private val NEW: Behavior[Event] = {
  Behaviors.receive {
    case (ctx, onUserClicked(payload)) =>
      //doSomething()

    case _ => Behaviors.unhandled
  }
}

now if to our surprise, if user manage to produce during NEW State, onUserDoubleClicked(payload) Event then this is a design error at our side.

The problem is, most of us will not run our applications in DEBUG level in Production, if we experience an unhandled transition in Production, we will never discover this and fix in the next iteration.

To be able to track this that we have to subscribe to Dead Letters does not pass at all to FSM concepts....

For this reason, I think even this situation is acceptable for normal Akka, with Finite State Machine concepts this situation must be logged at least in WARN level or an configuration option must be provided to configure the Behavior or do you see any other way to achieve this without huge code clutter?



Solution 1:[1]

with the tips given in the previous answers and my own ideas to use AspectJ to identify the places the code reaches 'unhandled' Behaviour, I developed an Aspect that capture unhanded Events in Akka Finite State Machines, which help identifying missing design elements and bugs in an iterative approach.

You can find the implementation details in my blog which explains how AspectJ can help in an Scala State Machine.

It capture the moment the unhandled is called and help identifying in which State, transition unhandled reached, what was the Event payload and a Snapshot of informations hold under this State.

Solution 2:[2]

I am not sure if this is the most accurate answer for your question, but I will share what has worked for us for this use case.

Check this out: https://github.com/bilal-fazlani/fsm-with-akka-typed

We tried to extract a pattern for FSM using akka typed. The key to this pattern is a custom receive function i.e. myReceive.

This allows to not only log unhandled messages ourself at any level but also send a reply to the sender that their message was Unhandled. Another benefit to this pattern is - since we created separate message hierarchies such as ReadBehaviorMessage and WriteBehaviorMessage, we are now forced to handled all messages "of a particular state"; otherwise compilers shows warning.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 posthumecaver
Solution 2 Bilal Fazlani