type
status
date
slug
summary
tags
category
icon
password
原文
Remote Procedure Call(RPC)
Marking Remote Look Local
远程调用的核心理念是假设分布式系统不存在,就像本地调用一样。隐藏远程调用的细节
Hiding the Network
Dealing with the network is complex 处理复杂的网络
构建分布式系统非常困难,困难的来源之一在于系统中有许多动态变化的组成部分。假设两个服务之间,地理跨度大,中间隔着数百个网络路由器,它们负责将你的数据包从这里传输到那里。有些是无线网或光纤,所有环节必须完美工作,你的分布式系统交互才能真正实现。隐藏复杂性的能力是远程调用被创造出来的主要原因。
关于 RPC 相关内容,可以参考下凤凰架构这本书。其中描述了 RPC 的发展历程和 RPC 的相关概念。
Client-Server Model
Many systems split into client-server pairs
- server exports an interface, client invokes it
- server acts like an abstract data type
系统分为 client、Server 端。
- 服务端提供接口,client 端负责调用
- 服务端视为一种实现抽象数据类型的实体。
Client、Server 都是针对接口而言。Server A 被 Server B 所使用,Client B 也可以向 client C 提供服务。 Client B 也扮演了 Server角色。
Interfaces
an interface consists of a set of related operations 接口由一系列相关操作组成。
- wide range of possible interfaces 多种可能的接口
- any function prototype is potentially a candidate
Chubby lock Manager 锁管理器
Historical Roots of RPC
RPC 的历史渊源
关于 RPC 的发展历程,可以阅读凤凰架构中的 RPC 章节。
Apparent opportunity
- potential parallelism in a distributed system 分布式系统潜在的并行性
- client and server on different machines
- no reason for client to block
主要的工作消除并行性,将原本并行的事物强行引入同步机制。
放弃隐式并行化,显示地实现并行化。
Early distributed systems used message passing
- A sends invocation message to B
- A does other work
- A polls for B’s reply or is interrupted
- A receives B’s reply
But experience showed 但是经验表明
- often little for A to do while awaiting B’s Reply 在等待B的答复时通常几乎没有什么可做的
- explicit matching of replies complicates code 回复的显式匹配使代码复杂化
- parallelism better exploited via threads 通过线程更好地利用并行性
Lesson
- message passing offers greater generality 消息传递提供了更大的通用性
- but that generality not useful in common cases 但这种通用性在一般情况下没有用
RPC is a paradigm born of these experiences RPC 是从这些经验中诞生的范例
- well suited for the client server model 非常适合client server模型
- sacrifices some generality 牺牲一些通用性
- can be made very efficient 可以变得非常高效
- simple to use
- emulates familiar procedure call paradigm 模拟熟悉的过程调用范例
Characteristics of RPC
RPC 的特点
Two aspects
- control flow 控制流
- invocation syntax 调用语法
Control flow
- caller makes request and blocks 调用者发起请求并阻塞
- callee services request and replies 被调用方请求和回复
- caller resumers 调用者回复
There is thus synchronous transfer of controller
在进行远程调用中即调用者的状态在调用期间(服务器执行时)被冻结,然后控制权返回,之后本地状态可能会再次发生变化。这是理想状况,实际上,要保证本地状态不发生变化是非常非常困难的,而分布式系统中最棘手的错误,往往就发生在这一假设被违背的时候。在你不知情的情况下,一部分本地状态悄然发生改变,而你对此毫无察觉。因此,当回复返回时,实际情况已与预期不符合。
迅速和零延迟是两个不同的概念。本地环境中延迟虽非零,但其值已及其接近零。
远程调用需要时间,如果调用一次消耗 60ms 的时间,在这 60毫秒内,其他进程或线程都可能会改变本地状态。因此在分布式系统中,要考虑延迟或传输时间。
Invocation syntax
- Local syntax : y = foo( x1, x2, ……)
- Remote syntax : y = foo(
cid
, x1, x2,……)
- cid is called connection handle
调用语法通常由编程语言决定过程调用的形式。


Limitations of RPC
RPC 的局限性
Client and Server do not share address space
- pointers don’t mean the same thing at the two ends
embedded pointers in parameters useless
- many implications
can’t share global data
can’t use call-by-reference
only call-by-value-result
large data structures can be expensive
procedure parameters don’t work easily
up-level addressing doesn’t work easily
标注下 VMware FT
Delayed binding in RPC
- conceptually similar to dynamic linking 概念上类似于动态链接
- hence runtime linking errors possible
- multiple instances of callee
many servers may export same of callee
client may wish to talk to more than one server 客户端可能希望与多个服务器通信
no counterpart in local procedure call 本地过程调用中没有对应项
编译时检查相较于运行时检查更重要、更具有价值,更强类型的语言越容易早期发现错误,越早期发现的错误越被容易解决。
在分布式系统中,有些错误无法被更早地发现。原因在于,根据其定义,服务器只能在运行时进行通信。
延迟绑定是复杂性的一个来源。
Failure independence of clients and Services
客户端和服务的故障独立性。
- in local case ,client and server live or die together
- in remote case ,client see new failure modes
- network failure 网络故障
- server machine crash (服务 机器崩溃)
- server process crash (服务 进程崩溃)
- nontransitive communication
- A can talk to B , and B to C, but not c to A
- failure handling code has to be more thorough(and more complex)
在本地场景中,故障的本质是命运共享。分布式环境下则不同,例如服务 A 宕机了,但是 B 服务器依然运行如常。
所有的复杂来自于故障独立性。
- Author:newrain-zh
- URL:https://alex.sh.cn/article/cmu-15-440/rpc-1
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!