Hi all,
I've been playing with akka a few years ago, and I'm just back to it, trying to implement some kind of internal queueing mechanism on top of a very big java8 application which perform a lot of message processing.
Background:
The current application is a message processing application which receive JMS messages (using AMQ) and that need to process them.
The application has a notion of a subscriber for a given JMS message type, and when a message is received, all the subscribers which are interested in that message type are executed one after the other.
The problem is that some of those subscribers require some kind of pessimistic locks on some resources, and that causes a lot of threads to just wait for the resource to be acquired before anything else happen.
Solution:
The change I've made (which uses actor system) is that when the JMS message is received, it sends the message to an actor (a MessageConsumerActor) based on the resource that needs to be locked, and then the actor sends the work to a child (a MessageExecutorActor) one at a time to lock the resource, and execute the actual subscriber code.
The MessageConsumerActor is then managing the queue of messages to be processed, and given work to the MessageExecutorActor one at a time, and the MessageExecutorActor is in charge to acquire the lock.
Because the MessageExecutorActor / MessageConsumerActor actor couple is unique for that specific locked resource, no other processes try to lock that resource, and then acquiring the lock is very quick as no other threads try to acquire that lock a the same time. With this the messages get executed nicely from the queue, one after the other.
The class which receive the message from JMS, is something which unfortunately doesn't support asynchronous calls (it needs to fulfill an existing API).
The API consider that once the method returns, then the JMS message is marked as processed, and if it throws an exception, then the transaction which is around the JMS message delivery is failed.
In order to make sure that I don't lose messages, when a JMS message is received, I return only when I get confirmation that the message have been queued by the MessageConsumerActor.
Unfortunately, the only way I've found to achieve this is to use Await.result() method.
Problem:
All this works greatly until I've got too many messages to process, at that time I receive timeouts when calling the Await.result() from the MessageConsumerActor, even though that actor should be very fast to acknowledge the result.
I think the problem might be due to those subscribers which are in general quite slow (from a few seconds to sometime a minute) to execute.
When a message is received on the MessageExecutorActor, it then calls the subscriber call in the same thread, and so that thread is getting busy with some very complex computation for a few seconds.
When there is a lot of messages received, then I've got the feeling that all the threads get busy running into those subscribers, and if another JMS message is received during that time, then I get a timeout due to the MessageConsumerActor not replying quick enough.
What I tried:
What I get from this is that already, I should never call the Await.result() method, but then how can I get the feedback that my message has been queued properly from the JMS thread (thread which has a transaction context associated to it)?
Then the next thing I tried is to use a different dispatcher for the MessageExecutorActor, and another one for the MessageConsumerActor.
I was hoping this would just work by magic, but unfortunately it didn't ;)
The way I've used it is by adding two new dispatcher configuration in my application.conf and then referencing them in the props when creating my actors.
I could see using JVisualVM (and watching logs) that the threads that now performed my application were named by the name of the dispatchers I gave in the configuration.
I've tried lot of different configurations, but none worked, and I'm starting to desperate here.
Here is the last configuration I tried:
# Dispatcher used by the ActorSystemPublisher and MessageConsumerActor to allow messages to be queued
queuing-dispatcher {
type = Dispatcher
executor = "fork-join-executor"
fork-join-executor {
parallelism-min = 1
parallelism-max = 1
}
# Because a single message in general doesn't have more much subscribers, a value of 20 should be enough
throughput = 1
}
# Dispatcher used by the MessageExecutorActor to execute messages by the subscribers
executing-dispatcher {
type = Dispatcher
executor = "thread-pool-executor"
thread-pool-executor {
parallelism-min = 1
parallelism-max = 4
}
# Because a single locked resource could have lot of messages to be processed, and because processing a message
# could be time consuming, we want to allow as much fairness for each resource to be executed.
throughput = 1
}
The queuing-dispatcher is used by the MessageConsumerActor, and the executing-dispatcher is used by the MessageExecutorActor.
As you can see I'm trying to use fork-join-executor and thread-pool-executor combinations, and all the possible variations, but none worked.
I tried different parallelism, but none work neither.
I tried to force a single thread for the MessageExecutorActor using parallelism-max=1 but that didn't work neither :(
Could someone please let me know how this kind of issue is being solved?
Am I on the right track with the dispatcher configuration?
Do I need to use some kind of routers, and if so, how?
Thanks in advance for all the suggestions :)
Cheers,
/Benoit