Chapter Five Remote Method Invocation 1
Chapter Five Remote Method Invocation 1
6
Distributed systems by Patrick ndungu
Request-Reply Protocols Cont…
server stub receives the messages from the kernel. It unmarshals the arguments
from the network message and possibly converts them.
v. The server stub executes a local procedure call to start the function / invoke the
actual server function andDistributed
passing systems
it the arguments(
by Patrick pass the parameters) that it
22
received in the network messagesndungufrom the client stub.
Remote Procedure Call (RPC) Cont…
• RPC Operation
vi The server process executes the procedure and returns the result to the server
stub i.e when the server procedure is finished, it returns to the server stub with
return values.
vii The server stub converts the return values, if necessary, and marshals them into
one or more network messages to send back to the client stub.
viii The messages get transferred back across the network to the client stub.
ix The client stub reads the network messages from the local kernel using read or
recv.
x After possibly converting the return values, the client stub finally returns to the
client function.
This appears to be a normal procedure return to the client.
The client and server routines use stub programs for the network
communication. The RPC routine hides all the details.
This simplifies the creation of the program and isolates the network
code.
Before these steps can be performed, the server must be started on the
appropriate host.
Distributed systems by Patrick ndungu 23
Remote Procedure Call (RPC) Cont…
• Summary of RPC Operation
The specific steps are:
1) The client calls the procedure. It calls the client stub, which packages the arguments into
network messages. Packaging is called marshaling.
2) The client stub executes a system call (usually write or sendto) into the local kernel to
send the message.
3) The kernel uses the network routines (TCP or UDP) to send the network message to the
remote host. Note that connection-oriented or connectionless protocols can be used.
4) A server stub receives the messages from the kernel and unmarshals the arguments.
5) The server stub executes a local procedure call to start the function and pass the
parameters.
6) The server process executes the procedure and returns the result to the server stub.
7) The server stub marshals the results into network messages and passes them to the
kernel.
8) The messages are sent across the network.
9) The client stub receives the network messages containing the results from the kernel
using read or recv.
10) The client stub unmarshals the results and passes them to the client routine
Distributed systems by Patrick ndungu 24
Remote Procedure Call (RPC) Cont…
Implementation issues
Stub generator
generates stub automatically.
For this, only need procedure signature:-
types of arguments,
return value.
Generates code on client to pack message, send it off
On server to unpack message, call procedure
Input: interface definitions in an interface definition language
(IDL)
Output: stub code in the appropriate source language (e.g.Java,…)
Implementation Detail(failure handling)
Client can’t locate the server
Procedure returns error upon binding time
Request and reply messages are acknowledged; do timeout and retransmit
All requests are accompanied by a sequence number for the client
Server maintains per client:
Sequence number of last request
Distributed systems by Patrick ndungu 25
Result generated by last request
Remote Procedure Call (RPC) Cont…
• RPC is normally considered to be at the presentation layer (layer 6). One of the
things it does is to provide the data conversion between hosts, so different
machine types can be used. Sun RPC uses the extended Data Representation
(XDR) standard to format data. Both TCP and UDP are supported
The net effect of all these steps is to convert the local call by the client procedure
to the client stub, to a local call to the server procedure without either client or
server being aware of the intermediate steps.
33
Implementing RPC Mechanisms Cont..
Marshalling Arguments and Results
– Marshalling – encoding and decoding data into/from stream form before and
after transmission
– Must reflect the structures of all types of program objects used in the
concerned language
– Are of two types:
i. Those provided by RPC software – supports pre-defined data types
ii. User-defined –for supporting user-defined data types
Server Management
• Call packets and result packets are passed from RPC to RPC until they reach
their destination
•Server implementation
– Stateful servers – stores clients information from one RPC to the next
– Stateless servers- does not maintain any client state information
Server Creation
• Instance-per-call server – call duration
• Instance-per-session servers
34
• Persistent servers
Implementing RPC Mechanisms Cont..
• Server Management - Examples
– Stateful Servers
• Read(fid, n, buffer) – get n bytes of data from file whose Id is fid into the
buffer named buffer.
• Write(fid, n buffer) - the server takes n bytes of data from the specified
buffer, writes into fid at the byte position currently addressed by read-
write pointer.
– Stateless Servers
• Read(filename, ,position,n, buffer) – get n bytes of data from file whose Id
is filename into the buffer named buffer.
• Write(filename,position, n buffer) - the server takes n bytes of data from
the specified buffer, writes into fid at the byte position position.
35
Implementing RPC Mechanisms Cont..
• Parameter-Passing-Semantics
– Call-by-value – parameters copied onto a message
– Call-by-reference – passing pointers is meaningless because of the differences
in address spaces. Only possible incase of shared memory
• Call Semantics
– Failures during message passing may affect the RPC, hence the need for call
semantics:
• Possibly Or May-Be Call Semantics – caller continues after a time-out
• At-Least-Once Call Semantics
• Exactly-Once Call Semantics
36
Implementing RPC Mechanisms Cont..
Failure Handling
•RPC failures can be difficult to handle. There are four generalized types of failures that can
occur when an RPC call is made:
i. The Client’s request message is lost.
• If the client’s message gets lost then the client will wait forever unless a time out
error detection mechanism is employed.
ii. The client process fails while the server is processing the request.
• If the client process fails then, the server will carry out the remote operation
unnecessarily.
iii. The sever process fails while servicing the request.
iv. The reply message is lost.
• If the operation involves updating a data value then this can lead to a loss of data
integrity.
• Furthermore, the server would generate a reply to client process that does not exist.
This must be discarded by the client’s machine. When the client re-starts, it may be send
the request again causing the server to execute more than once.
• A similar situation arises when the server crashes. The server could crash just prior to
the execution of the remote operation or just after execution completes but before a
reply to the client is generated. In this case, clients will time-out and continually
generate retries until either the server restarts or the retry limit is met.
37
Remote Procedure Call (RPC) Cont
Communication Protocols for RPCs:-
i) The Request Protocol – (the R Protocol)
Nothing to return
Acknowledgement not required
Caller continues with execution after the call
Provides May-Be-Call Semantics
This RPC is asynchronous
E.g. A time server node in a DS may send synchronization messages every T
seconds so losing a message or two is acceptable
2.
ii) The Request/Reply Protocol – (the RR Protocol)
– Suitable for simple RPCs – all arguments/results fit in a single packet buffer and has
less transmission time
– Based on implicit acknowledgement
– Has no failure-handling capabilities
– Uses timeouts-retries technique to handle failures, hence provides at-least-once
call semantics
– Can support exactly-once semantics if the server keeps records of replies. 38
Remote Procedure Call (RPC) Cont
Communication Protocols for RPCs cont…:-
iii) The Request/Reply/Acknowledgement Reply Protocol –(the RRA
Protocol)
Client acknowledge the receipt of reply messages and the server uses this to
delete reply information from its cache
Since acknowledgement messages may get lost, both messages (the reply and the
acknowledgement) have identifiers for matching purposes
• Exercise:
– Is there RARA Protocol? (Request, acknowledge, reply, acknowledge)
•Complicated RPCs
– Involving long duration
• Periodic probing of the server by the client
• Periodic generation of acknowledgement by the server
–Involving arguments/results that are too large
• Use several physical RPCs to handle one large logical RPC
• Use multidatagram messages
39
Remote Procedure Call (RPC) Cont
Server Binding
– Client stub needs to know the location of the server through binding
– Servers “export‟ their operations and clients “import‟ via the RPC Runtime.
– Interface names (type and instance) are used for servers
– Servers can be located by use of “broadcasting‟ or “Binding Agent‟.
Sun RPC
– Uses automatic stub generation but there is an option for manually writing
them
– RPC Language (RPCL) – an application's interface definition (IDL) which is an
extension of the Sun XDR (external data representation)
– Question?
• Do we use java or C++ for the case studies?
42
c) Remote Method Invocation (RMI)
Introduction
– Remote method invocation (RMI) strongly resembles remote
procedure calls but in a world of distributed objects.
• With this approach, a calling object can invoke a method in a
remote object.
• Similar to RPC, the underlying details are generally hidden
from the user.
• RMI implementations may, though, go further by supporting
object identity and the associated ability to pass object
identifiers as parameters in remote calls.
Introduction
•The commonalities between RMI and RPC
– They both support programming with interfaces(RPI),
with the resultant benefits that stem from this approach.
– They are both typically constructed on top of request-
reply protocols and can offer a range of call semantics
such as at-least-once and atmost-once.
– They both offer a similar level of transparency – that is,
local and remote calls employ the same syntax but
remote interfaces typically expose the distributed nature
of the underlying call, for example by supporting remote
exceptions.
44
Remote Method Invocation (RMI) Cont..
A Simple Overview
Java RMI allows one Java object to call methods on another Java object in a
different JVM (Java virtual machine)
A Java Virtual Machine (JVM) enables a set of computer software programs
and data structures to use a virtual machine model for the execution of other
computer programs and scripts
46
Remote Method Invocation (RMI) Cont..
A Java Virtual Machine (JVM) enables a set of computer software programs and
data structures to use a virtual machine model for the execution of other computer
programs and scripts
47
Remote Method Invocation (RMI) Cont..
Communicate with remote objects.
Communication between remote objects are handled by RMI.
Load class definitions for objects that are passed around.
provides mechanisms for loading an object's class definitions
as well as for transmitting an object's data
49
Remote Method Invocation (RMI) Cont..
• RMI distributed application
– RMI distributed application uses the RMI registry to obtain a reference to a remote object
– The server calls the registry to associate (or bind) a name with a remote object
– The client looks up the remote object by its name in the server's registry and then invokes a method on it
– RMI system uses an existing web server to load class definitions, from server to client and from client to
server, for objects when needed 50
Remote Method Invocation (RMI) Cont..
51
Java Remote Method Invocation
Implementation
Key components/class definitions in RMI:
Name server (provides location information of
services)
Interface definition of server code
Implementation of server
Implementation of client
Distributed Programming
Java RMI is interface based
A remote object (or distributed service) is specified by its interface
“interfaces define behaviour and classes define implementations”
Termed Remote Interfaces
57
Remote Method Invocation (RMI) Cont..
RMI implementation
RMI passes a remote stub for a remote object
The stub acts as the local representative, or proxy, for the remote
object and basically is, to the client, the remote reference.
The client invokes a method on the local stub, which is responsible
for carrying out the method invocation on the remote object
A stub for a remote object implements the same set of remote
interfaces that the remote object implements.
This property enables a stub to be cast to any of the interfaces that
the remote object implements
However, only those methods defined in a remote interface are
available to be called from the receiving Java virtual machine.
58
Remote Method Invocation (RMI) Cont..
59
The RMI Architecture
Remote Method Invocation (RMI) Cont..
60
Remote Method Invocation (RMI) Cont..
63
Remote Method Invocation (RMI) Cont..
64
Remote Method Invocation (RMI) Cont..
Parameter Passing
Parameter Passing in Java RMI is different from standard
Java
Reminder: In Java, primitives are passed by value, Objects are
passed by reference
In Java RMI
Objects and primitives are passed by value
Remote objects are passed by reference
You must be aware of the differences!
Otherwise you'll generate logical bugs that are difficult to
identify
65
Remote Method Invocation (RMI) Cont..
Parameter Passing 2
RMI-Pass by Value Client
All ordinary objects and primitives are serialised and a copy is
passed
Any changes to the copy do not affect the original
It is your job to ensure your Objects can be serialised!
• RMI-Pass by Reference
Remote Object is the parameter, a stub (reference) is sent the
stub is used to modify the object, the original object is modified
66
Remote Method Invocation (RMI) Cont..
In general terms:
Determine your application architecture
Include which components are local objects and which components are
remotely accessible
Define the remote interfaces.
A remote interface specifies the methods that can be invoked remotely by a
client.
– Implement the remote objects.
Remote objects must implement one or more remote interfaces
– Implement the clients
Clients that use remote objects can be implemented at any time after the
remote interfaces are defined
67
Remote Method Invocation (RMI) Cont..
Building a Java RMI system
– An RMI system must be composed of the following parts:
73
Remote Method Invocation (RMI) Cont..
Step 5 – Create the Client
Get a remote reference by calling Naming.lookup()
Remember - Lookup by service name
Receives a stub object for the requested remote object
Loads code for the stub either locally or remotely
Invoke methods directly on the reference
Much like on standard Java objects
74
Remote Method Invocation (RMI) Cont..
Example Chat Client
public class calculatorclient {
public static void main(String[] args) {
try {
// Get a reference to the remote object through the rmiregistry
ChatServer c = (ChatServer)
Naming.lookup("rmi://localhost/ChatService");
// Now use the reference c to call remote methods
c.login(“Chas”,”*****”);
c.chat(“Chas”, “Hello”);
// Catch the exceptions that may occur - rubbish URL, Remote
exception
} catch (RemoteException re) {
System.out.println("RemoteException“+re);
}
}
}
75
)
Remote Method Invocation (RMI) Cont..
76
Remote Method Invocation (RMI) Cont..
b) Callbacks
In many applications the server may want to callback the
client
All messages in the chat system are displayed on a central
server
You could change the server to callback every client with
each new chat message allowing the message to be displayed
on each client
Callbacks are just a reverse RMI
You create a remote object on the client and pass this
reference to the server, who can invoke it directly
– Using Callbacks in Chat
• See the diagram below
77
Remote Method Invocation (RMI) Cont..
78
The END
Q&A
THANKS