Alex Miller lives in St. Louis. He writes code for a living and currently work for Terracotta Tech on the Terracotta open-source Java clustering product. Prior to Terracotta he worked at BEA Systems and was Chief Architect at MetaMatrix. His main language for the last decade has been Java, although Alex have been paid to program in several languages over the years (C++, Python, Pascal, etc). Alex has posted 43 posts at DZone. You can read more from them at their website. View Full User Profile

Java Actors with Kilim

01.07.2009
| 9488 views |
  • submit to reddit

I finally got around to porting my actor process ring code (Erlang, Scala) to run with the Kilim library tonight.

Kilim is a Java library for lightweight message-passing. The three main things to know about are Tasks, Mailboxes, and @pausable. In Kilim, actors are replaced with Tasks, which are just lightweight runnable objects that serves basically the same role.

Mailboxes are designed for multiple-producer, single-consumer access just like Erlang and Scala actor mailboxes. In Kilim though, Mailbox is just a class and Tasks don’t necessarily have a Mailbox or could even have more than one. Mailboxes are also generic and typed by the kind of message they should receieve. This opens up new ways to compose Tasks and Mailboxes into a broader range of structures. Messages are typically simple (potentially mutable) classes.

Finally, there is an annotation called @pausable. This is used to specify that a method can be paused, continuation-style, during pausable calls; sleep and yield are two provided hooks and the mailbox get / put methods are also pausable. @pausable is also used to mark classes for instrumentation.

That leads me to the compile-time weaver. After you’ve compiled your classes, you need to run a compile-time weaver to modify the bytecode so that the continuation-style pausing is available. At runtime, pausing is used to schedule a large number of actors over a small number of kernel threads.

In all, this is a really interesting set of features that adapts the Erlang/Scala actor model into the statically typed world of Java pretty nicely.

From a usage point of view, it’s kind of a pain. Compile-time weaving sucks rocks as it puts a kink in every tool chain plus there’s nothing happening that couldn’t be done at runtime with a Java agent, as far as I can tell. I also had a lot of time to get the Weaver to run on my first stab at the code. It was far from obvious that the error messages were indicating I had forgotten @pausable on the non-Actor classes. Fortunately, I know enough about ASM to tell what I was missing. And once I got it running I got some weird NoSuchMethodErrors due to incorrectly specifying @pausable on methods that didn’t need it.

These bumps didn’t bother me too much though - this is a new project and I understand that it’s early for that kind of help.

Now to a little code. This is really pretty much a port from the Scala code into Java, which was pretty close. I broke the code (previously in one file) into Ring (the main code), Message, TokenMessage, NodeActor, and TimerActor. You’ll notice there is a LOT more code with the Java version than the Scala or Erlang versions.

import java.util.ArrayList;   
import java.util.List;

import kilim.Mailbox;
import kilim.pausable;

@pausable
public class Ring {

public static void main(String arg[]) {
new Ring().startRing(Integer.parseInt(arg[0]));
}

public void startRing(int n) {
List<NodeActor> nodes = spawnNodes(n, startTimer());
Mailbox<Message> mailbox = connectNodes(n, nodes);
mailbox.putnb(Message.START);

try { Thread.sleep(100000000); } catch(InterruptedException e) {}
}

private TimerActor startTimer() {
TimerActor actor = new TimerActor(new Mailbox<Message>());
actor.start();
return actor;
}

private List<NodeActor> spawnNodes(int n, TimerActor timer) {
System.out.println("constructing nodes");
long startConstructing = System.currentTimeMillis();

List<NodeActor> nodes = new ArrayList<NodeActor>(n+1);
for(int i=0; i<n; i++) {
nodes.add(new NodeActor(i, new Mailbox<Message>(), timer.getInbox()));
nodes.get(i).start();
}

long endConstructing = System.currentTimeMillis();
System.out.println("Took " + (endConstructing-startConstructing) + " ms to construct " + n + " nodes");
return nodes;
}

private Mailbox<Message> connectNodes(int n, List<NodeActor> nodes) {
System.out.println("connecting nodes");
nodes.add(nodes.get(0));
for(int i=0; i<n; i++) {
nodes.get(i).connect(nodes.get(i+1).getInbox());
}
return nodes.get(0).getInbox();
}
}

It’s very important that the Ring class is marked @pausable or the weaver won’t work. Now the message classes. I defined some immutable singleton messages in Message and a mutable TokenMessage:

public class Message {   
public static final Message START = new Message();
public static final Message STOP = new Message();
public static final Message CANCEL = new Message();
}

public class TokenMessage extends Message {
public final int source;
public int value;

public TokenMessage(int source, int value) {
this.source = source;
this.value = value;
}
}

And here’s the node actor translated into a Kilim Task (note the @pausable annotation on the execute() method, which is defined in Task):

import kilim.Mailbox;   
import kilim.Task;
import kilim.pausable;

public class NodeActor extends Task {
private final int nodeId;
private final Mailbox<Message> inbox;
private final Mailbox<Message> timerInbox;
private Mailbox<Message> nextInbox;

public NodeActor(int nodeId, Mailbox<Message> inbox, Mailbox<Message> timerInbox) {
this.nodeId = nodeId;
this.inbox = inbox;
this.timerInbox = timerInbox;
}

public Mailbox<Message> getInbox() {
return this.inbox;
}

public void connect(Mailbox<Message> nextInbox) {
this.nextInbox = nextInbox;
}

@pausable
public void execute() throws Exception {
while(true) {
Message message = inbox.get();
if(message.equals(Message.START)) {
System.out.println(System.currentTimeMillis() + " " + nodeId + ": Starting messages");
timerInbox.putnb(Message.START);
nextInbox.putnb(new TokenMessage(nodeId, 0));
} else if(message.equals(Message.STOP)) {
//System.out.println(System.currentTimeMillis() + " " + nodeId + ": Stopping");
nextInbox.putnb(Message.STOP);
break;
} else if(message instanceof TokenMessage) {
TokenMessage token = (TokenMessage)message;
if(token.source == nodeId) {
int nextVal = token.value+1;
if(nextVal % 10000 == 0) {
System.out.println(System.currentTimeMillis() + " " + nodeId + ": Around ring " + nextVal + " times");
}

if(nextVal == 1000000) {
timerInbox.putnb(Message.STOP);
timerInbox.putnb(Message.CANCEL);
nextInbox.putnb(Message.STOP);
break;
} else {
token.value = nextVal;
nextInbox.putnb(token);
}
} else {
nextInbox.putnb(token);
}
}
}
}
}

And for completeness, here’s the Timer Actor:

import kilim.Mailbox;   
import kilim.Task;
import kilim.pausable;

public class TimerActor extends Task {
private final Mailbox<Message> inbox;
private boolean timing;
private long startTime;

public TimerActor(Mailbox<Message> inbox) {
this.inbox = inbox;
}

public Mailbox<Message> getInbox() {
return this.inbox;
}

@pausable
public void execute() throws Exception {
while(true) {
Message message = inbox.get();
if(message.equals(Message.START) && !timing) {
startTime = System.currentTimeMillis();
timing = true;
} else if(message.equals(Message.STOP) && timing) {
long endTime = System.currentTimeMillis();
System.out.println("Start=" + startTime + " Stop=" + endTime + " Elapsed=" + (endTime-startTime));
timing = false;
} else if(message.equals(Message.CANCEL)) {
break;
}
}
}
}

To compile and weave, you’ll do something like this:

$ export KCP=$KILIM/libs/asm-all-2.2.3.jar:$KILIM/classes  
$ javac -cp $KCP -d bin src/*.java  
$ java -cp $KCP kilim.tools.Weaver -d ./bin ./bin  
$ java -cp $KCPL./bin Ring 100 

And finally the results, here in comparison with Erlang and Scala 2.7.3/JDK 1.6:

LanguageSpawn 100Send 100M messagesSpawn 20k
Erlang R12B0.2 ms77354 ms120 ms
Scala 2.7.310 ms121712 ms315 ms
Kilim3 ms78390 ms192 ms

So, Kilim demonstrates process creation faster than Scala (but still slower than Erlang) and message-passing in the realm of Erlang and significantly faster than Scala. That’s pretty darn impressive! I’m looking forward to watching where this library goes.

From http://tech.puredanger.com/

Published at DZone with permission of its author, Alex Miller.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

sriram srinivasan replied on Fri, 2009/01/16 - 5:37pm

Hi,

This is sriram, Kilim's author.  Thanks for the writeup. Just wanted to point out a slight error in the benchmark: you must allow the JIT to warm up. 

 

Here's the difference in creation times (on a 2.4GHz Mac Pro)

Warming up JIT
constructing nodes
Took 225 ms to construct 20000 nodes

JIT warmed up
constructing nodes
Took 51 ms to construct 20000 nodes
 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.