Distributed Object and RMI

advertisement
Distributed Information Systems
Distributed Objects and Remote Invocation
• Conventional models
– Procedure call.
• Objectives
– RPC and RMI.
– Remote invocation semantics.
– Implementation of RMI.
• References
– DSCD: Chapter 5
Y. Xiang, CIS 4400, Distributed Information Systems
– Method invocation in
OO programming.
• Extension to DS
– Remote procedure
call (RPC).
– Remote method
invocation (RMI).
– Event-based
programming.
1
Middleware Layer
• Provide location transparency.
• Provide independence from details of communication
protocols, OS, and hardware.
• Support components in different programming
languages.
Applications
RMI, RPC and events
Request reply protocol
Programming Models For Distributed Applications
Middleware
layers
External data representation
Y. Xiang, CIS 4400, Distributed Information Systems
– Distributed eventbased programs.
2
Interface b/w Program Modules In DS
• Parameter-passing mechanisms for local procedure
calls do not work.
– Ex: Pointer argument in C function
void insert(float *farray, float num);
– Implication to parameter-passing in DS?
• Service interface in RPC: a server provides a set of
procedures available to clients.
• Remote interface in RMI: specifies methods of an
object available for remote invocation.
– A subset of methods available to local objects.
Operating System
Y. Xiang, CIS 4400, Distributed Information Systems
3
Y. Xiang, CIS 4400, Distributed Information Systems
4
1
Distributed Information Systems
Remote Invocation Semantics
Remote Object References
• When a client invokes a method in a remote object, a
message is directed to the object.
– Object at different servers and providing different
services may be named identically.
– A remote object may be deleted and re-created at
a different state.
– How to direct message to the right object?
• A remote object reference must be unique
throughout DS and over time.
– Host IP addr + port number + creation time
Y. Xiang, CIS 4400, Distributed Information Systems
5
Remote Invocation Semantics
Y. Xiang, CIS 4400, Distributed Information Systems
6
Remote Invocation Semantics
• At-least-once semantics
– Request is retransmitted on timeout and each
received request is executed.
– Invoker receives either a result or an exception.
– What does invoker know upon receiving result?
– What does it know upon receiving exception?
– Useful with idempotent operations.
– Ex: Adopted by Sun RPC.
Y. Xiang, CIS 4400, Distributed Information Systems
• As middleware, the semantics of a RMI service
should be known to the applications that use it.
• Maybe semantics
– No request retransmission.
– Invoker receives a result or a timeout exception.
– What does invoker know upon receiving result?
– What does invoker know upon receiving timeout?
– Useful when occasional failure is acceptable.
– Ex: A update request in a series.
– Ex: Allowed by CORBA for some methods.
• At-most-once semantics
– Request retransmission, duplicate request deletion,
and history are applied.
– Invoker receives either a result or an exception.
– What does invoker know upon receiving result?
– What does invoker know after receiving exception?
– Ex: Adopted by Java RMI and CORBA.
• Summery of alternative remote invocation semantics.
7
Y. Xiang, CIS 4400, Distributed Information Systems
8
2
Distributed Information Systems
RMI Transparency
• Transparent RMI
– Same syntax for local & remote procedure calls.
– Locating and contacting remote objects, marshalling
and message passing are hidden.
• Issues
– RMI is more vulnerable than local invocation.
– Latency difference b/w RMI and local invocation
• Current consensus: keep invocation syntax the same
but invocation interface different.
– Ex: Java RMI.
Y. Xiang, CIS 4400, Distributed Information Systems
9
Develop Java RMI Server And Client
1.
2.
3.
4.
Remote Invocation In Java RMI
• Ex: Computing a product through a remote
calculator service.
– Client side:
Calc c = (Calc) Naming.lookup(“//bayes.cis/CalcServ”);
result = c.multiply(numb1, numb2);
– Any difference from local invocation?
Y. Xiang, CIS 4400, Distributed Information Systems
10
Implementation Of RMI
Define a remote interface (Calculator.java).
Implement the remote service (CalculatorImpl.java).
Host the service (CalculatorServer.java).
Use the service in a client (CalculatorClient.java).
• Several objects and modules are involved in a RMI.
server
client
object A proxy for B
skeleton
Request
remote
object B
for B
Reply
Communication
Remote
reference module
module
Y. Xiang, CIS 4400, Distributed Information Systems
11
Communication
module
Y. Xiang, CIS 4400, Distributed Information Systems
Remote reference
module
12
3
Distributed Information Systems
Implementation Of RMI
Implementation Of RMI
• Proxy (stub): behaves like a local ‘server’ to the client
object.
– One for each remote object.
– Request marshalling, request passing, and reply
unmarshalling.
• Skeleton: behaves like a local ‘client’ to the server
object.
– Receiving request from communication module,
request unmarshalling, remote (local) method
invocation, reply marshalling, and reply passing.
Y. Xiang, CIS 4400, Distributed Information Systems
13
Implementation Of RMI
Y. Xiang, CIS 4400, Distributed Information Systems
14
Remote Object Registration
• Communication module
– Two cooperative modules transmit request/reply.
– Use sender id, request id, and remote object
reference info to provide a specified remote
invocation semantics.
– Interact with proxy object at client, and skeleton
object at server.
Y. Xiang, CIS 4400, Distributed Information Systems
• Generation of proxy and skeleton
– Their classes are generated automatically by an
interface compiler (e.g., java rmic).
– What information is the generation based on?
• Remote reference module: maintains remote object
table for translation b/w local and remote object
reference (ROR).
– At client: RORs ↔ proxies
– At server: ROR ↔ remote (local) object
– Used by proxy and skeleton.
15
• Where does a client find the remote object reference
for a remote service?
• Binder: A service that maintains mapping b/w remote
object names (a string) and their remote object
reference.
• Upon creation, a remote object is registered at the
binder.
• A client can look up a service from binder using
remote object name to obtain the remote object
reference.
Y. Xiang, CIS 4400, Distributed Information Systems
16
4
Distributed Information Systems
Java Binder
RMI Compilation And Execution
• The binder program rmiregistry is supplied with jdk.
• Each host has at least one rmiregistry running.
• Server uses methods in class Naming to register a
remote object.
• Ex: Registering a calculator service.
Calc c = new CalcImpl();
Naming.rebind(“//bayes.cis:1099/CalcServ”, c);
Y. Xiang, CIS 4400, Distributed Information Systems
17
RMI Compilation And Execution
Y. Xiang, CIS 4400, Distributed Information Systems
18
Obtain Stub for Client
• Server side
– Compile 1,2 & 3 with javac.
– Create stub/skeleton: rmic CalculatorImpl
– Start RMI registry: rmiregistry &
– Start server: java CalculatorServer &
• Client side
– Compile 1,4 with javac.
– Obtain CalculatorImpl_Stub.class.
– Run client: java CalculatorClient
Y. Xiang, CIS 4400, Distributed Information Systems
• Components:
1. Remote interface: Calculator.java
2. Service implementation: CalculatorImpl.java
3. Server: CalculatorServer.java
4. Client: CalculatorClient.java
• How does a client developer obtain stub?
• Option 1: run rmic from service implementation.
– A client developer may not have the service
implementation code.
• Option 2: download from server’s public site.
– Downloading adds an extra step in client
development.
19
Y. Xiang, CIS 4400, Distributed Information Systems
20
5
Distributed Information Systems
Dynamic Stub Loading
Dynamic Stub Loading
Idea: Client obtains stub at runtime from a URL.
java -Djava.security.policy=policy_file CalculatorClient
1. Place the stub class file at a world-readable,
published location called the codebase.
2. Start server with codebase info:
java -Djava.security.policy=policy_file
-Djava.rmi.server.codebase=codebase_url
CalculatorServer &
Y. Xiang, CIS 4400, Distributed Information Systems
3. When server registers, registry saves codebase_url.
4. Start client as follows:
5. If client cannot find stub in its classpath, it uses
codebase_url from registry and downloads stub
from there.
6. From then on, client interacts with server through
the stub.
• Levels of sophistication in RMI deployment.
21
Y. Xiang, CIS 4400, Distributed Information Systems
22
On Demand Remote Object Creation
On Demand Remote Object Activation
• Idea: To reduce resource consumption, an object is
not created until needed.
• A server initializes and registers one remote (factory)
object T.
• A client invokes a remote method on T in order to
obtain a remote reference to another object Q.
• If Q does not exist yet, T creates it on demand.
• T returns to client the remote reference for Q.
• The client invokes a remote method on Q.
• Advantages?
• Idea: To reduce resource consumption, an object is
not activated until needed.
• Active: a remote object is instantiated and available
for invocation in a running process.
• Passive: a remote object is not instantiated but can be
made active.
• A passive remote object consists of two parts:
– the implementation of its methods, and
– its state in marshaled form.
Y. Xiang, CIS 4400, Distributed Information Systems
23
Y. Xiang, CIS 4400, Distributed Information Systems
24
6
Distributed Information Systems
On Demand Remote Object Activation
• Activator: a process that starts server processes to
host remote objects.
– Map activation id to info on passive object.
– Start a server process which creates a new instance
of the passive object class and initializes instance
variables to the stored state.
– Java RMI usually uses one activator per host.
Y. Xiang, CIS 4400, Distributed Information Systems
25
7
Download