Title here
Summary here
每个进程独立创建 runtime,以相同 namespace 和发现后端联通。Dynamo 1.6.2 的开发例子可使用文件发现,默认请求传输与事件传输分别是 TCP、ZMQ。以下 etcd/NATS 的配置对应明确选用这些后端的情况。 DistributedRuntime 源码
Dynamo 的运行时采用双层设计:Local Runtime 和 Distributed Runtime。这种设计遵循了 关注点分离 原则:
graph TB
subgraph app["应用层"]
App["用户应用"]
end
subgraph distributed["分布式运行时层"]
DRT["DistributedRuntime"]
ETCD["etcd Client"]
NATS["NATS Client"]
TCP["TCP Server"]
REG["Component Registry"]
end
subgraph local["本地运行时层"]
RT["Runtime"]
Primary["Primary Runtime<br/>主业务线程池"]
Secondary["Secondary Runtime<br/>后台任务线程池"]
CT["CancellationToken"]
end
subgraph tokio["Tokio 层"]
TokioRT["Tokio Runtime"]
end
App --> DRT
DRT --> RT
DRT --> ETCD
DRT --> NATS
DRT --> TCP
DRT --> REG
RT --> Primary
RT --> Secondary
RT --> CT
Primary --> TokioRT
Secondary --> TokioRT设计目标:
| 目标 | 实现方式 |
|---|---|
| 分层解耦 | Local Runtime 不依赖分布式组件,可独立测试 |
| 资源隔离 | 业务逻辑和后台任务使用不同的线程池 |
| 生命周期管理 | 统一的取消令牌机制 |
Runtime 是本地运行时,管理 Tokio 线程池和取消令牌:
// 源码位置: lib/runtime/src/lib.rs:54-67
pub struct Runtime {
id: Arc<String>,
primary: RuntimeType,
secondary: RuntimeType,
cancellation_token: CancellationToken,
}
enum RuntimeType {
Shared(Arc<tokio::runtime::Runtime>),
External(tokio::runtime::Handle),
}RuntimeType 支持两种模式:
graph LR
subgraph shared["Shared 模式"]
S1["Dynamo 创建 Runtime"]
S2["Dynamo 管理生命周期"]
S3["Dynamo 关闭 Runtime"]
S1 --> S2 --> S3
end
subgraph external["External 模式"]
E1["外部提供 Handle"]
E2["Dynamo 使用 Handle"]
E3["外部管理生命周期"]
E1 --> E2 --> E3
end| 模式 | 说明 | 使用场景 |
|---|---|---|
| Shared | Dynamo 创建和管理 Tokio Runtime | 独立运行的服务 |
| External | 使用外部提供的 Runtime Handle | 嵌入到其他应用中 |
Dynamo 使用两个运行时来隔离不同类型的任务:
graph TB
subgraph primary["Primary Runtime - 多线程"]
P1["推理请求处理"]
P2["HTTP 请求处理"]
P3["数据预处理"]
end
subgraph secondary["Secondary Runtime - 单线程"]
S1["etcd Keep-Alive"]
S2["NATS 订阅"]
S3["健康检查"]
S4["日志上报"]
end
Client["客户端请求"] --> P1
ETCD[("etcd")] <--> S1
NATS[("NATS")] <--> S2| 运行时 | 用途 | 特点 |
|---|---|---|
| Primary | 主业务逻辑 | 多线程,处理推理请求 |
| Secondary | 后台任务 | 单线程,处理 etcd/NATS 通信 |
为什么要分离?
impl Runtime {
/// 从当前 Tokio 运行时创建(嵌入模式)
pub fn from_current() -> Result<Runtime>;
/// 从外部 Handle 创建
pub fn from_handle(handle: tokio::runtime::Handle) -> Result<Runtime>;
/// 从配置文件创建(推荐)
pub fn from_settings() -> Result<Runtime>;
/// 创建单线程运行时(测试用)
pub fn single_threaded() -> Result<Runtime>;
}DistributedRuntime 扩展了 Runtime,增加了分布式系统所需的组件:
// 源码位置: lib/runtime/src/lib.rs:68-86
pub struct DistributedRuntime {
// 本地运行时
runtime: Runtime,
// 分布式组件
etcd_client: transports::etcd::Client,
nats_client: transports::nats::Client,
tcp_server: Arc<OnceCell<Arc<TcpStreamServer>>>,
// 本地组件注册表
component_registry: component::Registry,
}classDiagram
class Runtime {
-id: Arc~String~
-primary: RuntimeType
-secondary: RuntimeType
-cancellation_token: CancellationToken
+primary() Handle
+secondary() Handle
+child_token() CancellationToken
+shutdown()
}
class DistributedRuntime {
-runtime: Runtime
-etcd_client: etcd::Client
-nats_client: nats::Client
-tcp_server: Arc~OnceCell~
-component_registry: Registry
+namespace(name) Namespace
+tcp_server() TcpStreamServer
+nats_client() nats::Client
+etcd_client() etcd::Client
}
DistributedRuntime *-- RuntimeRegistry 用于管理本地组件,避免重复创建资源:
graph TB
subgraph registry["Component Registry"]
REG["Registry"]
subgraph services["Services"]
SVC1["Service: dynamo|frontend"]
SVC2["Service: dynamo|processor"]
end
subgraph handlers["Stats Handlers"]
SH1["Handler Map for frontend"]
SH2["Handler Map for processor"]
end
end
REG --> SVC1
REG --> SVC2
REG --> SH1
REG --> SH2Registry 解决的问题:
CancellationToken 是 Tokio 提供的取消信号机制,Dynamo 使用层级结构管理:
graph TB
subgraph tokens["CancellationToken 层级"]
Root["Root Token<br/>Runtime 级别"]
Child1["Child Token 1<br/>Component A"]
Child2["Child Token 2<br/>Component B"]
GC1["Grandchild Token<br/>Endpoint A1"]
GC2["Grandchild Token<br/>Endpoint A2"]
GC3["Grandchild Token<br/>Endpoint B1"]
end
Root --> Child1
Root --> Child2
Child1 --> GC1
Child1 --> GC2
Child2 --> GC3取消传播规则:
| 规则 | 说明 |
|---|---|
| 父取消 → 子取消 | 父 Token 取消时,所有子 Token 自动取消 |
| 子取消 ≠ 父取消 | 子 Token 取消时,不影响父 Token 和兄弟 Token |
| 异步等待 | 可以通过 cancelled() 异步等待取消信号 |
Dynamo 将 CancellationToken 与 etcd Lease 绑定:
sequenceDiagram
participant App as 应用
participant RT as Runtime
participant Lease as etcd Lease
participant etcd as etcd
App->>RT: shutdown()
RT->>RT: cancellation_token.cancel()
Note over RT,Lease: Token 取消传播
Lease->>Lease: 检测到 cancel
Lease->>etcd: Revoke Lease
Note over etcd: Lease 过期<br/>所有绑定的 Key 自动删除Worker 是运行用户应用的入口点:
pub struct Worker {
runtime: Runtime,
}
impl Worker {
/// 从配置创建 Worker
pub fn from_settings() -> Result<Worker>;
/// 执行应用
pub fn execute<F, Fut>(self, f: F) -> Result<()>
where
F: FnOnce(Runtime) -> Fut + Send + 'static,
Fut: Future<Output = Result<()>> + Send + 'static;
}配置优先级(从低到高):
graph LR
Default["代码默认值<br/>最低优先级"]
Defaults["/opt/dynamo/defaults/*.toml"]
Etc["/opt/dynamo/etc/*.toml"]
Env["环境变量 DYN_RUNTIME_*<br/>最高优先级"]
Default --> Defaults --> Etc --> Env --> Final["最终配置"]继续阅读 04-Component 系统详解,了解 Dynamo 的组件模型。