Today, ther have been some talking about concurrency in Clojure vs Scala. Most of the debate was based on an excellent article Scala vs Clojure – Round 2: Concurrency! by Lau B. Jensen.
The article discusses the classic “barber shop” problem and offers two implementations one in Clojure, using Clojure concurrency primitives, and another one in Scala using Scala implementation of actors.
Since I’ve been working in the latest months in Jobim, an actor’s library for Clojure, I thought it would be interesting to offer an additional implementation using actors in Clojure. The code can be found here.
The implementation of the actors is pretty straightforward. It is interesting to compare it with the Scala version from the previous article.
The shop actor:
(defn do-shop "Shop actor" ([max-seats max-clients] (let [seats (atom ) max-clients (atom max-clients)] (loop [[msg pid] (receive)] (condp = msg ;; allocate a seat for the client :get-seat (if (< (count @seats) max-seats) (do (swap! seats conj pid) (send! pid :seated)) (send! pid :no-space)) ;; barber asks for a client :next-client (let [pid (first @seats)] (swap! seats rest) (send! (resolve-name "the-barber") pid)) ;; A client is leaving the shop :exiting (swap! max-clients dec)) (if (> @max-clients 0) (recur (receive)) (notify-shop "Closing barber shop"))))))
;; Customer (defn customer "Customer actor" ([id] (spawn ;; enters the shop and ask for a seat #(loop  (println (str "Customer id " id "...")) (let [seated (do (send! (resolve-name "barber-shop") [:get-seat (self)]) (receive))] ;; we have a seat, wait to have a hair cut (if (= seated :seated) (let [_ (send! (resolve-name "the-barber") :hey) ;; Awaking the barber msg (receive)] ;; barber starting to do his thing (when (= msg :cutting) (notify-shop (str "Customer " id " getting his hair cut"))) ;; barber finished (let [msg (receive)] (when (= msg :finished) (notify-shop (str "Customer " id " got his hair cut"))) ;; leaving the shop (send! (resolve-name "barber-shop") [:exiting (self)]) (notify-shop (str "Customer " id " leaving the shop")))) ;; No space available in the shop, exiting (do (notify-shop (str "Customer " id " could not get his hair cut")) (Thread/sleep (rand 5000)) (recur))))))))
And finally the barber:
;; Barber (defn barber "Barber actor" ( (let [pid (spawn #(loop [msg (receive)] ;; wait for a customer (when (= msg :hey) ;; awaken! ;; ask for the customer to come in (send! (resolve-name "barber-shop") [:next-client (self)]) ;; ignore the customers trying to awake us (let [client-pid (receive (fn [msg] (not (= :hey msg))))] ;; starting to cut (send! client-pid :cutting) (Thread/sleep (rand 4000)) ;; finished cutting (send! client-pid :finished))) (recur (receive))))] (register-name "the-barber" pid))))
Additionally, the implementation includes the user of a behaviour. Behaviours is a concept taken from Erlang that refers to a piece of functionality in a distributed system that is encapsulated so it can be easily reused.
In this case, since the customers can be executed in any node, if we would like to collect the messages produced by all the customers, we could not use the traditional mechanism of printing to the standard output. The messages will appear all across the cluster. Using an event manager and a event handler we can collect messages from all the customers in the cluster in a central location.
Behaviours in Jobim are implemented using Clojure’s protocols. In this case:
;; Message logger (def-event-handler BarberLogger (init [this initial-state] initial-state) (handle-event [this event state] (println (str "*** " event))) (terminate [this state] (println ("BarberLogger exiting")))) (defn notify-shop [msg] (notify (resolve-name "barber-manager") msg))
More information about the event manager behaviour can be found in the Erlang manual.
One of the fundamental advantages of using a platform like Erlang is that you can run the same application in a single node or in a set of nodes with minimal changes. In this example, there are a couple of function to start the application in a single node or in multiple node. The only difference is the way the customer actors are initiated:
(defn run-barber-shop "Runs the barber shop problem in a single node" ([num-seats num-clients] (start-event-manager "barber-manager") (add-handler (resolve-name "barber-manager") [name jobim.examples.barber.BarberLogger] nil) (shop num-seats num-clients) (barber) (doseq [i (range 0 num-clients)] (customer i)))) (defn run-barber-shop-distributed "Runs the barber shop problem distributed in a set of nodes" ([num-seats num-clients nodes] (start-event-manager "barber-manager") (add-handler (resolve-name "barber-manager") [name jobim.examples.barber.BarberLogger] nil) (shop num-seats num-clients) (barber) (loop [ids (range 0 num-clients) nodes (cycle nodes)] (if (empty? ids) (println (str "*** All clients spawned")) (do (rpc-blocking-call (resolve-node-name (first nodes)) "jobim.examples.barber/customer" [(first ids)]) (recur (rest ids) (rest nodes)))))))
The result of a test run can be seen in this video:
As you can see, mixing actors in Clojure is just another possibility and it makes a really good alternative when you want to build systems spanning more than a single node.