MIT 6.5840:Lecture 18 Ray
本篇逐行译注 MIT 6.5840 Spring 2026 Lecture 18,覆盖 Ray 的 futures、actors、ownership、distributed scheduler、object store、lineage reconstruction、fate sharing 与 failure recovery。
MIT 6.5840:Lecture 18 Ray
TL;DR
- Ray 解决的问题:为 model serving、online video processing 和 emerging AI applications 提供低延迟、可组合的 distributed tasks runtime。
- 三项核心抽象:futures 表示异步计算结果,actors 在多次 invocation 之间持有 state,ownership 按 future 的 owner 分片管理 metadata。
- 为什么不是 centralized coordinator:集中式 coordinator 会在大量短任务和大量 futures 下成为 scalability bottleneck;Ray 通过 ownership 把管理责任分散到调用者。
- 恢复机制:对 owned object 丢失时沿 lineage reconstruction 重新执行;owner 丢失导致 dangling reference 时,用 fate sharing 让持有引用的 worker 一同终止并触发上层重提交。
- 关键取舍:future 的 value 可以存储在执行 task 的 worker 上,metadata 则由 owner 管理;这种分离减少通信,但要求精确处理引用、GC、调度和故障传播。
名词解释
| 名词 | 含义 | 本讲对应物 |
|---|---|---|
| Future | 异步计算结果的 handle/reference | g.remote(10) 返回的 object ref |
| Actor | 在多次 invocation 之间保留 state 的远程对象 | Counter actor |
| Ownership | 由调用者负责管理 returned future 的 metadata | owner table 按 worker 分片 |
| Borrowing | 把 future reference 作为参数传给其他 task | shared(x) |
| Lineage reconstruction | 依据 task lineage 重建丢失的 future/object | 重新执行 task 与 descendants |
| Fate sharing | owner 失败时让借用者一同终止 | 消除 dangling reference |
| Object store | 存放大对象 value 的存储层 | worker 直接 fetch immutable object |
| Resource lease | scheduler 暂时保留 worker 资源的 lease | reserve 后取得 lease(id) |
| Distributed scheduler | 跨 worker 选择资源并调度 task 的机制 | figure 8 |
| Ray | 面向分布式 task/actor applications 的 runtime/framework | futures + actors + ownership |
目录
| 章节 | 内容 |
|---|---|
| 版本与来源 | 固定 Lecture 18 源文件 |
| 原文逐行翻译 | 按 l-ray.txt 顺序译注 |
| 机制小结 | future、ownership、scheduler、GC 与 recovery |
| 本篇验收 | 源行、图表占位和代码记录 |
版本与来源
- 课程版本:MIT 6.5840 Spring 2026。
- Lecture 源文件:l-ray.txt,223 行,非空源行 187 行,SHA-256:024a34c98686cb20f9b67bd59f0c145980f02f984ed8eb7a9de63c3ed9c9d419。
- 配套论文:Ownership: A Distributed Future System For Fine-Grained Tasks,Stephanie Wang et al.,NSDI 2021。
- 课程日程:MIT 6.5840 Spring 2026 Schedule。
- Ray 文档:Ray Core tips for first-time users。
- 翻译边界:英文源行按原顺序保留,随后给出中文译文;future、actor、ownership、borrowing、lineage reconstruction、fate sharing、object store、object ref、worker、driver、task、GC 和 Ray 等术语原样保留。
- 图片结论:源文本没有独立图片文件;Figure 2、Figure 5、Figure 6、Figure 8、Figure 9 和 ASCII table/diagram 占位按原文位置保留。
原文逐行翻译
课程主题与阅读动机
6.5840 2026 Lecture 18: Ray
6.5840 2026 年 Lecture 18:Ray。
Ownership: A Distributed Future System For Fine-Grained Tasks by
《Ownership: A Distributed Future System For Fine-Grained Tasks》,作者是
Stephanie Wang et al., NSDI 2021
Stephanie Wang 等人,发表于 NSDI 2021。
Why are we reading this paper?
为什么要阅读这篇论文?
A modern version of MapReduce, Spark, etc.
这是 MapReduce、Spark 等系统的 modern version。
Moving large data efficiently with futures
通过 futures 高效地移动 large data。
Quite different from RPCs in lab
这与 lab 中的 RPCs 很不同。
Managing distributed futures efficiently using ownership
通过 ownership 高效管理 distributed futures。
Widely-used open-source project (e.g., by OpenAI)
广泛使用的 open-source project(例如 OpenAI)。
anyscale
Anyscale。
Parallel applications 的演进
Evolving parallel applications
Parallel applications 的演进。
Combine functional and stateful
结合 functional 和 stateful 两种方式。
Low latency requirements
Low latency requirements。
Examples:
Examples:
Model serving (3a)
Model serving(3a)。
quick response
quick response。
large data (client images)
large data(client images)。
router and model replicas maintain state between invocations
router 和 model replicas 在 invocations 之间维护 state。
On-line video processing (video stabilizing in 3b)
On-line video processing(3b 中的视频 stabilizing)。
compute trajectory of an object
计算一个 object 的 trajectory。
start processing frame before seeing next frame
在看到 next frame 之前就开始处理当前 frame。
actor for storing the decoded frame
用于存储 decoded frame 的 actor。
MapReduce and Spark aren't good at these
MapReduce 和 Spark 不擅长这些场景。
三个核心 ideas
Three ideas:
三个 ideas:
futures (handle for the result of a computation)
futures(某次 computation 结果的 handle)。
actors (to hold state between invocations)
actors(在 invocations 之间持有 state)。
shard state about futures based on ownership
基于 ownership 对 futures 的 state 做 sharding。
Futures API
Futures (API table 1)
Futures(API table 1)。
program can invoke a function asynchronously, which return a future
program 可以异步 invoke 一个 function,function 会返回一个 future。
program can pass the reference to a future as an argument to other functions
program 可以把 future 的 reference 作为 argument 传给其他 functions。
programs can force a future to evaluate
programs 可以 force 一个 future evaluate。
benefit: system can decide where to run future and when data is moved
benefit:system 可以决定在哪里运行 future,以及何时移动 data。
Borrowing 示例:Figure 2
Example of borrowing: fig 2
Borrowing 示例:fig 2。
f1 = C()
f1 = C()。
f2 = C()
f2 = C()。
f3 = Add(shared(f1), shared(f2)) # pass f1+f2 by reference
f3 = Add(shared(f1), shared(f2)) # 通过 reference 传递 f1+f2。
c = get(f3)
c = get(f3)。
Borrowing 示例:Figure 6
Example of borrowing: fig 6
Borrowing 示例:fig 6。
def A():
def A():
x = B()
x = B()。
y = C(shared(x)) # pass x by reference
y = C(shared(x)) # 通过 reference 传递 x。
f1 = A()
f1 = A()。
Ray 的 remote execution 与 futures
Ray example of remote execution with futures:
Ray 使用 futures 进行 remote execution 的示例:
see https://docs.ray.io/en/latest/ray-core/tips-for-first-time.html
# 参见 https://docs.ray.io/en/latest/ray-core/tips-for-first-time.html。
import ray
import ray。
ray.init()
ray.init()。
@ray.remote
@ray.remote。
def g(i):
def g(i):
return i
return i。
f = g.remote(10) # f is a future and ray invokes g asynchronously
f = g.remote(10) # f 是一个 future,Ray 异步 invoke g。
// the 4 g invocations run in parallel
// 这 4 个 g invocations 并行运行。
future_ids = [g.remote(i) for i in range(4)]
future_ids = [g.remote(i) for i in range(4)]。
results = ray.get(future_ids)
results = ray.get(future_ids)。
Ray 的 actors 与 object refs
Ray example of actors and object refs
Ray 使用 actors 和 object refs 的示例。
@ray.remote
@ray.remote。
class Counter:
class Counter:
def __init__(self):
def init(self):
self.value = 0
self.value = 0。
def increment(self):
def increment(self):
self.value += 1
self.value += 1。
return self.value
return self.value。
counter = Counter.remote() # Instantiate as a remote actor
counter = Counter.remote() # Instantiate as a remote actor。
f = counter.increment.remote() # future corresponding to an object ref
f = counter.increment.remote() # 与 object ref 对应的 future。
print(ray.get(f))
print(ray.get(f))。
Implementation challenges
Implementation challenges:
Implementation challenges:
keep track of futures and objects
追踪 futures 和 objects。
incl. garbage collection of future and objects
包括 future 和 objects 的 garbage collection。
worker machine crashes while running a future
worker machine 在运行 future 时 crash。
Ray: transparent recovery of idempotent futures (fig 5)
Ray:透明恢复 idempotent futures(fig 5)。
简单的 centralized coordinator 假设实现
Sketch of simple/hypothetical implementation: centralized coordinator
简单/假设性实现的 sketch:centralized coordinator。
Coordinator maintains a table (id, task, refs, worker, value)
Coordinator 维护一个 table(id、task、refs、worker、value)。
see example below
见下面的 example。
Invoking future task:
Invoke future task:
scheduler picks a worker
scheduler 选择一个 worker。
adds future to table
把 future 添加到 table。
Get() contacts coordinator
Get() 联系 coordinator。
If value present, return value
如果 value 存在,则返回 value。
If not present, block at coordinator until worker fills in value
如果 value 不存在,则在 coordinator 处 block,直到 worker 填入 value。
If value is large, store it in object store and make the value the object id
如果 value 很大,则把它存入 object store,并让 value 变为 object id。
caller fetches object directly from object store
caller 直接从 object store fetch object。
objects are immutable
objects 是 immutable 的。
Task can garbage collect its futures and objects
Task 可以 garbage collect 它的 futures 和 objects。
If a worker crashes, driver/task restarts invoked tasks
如果 worker crash,driver/task 会 restart 已 invoke 的 tasks。
"lineage reconstruction"
“lineage reconstruction”。
re-execute tasks and descendants
re-execute tasks 和 descendants。
which recreate futures, and further descendants
它们会 recreate futures 以及更下游的 descendants。
use second copies of objects to avoid recomputation
使用 objects 的 second copies 来避免 recomputation。
centralized coordinator 下的状态示例
Example: state when both B and C are running (fig 6b)
示例:B 和 C 都在运行时的 state(fig 6b)。
ID | Task | Refs | Locations
ID | Task | Refs | Locations。
----------------------------------
----------------------------------。
x B() W1,W3 W2
x B() W1,W3 W2。
y C(X) W1 W3
y C(X) W1 W3。
where worker 1 is running A, 2 running B, and 3 running C
其中 worker 1 正在运行 A,worker 2 正在运行 B,worker 3 正在运行 C。
refs for reference counting
用于 reference counting 的 refs。
if C returns, remove 3 from Refs for x
如果 C returns,从 x 的 Refs 中移除 3。
but cannot GC x yet, since A is using it
但还不能 GC x,因为 A 正在使用它。
if A returns, no refs to x and y
如果 A returns,x 和 y 都没有 refs。
safe to delete x and y
可以安全地 delete x 和 y。
recovery though lineage reconstruction
通过 lineage reconstruction 进行 recovery。
ex: re-executing A, re-executes B and C too
例如:re-executing A 也会 re-execute B 和 C。
centralized plan 的 scalability problem
Implementation challenge with centralized plan
centralized plan 的 Implementation challenge。
one round-trip for invocation and get
每次 invocation 和 get 都需要一次 round-trip。
many futures
many futures。
some run for a short time (a few ms)
有些只运行很短时间(a few ms)。
coordinator is scalability bottleneck
coordinator 成为 scalability bottleneck。
按 table 分片
Alternative: shard the table (e.g., by obj id)
Alternative:对 table 做 sharding(例如按 obj id)。
still one round-trip with coordinator
仍然需要与 coordinator 进行一次 round-trip。
GC maybe involve multiple shards
GC 可能涉及多个 shards。
Ray 的 ownership solution
Ray solution: shard by ownership
Ray 的 solution:按 ownership 做 sharding。
Example
Example。
A owns "x"
A owns “x”。
C "borrows" x
C “borrows” x。
it may pass it further
它可以继续向下传递 x。
does it reference counting
它负责 reference counting。
The caller of function is the owner of a returned future
function 的 caller 是 returned future 的 owner。
But value of object is stored at the worker that runs the task
但 object 的 value 存储在运行该 task 的 worker 上。
Ex. x:
例如 x:
A is owner of x,
A 是 x 的 owner,
value of x is store at the worker that runs B (2)
x 的 value 存储在运行 B 的 worker(2)上。
advantage
advantage。
A can invoke C without any communication with B
A 可以 invoke C,而无需与 B 通信。
ownership 下的状态示例
Example: state when both B and C are running
示例:B 和 C 都在运行时的 state。
Owner table at 1:
worker 1 上的 Owner table:
ID Task Owner Ref Loc Val
ID Task Owner Ref Loc Val。
x B() W1 W1,W3 W2
x B() W1 W1,W3 W2。
y C(x) W1 W1 W3
y C(x) W1 W1 W3。
Owner table at 2:
worker 2 上的 Owner table:
ID Owner Ref Loc Val
ID Owner Ref Loc Val。
x W1 W2
x W1 W2。
Owner table at 3:
worker 3 上的 Owner table:
ID Owner Ref Loc Val
ID Owner Ref Loc Val。
x W1 W2
x W1 W2。
y W1 W3
y W1 W3。
Application structure 与 sharding
structure of application guides sharding
application 的 structure 指导 sharding。
if one tasks invokes many futures, split task up into several
如果一个 task invokes many futures,就把该 task 拆成多个 task。
each subtask being in charge of some of the futures
每个 subtask 负责其中一部分 futures。
Ownership implementation
Ownership implementation
Ownership implementation。
owner = (IP, port, workerid)
owner = (IP, port, workerid)。
taskId = parentTaskId + task_index
taskId = parentTaskId + task_index。
objectId = taskId + obj_index
objectId = taskId + obj_index。
Distributed scheduler
distributed scheduler (figure 8)
distributed scheduler(figure 8)。
schedule(t)
schedule(t)。
id = local worker
id = local worker。
while true:
while true:
ok, id = reserve @id using resource request
ok, id = reserve @id using resource request。
if ok:
if ok:
l = lease(id)
l = lease(id)。
break
break。
table[t].Loc = l
table[t].Loc = l。
optimization: reuse leased worker
optimization:reuse leased worker。
Memory management
Memory management (figure 9)
Memory management(figure 9)。
API obj store: Create, Get, Pin, Release
API obj store:Create、Get、Pin、Release。
Get blocks until obj is created
Get 会 block,直到 obj 被 created。
Initial Create pins obj
Initial Create 会 pin obj。
tab[oid].Location = locations in object store
tab[oid].Location = object store 中的 locations。
may have secondary copies
可能有 secondary copies。
C's worker has a copy of x after fetching it from B's worker
C 的 worker 从 B 的 worker fetch x 后,拥有 x 的一个 copy。
if x is large
如果 x 很大。
Failure recovery:fate sharing
Failure recovery: fate sharing
Failure recovery:fate sharing。
check locations in table
检查 table 中的 locations。
loss of an owned object
owned object 丢失。
re-execute tasks following lineage
沿着 lineage re-execute tasks。
use secondary copy to avoid recomputing
使用 secondary copy 来避免 recomputing。
loss of an owner: risk of dangling reference
owner 丢失:存在 dangling reference 的风险。
fate sharing
fate sharing。
example: if 2 crashes, 3 may have a dangling ref
例如:如果 2 crash,3 可能拥有 dangling ref。
3 fails and 1 fails
3 fail,1 也 fail。
Failure recovery 示例
Example:
Example:
if worker 3 fails (after starting C but before finishing)
如果 worker 3 失败(已经启动 C 但尚未完成),
worker 1 (A) will learn about it
worker 1(A)会得知此事。
it is the owner of y (and has the Task info) and asks Ray to re-execute C(X)
它是 y 的 owner(并且拥有 Task info),会要求 Ray re-execute C(X)。
if worker 1 and 2 fail, worker 3 has a dangling ref to x
如果 worker 1 和 2 fail,worker 3 对 x 拥有 dangling ref。
it will never be resolved to a value
它永远不会被 resolve 为一个 value。
worker 3 "shares fate" with the owner of "x"
worker 3 与 “x” 的 owner “shares fate”。
i.e., it terminates itself, pretending a crash
也就是说,它终止自身,假装发生了 crash。
the caller of A will re-submit A
A 的 caller 会 re-submit A。
Homework
Homework: if C() in figure 6(a) is as follows:
Homework:如果 figure 6(a) 中的 C() 如下:
def C(x):
def C(x):
z = D(X)
z = D(X)。
return get(z) # return value of future z
return get(z) # 返回 future z 的 value。
Suppose the worker than runs D fails before finishing, which worker would
假设运行 D 的 worker 在完成前失败,哪个 worker 会
initiates the re-execution of D()?
发起 D() 的 re-execution?
who owns z?
谁 owns z?
the caller: C
caller:C。
who resubmits D? C
谁 resubmits D?C。
References
References
References。
"Ray: A Distributed Framework for Emerging AI Applications" (OSDI 2018)
“Ray: A Distributed Framework for Emerging AI Applications”(OSDI 2018)。
机制小结
1. Futures 与 actors 的组合
future 是 computation result 的 handle,可以先异步 invoke task,再把 reference 传给其他 task,最后由 get 强制求值。actor 则把 state 保存在多次 invocation 之间;两者结合后,既能表达 functional dataflow,也能表达 model replica、video frame buffer 等 stateful service。
2. 为什么 ownership 比 centralized coordinator 更可扩展
centralized coordinator 需要记录所有 futures、refs、workers 和 values;在短任务很多、invocation/get 频繁时,单个 coordinator 成为 bottleneck。Ray 让 function caller 成为 returned future 的 owner,把 metadata 按 ownership 分散;value 仍存放在实际运行 task 的 worker 上,从而让 owner 可以在不联系 value worker 的情况下继续 invoke downstream task。
3. GC 与 object store
future/object 的生命周期由 refs 影响;当所有 references 消失后,object 才能安全 GC。大 value 放进 object store,objects immutable,caller 可以直接从 object store fetch。owner table 维护 metadata,worker 可以保留 secondary copy 来降低 recovery 时的 recomputation。
4. Scheduler 与 memory management
distributed scheduler 先通过 resource request reserve worker,再用 lease 暂时占用资源,并把 task 的 Location 写入 table。obj store 提供 Create、Get、Pin、Release;Get 在 object 创建前 block,Initial Create pin object,secondary copies 则在 object 很大或 recovery 需要时提供帮助。
5. Lineage reconstruction 与 fate sharing
owned object 丢失时,Ray 沿 lineage re-execute 产生该 object 的 task 以及 descendants;secondary copy 可以避免不必要的重算。owner 丢失时,borrower 可能永远等不到 value,形成 dangling reference。fate sharing 让 borrower worker 假装 crash 并终止,促使上层 caller 重新提交原始 task,打破不可恢复的等待。
本篇验收
- [x] 官方 l-ray.txt 已固定:223 行、187 个非空源行,SHA-256 与来源记录一致。
- [x] 源文本的非空行按原顺序逐行保留,中文译文紧随对应英文源行。
- [x] futures、actors、borrowing、ownership sharding、centralized coordinator、lineage reconstruction、distributed scheduler、object store、GC、fate sharing 和 homework 全部覆盖。
- [x] Figure 2、Figure 5、Figure 6、Figure 8、Figure 9 和 ASCII tables 按原文位置保留;源文件没有独立图片。
- [x] Ray、future、actor、ownership、object ref、worker、driver、task、GC、lineage reconstruction 和 fate sharing 等专用词没有强行翻译。
- [ ] 中文译文仍建议进行一次人工术语审校;自动审计只验证源文覆盖与顺序,不证明翻译语义正确。
评论 (0)