Retire-Cluster 是一个分布式系统,旨在将闲置设备重新利用为统一的计算集群。该架构遵循主从模式,具有智能任务调度和实时监控功能。
┌─────────────────────────────────────────────────────────────┐
│ 主节点 │
│ (NAS / 常开服务器) │
│ │
│ ┌────────────────────────────────────────────────────┐ │
│ │ 核心组件 │ │
│ │ │ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌──────────┐ │ │
│ │ │设备注册表 │ │任务调度器 │ │心跳监控 │ │ │
│ │ │& 元数据管理 │ │& 队列管理 │ │器 │ │ │
│ │ └──────────────┘ └──────────────┘ └──────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────┐ │ │
│ │ │ 通信层 │ │ │
│ │ │ - TCP 套接字服务器 (端口 8080) │ │ │
│ │ │ - JSON 消息协议 │ │ │
│ │ │ - 连接池管理 │ │ │
│ │ └──────────────────────────────────────────────┘ │ │
│ └────────────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────────────────┐ │
│ │ Web仪表板 │ │
│ │ │ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌──────────┐ │ │
│ │ │CLI界面 │ │终端UI │ │REST API │ │ │
│ │ │(xterm.js) │ │渲染器 │ │端点 │ │ │
│ │ └──────────────┘ └──────────────┘ └──────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────┐ │ │
│ │ │ Flask Web服务器 │ │ │
│ │ │ - 多格式API (JSON/CSV/文本) │ │ │
│ │ │ - 服务器发送事件 (SSE) │ │ │
│ │ │ - 实时流式传输 │ │ │
│ │ └──────────────────────────────────────────────┘ │ │
│ └────────────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────────────────┐ │
│ │ 数据持久化 │ │
│ │ │ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌──────────┐ │ │
│ │ │SQLite │ │配置文件 │ │日志文件 │ │ │
│ │ │数据库 │ │JSON格式 │ │轮转 │ │ │
│ │ └──────────────┘ └──────────────┘ └──────────┘ │ │
│ └────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
↕ TCP/JSON
网络通信
↕ 心跳/任务
┌─────────────────────────────────────────────────────────────┐
│ 工作节点 │
│ (分布式设备) │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Android │ │ 旧笔记本 │ │ 树莓派 │ │
│ │ 设备 │ │ / 台式机 │ │ / IoT │ │
│ │ │ │ │ │ │ │
│ │ ┌──────────┐ │ │ ┌──────────┐ │ │ ┌──────────┐ │ │
│ │ │Termux │ │ │ │Linux/Win │ │ │ │Linux ARM │ │ │
│ │ │Python │ │ │ │Python │ │ │ │Python │ │ │
│ │ └──────────┘ │ │ └──────────┘ │ │ └──────────┘ │ │
│ │ │ │ │ │ │ │
│ │ ┌──────────┐ │ │ ┌──────────┐ │ │ ┌──────────┐ │ │
│ │ │设备 │ │ │ │系统 │ │ │ │硬件 │ │ │
│ │ │分析器 │ │ │ │监控器 │ │ │ │接口 │ │ │
│ │ └──────────┘ │ │ └──────────┘ │ │ └──────────┘ │ │
│ │ │ │ │ │ │ │
│ │ ┌──────────┐ │ │ ┌──────────┐ │ │ ┌──────────┐ │ │
│ │ │任务 │ │ │ │任务 │ │ │ │任务 │ │ │
│ │ │执行器 │ │ │ │执行器 │ │ │ │执行器 │ │ │
│ │ └──────────┘ │ │ └──────────┘ │ │ └──────────┘ │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
class DeviceRegistry:
def register_device(self, device_info: DeviceInfo) -> bool
def update_device_status(self, device_id: str, status: DeviceStatus)
def get_devices_by_capability(self, requirements: TaskRequirements) -> List[Device]
def mark_device_offline(self, device_id: str, reason: str)
class TaskScheduler:
def submit_task(self, task: Task) -> str # 返回 task_id
def schedule_task(self, task: Task) -> Optional[Device]
def handle_task_completion(self, task_id: str, result: TaskResult)
def handle_device_failure(self, device_id: str)
class HeartbeatMonitor:
def process_heartbeat(self, device_id: str, metrics: SystemMetrics)
def check_device_timeouts(self) -> List[str] # 返回离线设备
def update_cluster_metrics(self)
class ClusterServer:
def start_server(self, host: str, port: int)
def handle_client_connection(self, client_socket: socket.socket)
def process_message(self, message: Message) -> Response
def broadcast_message(self, message: Message, target_devices: List[str])
/text/* - 纯文本、CSV、TSV格式(AI友好)/api/v1/* - 编程访问的JSON API/stream/* - 实时更新的服务器发送事件# 文本API示例
GET /text/devices # 管道分隔的设备列表
GET /text/status # 键值对集群状态
GET /text/metrics # Prometheus格式指标
# JSON API示例
GET /api/v1/devices # 结构化设备数据
POST /api/v1/command # 执行CLI命令
GET /api/v1/cluster/status # 集群信息
# 流式API示例
GET /stream/devices # 实时设备更新
GET /stream/logs # 实时日志流
class DeviceProfiler:
def get_system_info(self) -> SystemInfo
def get_hardware_specs(self) -> HardwareSpecs
def detect_capabilities(self) -> List[Capability]
def get_performance_metrics(self) -> PerformanceMetrics
class TaskExecutor:
def execute_task(self, task: Task) -> TaskResult
def monitor_task_progress(self, task_id: str) -> ProgressReport
def cancel_task(self, task_id: str) -> bool
def cleanup_task_resources(self, task_id: str)
系统使用基于JSON的消息协议通过TCP套接字进行通信:
{
"message_type": "register",
"sender_id": "device-001",
"timestamp": "2024-01-15T10:30:00Z",
"data": {
"device_info": {
"hostname": "android-phone",
"platform": "android",
"architecture": "aarch64",
"python_version": "3.11.0",
"role": "mobile"
},
"hardware_specs": {
"cpu_cores": 8,
"memory_total": 8589934592,
"disk_space": 128849018880,
"network_interfaces": ["wlan0"]
},
"capabilities": [
"computational",
"network_services",
"mobile_specific"
]
}
}
{
"message_type": "heartbeat",
"sender_id": "device-001",
"timestamp": "2024-01-15T10:31:00Z",
"data": {
"system_metrics": {
"cpu_usage": 25.5,
"memory_usage": 45.2,
"disk_usage": 12.8,
"load_average": [0.5, 0.3, 0.2],
"uptime": 86400
},
"status": "online",
"active_tasks": 2,
"last_activity": "2024-01-15T10:30:45Z"
}
}
{
"message_type": "task_assign",
"sender_id": "main-node",
"target_id": "device-001",
"timestamp": "2024-01-15T10:32:00Z",
"data": {
"task": {
"task_id": "task-abc123",
"task_type": "python_eval",
"priority": "normal",
"timeout": 300,
"payload": {
"expression": "sum(range(100000))",
"environment": {}
},
"requirements": {
"min_cpu_cores": 2,
"min_memory_gb": 1
}
}
}
}
{
"message_type": "task_result",
"sender_id": "device-001",
"timestamp": "2024-01-15T10:32:30Z",
"data": {
"task_id": "task-abc123",
"status": "completed",
"execution_time": 28.5,
"result": {
"output": "4999950000",
"exit_code": 0,
"metrics": {
"cpu_time": 25.2,
"memory_peak": 1048576
}
}
}
}
工作节点 主节点
│ │
│──── TCP连接 ──────────────►│
│◄─── 连接确认 ──────────────│
│ │
│──── 注册消息 ──────────────►│
│ │ ┌─────────────────┐
│ │ │ 验证设备 │
│ │ │ 存储元数据 │
│ │ │ 更新注册表 │
│ │ └─────────────────┘
│◄─── 注册确认 ──────────────│
│ │
│──── 心跳循环 ──────────────►│
主节点 工作节点
│ │
│ ┌─────────────────┐ │
│ │ 接收任务 │ │
│ │ 匹配设备 │ │
│ │ 检查资源 │ │
│ └─────────────────┘ │
│ │
│──── 任务分配 ─────────────►│
│ │ ┌─────────────────┐
│ │ │ 验证任务 │
│ │ │ 安全执行 │
│ │ │ 监控进度 │
│ │ └─────────────────┘
│◄─── 任务进度 ─────────────│
│◄─── 任务结果 ─────────────│
│ │
│ ┌─────────────────┐ │
│ │ 处理结果 │ │
│ │ 更新状态 │ │
│ │ 通知客户端 │ │
│ └─────────────────┘ │
工作节点 主节点 Web仪表板
│ │ │
│──── 心跳数据 ────►│ │
│ │ ┌─────────────────┐ │
│ │ │ 聚合指标 │ │
│ │ │ 更新状态 │ │
│ │ └─────────────────┘ │
│ │ │
│ │◄─── API请求 ─────│
│ │──── SSE事件 ────►│
│ │──── JSON/文本数据 ►│
┌─────────────────────────────────┐
│ 开发主机 │
│ │
│ ┌───────────┐ ┌─────────────┐ │
│ │主节点 │ │工作节点 │ │
│ │(终端1) │ │(终端2) │ │
│ └───────────┘ └─────────────┘ │
│ │
│ ┌─────────────────────────────┐ │
│ │ Web仪表板 │ │
│ │ (终端3/浏览器) │ │
│ └─────────────────────────────┘ │
└─────────────────────────────────┘
┌─────────────────────┐ ┌──────────────────┐
│ NAS服务器 │ │ 旧笔记本 │
│ │ │ │
│ ┌─────────────────┐ │ │ ┌──────────────┐ │
│ │ 主节点 │ │ │ │ 工作节点 │ │
│ │ (Docker) │ │◄───────►│ │ (原生) │ │
│ └─────────────────┘ │ │ └──────────────┘ │
│ │ └──────────────────┘
│ ┌─────────────────┐ │
│ │ Web仪表板 │ │ ┌──────────────────┐
│ │ (Docker) │ │ │ Android手机 │
│ └─────────────────┘ │ │ │
└─────────────────────┘ │ ┌──────────────┐ │
│ │ 工作节点 │ │
┌─────────────────────┐ │ │ (Termux) │ │
│ 树莓派 │ │ └──────────────┘ │
│ │ └──────────────────┘
│ ┌─────────────────┐ │
│ │ 工作节点 │ │ ┌──────────────────┐
│ │ (原生) │ │ │ 其他设备 │
│ └─────────────────┘ │ │ │
└─────────────────────┘ │ ┌──────────────┐ │
│ │ 工作节点 │ │
│ │ (原生) │ │
│ └──────────────┘ │
└──────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ NAS服务器 │
│ │
│ ┌────────────────────────────────────────────────────┐ │
│ │ Docker环境 │ │
│ │ │ │
│ │ ┌──────────────────────┐ ┌──────────────────┐ │ │
│ │ │ retire-cluster │ │ nginx │ │ │
│ │ │ main-node │ │ 反向代理 │ │ │
│ │ │ │ │ (可选) │ │ │
│ │ │ ┌──────────────────┐ │ │ │ │ │
│ │ │ │ 主节点 │ │ │ │ │ │
│ │ │ │ (端口 8080) │ │ │ │ │ │
│ │ │ └──────────────────┘ │ │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ ┌──────────────────┐ │ │ │ │ │
│ │ │ │ Web仪表板 │ │ │ │ │ │
│ │ │ │ (端口 5000) │ │ │ │ │ │
│ │ │ └──────────────────┘ │ │ │ │ │
│ │ └──────────────────────┘ └──────────────────┘ │ │
│ └────────────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────────────────┐ │
│ │ 持久化卷 │ │
│ │ /volume1/docker/retire-cluster/ │ │
│ │ ├── config/ (配置文件) │ │
│ │ ├── database/ (SQLite数据库) │ │
│ │ └── logs/ (应用程序日志) │ │
│ └────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
该架构为Retire-Cluster系统提供了坚实的基础,同时保持了未来增强和可扩展性需求的灵活性。