Here is my scenario:
I have a master actor, which receives messages from multiple child actors. These messages contain data to be aggregated. In this aggregation logic, do I need to take care of synchronization issues, if I use a shared data structure to collect the aggregation?
else if(arg0 instanceof ReducedMsg){
ReducedMsg reduced = (ReducedMsg)arg0;
counter.decrementAndGet();
synchronized(finalResult){
finalResult.add((KeyValue<K, V>) reduced.getReduced());
if(counter.get() == 0){
if(checkAndReduce(finalResult)){
finalResult.clear();
}
else{
stop();
latch.countDown();
}
}
}
}
So as you can see I have a finalResult, to which each message will be aggregated, and after a processing logic the collection needs to be cleared as well.
Actually what I am trying to implement is a recursive (associative) reduction mapreduce. So I need to keep the synchronized block I assume? Or is it by any chance Akka executes the onReceive one thread at a time?
This logic produces accurate and predictable result on small data set. My problem is when my input data set is a little large, the code hangs. I want to be sure that is because of the context switches for my synchronization block, so that I may dwelve into a different design.