25
Winter, 2004 CSS490 RPC 1 CSS490 RPC and RMI CSS490 RPC and RMI Textbook Ch4 Textbook Ch4 Instructor: Munehiro Fukuda These slides were compiled from the course textbook, th e reference books, and the instructor’s original materi als.

CSS490 RPC and RMI Textbook Ch4

  • Upload
    gotzon

  • View
    48

  • Download
    0

Embed Size (px)

DESCRIPTION

CSS490 RPC and RMI Textbook Ch4. Instructor: Munehiro Fukuda These slides were compiled from the course textbook, the reference books, and the instructor’s original materials. Why RPC. Calling a function at a remote server:. Advanced form of communication Sockets and MPI: data transfer - PowerPoint PPT Presentation

Citation preview

Page 1: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 1

CSS490 RPC and RMICSS490 RPC and RMITextbook Ch4Textbook Ch4

Instructor: Munehiro Fukuda

These slides were compiled from the course textbook, the reference books, and the instructor’s original materials.

Page 2: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 2

Why RPC

Advanced form of communication Sockets and MPI: data transfer RPC: control transfer

Transparency in function calls No distinguish between local and remote calls Also applied to IPC on the same machine

Ease of use Compiled-time argument type checking Automatic interface generation

Calling a function at a remote server:

Page 3: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 3

RPC ModelCaller (Client) Callee (Server)

RPC and wait Request message including arguments

Reply messageIncluding a return value

Suspended

Resume execution

Request message acceptedExecution environment createdExecution of function bodySend reply and wait for the nextrequest

Page 4: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 4

Implementation Issues Transparency property

Syntactic transparency Semantic transparency

Analogy in semantics b/w local and remote procedure calls Caller capable of passing arguments Caller suspended till a return from a function Callee capable of returning a value to caller

Difference in semantics b/w local and remote procedure calls No call by reference and no pointer-involved arguments Error handling required for communication Performance much slower than local calls.

Page 5: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 5

RPC MechanismReturn Call

Receive Send

Define argumentsRegister remote functions

Client Program Server ProgramInterface Definition Language

File

Client StubIDL Compiler

MessageDecoding Encodingmarshaling

RPC Runtime RetransmissionacknowledgmentsRoutingencryption

Return Call

Server StubMessage

Decoding Encoding

Receive SendRPC Runtime

(1) Intelligible messages?(2) Unauthorized client?

(3) Invalid procedure?(4) Invalid arguments?(5) Exception?

msg idtype(call)client idPRC idargs

type(reply)

replystatus

fesultsfailur

msg id

Page 6: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 6

Stateful/Stateless Servers Stateful servers:

Servers keep track of client information. RPC reply depends on that client information. Pros: Simplify client design Cons: A server crash loses client information. A client

crash leaves old client information at server. Stateless servers:

Clients must maintain Inter-call information. RPC reply is always the same. Most RPC implementations take this design.

Page 7: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 7

Server Creation Semantics Instance-per-Call Servers

A new server process launched every call Statelss servers only OS resource allocation/de-allocation involved every call

Instance-per-Session Servers A server process maintained for the entire session with a client Stateful servers: inter-call state maintained for a single client OS resource allocation/de-allocation involved every session

Persistent Servers A server process remains in existence indefinitely. Stateful and shared servers: concurrency control required OS resource allocation/de-allocation involved only once

Page 8: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 8

Parameter-Passing Semantics

Call by Value Most PRCs take this semantics. Voluminous data incurs copying overhead.

Call by Reference Passing pointers and references are mean

ingless. Then, how about object-based systems?

The value of a variable is a reference to an object

Call by object reference Additional remote object invocations Call by visit: all related objects moved to a

server every RPC. Call by move: all related objects moved an

d left to a server upon the first RPC.

class objA { objB b; objC c;}

class objB {}

class objC {}

Client Server

Page 9: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 9

Call Semantics RPC may result in failure and thus be called

multiple times. Last-One Call Semantics

Killing all orphan calls before restarting a new call

Last-of-Many Call Semantics Neglecting all orphan calls using

identifiers At-Least-Once Call Semantics

Accepting the first response (maybe an orphan response)

Exactly-Once Call Semantics The strongest semantics as learnt in

message passing

Node 1

Node 2

Node 3

What if Node 1 temporally crashedBefore a RPC response?

Orphancall

2ndcall

Page 10: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 10

Client-Server Binding (SunRPC)

(1) pmap_set(prognum, versnum, protocol, port) (2) and (3) pmap_getport(addr, prognum, versnum, protocol) To check the status of portmap, rpcinfo

Client Machine Server Machine

Transport level or belowTCP or UDP TCP or UDP

Client

PortmapDaemon

Server

Network (LAN/WAN)

write(), read(),Sendto(), recvfrom()

write(), read(),Sendto(), recvfrom()

(1) register

(2) Locating server

(3) Server port (xxx)

(4) RPC with port xxx

port: 111

port: xxx

Page 11: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 11

Performance Improvement in RPC Callback RPC

Clinet: specify a function that should be called back from the server. Keep working its computation without blocked on a RPC.

Server: Call back the client function as a response.

Multithreaded Client/Server Client:

Can avoid a block on RPC by having two threads. Hide network latency

Server: Keep accepting requests from clients Overlap server’s disk access with server computation

Page 12: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 12

SunRPC

example_clnt.c

example_client.c

example_h.c

example_xdr.c

example_svc.c

example_server.c

example_clnt.o

example_client.o

example_xdr.o

example_svc.o

example_server.o

example.x

example_client

example_server

Marshalling

Client stub

Server stub

Your server

Your clientrpcgen –a example.x

gcc –c -o

ld -oHeader

Modify by yourself

Modify by yourself

Interfacedescriptions

Client program

Server program

Page 13: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 13

Sun RPC (Interface definition)/* * example.x - Speicification of some arithmetic/string service. * Define 2 procedures: * fact( int n ) returns n!. * power( double x, double y ) returns x^^y. * strconc( struct strings ) concatenates src to dest. */

const BUFFER_SIZE = 1024;

struct doubles {double a;double b;

};

struct strings {char src[BUFFER_SIZE];char dst[BUFFER_SIZE];

};

program EXAMPLE_PROG { version EXAMPLE_VERS { int FACT( int ) = 1; /* procedure number = 1 */ double POWER( doubles ) = 2; /* procedure number = 2 */

string STRCONC( strings ) = 3; /* procedure number = 3 */ } = 1; /* version number = 1 */} = 0x31234567; /* program number = 0x31234567 */

Page 14: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 14

Sun RPC (Client)#include "example.h"void example_prog_1(char *host){

CLIENT *clnt;int *result_1;int fact_1_arg;double *result_2;doubles power_1_arg;char * *result_3;strings strconc_1_arg;clnt = clnt_create (host, EXAMPLE_PROG, EXAMPLE_VERS, "udp");if (clnt == NULL) {

clnt_pcreateerror (host);exit (1);

}fact_1_arg = 10;result_1 = fact_1(&fact_1_arg, clnt);if (result_1 == (int *) NULL) {

clnt_perror (clnt, "call failed");}printf( "fact( 10 ) = %d\n", *result_1 );

power_1_arg.a = 2.0;power_1_arg.b = 6.0;result_2 = power_1(&power_1_arg, clnt);if (result_2 == (double *) NULL) {

clnt_perror (clnt, "call failed");}printf( "power( 2.0, 6.0 ) = %f\n", *result_2 );strncpy( strconc_1_arg.dst, "xyz\0", BUFFER_SIZE );strncpy( strconc_1_arg.src, "abc\0", BUFFER_SIZE );result_3 = strconc_1(&strconc_1_arg, clnt);if (result_3 == (char **) NULL) {

clnt_perror (clnt, "call failed");}printf( "strconc( \"xyz\", \"abc\" ) = %s\n", *result_3 );clnt_destroy (clnt);

}

intmain (int argc, char *argv[]){

char *host;

if (argc < 2) { exit (1);host = argv[1];example_prog_1 (host);

exit (0);}

Page 15: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 15

Sun RPC (Server)#include "example.h"#include <math.h>#include <string.h>

int *fact_1_svc(int *argp, struct svc_req *rqstp){

static int result;

int i;result = 1;for ( i = *argp; i > 0; i-- ) result *= i;

return &result;}

double *power_1_svc(doubles *argp, struct svc_req *rqstp){

static double result;

result = pow( argp->a, argp->b );

return &result;}

char **strconc_1_svc(strings *argp, struct svc_req *rqstp){

static char * result;

result = strcat( argp->dst, argp->src );

return &result;}

Page 16: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 16

SunRPC v.s. Java RMI

SunRPC Java RMI

Advantages Template generationDynamic port assignmentHigh performance

Automatic serializationManual IP port handlingGood security

Disadvantages

Manual serializationWeak to hackers

Low performance

Page 17: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 17

Serialization Manual Operations in C++

class SubObject {public:

int id;SubObject( int I ) { id = I; }void print( ) { cout << “SubObject: id=“ << id << endl; }

};

class MainObject {public:

id id;SubObject *subObj;MainObject( int I, SubObject *s ) { id = I; subObj = j; }void print( ) { cout << “MainObject: id=“ << id << endl; }

};

Int main( int argc, char** argv ){

int f1 = create( “sample.dat”, 0744 );MainObject *obj1 = new MainObject( 1, new SubObject( 2 ) );write( f1, obj1, sizeof( *obj1 ) );write( f1, obj1->subObj, sizeof( *obj1->subObj ) ); //manually write data pointed to by obj1//manually write data pointed to by obj1close( f1 );int f2 = open( “sample.dat”, O_RDONLY );MainObject *obj2 = new MainObject( 0, new SubObject( 0 ) );read( f1, obj2, sizeof( MainObject ) );read( f2, obj2->subObj, sizeof( SubObject ) ); //manually read data pointed to by obj2//manually read data pointed to by obj2close( f2 );obj2->print( );obj2->subObj->print( );

}

MainObjMainObj

SuObjSuObj

Read/writeRead/write

Page 18: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 18

Serialization Automatic Operations in Java

public class SubObject implements Serializable {private int id;public SubObject( int I ) { id = I; }public void print( ) { System.out.println( “SubObject: id =“ + id ); }

}

public class MainObject implements Serializable {public int id;public SubObject subObj;public MainObject( int I, SubObject s ) { id = I; subObj = s; }public void print( ) { System.out.println( “MainObject: id =“ + id ); }

}

Public class Test {public static void main( String args[] ) throws IOException, ClassNotFoundException {

FileOutputStream f1 = new FileOutputStream( “sample.dat” );ObjectOutputStream o = new ObjectOutputStream( f1 );MainObject obj1 = new MainObject( 1, new SubObject( 2 ) );o.writeObject( obj1 ); //automatically write all objects traceable from obj1//automatically write all objects traceable from obj1o.flush( );o.close( );FileInputStream f2 = new FileInputStream( “sample.dat” );ObjectInputStream i = new ObjectInputStream( f2 );MainObject obj2 = (MainObject)I.readObject( ); // automatically read all object traceable from obj2// automatically read all object traceable from obj2I.close( );obj2.print( );obj2.subObj.print( );

}}

Page 19: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 19

Serialization Serializable or Not Serializable

Serializable Classes implementing the Serializable interface Primitive data types such as int and double and their

arrays Not Serializable

Image and Thread classes Static variables Variables quantified with Transient

Zero or null initialized upon a de-serialization

Page 20: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 20

RMIProgramming ProcedureProgramming Procedure

Application Layer:Application Layer: Client .javaClient .java Server.javaServer.java(implements remote interface)(implements remote interface)

Stub/Skeleton:Stub/Skeleton: StubStub Skeleton Skeleton Server_Stub.classServer_Stub.class Server_Skel.classServer_Skel.class

Remote Reference:Remote Reference: rmiregistry [port#]rmiregistry [port#](object manager/name service)(object manager/name service)

Transport Layer:Transport Layer: TCP/IPTCP/IP

request and resultrequest and result

(1) Define a remote interface(1) Define a remote interface

(2) Program a Server.java class (2) Program a Server.java class

(3) javac Server.java(3) javac Server.java rmic Serverrmic Server

(4) Program a Client.java class(4) Program a Client.java class(5) javac Client.java(5) javac Client.java

(6) Inovke a rmi registry(6) Inovke a rmi registry

(7) Run Server with java Server(7) Run Server with java Server(8) Run Client with java Client(8) Run Client with java Client

Page 21: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 21

RMIRemote Interface & Return ObjectRemote Interface & Return Object

// Remote interface: A server must implements this interface in its class define// Remote interface: A server must implements this interface in its class defineimport java.rmi.*

public interface ServerInterface extends Remote {public ReturnObj get( ) throws RemoteException; // A server returns a ReturnObj to a client// A server returns a ReturnObj to a client

}

// Define the class of a object returned from a server to a client (ReturnObj)// Define the class of a object returned from a server to a client (ReturnObj)import java.io.*import java.util.*

public class ReturnObj implements Serializable { // A return object must be serializable.// A return object must be serializable.private id;SubObject subObj;public ReturnObj( int i, SubObject s ) {

id = i;subObj = s;

}public void print( ) throws IOException { System.out.println( “ReturnObj: id=“ + id ); }

}

Page 22: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 22

RMIServer ImplementationServer Implementation

import java.io.*;import java.util.*;import java.rmi.*;import java.rmi.server.*;

Public class Server extends UnicastRemoteObject implements ServerInterface { static private int i = 0; public Server( ) throws RemoteException{ } public static void main( String args[] ) {

try { Server server = new Server( ); Naming.rebind( “server”, server );} catch ( Exception e ) { System.err.println( “Exception: “ + e ); System.exit( 1 );}

} public ReturnObject get( ) throws RemoteException {

ReturnObject f = new ReturnObject( i++, new SubObject( i ) );return f;

}}

(1) Implement a remote interface(1) Implement a remote interface (2) Inherit a RemoteObject(2) Inherit a RemoteObject

(3) Define a constructor(3) Define a constructor

(4) Instantiate a RemoteObject(4) Instantiate a RemoteObject

(5) Implement all methods declared in a remote interface(5) Implement all methods declared in a remote interface

Page 23: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 23

RMIClient ImplementationClient Implementation

Import java.rmi.*;Import java.io.*;

Public class Client { public static void main( String[] args ) {

try { ServerInterface si = (ServerInterface)Naming.lookup( “rmi://adonis1/server” ); ReturnObject f = si.get( ); f.print( ); f.subObj.print( );} catch ( RemoteException re ) { System.out.println( “Exception : “ + re ); System.exit( 1 );} catch ( IOException ie ) { System.out.println( “Exception : “ + ie ); System.exit( 1 );} catch ( NotBoundException nbe ) { System.out.println( “Exception : “ + nbe ); System.exit( 1 );}

}}

(1) RMI registry returns a reference to RemoteObject(1) RMI registry returns a reference to RemoteObject

(2) Call a method of this RemoteObject(2) Call a method of this RemoteObject

(3) Exception handling for RMI(3) Exception handling for RMI

(4) Exception handling for I/O(4) Exception handling for I/O

(5) Exception handling for Naming(5) Exception handling for Naming

Page 24: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 24

RMICompilation and ExecutionCompilation and Execution

% javac ReturnObject.java% javac ReturnObject.java // Compile a return class// Compile a return class% javac Server.java% javac Server.java // Compile a server class// Compile a server class% rmic Server% rmic Server // Create a stub and a skelton// Create a stub and a skelton% javac Client.java% javac Client.java // Compile a client class// Compile a client class% ls% lsReturnObject.java ReturnObject.class Server.javaReturnObject.java ReturnObject.class Server.javaServer.class Server_Stub.class Server_Skel.classServer.class Server_Stub.class Server_Skel.class% rmiregistry&% rmiregistry& // Invoke RMI registory// Invoke RMI registory% java Server&% java Server& // Invoke a server// Invoke a server% java Client% java Client // Invoke a client// Invoke a clientReturnObject id = 0ReturnObject id = 0SubObject id = 1SubObject id = 1% java Client% java Client // Inovke a client again// Inovke a client againReturnObject id = 2ReturnObject id = 2SubObject id = 3SubObject id = 3%%

Page 25: CSS490 RPC and RMI Textbook Ch4

Winter, 2004 CSS490 RPC 25

Exercises (No turn-in)1. The caller process of an RPC must wait for a reply from the callee process after

making a call. Explain how this can actually be done.2. Which types of server did you implement for the programming assignment 2, a s

tateful or a stateless server? Then, why did you implement such a type of server?

3. Discuss the similarities and differences among the following parameter passing:1. Call-by-object-reference2. Call-by-move3. Call-by-visit

4. What is an orphan call? How are orphan calls handled in the implementation of the following types of call semantics:

1. Last-one call semantics2. Last-of-many call semantics3. At-least-once call semantics

5. Discuss about the pros and the cons of dynamic binding in RPC.