Professional Documents
Culture Documents
Called
Each process contains objects, some of which can receive remote invocations, others only local invocations Those that can receive remote invocations are called remote objects Objects need to know the remote object reference of an object in another process in order to invoke its methods. How do they get it? The remote interface specifies which methods can be invoked remotely
COMP 6231, Fall 2007 Distributed Objects and RMI 4
remote interface { m1 m2 m3
Data m4 m5 m6
implementation of methods
Communication module
Reply
Stubs
Creating code for marshalling and unmarshalling is tedious and error-prone. Code can be generated fully automatically from interface definition. Code is embedded in stubs for client and server. Client stub represents server for client, Server stub represents client for server. Stubs achieve type safety. Stubs also perform synchronization.
COMP 6231, Fall 2007 Distributed Objects and RMI 8
Synchronization
Goal: achieve similar synchronization to local method invocation Achieved by stubs:
Client stub sends request and waits until server finishes Server stub waits for requests and calls server when request arrives
32 bits
port number
32 bits
time
32 bits
object numberinterface of remote object
A remote object reference must be unique in the distributed system and over time. It should not be reused after the object is deleted. The first two fields locate the object unless migration or re-activation re activation in a new process can happen The fourth field identifies the object within the process its interface tells the receiver what methods it has (e.g. class Method) A remote object reference is created by a remote reference module when a reference is passed as argument or result to another process
It will be stored in the corresponding proxy It will be passed in request messages to identify the remote object whose method is to be invoked
COMP 6231, Fall 2007 Distributed Objects and RMI 10
maps the platform-independent stub/skeleton layer to the platform-dependent transport layer; carries out remote reference protocols sets up, maintains, and shuts down connections; and carries out the transport protocol
11
rmic i
SomeImpl S I l
5. 6.
The files generated can be found in the directory as SomeImpl_Skel.class and SomeImpl_Stub.class. Steps 3 and 4 must be repeated each time that a change is made to the interface implementation. Create the object server program SomeServer.java. Compile it until there is no more syntax error. Activate the object server
java
COMP 6231, Fall 2007
SomeServer
Distributed Objects and RMI 12
3.
4. 4 5.
SomeImpl_Stub.class.
14
String [] list()
This method returns an array of Strings containing the names bound in the registry.
COMP 6231, Fall 2007 Distributed Objects and RMI 15
HelloInterface
// A simple RMI interface file - M. Liu import java.rmi.*; /** * This is a remote interface. */ public interface HelloInterface extends Remote { /** * This remote method returns a message. * param name - a String containing a name. * return a String message. message */ public String sayHello(String name) throws java rmi RemoteException; java.rmi.RemoteException; } //end interface
COMP 6231, Fall 2007 Distributed Objects and RMI 16
Implementation of HelloInterface
import java.rmi.*; import java.rmi.server.*; /* This class implements the remote interface HelloInterface. */ public class HelloImpl extends UnicastRemoteObject implements HelloInterface { public HelloImpl() throws RemoteException { super( ); } public String sayHello(String name) throws RemoteException { return "Hello, World!" + name; } } // end class
COMP 6231, Fall 2007 Distributed Objects and RMI 17
Hello Server
import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.net.*; import java.io.*; java.io. ; /* This class represents the object server for a distributed object of class Hello, which implements the remote interface HelloInterface. */ p public class HelloServer { public static void main(String args[]) { InputStreamReader is = new InputStreamReader(System.in); ( ); BufferedReader br = new BufferedReader(is); String portNum, registryURL;
COMP 6231, Fall 2007 Distributed Objects and RMI 18
Hello Client
import ja a io * impo t java.io.*; import java.rmi.*; /* This class represents the object client for a distributed object of class Hello, which implements the remote interface HelloInterface. */ public class HelloClient { public static void main(String args[]) { try { int RMIPort; String hostName; InputStreamReader is = new InputStreamReader(System.in); BufferedReader br B ff dR d b = new BufferedReader(is); B ff dR d (i ) System.out.println("Enter the RMIRegistry host name:"); hostName = br.readLine(); y p ( g yp ); System.out.println("Enter the RMIregistry port number:"); String portNum = br.readLine(); RMIPort = Integer.parseInt(portNum);
COMP 6231, Fall 2007 Distributed Objects and RMI 20
21
22
23
The socket API is closely related to the operating system, and hence has less execution overhead. For applications which require high performance, this may be a consideration consideration. The RMI API provides the abstraction which eases the task of software development. Programs developed with a higher level of abstraction are more comprehensible and hence easier to debug.
Distributed Objects and RMI 24
Typical optimizations?
Compile the stub inline to put arguments directly into message Two versions of stub; if (at bind time) sender and destination found to have same data representations, use host-specific representation p p Use a special send, then receive system call (requires O/S extension) Optimize the O/S kernel path itself to eliminate copying treat RPC as the most important task the kernel will do
COMP 6231, Fall 2007 Distributed Objects and RMI 25
What about complex structures, pointers, big arrays? These will be very costly, and perhaps impractical to pass as arguments Most implementations limit size, types of RPC arguments. Very general systems less limited but much more costly. costly
COMP 6231, Fall 2007 Distributed Objects and RMI 26
27
Multithreading
Idea is to support internal concurrency as if each process was really multiple processes that share one address space Thread scheduler uses timer interrupts and context switching to mimic a physical i hi i i h i l multiprocessor using the smaller number of CPU s CPUs actually available
30
Multithreaded RPC
Each incoming request is handled by spawning a new thread Designer must implement appropriate mutual exclusion to guard against race conditions and other concurrency problems Ideally, server is more active because it can p process new requests while waiting for its q g own RPCs to complete on other pending requests
COMP 6231, Fall 2007 Distributed Objects and RMI 31
Negatives to multithreading
Users may have little experience with concurrency and will then make mistakes Concurrency bugs are very hard to find due to non nonreproducible scheduling orders Reentrancy can come as an undesired surprise Threads need stacks hence consumption of memory can be very high Deadlock remains a risk, now associated with concurrency control Stacks for threads must be finite and can overflow, corrupting the address space
COMP 6231, Fall 2007 Distributed Objects and RMI 32
Activation stacks (parameters, local variables) Heap (dynamic storage, objects, global variables) 'text' (program code) system-provided resources (sockets, windows, open files)
COMP 6231, Fall 2007 Distributed Objects and RMI 33
Client
35
b. Thread-per-connection
c. Thread-per-object
Research-based systems have implemented much more efficient invocation protocols. protocols Concurrent and asynchronous invocations
middleware or application doesn t block waiting for doesn't reply to each invocation
COMP 6231, Fall 2007 Distributed Objects and RMI 39
run()
A thread executes the run() method of its target object if it has one and object, one, otherwise its own run() method (Thread implements Runnable).
start()
Change the state of the thread from SUSPENDED to RUNNABLE.
sleep(int millisecs)
Cause the thread to enter the SUSPENDED state for the specified time.
yield()
Enter the READY state and invoke the scheduler.
destroy()
Destroy the thread.
COMP 6231, Fall 2007 Distributed Objects and RMI 40
thread.interrupt() Interrupts thread: causes it to return from a blocking method call such as sleep(). object.wait(long millisecs, int nanosecs) Blocks the calling thread until a call made to notify() or notifyAll() on object wakes the thread or the thread is thread,
interrupted, or the specified time has elapsed.
object.notify(), object.notifyAll()
Wakes, respectively, Wakes respectively one or all of any threads that have called wait() on object.
COMP 6231, Fall 2007 Distributed Objects and RMI 41
Thread 1
Thread 2
User 1
COMP 6231, Fall 2007
Kernel
User 2
42
Thread 1
Thread 2
User 2
43
Upcall model
Common in windowing systems Each incoming event is encoded as a event small descriptive data structure User registers event handling procedures Dispatch loop calls the procedures as new events arrive, waits for the call to finish, then dispatches a new event
COMP 6231, Fall 2007 Distributed Objects and RMI 44
Statefulness issues
Client-server system is stateless if:
Client is independently responsible for its actions, actions server doesnt track set of clients or ensure that cached data stays up to date.
The UNIX NFS file system is stateless. y
Server tracks its clients, takes actions to keep , p their cached states current. Client can trust its cached data.
Database systems are usually stateful stateful.
COMP 6231, Fall 2007 Distributed Objects and RMI 46
GET file name file ID send <file ID>, block 0 data from block 0 of file send <file ID>, block 1 data from block 1 of file
GET file name ready send next block data from block 0 of file send next block data from block 1 of file
...
COMP 6231, Fall 2007 Distributed Objects and RMI
...
47
48
The issue is that some techniques seem simple and seem to do the trick but are as much as thousands of times slower than other alternatives th lt ti
Forcing us to use those alternatives... And perhaps g y p pp driving us away from what the platforms support
COMP 6231, Fall 2007 Distributed Objects and RMI 50
Performance work is producing enormous gains: from the old 75ms RPC to RPC over U/Net with a 75usec round-trip time: a factor of 1000! 75 d t i ti f t f
COMP 6231, Fall 2007 Distributed Objects and RMI 51
53
Bershad's LRPC
Uses shared memory for interprocess communication
while maintaining protection of the two processes arguments copied only once (versus four times g p y ( for convenitional RPC)
A stack
1. Copy args
A
4. Execute procedure and copy results
User Kernel
stub t b
stub t b
2. Trap to Kernel
3. Upcall
5. Return (trap)
55
time
Callback
In h li I the client server model, the server is passive: the d l h i i h IPC is initiated by the client; the server waits for the q p p arrival of requests and provides responses. Some applications require the server to initiate communication upon certain events. Examples applications are:
monitoring games auctioning voting/polling chat-room message/bulletin board groupware
58
Callback
Server
...
Client A client issues a request to the server repeatedly until the desired response is obtained.
COMP 6231, Fall 2007
Client A client registers itself with the server, and wait until the server calls back.
59
Two-way communications
Some applications require that both sides may initiate IPC. Using sockets, duplex communication can be achieved by using two sockets on either side. With connection-oriented sockets, each side acts as both a client and a server.
Process 1 request response request response Process 1
60
RMI Callbacks
A callback client registers itself with an RMI server. The server makes a callback to each registered client upon the occurrence of a certain event.
Server Clients C1 The callback list C2 RMI calls callback C4
C3
C5
61
SomeInterface_skel.class SomeServer.class S S l
CallbackInterface_skel.class 5
CallbackInterface_stub.class 1. Cli t l k 1 Client looks up the interface object in the RMI i t on th server h t th i t f bj t i th RMIregistry the host. 2. The RMIRegistry returns a remote reference to the interface object. 3. Via the server stub, the client process invokes a remote method to register itself for callback, passing a remote reference to itself to the server. The server saves the reference in its callback list. 4. Via the server stub, the client process interacts with the skeleton of the interface object to access the methods in the interface object. 5. When the anticipated event takes place, the server makes a callback to each registered client via the callback interface stub on the server side and the callback interface skeleton on the client side. COMP 6231, Fall 2007 Distributed Objects and RMI
62
ClientImpl.class
S erverImpl_S tub.class ClientImpl_skel.class
ServerImpl.class
ClientImpl_S tub.class S erverImpl_skel.class
63
64
Callback Client
import java.io.*; import java.rmi.*; /** This class represents the object client for a distributed object of class * CallbackServerImpl, which implements the remote interface CallbackServerImpl * CallbackServerInterface. It also accepts callback from the server. */ p public class CallbackClient { public static void main(String args[]) { try { int RMIPort; String hostName; InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); System.out.println( "Enter the RMIRegistry host namer:"); hostName = br.readLine();
COMP 6231, Fall 2007 Distributed Objects and RMI 68
69
74
Callback Server
import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.Registry; import java rmi registry LocateRegistry; java.rmi.registry.LocateRegistry; import java.net.*; import java.io.*; / /** * This class represents the object server for a distributed object of p j j class Callback, which implements the remote interface CallbackInterface. @author M. L. Liu */ public class CallbackServer { public static void main(String args[]) { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); String portNum, registryURL;
76
77