基于 Ray 2.9.0 源码架构分析
2026年4月 | 分布式系统深度解读
Ray 是什么?
Ray 是一个开源的分布式计算框架,旨在简化 Python 应用的分布式化部署与运行。
┌─────────────────────────────────────────────┐
│ Ray API Layer │
│ ray() @ray.remote() ray.put() ray.get() │
└─────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────┐
│ Ray Core Components │
│ • Task Scheduler • Actor System • GCS │
└─────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────┐
│ Runtime Components │
│ • Raylet • Worker • Object Store • Plasma │
└─────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────┐
│ System Layer │
│ Linux Kernel + Network │
└─────────────────────────────────────────────┘
核心分层思想:清晰的分层设计确保各模块解耦,便于维护与扩展。
传统分布式计算的挑战
手动管理节点、网络通信、数据一致性、容错处理等复杂问题。
| 特性 | Ray | Dask | Spark | TensorFlow |
|---|---|---|---|---|
| 易用性 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ |
| 性能 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| ML 支持 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| Actor 模型 | ✅ | ❌ | ❌ | ✅ |
┌─────────────────────────────────────────────┐
│ Ray Client │
│ Python Application │
└─────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────┐
│ Ray API Layer │
│ • Task Submission • Object Management │
└─────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────┐
│ Ray Core Components │
│ • Scheduler • Actor System • GCS │
└─────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────┐
│ Runtime Components │
│ • Raylet • Worker • Object Store │
└─────────────────────────────────────────────┘
关键目录结构
Ray 源码采用模块化设计,核心组件分布在不同目录中。
ray/core - 核心实现
worker - Worker 进程实现raylet - Raylet 服务实现gcs - 全局状态服务scheduling - 任务调度器ray/python - Python API 封装
ray/_private - 内部实现ray/actor - Actor 管理ray/task - 任务管理全局状态服务 (GCS)
GCS 是 Ray 的核心协调服务,负责维护集群的全局状态信息。
┌─────────────────────────────────────────────┐
│ Application │
├─────────────────────────────────────────────┤
│ Python API Layer │
├─────────────────────────────────────────────┤
│ Object Store Interface │
├─────────────────────────────────────────────┤
│ Plasma Store │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Local Cache│ │ Shared Mem │ │ Disk │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Raylet Nodes │
└─────────────────────────────────────────────┘
任务执行流程
Ray Task 是 Ray 中的基本执行单元,支持函数的分布式执行。
┌─────────────────────────────────────────────┐
│ Client Process │
├─────────────────────────────────────────────┤
│ Actor Client │
├─────────────────────────────────────────────┤
│ Actor ID │
│ (Unique Actor Identifier) │
├─────────────────────────────────────────────┤
│ Actor State │
│ (Private Actor Data) │
├─────────────────────────────────────────────┤
│ Actor Node │
└─────────────────────────────────────────────┘
Actor 特点:有状态、消息传递、位置透明、故障隔离
Ray Object Store
提供高性能的分布式对象存储服务,支持对象的共享访问。
┌─────────────────────────────────────────────┐
│ Plasma Store │
├─────────────────────────────────────────────┤
│ Memory Management │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Object │ │ Reference │ │ Eviction│ │
│ │ Storage │ │ Counting │ │ Policy │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Object Transfer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Local │ │ Network │ │ Disk │ │
│ │ Access │ │ Transfer │ │ Spill │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
Raylet 节点服务
Raylet 是 Ray 集群中的核心服务进程,负责本地资源管理和任务调度。
┌─────────────────────────────────────────────┐
│ Raylet Process │
├─────────────────────────────────────────────┤
│ Worker Manager │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Worker │ │ Worker │ │ Worker │ │
│ │ Pool │ │ Lifecycle │ │ State │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Execution │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Task │ │ Object │ │ Memory │ │
│ │ Dispatch │ │ Transfer │ │ Mgmt │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
Ray ID 系列结构
Ray 使用多种 ID 结构来标识不同的分布式对象和任务。
┌─────────────────────────────────────────────┐
│ ObjectID (128-bit) │
├─────────────────────────────────────────────┤
│ 64-bit │ 64-bit │
│ Task ID │ Counter │
│ (Unique Task Identifier) │
├─────────────────────────────────────────────┤
│ Usage Examples │
│ • Object.put(data) → ObjectID │
│ • ray.get(ObjectID) → data │
│ • Object.ref_count() → reference count │
└─────────────────────────────────────────────┘
任务标识设计
TaskID 用于标识 Ray 中的每个任务,支持任务追踪和依赖管理。
┌─────────────────────────────────────────────┐
│ WorkerID (128-bit) │
├─────────────────────────────────────────────┤
│ Node ID │ Process ID │
│ (64-bit) │ (64-bit) │
│ Location │ Process Identifier │
│ Information │ within Node │
├─────────────────────────────────────────────┤
│ Functionality │
│ • Worker identification │
│ • Location tracking │
│ • Load balancing │
│ • Fault detection │
└─────────────────────────────────────────────┘
节点标识设计
NodeID 用于标识 Ray 集群中的每个计算节点。
┌─────────────────────────────────────────────┐
│ JobID (128-bit) │
├─────────────────────────────────────────────┤
│ 32-bit │ 96-bit │
│ Version │ Unique Identifier │
│ Information │ (UUID-based) │
├─────────────────────────────────────────────┤
│ Application Context │
│ • Application isolation │
│ • Resource quota management │
│ • Task grouping │
│ • Billing tracking │
└─────────────────────────────────────────────┘
核心工作进程
CoreWorker 是 Ray 中负责任务执行的核心组件。
┌─────────────────────────────────────────────┐
│ RayletClient │
├─────────────────────────────────────────────┤
│ Connection Management │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ gRPC │ │ Connection │ │ Status │ │
│ │ Channel │ │ Pool │ │ Monitor│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Operations │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Task │ │ Task │ │ Task │ │
│ │ Submit │ │ Cancel │ │ Status │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Object Operations │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Object │ │ Object │ │ Object │ │
│ │ Put │ │ Get │ │ Wait │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
全局状态客户端
GcsClient 提供与 GCS 服务的通信接口,用于查询和更新集群状态。
┌─────────────────────────────────────────────┐
│ ObjectManager │
├─────────────────────────────────────────────┤
│ Object Lifecycle │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Object │ │ Reference │ │ Eviction│ │
│ │ Creation │ │ Counting │ │ Policy │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Object Transfer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Local │ │ Cross-Node│ │ Memory │ │
│ │ Access │ │ Transfer │ │ Mgmt │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Object Metadata │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Size │ │ Location │ │ Type │ │
│ │ Information│ │ Tracking │ │ Info │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
资源分组管理
PlacementGroup 用于管理资源的分组和分配策略。
┌─────────────────────────────────────────────┐
│ ResourceManager │
├─────────────────────────────────────────────┤
│ Resource Tracking │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ CPU │ │ Memory │ │ GPU │ │
│ │ Resources │ │ Resources │ │ Usage │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Resource Allocation │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Task │ │ Actor │ │ PG │ │
│ │ Scheduling │ │ Allocation│ │ Mgmt │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Resource Monitoring │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Load │ │ Metrics │ │ Alerts │ │
│ │ Analysis │ │ Collection│ │ System │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
任务调度器
Scheduler 负责将任务分配到合适的 Worker 执行,优化资源利用率。
┌─────────────────────────────────────────────┐
│ ObjectStore │
├─────────────────────────────────────────────┤
│ Storage Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Memory │ │ Plasma │ │ Disk │ │
│ │ Storage │ │ Store │ │ Storage │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Access Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Local │ │ Remote │ │ Batch │ │
│ │ Access │ │ Access │ │ Access │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Management Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Reference │ │ Eviction │ │ Stats │ │
│ │ Counting │ │ Policy │ │ Tracking│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
Plasma 对象存储
PlasmaStore 提供高性能的对象存储服务,支持内存与磁盘管理。
┌─────────────────────────────────────────────┐
│ TaskSpec │
├─────────────────────────────────────────────┤
│ Task Execution Context │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Function │ │ Arguments │ │ Options │ │
│ │ Reference │ │ List │ │ Dict │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Dependencies │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Input │ │ Output │ │ Parent │ │
│ │ Objects │ │ Objects │ │ Tasks │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┐
│ Task Metadata │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ TaskID │ │ NodeID │ │ JobID │ │
│ │ Location │ │ Resources │ │ Stats │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
对象规格定义
ObjectSpec 定义了 Ray 中分布式对象的规格和属性。
┌─────────────────────────────────────────────┐
│ WorkerTableEntry │
├─────────────────────────────────────────────┤
│ Worker Identity Information │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ WorkerID │ │ JobID │ │ NodeID │ │
│ │ Identifier │ │ Assignment │ │ Location │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Worker Status Information │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ State │ │ Resources │ │ Load │ │
│ │ (Idle/Busy)│ │ Available │ │ Metrics │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Worker Capability Information │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Language │ │ Libraries │ │ Version │ │
│ │ Support │ │ Available │ │ Info │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
节点表条目
NodeTableEntry 存储集群中每个节点的状态和资源信息。
Raylet 核心实现
Raylet 是 Ray 集群中的核心服务,负责本地资源管理和任务调度。
raylet/raylet.h:主要接口定义raylet/scheduling.h:调度器实现raylet/worker.h:Worker 管理raylet/object_manager.h:对象管理Raylet:主服务类Scheduler:任务调度器WorkerPool:Worker 池管理ObjectManager:对象管理器
┌─────────────────────────────────────────────┐
│ Raylet │
├─────────────────────────────────────────────┤
│ Worker Request │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Worker │ │ Resource │ │
│ │ Allocation │ │ Request │ │
│ └─────────────┘ └─────────────┘ │
├─────────────────────────────────────────────┤
│ Worker Creation │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Process │ │ Environment│ │
│ │ Spawning │ │ Setup │ │
│ └─────────────┘ └─────────────┘ │
├─────────────────────────────────────────────┤
│ Worker Registration │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Connection │ │ State │ │
│ │ Setup │ │ Update │ │
│ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────┘
任务执行流程
Ray 中的任务执行涉及多个组件的协作。
┌─────────────────────────────────────────────┐
│ Object Transfer │
├─────────────────────────────────────────────┤
│ Local Transfer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Memory │ │ Reference │ │ Copy │ │
│ │ Copy │ │ Counting │ │ vs Move│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Network Transfer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ gRPC │ │ Zero-Copy │ │ Batch │ │
│ │ Transfer │ │ Transfer │ │ Transfer│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Storage Transfer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Memory to │ │ Plasma to │ │ Object │ │
│ │ Plasma │ │ Disk │ │ Locator │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
Actor 对象创建
Ray 中的 Actor 创建涉及复杂的分布式协调过程。
┌─────────────────────────────────────────────┐
│ Client Process │
├─────────────────────────────────────────────┤
│ Method Call │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Method │ │ Arguments │ │ Options │ │
│ │ Name │ │ List │ │ Dict │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Submission │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Task │ │ Task │ │ Task │ │
│ │ Creation │ │ Dispatch │ │ Result │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Actor Node │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Actor │ │ Method │ │ Return │ │
│ │ Execution │ │ Invocation │ │ Value │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
Actor 状态管理
Ray Actor 支持多种状态同步机制,确保数据一致性。
核心工作进程实现
CoreWorker 是 Ray 任务执行的核心组件。
core/worker/core_worker.h:主接口core/worker/task_interface.h:任务接口core/worker/object_interface.h:对象接口core/worker/actor_manager.h:Actor 管理
┌─────────────────────────────────────────────┐
│ Task Submission │
├─────────────────────────────────────────────┤
│ Task Creation │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Function │ │ Arguments │ │ Options │ │
│ │ Reference │ │ Processing │ │ Setup │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Serialization │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Pickle │ │ Compression│ │ Batch │ │
│ │ Processing │ │ Optimized │ │ Upload │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Dispatching │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Worker │ │ Resource │ │ Network │ │
│ │ Selection │ │ Matching │ │ Routing │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
对象存储访问
Ray 中的对象存储提供高性能的读写接口。
put(obj):存储对象get(object_id):获取对象wait(object_ids):等待对象delete(object_ids):删除对象全局状态服务
GCS 是 Ray 集群的协调服务,管理全局状态信息。
gcs/gcs_server.h:GCS 服务主类gcs/node_manager.h:节点管理gcs/worker_manager.h:Worker 管理gcs/actor_manager.h:Actor 管理
┌─────────────────────────────────────────────┐
│ GCS Server │
├─────────────────────────────────────────────┤
│ Service Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ gRPC │ │ HTTP │ │ REST │ │
│ │ Services │ │ Services │ │ APIs │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Business Logic Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Node │ │ Worker │ │ Actor │ │
│ │ Manager │ │ Manager │ │ Manager│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Storage Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ In-Memory │ │ Persistent │ │ Backup │ │
│ │ Cache │ │ Storage │ │ System │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
全局状态管理
GCS 维护 Ray 集群的全局状态信息。
┌─────────────────────────────────────────────┐
│ GCS Communication │
├─────────────────────────────────────────────┤
│ Protocol Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ gRPC │ │ Protocol │ │ Codec │ │
│ │ Transport │ │ Buffers │ │ System │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Message Types │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Node │ │ Worker │ │ Actor │ │
│ │ Messages │ │ Messages │ │ Messages│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Communication Pattern │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Request │ │ Response │ │ Stream │ │
│ │ Response │ │ Streaming │ │ Model │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
Python API 实现
Ray 的 Python API 层提供了丰富的分布式计算接口。
ray/_private/client.py:客户端接口ray/_private/worker.py:Worker 管理ray/actor.py:Actor 管理ray/task.py:任务管理
┌─────────────────────────────────────────────┐
│ @ray.remote │
├─────────────────────────────────────────────┤
│ Function Decorator │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Function │ │ Metadata │ │ Config │ │
│ │ Wrapper │ │ Extraction │ │ Setup │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Creation │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ TaskSpec │ │ Arguments │ │ Options │ │
│ │ Generation │ │ Processing │ │ Apply │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Submission │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Client │ │ Raylet │ │ Worker │ │
│ │ Interface │ │ Dispatch │ │ Execute │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┐
│ Result Collection │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ ObjectID │ │ Future │ │ Value │ │
│ │ Generation │ │ Handling │ │ Return │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
@ray.remote 装饰器实现
@ray.remote 是 Ray 中最核心的分布式编程接口。
@ray.remote:基本装饰器@ray.remote(num_cpus=2):资源声明@ray.remote(num_gpus=1):GPU 资源@ray.remote(actor=True):Actor 装饰器
┌─────────────────────────────────────────────┐
│ @ray.* Decorators │
├─────────────────────────────────────────────┤
│ Core Decorators │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ @ray.put │ │ @ray.get │ │ @ray.wait│ │
│ │ Object │ │ Object │ │ Objects │ │
│ │ Storage │ │ Retrieval │ │ Wait │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ ML Decorators │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ @ray.tune │ │ @ray.train │ │ @ray. │ │
│ │ Hyperparam │ │ Model │ │ Serve │ │
│ │ Tuning │ │ Training │ │ Deploy │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Utility Decorators │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ @ray.init │ │ @ray.shutdown││ @ray. │ │
│ │ Initialize │ │ Cleanup │ │ kill │ │
│ │ Ray │ │ Ray │ │ Actors │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
任务等待机制
Ray 的 wait 函数支持等待多个任务的完成。
wait(object_ids, num_ready=1):等待指定数量wait(object_ids, timeout=10):超时等待wait(object_ids, return_when='FIRST_COMPLETED'):条件等待
┌─────────────────────────────────────────────┐
│ ray.get() │
├─────────────────────────────────────────────┤
│ Object Retrieval │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ ObjectID │ │ Location │ │ Status │ │
│ │ Processing │ │ Discovery │ │ Check │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Object Fetching │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Local │ │ Remote │ │ Network│ │
│ │ Cache │ │ Transfer │ │ Flow │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Object Return │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Deserialization│ │ Type │ │ Value │ │
│ │ Processing │ │ Checking │ │ Return │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
对象存储实现
Ray 的 put 函数将对象存储到分布式存储系统中。
任务调度算法实现
Ray 的调度器实现了多种高效的任务调度算法。
scheduling/cluster_resource_manager.h:集群资源管理scheduling/scheduling.h:调度器接口scheduling/algorithm.h:调度算法scheduling/policy.h:调度策略
┌─────────────────────────────────────────────┐
│ FIFO Scheduler │
├─────────────────────────────────────────────┤
│ Task Queue │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Task │ │ Task │ │ Task │ │
│ │ 1 (First) │ │ 2 │ │ 3 │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Dispatching │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Worker │ │ Worker │ │ Worker │ │
│ │ Assignment │ │ Selection │ │ Status │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Completion │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Task │ │ Task │ │ Task │ │
│ │ Removal │ │ Result │ │ Next │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
Round Robin 调度算法
Round Robin 调度器按照轮询方式分配任务到各个 Worker。
┌─────────────────────────────────────────────┐
│ Resource Aware Scheduling │
├─────────────────────────────────────────────┤
│ Resource Profiling │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ CPU │ │ Memory │ │ GPU │ │
│ │ Utilization│ │ Usage │ │ Load │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Resource Matching │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Task │ │ Node │ │ Match │ │
│ │ Resources │ │ Resources │ │ Score │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Task Assignment │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Optimal │ │ Load │ │ Energy │ │
│ │ Placement │ │ Balancing │ │ Aware │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
资源分组算法
Placement Group 算法优化资源的分组分配策略。
┌─────────────────────────────────────────────┐
│ Load Balancing Algorithm │
├─────────────────────────────────────────────┤
│ Load Monitoring │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ CPU Load │ │ Memory │ │ Network│ │
│ │ Tracking │ │ Usage │ │ Traffic│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Load Analysis │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Load │ │ Hotspot │ │ Trend │ │
│ │ Distribution│ │ Detection │ │ Analysis│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Load Balancing │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Task │ │ Migration │ │ Scaling│ │
│ │ Migration │ │ Strategy │ │ Policy │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┊
故障恢复机制
Ray 提供了完善的容错机制,确保系统可靠性。
┌─────────────────────────────────────────────┐
│ Fault Detection │
├─────────────────────────────────────────────┤
│ Health Check │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Heartbeat │ │ Timeout │ │ Ping │ │
│ │ Monitoring │ │ Detection │ │ Test │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Fault Analysis │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Node │ │ Worker │ │ Actor │ │
│ │ Failure │ │ Crash │ │ Death │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Fault Reporting │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Event │ │ Alert │ │ Action │ │
│ │ Generation │ │ System │ │ Trigger│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
任务失败重试
Ray 的任务重试机制确保任务的最终执行成功。
┌─────────────────────────────────────────────┐
│ Actor Restart │
├─────────────────────────────────────────────┤
│ Actor Failure │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Crash │ │ Timeout │ │ Error │ │
│ │ Detection │ │ Detection │ │ State │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Restart Decision │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Restart │ │ Migration │ │ Terminate│ │
│ │ Strategy │ │ Strategy │ │ Option │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Recovery Process │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ State │ │ Migration │ │ Client │ │
│ │ Recovery │ │ Process │ │ Update │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
状态快照恢复
Ray 通过快照机制实现状态的持久化与恢复。
┌─────────────────────────────────────────────┐
│ Ray ML Ecosystem │
├─────────────────────────────────────────────┤
│ Core ML Tools │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Ray Tune │ │ Ray Train │ │ Ray Serve│ │
│ │ Hyperparam │ │ Model │ │ Serving │ │
│ │ Tuning │ │ Training │ │ Platform│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Advanced ML Tools │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ RLlib │ │ Ray Data │ │ Ray │ │
│ │ RL │ │ Processing │ │ Dataset │ │
│ │ Framework │ │ Pipeline │ │ Library │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Integration Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ TensorFlow │ │ PyTorch │ │ Scikit │ │
│ │ Integration│ │ Integration│ │ Learn │ │
│ │ Framework │ │ Framework │ │ Integration│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
超参数优化框架
Ray Tune 是 Ray 中的超参数优化工具。
tune/tune.py:主接口tune/analysis:分析模块tune/schedulers:调度器tune/trial.py:试验管理
┌─────────────────────────────────────────────┐
│ Ray Train │
├─────────────────────────────────────────────┤
│ Training Framework │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Distributed│ │ Parallel │ │ Model │ │
│ │ Training │ │ Training │ │ Scaling│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Integration Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ TensorFlow │ │ PyTorch │ │ JAX │ │
│ │ Backend │ │ Backend │ │ Backend│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Training Pipeline │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Data │ │ Model │ │ Loss │ │
│ │ Loading │ │ Training │ │ Function│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Result Collection │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Metrics │ │ Checkpoint │ │ Logging│ │
│ │ Aggregation │ │ Management │ │ System │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
模型服务框架
Ray Serve 提供高性能的模型推理服务。
┌─────────────────────────────────────────────┐
│ RLlib │
├─────────────────────────────────────────────┤
│ RL Framework │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Algorithms │ │ Environments│ │ Agents │ │
│ │ Library │ │ Interface │ │ Models │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Distributed RL │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Rollout │ │ Training │ │ Sync │ │
│ │ Collection │ │ Process │ │ Mechanism│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Performance │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Vectorized │ │ GPU │ │ Async │ │
│ │ Environments│ │ Acceleration│ │ Samplers│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
Ray 性能优化技术
Ray 通过多种优化技术实现高性能分布式计算。
┌─────────────────────────────────────────────┐
│ Zero-Copy Transfer │
├─────────────────────────────────────────────┤
│ Traditional Copy │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Memory │ │ Buffer │ │ Memory │ │
│ │ → Copy │ │ → Copy │ │ → Copy │ │
│ │ (3 copies) │ │ (3 copies) │ │ (3 copies) │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Zero-Copy Implementation │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Memory │ │ Reference │ │ Memory │ │
│ │ → Mapping │ │ Counting │ │ Sharing│ │
│ │ (1 copy) │ │ (1 copy) │ │ (1 copy) │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Performance Benefits │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Reduced │ │ Lower │ │ Higher │ │
│ │ Latency │ │ CPU Usage │ │ Throughput│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
批量处理优化
Ray 通过批处理技术提高整体处理效率。
┌─────────────────────────────────────────────┐
│ Memory Management │
├─────────────────────────────────────────────┤
│ Memory Hierarchy │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ CPU Cache │ │ RAM │ │ Disk │ │
│ │ (L1/L2) │ │ Memory │ │ Storage│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Memory Strategies │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Prefetching│ │ Caching │ │ Swapping│ │
│ │ Strategy │ │ Strategy │ │ Policy │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Memory Optimization │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Fragment │ │ Allocation │ │ Garbage│ │
│ │ Reduction │ │ Strategy │ │ Collection│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
网络通信优化
Ray 通过多种网络优化技术提高分布式通信效率。
┌─────────────────────────────────────────────┐
│ Ray Best Practices │
├─────────────────────────────────────────────┤
│ Design Patterns │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Task │ │ Actor │ │ Hybrid │ │
│ │ Parallel │ │ Pattern │ │ Design │ │
│ │ Pattern │ │ Pattern │ │ Pattern│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Performance Guidelines │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Resource │ │ Task │ │ Memory │ │
│ │ Management │ │ Design │ │ Layout │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Error Handling │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Retry │ │ Timeout │ │ Fallback│ │
│ │ Strategy │ │ Handling │ │ Pattern│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
任务设计模式
Ray 中常用的任务设计模式和方法论。
┌─────────────────────────────────────────────┐
│ Actor Design Patterns │
├─────────────────────────────────────────────┤
│ Actor Types │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Stateful │ │ Stateless │ │ Hybrid │ │
│ │ Actors │ │ Actors │ │ Actors │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Communication Patterns │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Request │ │ Publish │ │ Event │
│ │ Response │ │ Subscribe │ │ Driven │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Lifecycle Patterns │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Creation │ │ Migration │ │ Death │
│ │ Pattern │ │ Pattern │ │ Pattern │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
错误处理模式
Ray 中的错误处理策略和最佳实践。
┌─────────────────────────────────────────────┐
│ Common Pitfalls │
├─────────────────────────────────────────────┤
│ Performance Issues │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Task │ │ Object │ │ Network │ │
│ │ Overhead │ │ Churn │ │ Latency │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Resource Issues │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Memory │ │ CPU │ │ GPU │ │
│ │ Leaks │ │ Starvation │ │ Exhaust│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Design Issues │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ State │ │ Dependencies│ │ Race │
│ │ Management │ │ Complexity │ │ Condition│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└─────────────────────────────────────────────┘
调试与故障排查
Ray 项目的调试工具和技巧。
┌─────────────────────────────────────────────┐
│ Monitoring & Operations │
├─────────────────────────────────────────────┤
│ Monitoring Metrics │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ System │ │ Application│ │ Business│ │
│ │ Metrics │ │ Metrics │ │ Metrics│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Alerting System │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Threshold │ │ Notification│ │ Action │ │
│ │ Based │ │ System │ │ Triggers│ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
├─────────────────────────────────────────────┤
│ Automation │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │
│ │ Auto │ │ Auto │ │ Auto │ │
│ │ Scaling │ │ Healing │ │ Backup │ │
│ └─────────────┘ └─────────────┘ └─────────┘ │
└──────────────── Analytics dashboards are the key to understanding Ray performance
进一步学习资源
深入了解 Ray 框架的更多资源和文档。
Ray 框架核心价值
Ray 通过简化分布式编程,让开发者能够轻松构建高性能的分布式应用。
感谢阅读
希望这份 Ray 源码解读对您有所帮助!
Ray 分布式计算框架源码解读
基于 Ray 2.9.0 源码架构深度分析