24
RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Embed Size (px)

DESCRIPTION

Remote Procedure Calls (RPC) Avoid explicit message exchange between processes Basic idea is to allow a process on a machine to call procedures on a remote machine –Make a remote procedure possibly look like a local one

Citation preview

Page 1: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

RPC Model, Stubs and Skeletons

Divya Nampalli14450 25

Page 2: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Assume that you are developing a client-server application: How to let the two processes (client and server)

located on two machines communicate with each other?

Socket programming: using functions like connect(sd, (struct sockaddr *)&sin, sizeof(sin)), write(sd, buf, strlen(buf)) etc.

Client-Server Communication

Page 3: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Remote Procedure Calls (RPC)

• Avoid explicit message exchange between processes• Basic idea is to allow a process on a machine to call

procedures on a remote machine– Make a remote procedure possibly look like a local one

Page 4: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

• How are parameters passed in a local procedure call– E.g., #include <sys/types.h>

#include <unistd.h> ... char buf[20]; size_t nbytes; ssize_t bytes_read; int fd; ... nbytes = sizeof(buf); bytes_read = read(fd, buf, nbytes); ...

Conventional Procedure Call

Page 5: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Conventional Procedure Call

Figure 4-5. (a) Parameter passing in a local procedure call: the stack before the call to read. (b) The stack while the called procedure is active.

Page 6: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Remote Procedure Calls (RPC)

• How are parameter passed in a remote procedure call, while making it look like a local procedure call?

Page 7: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Remote Procedure Calls (RPC)

Suited for Client-Server structure. Combines aspects of monitors and synchronous message

passing: ∗ Module (remote object) exports operations, invoked with

call. ∗ call blocks (delays caller) until serviced.call causes a new thread to be created on remote (server).Client-server synchronization and communication is implicit.

Page 8: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Terminology / Notation

Server module: operations, (shared) variables, localprocedures and threads for servicing remote procedurecalls.Interface (specification): describes the operations, parametertypes and return types.Server process: thread created by call to service an operation.Background process: threads running in a module that aren’tcreated in response to call.

Page 9: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Lookup and registration

How does the client find the server?

Often server registers (binds) with a naming service (registry). Client obtains information (lookup) about server from this server. This changes the question to: How does the client find the

registry?

Page 10: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Synchronization

Synchronization within a module (server).

Two approaches: 1. Assume mutual exclusion in server (only one server

process/background process executing at a time). ∗ Similar to monitors.

∗ Still need conditional synchronization. 2. Program it explicitly (i.e., using semaphores, monitors etc.)

Page 11: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Client and Server Stubs

Principle of RPC between a client and server program.

Page 12: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Steps of a Remote Procedure Call1. Client procedure calls client stub in normal way2. Client stub builds message, calls local OS3. Client's OS sends message to remote OS4. Remote OS gives message to server stub5. Server stub unpacks parameters, calls server6. Server does work, returns result to the stub7. Server stub packs it in message, calls local OS8. Server's OS sends message to client's OS9. Client's OS gives message to client stub10. Stub unpacks result, returns to client

Page 13: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Skeletons and Stubs

Page 14: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Skeletons and Stubs 1. ‘Stub’ objects implement the same interface as the

server objects. (Proxy pattern)2. • (0), (5) Client threads call a stub local to their JVM

instance.3. • (1), (4) Stub messages (TCP/IP) to Skeleton object

in remote JVM & waits for reply.4. • (2), (3) Skeleton creates a new server thread which

calls the server. 5. • Stub and skeleton classes are synthesized by a RMI

Compiler (rmic).

Page 15: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Passing Value Parameters (1)

Steps involved in doing remote computation through RPC

2-8

Page 16: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Passing reference parameters

– What is Call By Value and Call By Refernce?– Example: call foo(int, int * ) or read(fd, buf, nbytes)

– Call by copy/restore

– The dreaded “pointer problem”• Linked list• Complex graph

ab

a’b’

foo(a, &b ) Call foo(a, &b’ )

Copy value a and contents of loc b into a’ and loc b’

Return Copy contents of loc b’ into b

Machine A Machine B

Page 17: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

MarshallingValues must cross the networkMachine formats differ

– Integer byte order• Little-endian or big-endian

– Floating point format• IEEE 754 or not

Marshalling transferring data structure used in remote procedure call from one address space to another.

Page 18: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

RPC: The basic mechanism

Client routines

Client stub

RPC runtime

Networkroutines

Server routines

Server stub

RPC runtime

Network routines

Processkernel

Processkernel

Client process Server process 1. Client calls a local procedure on the client stub

2. The client stub acts as a proxy and marshalls the call and the args.

3. The client stub send this to the remote system (via TCP/UDP)

4. The server stub unmarshalls the call and args from the client

5. The server stub calls the actual procedure on the server

6. The server stub marshalls the reply and sends it back to the client

1

2

3

4

5

6

Page 19: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Example1: A Time Server Interfacestruct time {

int seconds;int minutes;int hours;int day;int month;int year;char timezone[4];

}int gettime(t); struct time *t;int settime(t); struct time *t;

Page 20: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Example1: Client Stub for Settimeint settime(t); struct time *t; {

char *p, message[32];int stat;

p = message;p = put_int(p, SETTIME);p = put_int(p, t->seconds);p = put_int(p, t->minutes);p = put_int(p, t->hours);p = put_int(p, t->day);p = put_int(p, t->month);p = put_int(p, t->year); p = put_string(p, t->timezone, 4);stat = do_operation(“time_server”, message, 32);if(stat == SUCCESS) get_int(message, &stat);return(stat);

}

Page 21: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Example1: Server Stub (1)void main_loop() {

char *p, message[32];int len, op_code;struct time t;

for(;;) {len = receive_request(message, 32);if(len < 4) {/* error handling code */}p = message;p = get_int(p, op_code);switch(op_code) {case SETTIME: if (len < 32) {/* error handling code */ }

p = get_int(p, &t.seconds);

p = get_int(p, &t.minutes);p = get_int(p, &t.hours);p = get_int(p, &t.day);p = get_int(p, &t.month);p = get_int(p, &t.year);p = get_string(p, &t.timezone, 4);len = settime(&t);put_int(message, len);len = 4; break;

case GETTIME:

/* code for unmarshalling and calling gettime */

}

send_reply(message, len);

}

}

Page 22: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

LPC v.s. RPC

• Global variables

• Client and server fail independently– RPC: requires code to deal with server crashes

Page 23: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

When Things Go Wrong

• Semantics of remote procedure calls– Local procedure call: exactly once

• How many times a remote procedure call may be called?

• A remote procedure call may be called:– 0 time: server crashed or server process died before

executing server code– 1 time: everything worked well– 1 or more: due to excess latency or lost reply from server,

client retransmitted• Exactly once may be difficult to achieve with RPC

Page 24: RPC Model, Stubs and Skeletons Divya Nampalli 14450 25

Thanks...