Tuple space in few words
Recently I got asked by a good friend of mine to lend a hand on a project he is running, SemiSpace. SemiSpace is a light weight open source implementation of Tuple space / Object space. I've only had a vague understanding of what a space is, so my first task was more or less to wrap my head around it. I am a straightforward guy and like simple examples. But I did not find many good articles explaining a space in a simple manner. In this post I will try to do so.
What is a Tuple space used for?
To put it very shortly; a space is ideal to use on data you do not want to store persistently. It is suitable for short lived data which you want to distribute. A space is also well suited for a distributed master / worker or actor pattern. I will run through such an example of use of the master / worker pattern later in this post.
How to work with a Tuple Space
Working with a space is pretty simple. There are a small set of methods you use when working with a Space.
These methods are:
Write which writes an object into a Space.
Read which read an object from the Space.
Take which takes an object out of the Space.
Notify which notify about changes or occurrences of an object in the Space.
There are also two convenience methods:
ReadIfExists which read an object from the space only if the object is present in the Space.
TakeIfExists which take an object from the space only if the object is present in the Space.
With SemiSpace, more or less any object can be written, and accessing the objects is done by matching objects using the fields within. In other words; to read or take an object in the space you create an object with the same or partially equal characteristics as the object one want to match, and pass that on to the read or take method.
Let's say you have the following “Person” objects (here represented as JSON objects) in a Space:
{Person : {
firstname : 'John',
lastname : 'Doe'
}
}
{Person : {
firstname : 'John',
lastname : 'Dillinger'
}
}
If you want to read the object where “lastname” has the value “Doe”, you can set up a template object which is similar to the object you want to read:
{Person : {
lastname : 'Doe'
}
}
When you pass this template object to the read or take method, you get the “John Doe” object but not the “John Dillinger” object from the space. The same way of matching objects goes for the notify method also.
A master / worker pattern example
The best way to explain how to work with a space is to run through an example. A master / worker pattern gives you a good understanding how you can use a Space.
Let's say you have an online service where users can upload video files in one format and the service converts it to different format. Video conversion is very CPU intensive, and you definitely want to separate your service into different modules where the upload feature is one module running on dedicated front end servers, and the video converters is another module running on another set of dedicated servers. These two modules need to talk to each other, and a space is well suited for this communication.
In the examples the brown blobs are the front end servers where users can upload videos. They are the master. The green blob is the space and the grey blobs are the servers converting videos. The video converters are the workers.
For easy understanding I will be using JSON objects to illustrate the flow in and out of the space.
User John arrives to our service and have a video he want to convert.
John uploads his video and the frontend server John happens to be connected to store the video file on a hard disk (videos are a bit to big to be put in the space itself). A “video” object is then created with a reference back to John and some information about where the original video file is stored at disk. This object is then written to the space by the write method. The video file itself are omitted in the illustration.
In the space there are already several video objects laying around with references to video files ready to be converted. The video converters are at this point busy converting other videos and therefor has a CPU usage of 100% and are not able to handle more files before some are freed.
To be able to provide John with information about the video, for instance when conversion in done, you need to set up a notify listening for changes made to the video object holding John's video. You then set up a video template object with enough data to be able to link it to John. We pass this template on to the notify method.
When one of the converter servers get free resources to work on a video, it does a take against the space. The take is done with a very simple template which matches any “video” object not converted.
The converter server takes the video object out of the space to work on the video. The object is taken out of the space so no other converter starts working on the same video file.
When the video has been converted, the video converter writes a video object back to the space with the write method. This object now have a reference to where the new converted video file is stored on disk and the variable "converted" set to "true" to mark this video as converted.
When the new object appears in the space, the notify method, set up by the frontend server John is connected to, will match with the new object in the space and this server then gets knowledge of that something has changed in the video object.
The frontend server John is connected to will take the video object from the space and the upload service provides John with a message that his video now is converted.
In this simple manner; this is how you can use a space in a master / worker pattern. This is one of may things a Tuple space, such as SemiSpace, is well suited for.
08.07.2010 11:46 - Posted by Trygve - Comments: 0 - Technical
About:
My name is Trygve Lie. I live in Oslo, Norway where I deal with web technology. You can read a bit more about me here.
Search:
Categories:
Links:
Feeds:
Tags
- atom
- audio
- components
- css
- entitys
- esi
- html
- html5
- ie6
- ie7
- ie8
- java
- javascript
- jsp
- jstl
- performance
- roller
- rss
- scriptless
- selectors
- streaming
- svg
- theme
- validation
- webslices
- welcome
- xalan
- xhtml
- xml
- xslt
Comments: