next up previous contents
Next: Run-time Support for WWC Up: WWC Implementation in SALSA Previous: Migration


Actors as Network Service

There are many kinds of practical distributed applications: some are designed for scientific computation, which may produce a lot of temporary actors for parallel processing; some are developed for network services, such as a web server, a web search engine, etc. Useless actors should be reclaimed for memory reuse, while service-oriented actors must remain available under any circumstance.

The most important reason for reclamation of useless actors is to avoid memory leakage. For example, after running the HelloWorld actor (shown in Section 3.6) in the World-Wide Computer, the World-Wide Computer must be able to reclaim this actor after it prints out "Hello World". Reclamation of actors is formally named actor garbage collection.

Reclamation of useless actors introduces a new problem: how to support non-collectable service-oriented actors at the language level. This is important because a service-oriented actor cannot be reclaimed even if it is idle. For instance, a web service should always wait for requests. Reclamation of an idle service is wrong.

Services written in the C or Java programming languages use infinite loops to listen for requests. A SALSA service cannot use this approach because loops inside a message handler preclude an actor from executing messages in its message box. The way SALSA keeps a service actor alive is by specifying it at the language level - a SALSA service actor must implement the interface ActorService to tell the actor garbage collector not to collect it.

The following example illustrates how a service actor is implemented in SALSA. The example implements a simple address book service. The AddressBook actor provides the functionality of creating new <name, email> entities, and responding to end users' requests. The example defines the addUser message handler which adds new entries in the database. The example also defines the getEmail message handler which returns an email string providing the user name.
 
module examples;

import java.util.Hashtable; import java.util.Enumeration;

behavior AddressBook implements ActorService{ private Hashtable name2email;

AddressBook() { // Create a new hashtable to store name & email name2email = new Hashtable(); }

// Get the email of this person String getEmail(String name) { if (name2email.containsKey(name)) { // If name exists return (String) name2email.get(name); } else { return new String("Unknown user"); } }

// Add a new user to the system, returns success boolean addUser (String name, String email) { // Is the user already listed? if (name2email.containsKey(name) || name2email.contains(email)) { return false; } // Add to our hash table name2email.put(name, email); return true; }

void act(String args[]) { if (args.length > 0) { standardOutput<-println( "Usage:" + "java -Duan=<UAN> -Dual=<UAL> examples.AddressBook" ); return; } else { standardOutput<-println("AddressBook at: ") @ standardOutput<-println("uan: " + this.getUAN()) @ standardOutput<-println("ual: " + this.getUAL()); } } }

The AddressBook actor is bound to the UAN and UAL pair specified in the command line. This will result in placing the AddressBook actor in the designated location and notifying the naming service.

To be able to contact the AddressBook actor, a client actor first needs to get the remote reference of the service. The only way to get the reference is by the message handler getReferenceByName(). The example we are going to demonstrate is the AddUser actor, which communicates with the AddressBook actor to add new entries. Note that the AddUser actor can be started anywhere on the Internet.
 
module examples;

behavior AddUser { void act(String args[]) { try{ if (args.length == 3 ) { AddressBook book = (AddressBook)AddressBook.getReferenceByName( new UAN(args[0]) ); book<-addUser(args[1], args[2]); return; } } catch (Exception e) {standardError<-println(e);} standardError<-println( "Usage:" + "java examples.AddUser <bookUAN> <Name> <Email>" ); } }


next up previous contents
Next: Run-time Support for WWC Up: WWC Implementation in SALSA Previous: Migration
Wei-Jen Wang
2007-11-28