跳到主要内容

分层存储

通过自动冷热数据分层,可降低 60-80% 的存储成本。最新数据保留在速度快的本地存储中,而较旧的数据则移至经济高效的归档存储中。

概述

IEDB 采用两层存储模型:

│ 分层存储(Tiered Storage)

│ 热存储层(本地 / 主存储后端)
│ - 近期数据(可配置,默认:30 天)
│ - 针对低延迟查询进行了优化
│ - 成本:高

│ 按数据时间自动迁移

│ 冷存储层(S3 / MinIO)
│ - 历史数据(30 天以上)
│ - 针对成本效率进行了优化
│ - 成本:低

关键特性:

  • 基于数据生命周期的自动迁移 —— 超过时间阈值的数据会自动迁移至冷存储层
  • 指定数据库存储策略 —— 可为特定数据库覆盖全局默认策略
  • 仅热存储 —— 可将指定数据库完全排除在分层存储之外
  • 定时迁移机制 —— 基于 Cron 的调度器自动执行数据迁移
  • 手动迁移能力 —— 可通过 API 按需触发迁移操作
  • 零压缩开销 —— 文件原样迁移,无需重新编码或压缩
  • 透明化查询 —— 查询会自动跨越热存储与冷存储两层数据

配置

全局设置

[tiered_storage]
enabled = true
migration_schedule = "0 2 * * *" # Cron 表达式:每天凌晨 2 点执行
migration_max_concurrent = 4 # 并行传输文件数
migration_batch_size = 100 # 每批迁移的文件数
default_hot_max_age_days = 30 # 超过此时长的数据将移至冷存储层

存储后端

  • AWS S3 / MinIO
[tiered_storage.cold]
enabled = true
backend = "s3"
s3_bucket = "iedb-archive"
s3_region = "us-east-1"
s3_access_key = ""
s3_secret_key = ""
s3_use_ssl = true
s3_path_style = false
s3_storage_class = "GLACIER" # GLACIER, DEEP_ARCHIVE, GLACIER_IR, STANDARD_IA
retrieval_mode = "standard" # standard(标准), expedited(快速), bulk(批量)

存储类别:

存储类使用场景取回时间
STANDARD_IA不频繁访问,即时取回毫秒级
GLACIER_IR可即时取回的归档存储毫秒级
GLACIER长期归档(默认)数分钟至数小时
DEEP_ARCHIVE最低成本,极低访问频率最长 12 小时

数据库分层策略

default_hot_max_age_days覆盖特定数据库的全局设置,或将数据库完全排除在分层之外。

创建策略

curl -X POST http://localhost:8000/api/v1/tiering/policies \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"database": "telemetry",
"hot_max_age_days": 7
}'

创建热存储策略

将数据库从分层中排除:

curl -X POST http://localhost:8000/api/v1/tiering/policies \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"database": "realtime",
"hot_only": true
}'

策略列表

curl -H "Authorization: Bearer $TOKEN" \
http://localhost:8000/api/v1/tiering/policies

获取策略

curl -H "Authorization: Bearer $TOKEN" \
http://localhost:8000/api/v1/tiering/policies/telemetry

更新策略

curl -X PUT http://localhost:8000/api/v1/tiering/policies/telemetry \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"hot_max_age_days": 14}'

删除策略

curl -X DELETE http://localhost:8000/api/v1/tiering/policies/telemetry \
-H "Authorization: Bearer $TOKEN"

API 参考

所有分层接口都需要管理员身份验证。

获取分层状态

curl -H "Authorization: Bearer $TOKEN" \
http://localhost:8000/api/v1/tiering/status

响应:

{
"success": true,
"data": {
"enabled": true,
"cold_backend": "s3",
"default_hot_max_age_days": 30,
"migration_schedule": "0 2 * * *",
"last_migration": "2026-02-13T02:00:00Z"
}
}

按层级列出文件

# 查询所有文件
curl -H "Authorization: Bearer $TOKEN" \
http://localhost:8000/api/v1/tiering/files

# 按存储层过滤
curl -H "Authorization: Bearer $TOKEN" \
"http://localhost:8000/api/v1/tiering/files?tier=cold"

# 按数据库过滤
curl -H "Authorization: Bearer $TOKEN" \
"http://localhost:8000/api/v1/tiering/files?database=telemetry&limit=50"

手动迁移

curl -X POST http://localhost:8000/api/v1/tiering/migrate \
-H "Authorization: Bearer $TOKEN"

响应:

{
"success": true,
"data": {
"message": "Migration started",
"files_eligible": 42
}
}

获取迁移统计信息

curl -H "Authorization: Bearer $TOKEN" \
http://localhost:8000/api/v1/tiering/stats

响应:

{
"success": true,
"data": {
"total_files_migrated": 1250,
"total_bytes_migrated": 53687091200,
"hot_files": 340,
"cold_files": 1250,
"last_migration_duration_ms": 45000,
"last_migration_files": 42
}
}

最佳实践

  • 从 30 天热存储保留期开始 —— 这对大多数工作负载来说都是不错的默认值。请监控查询模式,并根据历史数据的访问频率进行调整。
  • 使用 GLACIER 进行长期归档 —— 它在成本和取回时间之间提供了最佳平衡。如果冷数据需要频繁访问,请使用 STANDARD_IA。
  • 在非高峰时段安排迁移任务 —— 默认的 0 2 * * *(每天凌晨 2 点)适用于大多数部署场景。
  • 为不同的需求设置数据库的策略 —— 实时仪表盘可能只需保留 7 天的热数据,而合规性数据库可能需要 90 天。
  • 将实时数据库标记为“热存储” —— 专用于实时仪表盘的数据库应跳过分层存储,以避免任何取回延迟。
  • 使用 IAM 角色或托管身份 —— 在云环境中,建议使用 IAM 角色,而不是访问密钥。

后续步骤