CS6223: Distributed Systems Remote Procedure Call (RPC)

Preview:

Citation preview

CS6223: Distributed Systems

Remote Procedure Call (RPC)

2

An Example of Local Procedure Call

/* save a string to file msg.dat */savemsg(char *msg) { FILE *fp; fp = fopen("msg.dat", "w+"); fprintf(fp, "%s\n", msg); fclose (fp);}

main() { char *str = "This is an example of LPC."; savemsg(str);}

3

Moving the local procedure to a remote machine

main() { char *str = "This is an example of RPC."; char *remote_host = "sus1.cs.cityu.edu.hk"; CLIENT *clnt; clnt = clnt_create(remote_host, MSGPROG, MSGVER, "netpath"); if (clnt == (CLIENT *) NULL) { clnt_pcreateerror(host); exit(1); } savemsg_1(&str, clnt);}

…………savemsg_1(char **argp; struct svc_req *rqstp){ FILE *fp; fp = fopen("msg.dat", "w+"); fprintf(fp, "%s\n", *argp); fclose (fp);}

server:

client:

4

Remote Procedure Call

1984: Birrell & Nelson– Mechanism to call procedures on other machines

– Process on machine A can call a procedure on machine B• A is suspended

• Execution continues on B

• When B returns, control passed back to A

Goal: Make a remote procedure call looking the same as a local call to programmers.

5

RPC implementation

The RPC compiler auto-generates stub/skeleton routines to make an RPC to the user as if it is a local call. What stub routines do:

• marshalling / unmarshalling parameters and return values

• handling different data formats between different machines

• detecting and handling failures of client/server processes and the networks

serviceroutines

serverskeleton

clientstub

clientprogram

message passing

client server

6

Compilation in SUN RPC

server skeleton (main)

service routines

data conversion

client program (main)

client stub

RPCcompiler

interfacedefinition

Ccompiler

serverexecutable

Ccompiler

clientexecutable

7

Demo of RPC

Interface file msg.x:program MSGPROG { version MSGVER{ int SAVEMSG(string)= 1; string READMSG(int)= 2; } = 2;} = 345678;

Generate stub routines by: rpcgen –a msg.x

Compile program by: cc –o object xxx.c

8

Steps in a RPC

client program (msg_client.c)

service routines

client stub (msg_clnt.c

)

server skeleton

network routines network routines

1. Client calls stub (push parameters onto stack)

9

Steps in a RPC

client program service routines

client stub (msg_clnt.c)

network routines network routines

2. Clnt_stub marshals parameters to message &makes an OS call (client blocked)

server skeleton

10

Steps in a RPC

client program

client stub

network routines network routines

3. Network message sent to server

server skeleton

service routines

11

Steps in a RPC

client program

client stub

network routines network routines

4. Deliver message to server stub & unblock server

server skeleton (msg_svr.c)

service routines

12

Steps in a RPC

client program

client stub

network routines network routines

5. Svr-stub unmarshals parameters & calls service routine (local call)

server skeleton (msg_svr.c)

service routines (msg_server.c)

13

Steps in a RPC

client program

client stub

network routines network routines

6. Return to the stub from service routine

server skeleton

service routines

14

Steps in a RPC

client program

client stub

network routines network routines

7. Svr_stub marshals return-value &requests OS to send out message

server skeleton

service routines

15

Steps in a RPC

client program

client stub

network routines network routines

8. Transfer message over network

server skeleton

service routines

16

Steps in a RPC

client program

client stub

network routines network routines

9. Deliver message to client (unblock client)

server skeleton

service routines

17

Steps in a RPC

client program

client stub

network routines network routines

10. Clnt_stub unmarshals return-value & returns to client program…

server skeleton

service routines

18

Compilation in SUN RPC

server skeleton (main)

service routines

data conversion

client program (main)

client stub

RPCcompiler

interfacedefinition

Ccompiler

serverexecutable

Ccompiler

clientexecutable

19

Writing the programs

Programmers need to write two pieces of programs:

• Client program– Specify server’s location

– Parameters and return value of RPC are pointers

• Service routines– Generally the same as local procedures, except the parameters and

parameter types

20

Interface definition (1)

• Define machine/OS/language-independent data types and data structures

• Specify interfaces: program # and version #

• Define remote procedures: procedure #, parameters, return type

constant and type definitions /*optional */

program IDENTIFIER {version VERSION_ID {

procedure list} = value;…

} = value;

program MSGPROG { version MSGVER{ int SAVEMSG(string)= 1;

string READMSG(int)= 2; } = 3;} = 345678;

21

Interface definition (2)

• one program block in an interface definition

• one or more sets of versions in the program block

• one or more sets of procedures in each version block

• one argument in each procedure by default– If more than one parameter is required, use a struct to group them

– “–N” option in “rpcgen” allows multiple arguments

22

Incompatible data representation (1)

• Different byte ordering– Big endian: Most significant byte in low memory (e.g. Sun Sparcs)

– Little endian: Most significant byte in high memory (e.g. Pentiums)

The problem was solved in the IP protocol suite by forcing everyone to use big endian byte ordering for all 16 and 32 bit fields by htons (host-to-network-short) and htonl functions.

main() { unsigned int n; char *a = (char *) &n;

n = 0x11223344; printf("%02x, %02x, %02x, %02x\n", a[0], a[1], a[2], a[3]);}

Output on a Sparc:11, 22, 33, 44

Output on a Pentium:44, 33, 22, 11

23

Incompatible data representation (2)

• Different sizes of integers and other types

• Different floating point representations

• Different character sets

• Alignment requirements

Need standard data representation and associated encoding /decoding functions to enable communication between heterogeneous systems– e.g. Sun’s RPC uses XDR (eXternal Data Representation)

24

Interface Definition Language (IDL)

SUN XDR (eXternal Data Representation)

• A subset of ASN.1 (ITU), a standard data representation for message exchanges between clients and servers.

• Data types and structs are translated into C language by rpcgen (see XXX.h, generated by rpcgen).

• A set of encode/decode routines (see XXX_xdr.c) to convert between XDR and the local representations.

25

Student registration system: regist.x

const MAXSUBJ = 64;typedef char id[8];typedef char code[8];

struct registargs { code subj_code; id stud_id;};

struct status { code subj_code; int regist_stat;};

struct regist_status { int total_regist; struct status subjs_status<MAXSUBJ>;};

program REGISTPROG { version REGISTVER { int REGIST(registargs) = 1; int DEREGIST(registargs) = 2; regist_status VIEW_STATUS(id) = 3; } = 1;} = 100023;

26

Important data types

simple data types: similar to C

array fixed-length: type-name identifier[n];variable-length: type-name identifier<n>;

type-name identifier<>;

array of stringsstring identifier<n>; string identifier<>;

different from array of char in representation (explained later)

27

Important data types

constant/enumeration/type defintion/struct: similar to C

const MAXSIZE = 512;

enum state { BUSY=1, IDLE=2, TRANSIT=3 };

typedef long counter;

typedef char code[8];

struct status {

code subj_code;

int regist_stat;

};

28

Data representation/alignment (1)

• The basic data item size is 4 bytes. All variables or data structures should be aligned in 4-bytes.

• Variable-length objects, e.g., variable-length arrays, structures, and strings, have a length in front of them.

29

Data representation/alignment (2)

• simple data types – int, unsigned int, char, bool, float, etc.: 4 bytes

– double, etc.: 8 bytes

• array– sequence of representations of individual elements

– variable-length array has a 4-byte length in the front

• string– an integer of string length, followed by a sequence of chars, one for a byte

– a residue of (n mod 4) bytes are stuffed to make the total byte count a multiple of 4, e.g. string “exam-paper_01” is represented as:

e x a m - p a p e r _ 0 1 \ 0\ 0 \ 013

4 bytes 4 bytes4 bytes4 bytes4 bytes

Recommended