User Tools

Site Tools


Sidebar

Jump to
AmbientTalk
CRIME
iScheme

at:tutorial:distribution

This is an old revision of the document!


This tutorial is under heavy construction!

Distributed Programming

This tutorial chapter discusses how AmbientTalk virtual machines can discover and communicate with each other over the network. The integration of distribution was one of the main concerns in the design of AmbientTalk programming model.

More specifically, as a distributed programming language that adheres to the Ambient-Oriented Programming paradigm, AmbientTalk incorporates partial failures and discovery lookup facilities at the heart of its distributed programming model. Rather than creating stubs and skeletons to manage remote communications, AmbientTalk integrates them transparently to the developer thanks to its concurrency model based on actors and far references. Far references are in fact a vital feature of the distributed model of AmbientTalk that allows the language to be able to handle the so-called volatile connections featured in mobile ad hoc networks. This chapter mainly explains the language abstractions to export and discover other remote objects, and handle partial failures. But first, let us start simply by showing how to enable the network functionality.

Starting the Network..

AmbientTalk provides an unique native object, named network, that responds to two methods that control the network access to an AmbientTalk virtual machine. More specifically, network.online() and network.offline() make a virtual machine go online and offline, respectively.

When the virtual machine goes online, this allows the built-in discovery lookup mechanism to export the local objects and let local objects to find other remote objects. AmbientTalk's discovery support will be further explained in the following section. Taking offline a virtual machine breaks immediately the connections with other virtual machines and thus, all remote reference between them are disconnected. This is a deliberate design choice made to facilitate the simulation of transient disconnections.

Be aware that by default the network access is shut down.

Exporting and discovering objects

AmbientTalk provides language support to make some objects available to other objects residing in remote actors by means of the export:as: construct. The export:as: construct takes as argument an object that is made remotely accessible and a service type under which the object can be discovered. For example, one exports a printing service as follows:

defstripe Printer;
def service := object: { 
	def print(aDoc) {
		system.println("printing " +aDoc);
    }
};
export: service as: Printer;

When an object its exported by its actor, it becomes discoverable by other actors by means of the service type. Internally, this means that the object is placed in the export table of its actor. As shown in the example, a service type is represented by a stripes. This means that services types are not associated with a set of methods, but they denote an abstract publication topic that objects exports. As a stripe, a service type can thus be a subtype of one or more other service types. For example, an object could offer a color printing services by exporting the following stripe:

defstripe ColorPrinter <: Printer;

The export:as: construct returns a publication object that responds to a cancel method that can be used to cancel the publication, i.e, unexport the object.

Discovering objects

AmbientTalk has a built-in peer-to-peer discovery lookup mechanism based on a publish-subscribe scheme that was designed to be able to discover objects in mobile ad hoc network interactions where no centralized lookup infrastructure may be available.

As previously explained, objects broadcast to the network the service types they offer using the export statement. AmbientTalk also provides language constructs to install an observer whose block of code will be triggered when a remote object of a certain service type becomes available in the network. For example, one can discover a proximate buddy of an instant messenger application by means of the when:discovered: construct as follows:

when: InstantMessenger discovered: { |messenger|
     when: (messenger<-getName()) becomes: { |name|
	buddyList.put(name, messenger);
	system.println("Added buddy: " + name);			 
     };
};

The code block to execute when the service type becomes available is parameterized with the actual remote reference to the discovered service object. In the example above, messenger is a remote reference to the remote object exporting the InstantMessenger service type. Imagine the interaction between the instant messenger applications executing the above code of two persons, e.g. Bart and Lisa. When Bart's instant messenger and Lisa's instant messenger come into one another's communication range, Bart will discover Lisa and Lisa will discover Bart since both are exporting and also searching the InstantMessenger service type. Once discovered, both will interchange their names and store it in their buddyList.

We are using a future to get the return value of the getName asynchrnonus message invocation. For further details about futures and the when:becomes: language construct, we refer the reader to the previous chapter on the concurrency model of AmbientTalk.

The when:discovered: observers will be triggered only once when an InstantMessenger service type becomes available in the network. In order to be able to discover all other instant messenger buddies available in the network, the whenever:discovered: construct should be used. As when:discovered, whenever:discovered takes a service type and a block of code that will be triggered when a remote object of a certain type becomes available in the network. However, the block of code specified in whenever:discovered can be fired multiple times upon discovering several exported objects.

As export:as, both constructs returns a subscription object that responds to a cancel method that can be used to cancel the subscription so that the block of code is no longer invoked. Note that objects exported by an actor do not trigger the actor's own when:discovered: nor whenever:discovered: observers.

Partial Failure Handling

Let us consider again the example instant messenger application described in previous section to further explain the semantics of AmbientTalk's remote object references and how they deal with transient disconnections.

When an object discovers a service type, the when observers are triggered receiving as parameter a remote far reference to the remote object discovered. As explained in previous sections, far references operates asynchronously. When a client object sends a message via a remote reference, the message is buffered in the remote far reference and the client does not even wait for the message to be delivered. This is crucial in distributed computing in order to prevent race conditions. The parameter passing semantics for messages sent to remote objects works similar to the inter-actor message sending semantics:

  1. Objects are always passed by far reference, except for isolate objects which are passed by copy.
  2. Native data types are always passed by copy.

When a remote far reference receives a messages, it flushes the message to the remote object providing that it is connected. If the remote far reference is disconnected, messages are accumulate in its inbox in order to be transmitted once the reference becomes reconnected at a later point in time once the network connection is restored.

Therefore, a remote far reference abstracts a client object from the actual network connection state. However, it is often useful for an application to be informed when a connection to a remote object is lost or reconnected. To this end, AmbientTalk offers language constructs to install observers on a far reference which are triggered when the reference becomes disconnected or reconnected. For example, the instant messenger application can notify the user whenever a buddy moves in and out of the communication range as follows:

when: InstantMessenger discovered: { |messenger|
 ...
	when: messenger disconnected: {
     		system.println("Buddy offline: " + name);
	};
	when: messenger reconnected: {
     		system.println("Buddy online: " + name);
	};
};

This code illustrate how the instant messenger application notifies when a buddy goes online or offline. In the above code, messenger is a remote reference to another remote buddy discovered. Note that installing disconnected observers also allows developers to clean certain resources when a remote reference becomes disconnected. However, when an instant messengers disconnects, the remote object referred to by messenger remains exported. This implies that remote objects remains pointed by a disconnected remote reference which prevents them from being garbage collected. In fact, messenger are never garbage unlesss explicit cancelation of the subscription. But other types of objects which are only relevant within the context of an interaction should become eventually candidates for garbage collection. In the next section, we detail how AmbientTalk deals with distributed memory management.

In other to cope with partial failures, AmbientTalk also allows developers to retract all currently unsent messages from the far reference outbox by means of the retract language construct. This is specially useful in the context of distribution, since developers can have explicit control on the messages that are buffered but have not been sent while the remote far reference is disconnected. Any undelivered messages accumulated by the remote reference can be then for example forwarded to another remote object or simply cancelled.

The retract language construct takes as argument the far reference of which to retract outgoing message send. One can store the unsent messages upon disconnection of a service type Service as follows:

when: Service discovered: { | reference |
    when: reference disconnected: {
         messages := retract: reference;
    }
}

The construct returns a table containing copies of all messenges that were sent to this far reference, but not yet transmitted by the far reference to the remote object pointed to. Note that this has the side effect that the returned messages will not be sent automatically anymore; the programmer is thus responsible to explicitly resend all messages that were retracted but still need to be sent.

Garbage collecting remote references

As explained in the previous section, AmbientTalk's remote references are by default resilient to disconnections. This has significant repercussions at a distributed memory management level. The main impact is that an exported object cannot be reclaimed when all of its clients are disconnected since the remote references remain pointing to the server object. In other words, servers objects are kept alive even though there are only remotely accessible by a disconnected remote reference. AmbientTalk provides built-in support to unexport explicitly remotely accessible objects by means of the takeOffline language construct. The construct look as follows:

takeOffline: object

The construct removes from the export table of the actor where the code is executed the object passed as argument. When the object is removed from the export table, it no longer belongs to the set of root objects and as such, it can be reclaimed by the local garbage collector once when it is no longer locally referenced. Although the actual reclamation of an unexported object may be trigger at a later point in time, any attempt to access it via a far reference will result in a XObjectOffline exception.

As you may have noticed, the takeOffline language construct is reminiscent to the so-called delete operation provided by some languages without built-in local garbage collection. It is indeed a design decision providing the minimal set of language constructs for distributed memory management. Rather than defining one specific distributed garbage collector directly in the interpreter, we have opted to enable the experimentation of distributed garbage collection techniques from within AmbientTalk itself. Thanks to AmbientTalk reflective infrastructure, it has been possible to build more sophisticated distributed garbage collection techniques to avoid the extra burden that takeOffline places on developers. More specifically, AmbientTalk integrates leasing techniques with the notion of a remote object reference, and provides the resulting leased object references. The system library shipped with AmbientTalk contains a reflective implementation of leased object references together with a set of language constructs to manipulate them.

On the client side, taking offline an object results in a permanent disconnection of the remote references pointing to it. In other words, despite having network connection, unexporting an object renders remote far references permanently disconnected. This implies that client have to deal explicitly with unexported objects. To this end, when:takenOffline has been implemented in order for developers to be able to install observers on an far reference which are notified when the object pointed to is taken offline. The construct takes as parameter a far reference and a block of code that is executed when the taken offline event is notified. As an example, the instant messenger application can clean certain resources when a buddy shuts down its instant messenger application as follows:

when: messenger takenOffline: {
   system.println("Buddy offline: " + name);
   //clean certain resources associated to the buddy
};

Be aware that unexporting a object will not only trigger the takenOffline observers but also the disconnected observers since the taking offline event is also considered as a logical disconnection between two devices. Unlike the network observers, the block of code of a takenOffline observer will be triggered only once. The remote object becomes then subject to be eventually reclaimed by the garbage collector. As a result, the disconnected and reconnected observers will be no longer trigger for such remote reference.

Note that disconnection, reconnection and takenOffline observers can be also installed for the inter-actor far references. Such inter-actor far references are local to a virtual machine and as such, network failures cannot happen. In that case, the disconnection and takenOffline observers are triggered when the object pointed to by the far reference is taken offline. But the reconnected observers won't be never triggered since an unexported object cannot be exported again.

The complete implementation of the instant messenger application explained along this chapter can be found in the file at/demo/InstantMessenger.at.
at/tutorial/distribution.1178262381.txt.gz · Last modified: 2007/06/19 16:09 (external edit)