Playing with making an Actor-like distributed system in REBOL #3
June 8th, 2009In this part we finally get to the simple example and current library source code. See the previous two posts to get more info about what is what and why: #1 , #2 .
Dispatcher + workers: distributed logging system
This is probably the simplest kind of distributed system that actually does something practical. It is made by one work dispatcher + any number of workers.
In our case work dispatcher holds all the code and there are empty worker shells. I call them shells because they are waiting there empty and can be filled with any actors at runtime over the wire. This means generic workers can be spawned and added to the system and bosses will decide what they should do at this point in time.
A worker shell
If we remove the command-line parsing boilerplate generic worker is coded with 3 lines:
add-local-actor director start-remote-listener start-actors
This is the code that basically every actor-net node has. It adds the director actor (see #2 to see what it does), it starts the listener for remove messages and starts actors.
A work dispatcher
Dispatcher holds all the code in our example. This first part set’s up dispatcher’s address and then a block of addresses of all the workers. I tried the example on 3 computers I have in network here and it worked the same as with one computer.
setup-my-addr 192.168.1.5 5602 *workers*: reduce [ make actor-addr! [ host: 192.168.1.5 port: 5603 ] make actor-addr! [ host: 192.168.1.5 port: 5604 ] make actor-addr! [ host: 192.168.1.2 port: 5605 ] make actor-addr! [ host: 192.168.1.3 port: 5605 ] ]
Now we create 3 actors. First is called init and is of once-actor type. This means that is acts just once and then removes itself.
init: make once-actor [
act-once: [
foreach worker *workers* [
~spawn worker (
make actor [ act-match: [ [ 'log data ] [ print join "log at " now ": " probe data ] ] ]
) *my-addr* "logger" print "spawned remote actor"
] print "will remove myself now"
]
]
init actor spawns logger actors in all the worker nodes and removes itself. Inside make actor block is the code of actor that gets spawned in workers.
Second actor is called my-bookman (stupid name I know, but my-bookkeeper seemed too long to me
).
my-bookman: make actor [ act-match: [ [ 'actor-spawned ident addr ] [ print [ "actor " ident " was spawned at " ] probe addr group-actor ident addr if (group-count ident) >= (length? *workers*) [ add-local-actor event-sim print "added actor that will simulate logging events.." ] ] ] ]
After actors get spawned on worker nodes it’s director sends the 'actor-spawned with ident and addr to address you provided in ~spawn. Our bookman accepts it and books remote actor to a group. When it sees that all the workers have spawned actors it adds event-sim actor to the current node. This actor will simulate the logging events.
event-sim: make timer-actor [ act-timer: [ ~send group-select "logger" compose [ 'log (random "abcdefghijklmn") ] print "sending to one of loggers" delay: to-time random 2 ] ]
This is a timer-actor which means it acts in timed timed intervals. Basically it just sends the message to random actor from the group that my-bookman added actors to.
We are ready now to start up the system. We add my-bookman and init actors to system and start it.
add-local-actor my-bookman add-local-actor init start-remote-listener start-actors
A screen shot probably says more than 100 words, if you look at the text in the screen shot you should see the whole work flow.

The source code of the library and examples can be found here.
—
For this very simple example current library worked well, code was simple and tiny. But there are many more complex examples to try and they will show new requirements and directions where it can evolve.

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=434cb352-85dd-4247-b9ae-628d8f7a51bd)
July 14th, 2009 at 12:48 pm
Hi,
Very interesting. Did you develop the whole web-assistant in Rebol ?
July 14th, 2009 at 3:47 pm
yes, it’s all rebol.. the bots and the web-app
October 19th, 2009 at 7:12 am
[...] how actors + message passing system could look in REBOL. I wrote 3 posts about that, the latest is this one, and you can follow down from [...]