0%

DDS高可用行情发布QoS设计实践

DDS高可用行情发布QoS设计实践

用DDS内置机制实现主备切换,不用自己写故障检测,它不香吗?

背景

金融行情系统最怕什么?——宕机!传统方案要自己写主备切换、故障检测,代码写得头皮发麻。

其实,DDS自带了一套”主备切换”机制,配置几个QoS就能搞定的事儿,干嘛非要自己造轮子?

场景描述

1
2
3
4
5
6
7
8
9
10
11
12
13
┌─────────────────┐      ┌─────────────────┐
│ Publisher A │ │ Publisher B │
│ (strength=100) │ │ (strength=50) │
│ 🦸 主发布者 │ │ 🔒 备份发布者 │
└────────┬────────┘ └────────┬────────┘
│ │
└────────┬───────────────┘


┌────────────────┐
│ DataReader │
│ 谁强听谁的 👂 │
└────────────────┘

这就是DDS的”选秀机制”——谁强谁上场!

核心QoS配置

1. Ownership - 排他性所有权 🎯

这是核心中的核心!告诉DDS:同一个主题只能有一个”选手”出道。

1
2
3
4
5
// 发布端
writer_qos.ownership.kind = DDS::EXCLUSIVE_OWNERSHIP_QOS;

// 订阅端也要配!
reader_qos.ownership.kind = DDS::EXCLUSIVE_OWNERSHIP_QOS;

效果:多个发布者同时发数据?没问题,DDS会自动屏蔽弱的,只接收最强的那个。

2. Ownership Strength - 发布强度 💪

给每个发布者打个”实力分”,分数高的上位!

1
2
3
4
5
// 主发布者:满分选手 👑
writer_qos.ownership_strength.value = 100;

// 备份:及格水平 👶
writer_qos.ownership_strength.value = 50;

3. Reliability - 可靠性 📦

行情数据不能丢!配置这个,保证数据完整到达。

1
2
writer_qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS;
reader_qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS;

4. History - 历史记录 🎒

行情只需要最新的一条,旧数据留着干嘛?占内存嘛!

1
2
3
4
5
writer_qos.history.kind = DDS::KEEP_LAST_HISTORY_QOS;
writer_qos.history.depth = 1; // 留一条就够了

reader_qos.history.kind = DDS::KEEP_LAST_HISTORY_QOS;
reader_qos.history.depth = 1;

完整配置示例

发布端 (BA_Publish)

1
2
3
4
5
6
7
8
9
10
11
DDS::DataWriterQos writer_qos;
publisher->get_default_datawriter_qos(writer_qos);

// 🦾 核心:高可用铁三角
writer_qos.ownership.kind = DDS::EXCLUSIVE_OWNERSHIP_QOS;
writer_qos.ownership_strength.value = strength_; // 从配置读,灵活!

// 可靠性 + 轻装上阵
writer_qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS;
writer_qos.history.kind = DDS::KEEP_LAST_HISTORY_QOS;
writer_qos.history.depth = 1;

订阅端 (BA_Subscribe)

1
2
3
4
5
6
7
8
DDS::DataReaderQos reader_qos;
subscriber->get_default_datareader_qos(reader_qos);

// 跟着配就对了
reader_qos.ownership.kind = DDS::EXCLUSIVE_OWNERSHIP_QOS;
reader_qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS;
reader_qos.history.kind = DDS::KEEP_LAST_HISTORY_QOS;
reader_qos.history.depth = 1;

配置文件

1
2
3
4
5
6
7
{
"pub": {
"strength": 100,
"symbol_list": ["btcusdt"],
"frame_list": ["1m", "5m", "30m", "4h", "1d", "1w"]
}
}

主发布者 strength: 100,备份 strength: 50。主驾崩后,备份自动上位——就问你帅不帅?

其他推荐QoS

QoS 推荐值 理由
durability VOLATILE 实时行情要啥持久化?延迟优先!
latency_budget 0 行情要的就是快!
transport_priority 最高 行情数据插队先走!
liveliness AUTOMATIC DDS帮你盯着发布者还活着没
deadline 看帧率 发慢了马上报警
destination_order BY_SOURCE_TIMESTAMP 谁先发谁先到,别乱序

总结

搞定高可用很难吗?就4个QoS的事儿!

1
EXCLUSIVE_OWNERSHIP + OWNERSHIP_STRENGTH = 自动主备切换

不用自己写故障检测,不用写心跳,不用写切换逻辑——DDS全帮你包了!

好处:

  • 简单 - DDS原生支持,配置就行
  • 靠谱 - 自动切,订阅端无感知
  • 无单点 - 多个发布者,谁强谁上

这就是”让专业的人做专业的事”——DDS handles everything.


参考