• No results found

Tuple spaces and Object spaces. Distributed Object Systems 12. Tuple spaces and Object spaces. Communication. Tuple space. Mechanisms 2.

N/A
N/A
Protected

Academic year: 2021

Share "Tuple spaces and Object spaces. Distributed Object Systems 12. Tuple spaces and Object spaces. Communication. Tuple space. Mechanisms 2."

Copied!
7
0
0

Loading.... (view fulltext now)

Full text

(1)

Distributed Object Systems

– 12 –

Tuple spaces and Object spaces

Piet van Oostrum

Oct 21, 2008

DOS-12

Tuple spaces and Object spaces

I

Tuple spaces

I Shared memory as a mechanism for exchanging data

I in a distributed setting (i.e. separate from processes) I

Contents

I What is it?

I Principles of tuple spaces

I Comparison with other mechanisms

I JavaSpaces/Jini as OO variant

Piet van Oostrum DOS-12 1

DOS-12

Tuple space

I

Tuple

I Ordered sequence of (typed) values

I Usually it has a “name” (first element) I

Tuple space

I Bag of tuples

I Identical tuples may be present multiple times

I Tuple space is associative memory i.e. search on contents only, not on identity I

Linda program

I Works on collection of ordered tuples

I Tuple contains data (or possibly code)

I Consists of a number of cooperating processes or threads

I Only communication is by putting tuples in and getting tuples out of tuple space

I

Linda is an abstract concept (ideas)

Piet van Oostrum DOS-12 2

DOS-12

Communication

puts tuple in tuple space

withdraws tuple from tuple space

B

A

Tuple Space

tuple tuple

No direct communication between A and B

Piet van Oostrum DOS-12 3

Mechanisms 1

I

Putting

out( N, P2, ..., Pj ) ;

I out statement puts a tuple in tuple space

I Continues immediately

I First parameter is a name (string)

I Instead of values also ‘formals’ in the form i: integer may be given

I This is an unspecified but typed part of the tuple (wildcard)

Mechanisms 2

I

Withdrawing

in( N, P2, ..., Pj ) ;

I in statement withdraws a tuple from tuple space

I Parameter is a template: contains values and typed variables

I Matching based on value parts

I Blocks until matching tuple is available

I Variables are filled with tuple values

I Tuple is removed from tuple space I

Reading

read( N, P2, ..., Pj ) ;

I read statement same as in

(2)

DOS-12

Properties

I

Pattern matching

in( P, i:integer, j:integer )

I Matches (+withdraws) tuple with name "P" and any integer value on the rest

in( P, i:integer, FALSE )

I Matches (+withdraws) tuple with name "P", any integer value in second place and FALSE in third place

I Tuples with wildcards match any actual value in in statement

I Compare with Prolog: binding of free variables

Piet van Oostrum DOS-12 6

DOS-12

System characteristics

I

Nondeterminism

I Which process succeeds in()’s or read()’s

I Which tuple you get when there are more matches I

Atomicity

I Of insertion and withdrawing

I

Sender and receiver do not know each other

I

Distribution in space

I Shared memory alike I

Distribution in time

I Mailbox alike: once put, stays forever until withdrawn

I No explicit synchronisation

I Communication possible after sender dies

I Semantics is independent from timing (modulo nondeterminism)

Piet van Oostrum DOS-12 7

DOS-12

Process notation 1

I

Simple statement

in() out()

read()

I

Compound statement

[ ... ]

Piet van Oostrum DOS-12 8

DOS-12

Process notation 2

I

Combinations

I Sequence

s1 ; s2 ; ... ; sj

I Concurrency (and parallellism):

All sirun parallel

s1 & s2 & ... & sj

I Mutual exclusive concurrency (or parallellism):

Only one of the siwill run (one that is ready) non-deterministically

Blocking in or read will not be chosen If all siblock then the whole statement blocks s1 | s2 | ... | sj

Piet van Oostrum DOS-12 9

Process notation 3

I

Repetition

*s

I

Input and action syntactic sugar

*[in( -t ) ; s ]

in( -t ) => s

I

Macro

def SYNCH_SEND(s:tuple) [ ... ]

Small examples

I

Remote procedure equivalent

I Client side out( P, me, ... ) in( me, ... )

I Server side

in( P, who:name, ... ) =>

[ body ;

out( who, ... ) ]

I me is replaced by unique process identification I

How to do Remote Method Invocation (RMI)?

(3)

DOS-12

Semaphore equivalent

I

P(sem)

in( sem )

I

V(sem)

out( sem )

Piet van Oostrum DOS-12 12

DOS-12

Explicit synchronisation

I

Rendez-vous style of communication

I Communication style in Ada programming language

I ‘client’ does procname(p1, p2, ...)

I ‘server’ does

accept procname(p1, p2, ...) do statements end

I parameters p1, p2, ... may be in or out

I client and server wait until they are both ready before data transfer

I both client and server continue only after exchange of data completed and accept body executed

Simplified emulation (if only one pair of communicating

processes):

def SYNCH_SEND(s:tuple)

[ in(get); out(s); in(got) ]

def SYNCH_RECV(s:tuple)

[ out(get); in(s); out(got) ]

Piet van Oostrum DOS-12 13

DOS-12

Disk head scheduler

I

Active monitor, filtering/sorting requests

Note: A −→ B means messages go from A to B through

tuple space, not direct communication.

DISK_FILTER

DISK

I

Client side

def DISK_SVC( t:track; d:data )

[ out( disk_req, me, t, d ) ;

in( me )

]

Piet van Oostrum DOS-12 14

DOS-12

Disk head scheduler – Continued

I

Server side

DISK_FILTER & DISK

def DISK

* [ out( get_diskreq ) ;

in( disk_driver, who:name, i:track, d:data ) ;

perform physical i/o ;

out( who )

]

Piet van Oostrum DOS-12 15

Disk head scheduler – Continued

def DISK_FILTER

DECLARATIONS & *[DISK_ENQUEUE | DISK_DEQUEUE]

def DECLARATIONS

disk_idle:boolean = false

& other necessary data structures

def DISK_ENQUEUE

[ in( disk_req, who:name, i:track, d:data ) ;

if ( disk_idle )

[ out( disk_driver, who, i, d ) ;

disk_idle = false

] else insert who, i, d in i/o queue

Disk head scheduler – Continued

def DISK_DEQUEUE

[ in( get_diskrequest ) ;

who:name, i:track, d:data &

[ if queue is empty

disk_idle = true

else [ who, i, d = get and remove from queue ;

out( disk_driver, who, i, d ) ;

] ]

]

(4)

DOS-12

Linda implementation

I

All the tricky stuff is hidden in an implementation

I

Absence of shared memory

I How to find tuples?

I Where to keep tuples?

I

Naming

in( xx, ... )

I Implies naming system able to look for data named xx

I Naming system: distributed directory/database

Piet van Oostrum DOS-12 18

DOS-12

Linda implementation

I

Tuple location

I Tuple availability (via out()) or tuple need (via in()) must be known everywhere

I Either broadcast when information is available

I Or ask (everywhere) for it

I Or in between I

Other issues

I Buffering (of many out()’s)

I Failure, transaction problems

Piet van Oostrum DOS-12 19

DOS-12

Linda extensions – 1

I

Non-blocking operations

I Original Linda had a non-blocking in, called inp

I In case there is no matching tuple this returns a ’not-found’

indication.

I Was thought to be nice to prevent eternally blocking processes

I This makes Linda programs time-dependent, however

I Similar for read

I Definitions of inp and rdp where ambiguous I

Other interpretation

I inp blocks until a tuple is available or it can be proven that there will never be one (a deadlock occurs)

I In case of a deadlock one participant will receive a failure

I this can only be done if all Linda processes are known

I This interpretation tries to remove the time-dependency

Piet van Oostrum DOS-12 20

DOS-12

Linda extensions – 2

I

Multiple Tuple Spaces

I

Good for modularity

I

Tuples spaces in tuples (recursion)

Nested tuplespaces Flat tuplespaces

Piet van Oostrum DOS-12 21

Linda extensions – 3

I

Bulk Tuple Operations

I

The “multiple read problem” is the problem that you can’t

reliably do a read for a collection of tuples.

I

You might get the same tuple multiple times

I

So you must use in multiple times

I

And then put back the tuples

I

You can’t do it atomically

I

Solution: bulk operations:

I collect (T1, T2, template):

moves all tuples that match template from tuple space T1 to tuple space T1

I copy-collect (T1, T2, template) makes a copy

PyLinda

I

A Linda(-like) system written in Python

I

Multiple tuple spaces

I

Tuple spaces live in servers

I

Servers connect to a master server

I

inp and rdp extensions with deadlock detection

I

bulk operations

# start the master server

> linda_server

# start server connected to server at <ip-address>

> linda_server -c <ip-address>

A Linda master server has a main tuple space called “universe”.

(5)

DOS-12

PyLinda

Client:

import linda

linda.connect()

linda.universe._out((1, 2, 3))

linda.universe._in((1, 2, int))

I

Template “wild cards” are given by a type instead of a value.

I

The _in returns a tuple.

Piet van Oostrum DOS-12 24

DOS-12

PyLinda

# create a new tuple space

# and put it in the universe

ts = linda.TupleSpace()

linda.universe._out(("My Space", ts))

ts._out((4, 5, "piet"))

ts2 = linda.TupleSpace()

ts.collect(ts2, (int, int, int))

x = ts2._in((int,int,int))

Piet van Oostrum DOS-12 25

DOS-12

JavaSpaces

I

Tuple Space + Java

I

On top of

I Serialization + RMI

I Events & Transactions I

Differences w.r.t. Linda

I Multiple spaces

I Tuple: class Entry, typed, fields may be objects

I Matching via templates (with wildcards)

I Notification mechanism for availability of tuple (instead of blocking read)

I Transaction properties

I Identity mechanism for security

Piet van Oostrum DOS-12 26

DOS-12

JavaSpaces – Overview

Piet van Oostrum DOS-12 27

JavaSpaces – Usage

I

Writing (out)

JavaSpace space = getSpace();

AttrEntry e = new AttrEntry();

e.name = "DOS";

e.value = new GIFImage("DOSexample.gif");

EntryRep erep = e.rep(); // or new EntryRep(e)

space.write(erep, null, null);

I Tuple = Entry, out = write

I AttrEntry is subclass of jive.javaSpace.Entry

I Match implies class ’equality’

I Entry is packaged into EntryRep before handing it over to a space

JavaSpaces – Usage

I

Taking (in)

JavaSpace space = getSpace();

AttrEntry e = new AttrEntry();

e.name = "DOS";

e.value = null ; // wildcard

EntryRep erep = e.rep();

space.take(erep, null, null);

I in = take

I null value means wildcard

I Parameters for transaction & identification

(6)

DOS-12

Jini – 1

I

Purpose: allow groups of services and users to federate into a

single, dynamic distributed system (Jini community)

I

Goals

I Simplicity of access

I Ease of administration

I Support for easy sharing – “spontaneous” interactions

I Self-healing of Jini communities I

Main operations

I Discovery: find a lookup service

I Join: register your service with a lookup service

I Lookup: find a service in the lookup service I

Done by type: Java interface type

I

Local object (like CORBA proxy/stub) returned to client

I

Invoke: use the local object to call the service

Piet van Oostrum DOS-12 30

DOS-12

Jini – scenario

I

Jini Lookup Service is running on the network

Piet van Oostrum DOS-12 31

DOS-12

Jini – scenario

I

Device is plugged into the network

I

Device registers service(s)

I

Class to use the service is copied to the Lookup Service

Piet van Oostrum DOS-12 32

DOS-12

Jini – scenario

I

Two devices have been registered

I

There UI classes have been copied to the Lookup Service

Piet van Oostrum DOS-12 33

Jini – scenario

I

A new device is plugged into the network

I

It wants to use one of the services

I

It loads the UI class(es) from the Lookup Service

Jini Example

I

Start: one service – lookup – running on network

I

Printer starts up

I Finds lookup service

I Registers self with lookup service (no user intervention) I

Laptop with word processor enters room

I Word processor finds lookup service

I Word processer looks up printer

I Word processor can also optionally

I Register to get callback if printer goes away

I Register to get callback if a new printer registers itself

I Word processor invokes printer (sends it a printer job) I

Printer (not word processor) controls dialog box – only it

knows what it should look like, perhaps in ways not known

when word processor made

(7)

DOS-12

Jini code example (Client)

import net.jini.core.entry.*;

import net.jini.core.lookup.*; // etc.

try {

System.setSecurityManager(new RMISecurityManager());

lookup = new LookupLocator("jini://localhost");

registrar = lookup.getRegistrar(); // Lookup Service id = registrar.getServiceID();

aeAttr = new Entry[1];

aeAttr[0] = new Name("MyServer");

// First 2 params: ServiceID (UUID) and class (servicetypes).

template = new ServiceTemplate(null, null, aeAttr);

myServerInterface =

(MyServerInterface) registrar.lookup(template);

if (myServerInterface instanceof MyServerInterface) { ... myServerInterface.sayHello() ...

} }

Piet van Oostrum DOS-12 36

DOS-12

Jini code example (Server)

public class MyServer extends UnicastRemoteObject implements MyServerInterface, ServiceIDListener { public String sayHello() throws RemoteException {

return ("Hello World from MyServer!");

}public static void main(String[] args) { int i;

MyServer myServer;

Entry[] aeAttr;

JoinManager joinmanager;

Piet van Oostrum DOS-12 37

DOS-12

Jini code example (Server) contd.

try {

System.setSecurityManager(new RMISecurityManager());

aeAttr = new Entry[1];

aeAttr[0] = new Name("MyServer");

myServer = new MyServer();

joinmanager = new JoinManager(myServer, aeAttr, myServer, new LeaseRenewalManager());

// Third parameter is a ServiceID or ServiceIDListener } ...

catch (Exception e)

Piet van Oostrum DOS-12 38

DOS-12

Jini – facilities

I

Leasing: automatic garbage collection

I Service granted for a limited period of time: a lease

I If lease not renewed (it expires), resources freed I

Transactions

I Two-phase commit

I Note: Jini, and JavaSpaces are not databases

I Jini (JavaSpaces) supports full transactions (two-phase commit)

I

Events

I Can register for callbacks for events of interest

Piet van Oostrum DOS-12 39

References

I

David Gelernter, Generative Communication in Linda. ACM

TOPLAS 7(1), Jan 1985, pg 80-112

I

The S/Net’s Linda Kernel. Nicholas Carriero and David

Gelernter, ACM TOCS 4(2), May 1986, pg 110-129

I

Linda Bibliography,

http://www.cs.yale.edu/HTML/YALE/CS/Linda

I

Javaspaces

http://java.sun.com/docs/books/jini/javaspaces/

I

Jim Waldo, The Jini Architecture for Network-centric

computing. Comm. of the ACM, July 1999, vol 42, no. 7, pg

76-82

References

Related documents