模块定位
本模块是 InfluxDB 3 Core 进程的入口与编排中枢。influxdb3 crate 是二进制入口(main.rs 仅 6 行),influxdb3_startup 负责启动前兼容层,influxdb3_clap_blocks 用 clap derive 定义所有配置块。serve_main(influxdb3/src/lib.rs)按固定顺序装配 catalog → write → cache → query → processing engine → authz → server 共 8 个子系统——装配顺序本身就是系统正确性的保证(上游必须先于下游构造)。本模块的职责边界是”进程生命周期管理”:从 CLI 解析、配置加载、对象装配到优雅关闭,不涉及任何业务逻辑。
模块架构
模块内部按生命周期阶段切分:启动前(influxdb3_startup 的 env 兼容与早期日志)→ CLI 解析(influxdb3 的 Config + Command enum + 自定义 help)→ serve 装配(commands/serve.rs 的 command() 函数)→ 运行循环(futures::select! 四路等待)→ 关闭(influxdb3_shutdown 的 ShutdownManager)。influxdb3_clap_blocks 是横切配置库,被 serve.rs 和 influxdb3_commands 共享,用 #[clap(flatten)] 组合 ObjectStoreConfig/TokioIoConfig/ProcessingEngineConfig 等结构体。
调用链路
main() in influxdb3/src/main.rs:4
└─ startup(args) in lib.rs:172
├─ rustls ring crypto provider 安装
├─ install_crash_handler()(SIGSEGV → 栈迹到 stderr)
├─ load_dotenv() + env_compat::copy_env_aliases(ENV_ALIASES)(36 对别名)
├─ maybe_print_help()(手写 help,禁用 clap 内置)
├─ Config::command().get_matches_from(args) → Config::from_arg_matches
└─ [Serve] serve_main(serve_config, matches, runtime_config) in lib.rs:330
├─ tokio_runtime.block_on(async {
│ ├─ init_logs_and_tracing(...)
│ └─ commands::serve::command(serve_config, user_params).await ← serve.rs:937
├─ })
└─ [非 Serve] non_serve_main(other, runtime_config) ← lib.rs:466
command() 内的装配顺序(serve.rs:937-1608):
1-2. resolve_legacy_size_options / get_node_id
3-5. metric registry ×2(主 + write_path 独立)/ panic handler / shutdown token
6-7. time_provider / SysEventStore
8. object_store 包装链(base → ObjectStoreMetrics → parquet_cache)
9-10. trace_exporter / ParquetStorage
11-12. 双 DataFusion Executor(exec 主查询 20% 内存 / write_path_executor usize::MAX)
13-15. trace_header_parser / table_index_cache / Persister
16. Catalog::new_with_shutdown()(含 catalog_uuid + limits)
17-20. table_index_cache / admin_token / cli_params / catalog.register_node
21-24. last_cache / distinct_cache / gen1_duration / wal_config
25. WriteBufferImpl::new()(持有 persister/catalog/wal/cache/executor)
26-28. persisted_files / deleter / background_buffer_checker
29-31. processing_engine_env / telemetry / telemetry_store
32-33. write_buffer 类型擦除为 Arc<dyn WriteBuffer> / CommonServerState
34. QueryExecutorImpl::new()
35-36. TcpListener::bind(:8181) / 可选 admin token recovery listener
37-40. ProcessingEngineManagerImpl / set_processing_engine / start_triggers / wal.add_file_notifier
41-43. authorizer(TokenAuthenticator 或 NoAuthAuthenticator)/ HttpApi::new / Server::new
44. futures::select!{ signal | backend | frontend | recovery_frontend }
方法速查表
| 方法名 | 一行职责 | 关键设计决策 |
|---|---|---|
startup in lib.rs:172 |
CLI 入口,启动前准备 + 分发 | env alias 必须在 clap 前 |
serve_main in lib.rs:330 |
构造 tokio runtime + 调 serve::command | 独立 runtime 隔离 |
command in serve.rs:937 |
44 步子系统装配 | 顺序即正确性 |
maybe_print_help in lib.rs:551 |
手写 help 输出 | 禁用 clap help 以支持分组 |
extract_user_params in lib.rs:907 |
提取用户参数做 telemetry | ValueSource 过滤默认值 + 脱敏 |
get_node_id in serve.rs |
解析 node-id(必填组约束) | #[group(required=true)] |
核心实现
Command enum 与 CLI 子命令分发
influxdb3/src/lib.rs:125 定义了 Command enum,通过 clap::Subcommand derive 将 12 个子命令(Serve/Create/Delete/Query/Write/Enable/Disable/Install/Debug/Show/Test/Update)映射到各自的 Config struct。关键设计:command: Option<Command> 是 Option 而非直接 Command——当用户直接运行 influxdb3 不带子命令时,startup() 的 None 分支(lib.rs:223)自动注入 serve 子命令并标记 --serve-invocation-method QuickStart,让 telemetry 区分显式与隐式调用,同时用 FlagCaseActions 尝试两种策略自动补全 --node-id。
serve_main 的 44 步装配与依赖注入
commands::serve::command()(serve.rs:937)是整个进程的装配中心。装配顺序严格遵循依赖拓扑:catalog 必须先于 write_buffer(write 依赖 catalog 的 schema),write_buffer 必须先于 query_executor(query 通过 write_buffer 读 chunks),processing_engine 必须在 query/write 之后(它持有 InProcessQueryEndpoint/InProcessWriteEndpoint 引用它们)。依赖通过 Arc<dyn Trait> 共享——Arc<dyn WriteBuffer>、Arc<dyn AuthProvider>、Arc<dyn PythonEnvironmentManager> 等,具体实现注入到需要它们的组件,实现解耦。
双 DataFusion Executor 读写分离
serve.rs:1055 与 serve.rs:1082 构造了两个 Executor:主查询 executor 内存池 20%(exec_mem_pool_size),写路径 executor 内存池 usize::MAX。两者用独立的 metrics registry——serve.rs:1081 注释明确指出共享 registry 会 panic。写路径不限制内存是因为持久化操作需要尽可能多的内存。两个 DedicatedExecutor 线程池(名为 datafusion 与 datafusion_write_path)隔离读写,避免互相阻塞。
优雅关闭的四路 select
serve.rs:1488-1608 用 futures::select!(而非 tokio::select!)等待四路 future:wait_for_signal(SIGINT/SIGTERM)、shutdown_manager.join()(后台任务)、serve(server, ...)(前端)、serve_admin_token_recovery_endpoint(恢复端点)。serve.rs:1466 注释解释为何选 futures::select——它要求 FusedFuture(.fuse()),可在循环中安全重复 poll,而 tokio::select 会 take ownership 不适合循环。恢复端点未启用时注入 Either::Right(pending())(永不完成的 future),避免 select 分支被误触发。
设计模式
| 模式 | 位置 | 为什么用 |
|---|---|---|
| 命令模式 | Command enum lib.rs:127 + 分发 lib.rs:221 |
将 CLI 请求封装为对象,enum 分发 |
| Builder | ProcessingEngineManagerOptions serve.rs:1383 |
链式配置可选参数,不改构造签名 |
| 依赖注入 | Arc<dyn Trait> 共享 serve.rs:1330/1407 |
解耦子系统实现 |
| 策略 | NoAuthAuthenticator vs TokenAuthenticator serve.rs:1407 |
配置驱动选择认证实现 |
| 组合 | ObjectStore 包装链 serve.rs:1009-1038 |
装饰器层层组合 store 功能 |
扩展方式
- 新增 CLI 子命令:
lib.rs的Commandenum 加变体 +non_serve_mainmatch 加分发 + 新建commands/xxx.rs;help 文本文件更新。 - 新增 serve 配置选项:
serve.rs的Configstruct 加#[clap(...)]字段 +command()读取;若有弃用旧名在influxdb3_startup/src/env_compat.rs的ENV_ALIASES加别名;敏感参数加到serve/cli_params.rs的SENSITIVE_PARAMS。 - 新增子系统装配:在
command()依赖拓扑的正确位置插入构造,构造参数用Arc::clone注入下游CreateQueryExecutorArgs/HttpApi::new。