open-courses
  • 公开课笔记
  • CMU 15-445/645 Database Systems
    • Relational Data Model
    • Advanced SQL
    • Database Storage
    • Buffer Pools
    • Hash Tables
    • Tree Indexes
    • Index Concurrency Control
    • Query Processing
    • Sorting&Aggregations
    • Join Algorithms
    • Query Optimization
    • Parallel Execution
    • Embedded Database Logic
    • Concurrency Control Theory
    • Two Phase Locking
    • Timestamp Ordering Concurrency Control
    • Multi-Version Concurrency Control
    • Logging Schemes
    • Database Recovery
    • Introduction to Distributed Databases
    • Distributed OLTP Databases
    • Distributed OLAP Databases
  • UCB - CS162
    • OS intro
    • Introduction to the Process
    • Processes, Fork, I/O, Files
    • I/O Continued, Sockets, Networking
    • Concurrency: Processes & Threads
    • Cooperating Threads, Synchronization
    • Semaphores, Condition Variables, Readers/Writers
    • Scheduling
    • Resource Contention & Deadlock
    • Address Translation, Caching
    • File System (18,19,20)
    • Distributed Systems, Networking, TCP/IP, RPC (21,22)
    • Distributed Storage, Key-Value Stores, Security (23)
    • Security & Cloud Computing (24)
    • Topic: Ensuring Data Reaches Disk
  • MIT - 6.006
    • Sequence and Set Interface
    • Data Structure for Dynamic Sequence Interface
    • Computation Complexity
    • Algorithms and Computation
    • Structure Of Computation
    • Graph & Search
    • Tree & Search
    • Weighted Shortest Paths
    • String Matching, Karp-Rabin
    • Priority Queue Interface & Implementation
    • Dictionary Problem & Implementation
    • Sorting
    • Dynamic Programming
    • Backtracking
    • Self-Balancing Tree
  • MIT - 6.824
    • 2PC & 3PC
    • Introduction and MapReduce
    • RPC and Threads
    • Primary/Backup Replication
    • Lab: Primary/Backup Key/Value Service
    • Google File System (GFS)
    • Raft
    • Lab: Raft - Leader Election
    • Lab: Raft - Log Replication
  • Stanford-CS107
    • 原始数据类型及相互转化
    • 指鹿为马
    • 泛型函数
    • 泛型栈
    • 运行时内存结构
    • 从 C 到汇编
    • 函数的活动记录
    • C 与 C++ 代码生成
    • 编译的预处理过程
    • 编译的链接过程
    • 函数的活动记录续、并发
    • 从顺序到并发和并行
    • 信号量与多线程 1
    • 信号量与多线程 2
    • 复杂多线程问题
    • 函数式编程 - Scheme 1
    • 函数式编程 - Scheme 2
    • 函数式编程 - Scheme 3
    • 函数式编程 - Scheme 4
    • 函数式编程 - Scheme 5
    • Python 基础
  • MIT - 6.001 - SICP
    • 什么是程序
    • 程序抽象
    • 替代模型
    • 时间/空间复杂度
    • 数据抽象
    • 高阶函数
    • Symbol
    • 数据驱动编程与防御式编程
    • 数据抽象中的效率与可读性
    • 数据修改
    • 环境模型
    • 面向对象-消息传递
    • 面向对象 - Scheme 实现
    • 构建 Scheme 解释器
    • Eval-Apply Loop
    • Normal Order (Lazy) Evaluation
    • 通用机
    • 寄存器机器
    • 子程序、栈与递归
    • 在寄存器机器中执行
    • 内存管理
  • MIT - 6.046
    • Randomized Algorithms
    • Skip Lists
  • System Design
    • Twitter
    • Cache Consistency & Coherence
  • DDIA 笔记
    • Replication
    • Transactions
    • The Trouble with Distributed Systems
    • Consistency & Consensus
  • Papers We Love
    • Consistent Hashing and Random Trees (1997)
    • Dynamic Hash Tables (1988)
    • LFU Implementation With O(1) Complexity (2010)
    • Time, Clocks, and the Ordering of Events in a Distributed System (1978)
    • Dapper, a Large-Scale Distributed Systems Tracing Infrastructure (2010)
    • Gorilla: A Fast, Scalable, In-Memory Time Series Database (2015)
  • Release It 笔记
    • Anti-patterns & Patterns in Microservice Architecture
  • Database Design
    • Log Structured Merge (LSM) Tree & Usages in KV Stores
    • Prometheus
Powered by GitBook
On this page
  • 线程
  • 程序中线程的数量
  • 线程的挑战
  • 应用举例:爬虫
  • RPC
  • RPC 架构
  • 执行细节
  • 问题与解决方案
  • 应用举例:KV
  • 参考
  1. MIT - 6.824

RPC and Threads

2015/2018 - Lecture 2

PreviousIntroduction and MapReduceNextPrimary/Backup Replication

Last updated 6 years ago

线程

线程的存在使得程序可以在逻辑上同时做许多事情。程序中的线程共享内存,同时也保存着自己的局部信息,包括程序指针 (PC),寄存器,栈等。

程序中线程的数量

  • 由程序的功能结构决定:如 Web 服务器

  • 由 CPU 的核数决定:最大化并行

  • 由 I/O 决定:最大化吞吐量

线程的挑战

  • 数据共享:竞争条件 (Race Condition)

  • 线程之间合作:

  • 并发的粒度:

    • 粗粒度:并发程度低,但容易处理

    • 细粒度:并发程度高,但容易产生更多的竞争和死锁

应用举例:

RPC

RPC 的目的在于:让远程调用看起来就像本地方法调用一样。但实践起来并不简单。

RPC 架构

执行细节

  • Marshalling: 将 RPC 调用的函数名、参数放到数据包 (packets) 中

  • Binding: 客户端绑定 RPC 服务端的过程

    • 使用服务器的域名和端口

    • 服务发现配置

问题与解决方案

RPC 调用过程可能出现哪些问题?

  • 丢包

  • 断网

  • RPC 服务器响应慢

  • RPC 服务器宕机

这些问题在客户端上的 RPC Lib 眼中的样子

  • 永远得不到请求的结果

  • 甚至不知道究竟是服务器的问题还是网络的问题

解决方案1:At Least Once/Best Effort

做法:在服务器未响应后重试几次,若仍如此则返回错误。 缺点:有写操作存在时可能出现竞争条件 结论:只能在读或幂等操作的情况下使用

思考实验:假设客户端执行

Put("k", 10);
Put("k", 20);
Get("k");

可能会出现哪些结果?

解决方案2:At Most Once

做法:RPC 客户端发送请求时为每个请求附加唯一的 xid, 服务端根据 xid 检测重复请求,检测成功后直接返回之前缓存的结果,伪代码如下所示:

if seen[xid]:
  r = old[xid]
else:
  r = handler()
  old[xid] = r
  seen[xid] = true

思考实验:

  • 如何保证 XID 的唯一性?

    • 大随机数

    • ip + sequence number

  • 服务端如何清理不必要的缓存?

    • TCP

  • 当前请求还在执行时,又接收到相同的请求?

    • pending flag

    • 等待或者忽略

  • RPC 服务器崩溃重启?

    • 持久化数据

例子:Go RPC

  • 建立 TCP 连接,调用请求通过 TCP 传输

  • Go RPC 不重发请求,因此服务器不会看到重复请求

  • Go RPC 得不到回复直接返回错误

解决方案3:Exactly Once

At Most Once + 无限重试 + 能够容错的 RPC 服务

参考

实现举例:

应用举例:

course note , , , , Youtube video

爬虫
Toy RPC
KV
2015
2018
toy RPC
crawler
kv
2015
图1 - RPC 架构