iconara / mikka

JRuby wrapper for Akka
107 stars 14 forks source link

Load balancing failing #3

Closed antunderwood closed 12 years ago

antunderwood commented 12 years ago

If I try the following code to test load balancing over 8 cores, I often find that not all 100 items are processed and the "a message to everyone" message gets printed less that 8 times. Is the code incorrect? Do I need to wait for all jobs to complete?

require 'mikka'

def word_count
  (`curl -s http://www.gutenberg.org/dirs/etext98/2ws3410.txt | wc -w`).match(/\A\s*(\d+)/)[1].to_i
end

class Worker < Mikka::Actor
  def receive(message)
    if message.match(/everyone/)
      puts message
    else
      puts "Working on #{message}"
      puts word_count
      puts
      sleep 1
    end
  end
end

balancer = Mikka.load_balancer(:type => Worker, :count => 8)

100.times do |i|
  balancer << "item #{i}"
end

balancer << Mikka::Messages.broadcast("a message to everyone")
balancer << Mikka::Messages.poison_pill
Mikka.registry.shutdown_all
iconara commented 12 years ago

This is what's happening: the actors are shut down before they have time to process all the messages in their mailboxes by the Mikka.registry.shutdown_all call. If you remove that line you should see the broadcasted message eight times -- but the process will not exit.

I must admit I haven't quite figured out what it is that is still running, the poison pill sent to the balancer should have shut down all the actors you created, but there's still something running in the background.