0

I'm new to the actor model and was trying to write a simple example. I want to traverse a directory tree using Scala and Akka. The program should find all files and perform an arbitrary (but fast) operation on each file.

I wanted to check how can I model recursion using actors? How do I gracefully stop the actor system when the traversal will be finished? How can I control the number of actors to protect against out of memory? Is there a way to keep the mailboxes of the actors from growing too big? What will be different if the file operation will take long time to execute?

Any help would be appreciated!

1 Answers1

1
  1. Actors are workers. They take work in and give results back, or they supervise other workers. In general, you want your actors to have a single responsibility.
  2. In theory, you could have an actor that processes a directory's contents, working on each file, or spawning an actor for each directory encountered. This would be bad, as long file-processing time would stall the system.
  3. There are several methods for stopping the actor system gracefully. The Akka documentation mentions several of them.
  4. You could have an actor supervisor that queues up requests for actors, spawns actors if below an actor threshold count, and decrementing the count when actors finish up. This is the job of a supervisor actor. The supervisor actor could sit to one side while it monitors, or it could also dispatch work. Akka has actor models the implement both of these approaches.
  5. Yes, there are several ways to control the size of a mailbox. Read the documentation.
  6. The file operation can block other processing if you do it the wrong way, such as a naive, recursive traversal.

The first thing to note is there are two types of work: traversing the file hierarchy and processing an individual file. As your first implementation try, create two actors, actor A and actor B. Actor A will traverse the file system, and send messages to actor B with the path to files to process. When actor A is done, it sends an "all done" indicator to actor B and terminates. When actor B processes the "all done" indicator, it terminates. This is a basic implementation that you can use to learn how to use the actors.

Everything else is a variation on this. Next variation might be creating two actor B's with a shared mailbox. Shutdown is a little more involved but still straightforward. The next variation is to create a dispatcher actor which farms out work to one or more actor B's. The next variation uses multiple actor A's to traverse the file system, with a supervisor to control how many actors get created.

If you follow this development plan, you will have learned a lot about how to use Akka, and can answer all of your questions.

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42