Distributed Systems/Middleware JavaSpaces Alessandro Sivieri Dipartimento di Elettronica e Informazione Politecnico, Italy sivieri@elet.polimi.it http://corsi.dei.polimi.it/distsys Slides based on previous works by Alessandro Margara
Linda and tuple spaces Data sharing model proposed in the 80s by Carriero and Gelernter, mostly used for parallel computation Recently revitalized in the context of distributed computing E.g., IBM TSpaces, Sun JavaSpaces, GigaSpaces Also used in WSNs, e.g. TeenyLime Communication is persistent, implicit, content-based, generative High degree of decoupling Workers Tuple Space < Japan, Kyoto > in(p) < USA, Los Angeles > < USA, St. Louis > rd(p) out(t) < Italy, Milan > Distributed Systems: MOM & Shared Data Spaces 2
Linda in a nutshell Data is contained in ordered sequences of typed fields (tuples) Tuples are stored in a persistent, global shared space (tuple space) Standard operations: out(t): writes the tuple t in the tuple space rd(p): returns a copy of a tuple matching the pattern (or template) p, if it exists Blocks waiting for matching tuples otherwise If many matching tuples exist, one is chosen nondeterministically in(p): like rd(p), but withdraws the matching tuple from the tuple space Some implementations provide also an eval(a), which inserts the tuple generated by the execution of a process a Distributed Systems: MOM & Shared Data Spaces 3
Linda in a nutshell Many variants: Asynchronous, non-blocking primitives (probes): rdp(p) and inp(p) Return immediately a null value if the matching tuple is not found Bulk primitives: e.g., rdg(p) Some of the non-standard primitives have non-trivial distributed implementations E.g., if atomicity is to be preserved, probes require a distributed transaction Distributed Systems: MOM & Shared Data Spaces 4
Architectural issues The tuple space model is not easily scaled on a wide-area network How to store/replicate tuples efficiently How to route queries efficiently The model is only proactive Processes explicitly request a tuple query reactive/asynchronous behavior must implemented with an extra process and a blocking operation As a consequence, commercial implementations: Provide only client access to a server holding the tuple space Instead of a fully distributed, decentralized implementation Introduce reactive primitives e.g., notify allows to register a listener, invoked when a matching tuple is written Distributed Systems: MOM & Shared Data Spaces 5
Jini Jini is a distributed services architecture developed at Sun focusing on network dynamicity It is composed by several specifications Discovery, registration, and binding of service Configuration Security Coordination Javaspaces, Mailboxes, Remote Event Notification Fault Tolerance Leasing, Transactions Specs reference implementation: Jini Starter Kit Now called Apache River Distributed Systems: MOM & Shared Data Spaces 6
Apache River Apache River provides Jini Specifications Helper Classes that implement them Services that implement them It is built on top of J2SE technology Not compatible with Java 7 Security Discovery Leasing Transactions Notifications Configuration Discovery Classes Join Manager Lease Renewal Manager Lookup Service Lease Renewal Service Transaction Manager JavaSpace RMI JERI Activation Distributed Systems: MOM & Shared Data Spaces 7
Browsing the SDK Three important services (for our examples): 1. HTTP server (codebase) 2. Lookup service ( Reggie ) 3. JavaSpaces service ( Outrigger ) Other services: Transaction service ( Mahalo ) RMI implementation ( Phoenix ) Event Mailbox service ( Mercury ) Leases ( Norm ) Distributed Systems: MOM & Shared Data Spaces 8
Discovery Service Oriented Architecture Applications are composed of services When an application needs to perform a task, it searches for a service providing the suitable functionality Services are usually not bound to each other at design/compile time but they discover each other at runtime The process of searching for a suitable service is performed through a special service called registry Distributed Systems: MOM & Shared Data Spaces 9
Typical Jini interaction One of Jini s main goal is to seamlessly discover who is providing a service: once this is achieved, the service is usually performed using RMI Discovery of services is aided by a Lookup Service (LUS) You can think of it as an RMI registry for resolving interfaces instead of names Exploits mobile code: service proxies are dynamically downloaded, and enable the client to use a service without pre-deployed code Lookup Service usually returns a proxy Service usually RMI invocation Consumer Distributed Systems: MOM & Shared Data Spaces 10
Discovery protocol The Lookup Service is essential for discovering other services To bootstrap the system (i.e. to discover the Lookup Services) a discovery protocol is used Each Lookup Service belongs to one or more groups Discovery is done broadcasting a simple UDP packet with the groups of interests In response to this UDP packet, a TCP connection is created and a ServiceRegistrar is transferred (a proxy of the Lookup Service) Distributed Systems: MOM & Shared Data Spaces 11
Discovery: Synch Helper Classes Discovery Classes implement the discovery protocol LookupLocator The simplest: it has a synchronous interface unicast discovery Takes a String url (es. jini://bla.bla.bla ) Distributed Systems: MOM & Shared Data Spaces 12
Discovery: Async Helper Classes More general with asynchronous interfaces LookupDiscovery(String[] groups) LookupLocatorDiscovery(String[] urls) LookupDiscoveryManager(String[] urls, String[] groups) It combines the behaviors of LookupDiscovery and LookupLocatorDiscovery Distributed Systems: MOM & Shared Data Spaces 13
Discovery: Async Helper Classes After the creation of a xxxdiscovery class you have to add a listener to receive notifications of DiscoveryEvent The listener must implement two methods void discovered(discoveryevent e) Brings information about all discovered Lookup Services void discarded(discoveryevent e) It is called after 4 missed multicast announcements e.getregistrars() returns an array of discovered Registrars Distributed Systems: MOM & Shared Data Spaces 14
Discovery: Join example After obtaining a ServiceRegistrar (a stub to the lookup service) we can join or lookup a service Lease period Service Id: null if the service was never exported before Stub of the object (through UnicastRemote Object.toStub) Optional attributes (service description) Distributed Systems: MOM & Shared Data Spaces 15
Discovery: Lookup example In the case I want to specify the service instance This is the interface to lookup These are the attributes describing the service Distributed Systems: MOM & Shared Data Spaces 16
Leasing Service registration, like many Jini operations is subject to leasing It s a way to provide self-healing in the network Whenever an operation which is subject to a lease is invoked The client requests a lease duration The server (lease grantor) could provide a lease with a shorter duration The client must check Distributed Systems: MOM & Shared Data Spaces 17
Leasing Lease interface renew cancel getexpiration A convenient way of dealing with leases is using a LeaseRenewalManager It offers methods for automatic leases renewal renewfor renewuntil Distributed Systems: MOM & Shared Data Spaces 18
JavaSpaces Sun OO Implementation of the Linda Model Fields are Java Object derived from the Entry object It is provided as part of the Jini Middleware Architecture Also supports Remote Event Notifications and Transactions API Write an item (OUT) Read any one item given a template (READ) Blocking or not blocking Is not possible to read ALL matching items Take an item (IN) Probing versions are named xxxifexists Distributed Systems: MOM & Shared Data Spaces 19
JavaSpaces API Entry read(entry tmpl, Transaction txn, long timeout) Read any matching entry from the space, blocking until one exists. Entry readifexists(entry tmpl, Transaction txn, long timeout) Read any matching entry from the space, returning null if there is currently is none. Entry take(entry tmpl, Transaction txn, long timeout) Take a matching entry from the space, waiting until one exists Entry takeifexists(entry tmpl, Transaction txn, long timeout) Take a matching entry from the space, returning null if there is currently is none Lease write(entry entry, Transaction txn, long lease) Write a new entry into the space EventRegistration notify(entry tmpl, Transaction txn, RemoteEventListener listener, long lease, MarshalledObject handback) When entries are written that match this template notify the given listener with a RemoteEvent that includes the handback object. Distributed Systems: MOM & Shared Data Spaces 20
Entry Entries in Jini are used both to describe services and to write in JavaSpaces. It s a Java tuple An Entry must implement the tagging interface Entry (which extends serializable) may extend the AbstractEntry which provides equals, hashcode and tostring (calls these on each internal field of the entry) Fields of an entry are those of its members which are of public reference (i.e., Object) type You cannot store primitive types in fields of an Entry, use wrapper objects instead private/default fields are not considered An Entry must have an empty constructor (may have any number of other constructors or methods) Each field is serialized separately, so references between two fields of an entry will not be reconstituted to be shared references, but instead to separate copies of the original object Distributed Systems: MOM & Shared Data Spaces 21
Matching of entries Entries can be used also as templates for matching other entries An entry matches an entry template if the class of the template is the same as, or a superclass of, the class of the entry and every non-null field in the template equals the corresponding field of the entry entries can match more than one template Distributed Systems: MOM & Shared Data Spaces 22
Entry Example Distributed Systems: MOM & Shared Data Spaces 23
Example: service discovery Distributed Systems: MOM & Shared Data Spaces 24
Example: writer Distributed Systems: MOM & Shared Data Spaces 25
Example: reader Distributed Systems: MOM & Shared Data Spaces 26
Example: reader (blocking) Distributed Systems: MOM & Shared Data Spaces 27
Example: reader with listener Includes a marshalled object. A copy will be returned in the event captured by the listener Distributed Systems: MOM & Shared Data Spaces 28
Example: reader with listener We use the MarshalledObject to distinguish among events generated for different calls to the notify() method Distributed Systems: MOM & Shared Data Spaces 29