基于阿里云抢占式实例的“费用节省报告”Grafana 模板 JSON

解读

面试官把“Storm 实时计算”与“云上成本治理”两个看似无关的点拧在一起,考察的是候选人能否把流式框架的思维迁移到 FinOps(云成本运营)场景。
在国内实际生产中,Storm 集群往往跑在阿里云抢占式实例(Spot)上,利用其 10%~20% 的按需价换取 70% 成本节省,但 Spot 实例 5 分钟释放预警会导致 Worker 频繁掉线。
因此,企业需要一张实时 Grafana 大屏:

  1. 秒级汇总每个租户/拓扑在 Spot 与按需实例上的消耗;
  2. 实时计算“节省金额 = 按需价 - 实际价”;
  3. 当节省率低于阈值或 Spot 回收率高于阈值时,通过 Storm Bolt 调用钉钉、飞书或阿里云事件总线触发扩容,保证 SLA 的同时把成本压到最低。
    面试官让你“写 JSON”并不是考语法,而是看你是否能把 Storm 的实时度量体系(Metrics2 接口→Logstash→阿里云 SLS→Grafana)与抢占式实例的账单数据(CUR/费用 API)拼成一张可落地的模板,并解释清楚每条查询背后的流式计算逻辑。

知识点

  1. Storm Metrics2 框架:IMetricsConsumer 自定义 Consumer,把 Task 级 CPU、内存、延迟、吞吐写入外部系统。
  2. 阿里云抢占式实例元数据:
    • 实例元数据服务(metadata)提供 spot/ondemand 类型、中断通知时间戳;
    • 费用与账单 API(DescribeInstanceBill、DescribeSpotPriceHistory)可拉取实时单价。
  3. Grafana 变量与 Query:
    • 使用 Aliyun SLS 数据源,SQL 语法支持流式窗口;
    • 变量 region、instance_type、topology_id 联动下拉框。
  4. 实时节省计算:
    • 节省金额 = ∑(ondemand_unit_price × usage_seconds) − ∑(spot_unit_price × usage_seconds);
    • 窗口用 tumble(time, 1m) 对齐 Storm 的 60 s Metrics 周期。
  5. 告警通道:Grafana Alert → Webhook → Storm REST API(/api/v1/topology/resize)或阿里云函数计算,实现弹性扩容。
  6. 国内合规:费用数据属敏感信息,需走阿里云内网 VPC Endpoint,避免公网泄露;RAM 账号仅授予 ReadOnlyAccess 与 SLS 查询权限。

答案

以下给出可直接导入 Grafana 的 JSON 骨架(已脱敏),并逐段解释 Storm 侧如何产生数据、Grafana 侧如何消费。
(为符合纯文本要求,JSON 用缩进描述,不含图片、链接、表格)

{
  "dashboard": {
    "title": "Storm-Spot 费用节省实时大屏",
    "timezone": "Asia/Shanghai",
    "refresh": "30s",
    "time": {
      "from": "now-1h",
      "to": "now"
    },
    "variables": [
      {
        "name": "region",
        "type": "query",
        "query": "select distinct region from sls_log"
      },
      {
        "name": "topology_id",
        "type": "query",
        "query": "select distinct topology_id from sls_log where region='$region'"
      }
    ],
    "panels": [
      {
        "title": "实时节省金额(元)",
        "type": "stat",
        "targets": [
          {
            "datasource": "SLS",
            "query": "
              select 
                round(
                  sum(
                    case when instance_charge_type='PrePaid' then 0
                         else (spot_ondemand_price - spot_price) * usage_seconds / 3600.0
                    end
                  ), 2
                ) as save_amount
              from sls_log
              where region='$region' and topology_id='$topology_id'
                and __time__ >= to_unixtime(now()) - 3600
            "
          }
        ],
        "fieldConfig": {
          "defaults": {
            "unit": "currencyCNY",
            "color": {
              "mode": "thresholds",
              "steps": [
                {"value": 0, "color": "red"},
                {"value": 100, "color": "green"}
              ]
            }
          }
        },
        "alert": {
          "conditions": [
            {
              "query": {"params": ["A", "5m", "now"]},
              "reducer": {"type": "last", "params": []},
              "evaluator": {"params": [50], "type": "lt"}
            }
          ],
          "executionErrorState": "alerting",
          "notifications": [
            {"uid": "dingding_webhook_uid"}
          ]
        }
      },
      {
        "title": "Spot 回收预警次数",
        "type": "graph",
        "targets": [
          {
            "datasource": "SLS",
            "query": "
              select 
                __time__ as time,
                sum(case when event_name='SpotInstanceInterrupted' then 1 else 0 end) as recycle_cnt
              from sls_log
              where region='$region' and topology_id='$topology_id'
              group by tumble(__time__, 1m)
              order by time desc
              limit 60
            "
          }
        ],
        "yAxes": {
          "left": {"unit": "short", "min": 0}
        }
      }
    ]
  }
}

Storm 侧配套代码(核心逻辑,不含依赖包):

  1. 实现 IMetricsConsumer 接口,在 handleDataPoints() 里把 TaskMetrics 序列化为 JSON,写入 Logstash TCP 端口;
  2. 每台 Supervisor 启动脚本里,每分钟调用 curl http://100.100.100.200/latest/meta-data/instance/instance-type 与 spot/termination-time,把元数据拼到日志;
  3. Logstash 通过阿里云 SLS Output 插件直接打到指定 logstore,index 字段包含 region、topology_id、instance_charge_type、spot_price、spot_ondemand_price、usage_seconds。

拓展思考

  1. 如果 Spot 价格瞬间高于按需价,如何反向触发 Storm 拓扑降级(降低并行度)而不是扩容?
    答:在 Grafana Alert 里增加一条“spot_price > ondemand_price”条件,通过 Webhook 调用自研降级 Bolt,该 Bolt 向 ZooKeeper 写入 /storm/downgrade 节点,Nimbus 监听后自动调整并行度并重启 Worker。
  2. 国内双 11 大促 0 点流量洪峰,Spot 回收率 90% 以上,如何保证 Exactly-Once 语义不丢数据?
    答:Storm 1.x 本身仅支持 At-Least-Once;需要借助 Kafka 事务 + 幂等 Bolt,在 Spot 回收前 5 分钟通过 metadata 信号触发 Checkpoint,将偏移量与状态写入阿里云 Tablestore,新实例启动后从 Tablestore 恢复。
  3. 多租户场景下,如何防止 A 租户 Spot 实例被回收却由 B 租户承担 SLA 惩罚?
    答:在 Grafana 变量里加入 tenant_id,节省金额按 tenant 分组;同时在 Storm 侧为每个租户单独设置 ResourceAwareScheduler 的 priority 与 weight,确保回收时优先杀掉低优先级租户的任务,把 SLA 违约成本算到对应租户账上。