JavaSpaces and TSpaces Theresa Tamash CDA 5937 November 4, 2002.
JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.
-
Upload
della-norman -
Category
Documents
-
view
234 -
download
1
Transcript of JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.
![Page 1: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/1.jpg)
JavaSpacesTM
By Stephan Roorda
Source: JavaSpaces specification
![Page 2: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/2.jpg)
Presentation Outline
• Review of Linda
• Overview of JavaSpaces
• In depth description of JavaSpaces
• Why is JavaSpaces better?
![Page 3: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/3.jpg)
Review of Linda
![Page 4: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/4.jpg)
Linda Basics
• Tuple space is Linda's name for its shared data space– A Tuple is simply a list of fields, separated by
commas and enclosed in parentheses
• A tuple is accessed by specifying its contents– Associative memory model– There is no address associated with a tuple
![Page 5: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/5.jpg)
Tuple Space
Tuple Space
Sender
Sender
Receiver Receiver
![Page 6: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/6.jpg)
Linda Operations
• There are four basic operations:– out
• Generates a data (passive) tuple.
• Each field is evaluated and put into tuple space.
– in • Uses a template to retrieve tuple from tuple space
• Once retrieved, the tuple is taken out of tuple space and is no longer
• If no matching tuple is found process will block. Provides for synchronization between processes.
![Page 7: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/7.jpg)
Linda Templates
• Specifies tuple to retrieve – Consists of sequence of typed fields
• Two kinds of fields – Actuals
• Variables, constants or expression that resolve to constant
– Formals • Holders for data to retrieve
• Preceded by a question mark
• Assigned values of corresponding fields in matched tuple
![Page 8: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/8.jpg)
Matching Templates
• In order for a template to match a tuple: – Have to have the same number of fields– Actuals must have same type, length and values as
those in corresponding tuple fields– Formals in template must match type and length of
corresponding fields in tuple
• If several tuples match the template, impossible to predict which will be selected
• The order of evaluation of fields within a tuple or template is undefined.
![Page 9: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/9.jpg)
Linda Operations
– rd • Uses a template to copy data without removing it from tuple
space.
• Once read, the tuple is still available for others.
• If no matching tuple is found process will block.
– eval • Generates process (active) tuple
• Control is immediately returned to invoking program – Logically, each field is evaluated concurrently, by a separate
process and then placed into tuple space
![Page 10: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/10.jpg)
Quote
![Page 11: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/11.jpg)
Overview of JavaSpaces
![Page 12: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/12.jpg)
Mapping Linda to JavaSpaces
• JavaSpace = Tuple Space
• entry = tuple
• write = out
• take = in
• read = rd
![Page 13: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/13.jpg)
Differences between Linda and JavaSpaces
• Entries in Java are typed as objects – associates behavior with entries
• JavaSpaces allows matching of subtypes– result of having typed entries
• Fields in an entry are objects in Java– systems built with this are object-oriented
![Page 14: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/14.jpg)
Differences
• Support for multiple JavaSpaces– transactions can span multiple threads and
spaces
• Leasing– frees system from garbage left behind from
crashes
• JavaSpaces does not provide eval
![Page 15: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/15.jpg)
JavaSpaces Design Goals
• Provide a simple platform for designing and implementing distributed systems
• Thin clients – simple – quick to download– run on limited local memory
![Page 16: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/16.jpg)
JavaSpaces Design Goals
• Variety of server implementations– relational databases– object oriented databases
• It should be possible to create a replicated JavaSpaces service
![Page 17: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/17.jpg)
Requirements for Application Clients
• Must be possible to write a 100% Pure Java client
• Clients implementation must be independent of the implementation details of the Server
![Page 18: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/18.jpg)
Key features of JavaSpaces
• Spaces are shared– handles the details of concurrent access
• Spaces are persistent– objects can outlive the processes that created them
• Spaces are associative– associative lookup is used to locate objects– this is based on content and not memory location
![Page 19: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/19.jpg)
Key features
• Spaces are transactionally secure– transaction model ensures that an operation on a
space is atomic– supported for one or more spaces
• Spaces allow us to exchange executable content– objects are passive in the space( immutable )– when removed we can change their attributes and
invoke methods on them
![Page 20: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/20.jpg)
Entry
• Collection of typed objects
package net.jini.core.entry;
public interface Entry extends java.io.Serializable {
// this interface is empty
}
![Page 21: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/21.jpg)
Example Entry
import net.jini.core.entry.*;
public class SpaceShip implements Entry {
public Integer score;
public String name;
public MessageEntry() {
}
public SpaceShip( String n, int s ) {
score = s;
name = n;
}
}
![Page 22: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/22.jpg)
JavaSpace Interface
• All of the operations have to be invoked on an object that implements the JavaSpace interface
• Not a remote interface
• Exports objects that implement the JavaSpace interface locally on the client
![Page 23: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/23.jpg)
JavaSpace Interface
package net.jini.space;
<import statements>
public interface JavaSpace {
public final long NO_WAIT = 0;
Lease write( Entry e, Transaction txn, long lease )
Entry read( Entry tmpl, Transaction txn, long timeout )
Entry take( Entry tmpl, Transaction txn, long timeout )
EventRegistration notify( Entry tmpl, Transaction txn,
RemoteEventListener listener, long lease,
MarshalledObject handback )
}
![Page 24: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/24.jpg)
Accessing a JavaSpace
• Space might be registered as a Jini lookup service
• Space might register with an RMI registry
![Page 25: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/25.jpg)
Operations
• Write
• Read
• Take
• Notify
![Page 26: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/26.jpg)
write
• Write the given entry into this JavaSpaces service
public void writeShip( SpaceShip ship ) {
try {
space.write( ship, null, Lease.FOREVER );
}
catch( Exception e ) {
e.printStackTrace();
}
}
![Page 27: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/27.jpg)
read and readIfExists
• Read an entry from the JavaSpaces service that matches the given template
• Passing a null reference for the template will match any Entry
• Multiple read requests may return different Entry objects even if no changes are made to the space in between each
![Page 28: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/28.jpg)
read and readIfExists
public int getScore( String name ) {
SpaceShip template = new SpaceShip();
template.name = name;
try {
SpaceShip ship =
(SpaceShip)space.read( template, null, Long.Max_VALUE );
return ship.score.intValue();
}
catch( Exception e ) {
e.printStackTrace();
return -1;
}
}
![Page 29: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/29.jpg)
Silly Sample
public static void main( String args[] ) {
JavaSpace space = SpaceAccessor.getSpace();
SpaceGame game = new SpaceGame( space );
// create an entry
SpaceShip enterprise = new SpaceShip( “enterprise”, 10 );
// demonstrate read and write
game.writeShip( enterprise );
System.out.println(enterprise.name + “ written into space”);
System.out.println(“The “ + enterprise.name + “’s score is “
+ game.getScore(“enterprise”) );
}
![Page 30: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/30.jpg)
take and takeIfExists
• Same as Read operations, except that the entry is removed from the space
• Will never return copies of the same Entry
![Page 31: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/31.jpg)
take and Exceptions
• RemoteException - may or may not have been successful
• UnusableEntryException - removes the unusable entry from the space
• Any other exception - take did not occur and no entry was removed from the space
![Page 32: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/32.jpg)
notify
• Notify a specific object when entries that match the given template are written into this JavaSpaces service
• A lease time is given which is how long you want the registration to be remembered by the server
![Page 33: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/33.jpg)
Events in Java
• Event Source
• Event Object
• Event Listener
![Page 34: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/34.jpg)
Distributed Events in JavaSpaces
• Events might have to travel from one JVM to another over a network
• Events may arrive:– multiple times– out of order– not at all
• Programmer’s responsibility to ensure correctness
![Page 35: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/35.jpg)
Event notification in JavaSpaces
![Page 36: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/36.jpg)
Notification Example
public static void main( String args[] ) {
JavaSpace space = SpaceAccessor.getSpace();
Listener listener = new Listener( space );
Message template = new Message();
space.notify(template, null, listener, Lease.FOREVER, null);
Message msg = new Message();
msg.content = “Hello World”;
space.write( msg, null, Lease.FOREVER );
}
![Page 37: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/37.jpg)
Notification Example
public class Listener implement RemoteEventListener {
private JavaSpace space;
public Listener( JavaSpace space ) throws RemoteException {
this.space = space;
UnicastRemoteObject.exportObject( this );
}
public void notify( RemoteEvent ev ) {
Message result =
(Message)space.read( template, null, Long.MAX_VALUE );
System.out.println( result.content );
}
}
![Page 38: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/38.jpg)
notify and Transactions
• Transactions can be:– null– non - null
![Page 39: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/39.jpg)
notify and Transactions
• entries that are written and taken in the same transaction - before a commit - listeners will not be notified that are registered under a null transaction
• server retries until the notification request’s lease expires
• notifications may be delivered in any order
![Page 40: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/40.jpg)
Operation Ordering
• operations on a space are unordered• Example: if T and U are 2 threads. T performs a
write and U performs a read with a template that matches the written entry, the read may not find the written entry even if the write returns before the read.
• The only way to guarantee this is if the threads work together and that is independent of JavaSpaces
![Page 41: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/41.jpg)
Transactions
• uses net.jini.core.transaction to group operations into a bundle that act as a single transaction
• either all operations within the transaction complete or none do
• null - performs as if a transaction was created just for that operation
![Page 42: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/42.jpg)
Benefits of JavaSpaces
• Simple
• Expressive
• Supports loosely coupled protocols
• Eases the implementation of client/server systems
![Page 43: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/43.jpg)
Applications of JavaSpaces
• Any system/problem that needs a distributed solution– Human Genome project– Cryptography– Rendering– Chat program– Auction server( such as e-bay, amazon, etc )
![Page 44: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/44.jpg)
Examples
![Page 45: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/45.jpg)
Conclusion
• Simple to learn
• Easy to understand
• Map a lot of problems
• fairly new - not many “real-world” uses yet
• best implementation of Linda yet
![Page 46: JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.](https://reader035.fdocuments.us/reader035/viewer/2022062314/56649eb45503460f94bbc888/html5/thumbnails/46.jpg)
References and Sources
• JavaSpaces Principles, Patterns, and Practice by Freeman, Hupfer, Arnold
• Official JavaSpaces specification
• visit http://www.cs.rit.edu/~sjr1521 for full links and bibliography