06 RPC
06 RPC
Lecture 6 – RPC
1
Building up to today
Key question:
working {
Done/Result
Writing it by hand...
struct foomsg {
u_int32_t len;
}
send_foo(char *contents) {
int msglen = sizeof(struct foomsg) + strlen(contents);
char buf = malloc(msglen);
struct foomsg *fm = (struct foomsg *)buf;
fm->len = htonl(strlen(contents));
memcpy(buf + sizeof(struct foomsg),
contents,
strlen(contents));
write(outsock, buf, msglen);
}
• RPC overview
• RPC challenges
6
RPC
// Synchronous call
args := &server.Args{7,8}
var reply int
err = client.Call("Arith.Multiply", args, &reply)
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Arith: %d*%d=%d", args.A, args.B, reply)
8
RPC Goals
• Ease of programming
• Hide complexity
• Automates task of implementing distributed
computation
• Familiar model for programmers (just make a
function call)
Historical note: Seems obvious in retrospect, but RPC was only invented in the ‘80s.
See Birrell & Nelson, “Implementing Remote Procedure Call” ... or
Bruce Nelson, Ph.D. Thesis, Carnegie Mellon University: Remote Procedure Call.,
1981 :)
Remote procedure call
10
But it’s not always simple
12
Marshaling and Unmarshaling
Continued …
15
Remote Procedure Calls (2)
6. The server does the work and returns the result to the
stub.
7. The server stub packs it in a message and calls its
local OS.
8. The server’s OS sends the message to the client’s
OS.
9. The client’s OS gives the message to the client stub.
10.The stub unpacks the result and returns to the client.
16
Passing Value Parameters (1)
19
Today's Lecture
• RPC overview
• RPC challenges
20
RPC vs. LPC
21
RPC failures
• In local computing:
• if machine fails, application fails
• In distributed computing:
• if a machine fails, part of application fails
• one cannot tell the difference between a machine failure
and network failure
• How to make partial failures transparent to client?
23
Strawman solution
24
Real solution: break transparency
25
Real solution: break transparency
30
Important Lessons
• Procedure calls
• Simple way to pass control and data
• Elegant transparent way to distribute application
• Not only way…
31
Today's Lecture
• RPC overview
• RPC challenges
32
Asynchronous RPC (1)
33
Asynchronous RPC (2)
34
Asynchronous RPC (3)
// Asynchronous call
quotient := new(Quotient)
divCall := client.Go("Arith.Divide", args, quotient, nil)
replyCall := <-divCall.Done // will be equal to divCall
// check errors, print, etc.
36
Using RPC
37
Using RPC
• Worker-->Server.
• Synch RPC, but no return value.
• "I'm a worker and I'm listening for you on host XXX, port
YYY."
• Server-->Worker.
• Synch RPC? No that would be a bad idea. Better be
Asynch.
• Otherwise, it would have to block while worker does its work,
which misses the whole point of having many workers.
38
Binding a Client to a Server
39
Example Marshaling Format: JSON
40
Example Marshaling Format: JSON
• Empty buffer
{
"Head": null
}
41
Example Marshaling Format: JSON
• Deep integration.
• Data formatting done based on type declarations
• (Almost) all public methods of object are registered.
• Go is the latter.
43
Other RPC systems