Title here
Summary here
一次服务调用可能等待成员发现、网络或下一项流式输出。Rust Future 在等待期间通过 waker 请求唤醒,使 Tokio 可以推进其他就绪任务。以下将这些基础机制与 Dynamo 1.6.2 的运行时 对照。
传统的同步 I/O 模型中,当程序执行 I/O 操作(如网络请求、文件读写)时,线程会被阻塞,直到操作完成:
sequenceDiagram
participant T1 as Thread 1
participant IO as I/O 操作
T1->>IO: 发起请求
Note over T1: 阻塞等待...
IO-->>T1: 完成
T1->>T1: 继续执行问题:
异步 I/O 模型允许在等待 I/O 时切换到其他任务:
graph LR
subgraph thread["单线程执行"]
A["任务 A<br/>发起 I/O"] --> B["任务 B<br/>发起 I/O"]
B --> C["任务 A<br/>I/O 完成"]
C --> D["任务 C<br/>发起 I/O"]
D --> E["任务 B<br/>I/O 完成"]
end优势:
Rust 的异步模型基于 Future trait,这是一种 惰性求值 的设计:
pub trait Future {
type Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
}
pub enum Poll<T> {
Ready(T), // 计算完成,返回结果
Pending, // 计算未完成,需要稍后再试
}| 概念 | 说明 |
|---|---|
| 惰性(Lazy) | Future 创建后不会立即执行,必须被 poll 才会推进 |
| 协作式(Cooperative) | Future 主动让出控制权,而非被抢占 |
| 零成本抽象 | 编译器将 async 函数转换为状态机,调度、唤醒与 I/O 轮询仍有成本 |
async/await 是对 Future 的语法糖,使异步代码看起来像同步代码:
// 使用 async/await
async fn fetch_and_process() -> Result<Data> {
let response = client.get("http://api.example.com").await?;
let data = response.json::<Data>().await?;
Ok(data)
}编译器将 async 函数转换为状态机:
stateDiagram-v2
Start --> WaitingForResponse: client.get() 发起请求
WaitingForResponse --> WaitingForJson: 响应到达
WaitingForJson --> Done: JSON 解析完成
note right of WaitingForResponse: 网络 I/O 进行中
note right of WaitingForJson: JSON 解析中编译器生成的状态机:
enum FetchAndProcessState {
Start,
WaitingForResponse { client: Client },
WaitingForJson { response: Response },
Done,
}Tokio 是 Rust 生态中最流行的异步运行时,Dynamo 选择 Tokio 作为底层运行时。
graph TB
subgraph tokio["Tokio Runtime"]
subgraph scheduler["Scheduler 调度器"]
MT["Multi-Thread<br/>多线程调度器"]
CT["Current-Thread<br/>单线程调度器"]
end
subgraph driver["I/O Driver"]
EP["Epoll/Kqueue/IOCP<br/>系统事件循环"]
TIM["Timer<br/>定时器"]
end
subgraph task["Task System"]
Q1["Work Queue 1"]
Q2["Work Queue 2"]
GQ["Global Queue"]
end
BP["Blocking Pool<br/>阻塞线程池"]
end
MT --> Q1
MT --> Q2
MT --> GQ
EP --> MT
TIM --> MTTokio 的多线程调度器使用 Work-Stealing 算法来平衡负载:
graph TB
subgraph workers["Worker 线程"]
W1["Worker 1<br/>任务 A, B, C"]
W2["Worker 2<br/>任务 D, E"]
W3["Worker 3<br/>空闲"]
end
W3 --> |steal| W1
GQ["Global Inject Queue<br/>新任务首先进入"]
GQ --> W1
GQ --> W2
GQ --> W3Work-Stealing 的优势:
| 优势 | 说明 |
|---|---|
| 负载均衡 | 空闲 Worker 主动获取任务 |
| 缓存友好 | 任务优先在创建它的 Worker 上执行 |
| 低竞争 | 每个 Worker 有自己的队列 |
某些操作无法异步化(如文件系统操作、CPU 密集计算),Tokio 提供 Blocking 线程池:
// 在 blocking 线程池中执行同步操作
let result = tokio::task::spawn_blocking(|| {
// 这里的代码在独立线程中运行
std::fs::read_to_string("large_file.txt")
}).await?;graph LR
subgraph async_rt["Async Runtime"]
AT1["Async Task 1"]
AT2["Async Task 2"]
end
subgraph blocking["Blocking Pool"]
BT1["Blocking Task 1"]
BT2["Blocking Task 2"]
end
AT1 --> |spawn_blocking| BT1
BT1 --> |JoinHandle| AT1Dynamo 基于 Tokio 构建运行时:
// Dynamo Runtime 的核心结构
pub struct Runtime {
id: Arc<String>,
primary: RuntimeType, // 主任务运行时
secondary: RuntimeType, // 后台任务运行时
cancellation_token: CancellationToken,
}graph TB
subgraph primary["Primary Runtime - 多线程"]
P1["推理请求处理"]
P2["HTTP 请求处理"]
P3["数据预处理"]
end
subgraph secondary["Secondary Runtime - 单线程"]
S1["etcd Keep-Alive"]
S2["NATS 订阅"]
S3["健康检查"]
end
Client["客户端请求"] --> P1
ETCD[("etcd")] <--> S1
NATS[("NATS")] <--> S2分离的原因:
继续阅读 02-分布式系统基础,了解分布式系统的核心概念。