高可用

时刻在线,不错过任何一拍。MynaDB 支持主从复制以实现高可用性和容错能力。 主节点处理所有写入操作,同时将数据实时复制到一个或多个从节点。 如果主节点发生故障,可以提升从节点接管服务。

💡 核心优势
  • 容错能力:即使主节点宕机,仍可继续提供读取服务
  • 读取扩展:将读查询分散到从节点
  • 数据安全:实时复制确保数据持久性
  • 零停机故障转移:将从节点提升为主节点,不丢失数据

架构概览

高可用系统采用简单的主从架构,通过 WAL(预写日志)流进行复制:

                    +-----------------+
                    |     Clients     |
                    +--------+--------+
                             |
              +--------------+--------------+
              |                             |
      +-------v-------+             +-------v-------+
      |    MASTER     |             |     SLAVE     |
      |   (Active)    |------------>|   (Standby)   |
      |               | WAL Stream  |               |
      +-------+-------+             +-------+-------+
              |                             |
      +-------v-------+             +-------v-------+
      |  Parquet      |             |  Parquet      |
      |  Files        |             |  Files        |
      +---------------+             +---------------+
        

工作原理

  1. 写入路径:所有写入都发送到主节点
  2. WAL 复制:主节点通过 TCP 将 WAL 条目流式传输到已连接的从节点
  3. 从节点回放:从节点将 WAL 条目应用到本地存储
  4. 心跳检测:主节点定期发送心跳;从节点监控故障
  5. 故障转移:主节点故障时,从节点可被提升为主节点(手动或自动)

配置

主节点配置

config-master.toml
[server]
http_port = 7000
flight_port = 7001

[storage]
data_dir = "/data/master"

[ha]
enabled = true
role = "master"
node_id = "master-01"

[ha.replication]
mode = "async"                    # "async" or "sync"
listen_addr = "0.0.0.0:7003"      # Replication port

[ha.heartbeat]
interval_secs = 5                 # Send heartbeat every 5 seconds
timeout_secs = 15                 # Slave timeout threshold

从节点配置

config-slave.toml
[server]
http_port = 7100                  # Different port from master
flight_port = 7101

[storage]
data_dir = "/data/slave"

[ha]
enabled = true
role = "slave"
node_id = "slave-01"

[ha.replication]
master_addr = "master-host:7003"  # Master's replication address
reconnect_interval_secs = 5       # Retry interval on disconnect

[ha.heartbeat]
interval_secs = 5
timeout_secs = 15

[ha.failover]
mode = "manual"                   # "manual", "automatic", or "disabled"
failure_threshold = 3             # Missed heartbeats before failover
promotion_delay_secs = 10         # Wait before auto-promotion

配置选项

选项 描述 默认值
ha.enabled 启用高可用模式 false
ha.role 节点角色:"master" 或 "slave" "master"
ha.node_id 当前节点的唯一标识符 "node-{pid}"
ha.replication.mode "async"(更快)或 "sync"(更安全) "async"
ha.replication.listen_addr 主节点:监听从节点连接的地址 "0.0.0.0:7003"
ha.replication.master_addr 从节点:主节点的复制地址 必填
ha.failover.mode "manual"、"automatic" 或 "disabled" "automatic"

高可用 API 端点

GET /ha/status

返回当前节点的高可用状态。

bash
curl http://localhost:7000/ha/status

响应:

{
  "enabled": true,
  "node_id": "master-01",
  "role": "master",
  "state": "running",
  "replication": {
    "role": "master",
    "current_sequence": 12345,
    "slave_count": 1,
    "connected": true
  },
  "promotion": {
    "role": "master",
    "state": "running",
    "can_promote": false,
    "failover_mode": "manual"
  }
}

POST /ha/promote

手动将从节点提升为主节点(需要确认)。

bash
curl -X POST http://localhost:7100/ha/promote \
  -H "Content-Type: application/json" \
  -d '{"confirm": true}'

响应:

{
  "status": "success",
  "message": "Promoted to master",
  "new_role": "master"
}

运维指南

启动集群

bash
# Terminal 1: Start master
./tsdb-stats -c config-master.toml

# Terminal 2: Start slave
./tsdb-stats -c config-slave.toml

# Verify connection
curl http://localhost:7000/ha/status | jq '.replication.slave_count'
# Should return: 1

写入数据

始终向主节点写入数据。向从节点写入会被拒绝:

# Write to master (succeeds)
curl -X POST http://localhost:7000/write/mydb \
  -d 'cpu,host=server1 usage=75.5'

# Write to slave (fails with 503)
curl -X POST http://localhost:7100/write/mydb \
  -d 'cpu,host=server1 usage=75.5'
# Error: This node is a slave; writes must be sent to the master

读取数据

主节点和从节点都可以提供读查询服务:

# Query master
curl -X POST http://localhost:7000/query/mydb \
  -d 'SELECT * FROM cpu LIMIT 10'

# Query slave (same data after replication)
curl -X POST http://localhost:7100/query/mydb \
  -d 'SELECT * FROM cpu LIMIT 10'

手动故障转移

当主节点不可用时,提升从节点:

# 1. Check slave status
curl http://localhost:7100/ha/status | jq '{role, state, can_promote: .promotion.can_promote}'

# 2. Promote slave to master
curl -X POST http://localhost:7100/ha/promote \
  -H "Content-Type: application/json" \
  -d '{"confirm": true}'

# 3. Verify new role
curl http://localhost:7100/ha/status | jq '.role'
# Should return: "master"

# 4. Update your application to point to the new master

复制模式

异步复制(默认)

同步复制

[ha.replication]
mode = "sync"
timeout_ms = 5000  # Wait up to 5 seconds for slave ACK

故障转移模式

模式 描述 使用场景
manual 运维人员必须调用 /ha/promote API 有人值守的生产环境
automatic 从节点检测到主节点故障后自动提升 无人值守的运行环境
disabled 不允许提升 只读副本

本地测试

在单台机器上测试主从模式:

bash
# Use the included test script
./scripts/ha_setup_local.sh setup   # Create configs
./scripts/ha_setup_local.sh master  # Terminal 1
./scripts/ha_setup_local.sh slave   # Terminal 2
./scripts/ha_setup_local.sh test    # Terminal 3 - Run tests

# Or run automated tests
./scripts/test_ha_local.sh

监控

需要监控的关键指标

# Monitor script example
while true; do
  echo "=== Master ==="
  curl -s http://master:7000/ha/status | jq '{seq: .replication.current_sequence, slaves: .replication.slave_count}'

  echo "=== Slave ==="
  curl -s http://slave:7100/ha/status | jq '{seq: .replication.last_sequence, connected: .replication.connected, lag: .replication.seconds_since_heartbeat}'

  sleep 5
done

最佳实践

⚠ 生产环境建议
  • 网络:主从节点之间使用低延迟网络
  • 存储:每个节点使用独立的存储卷
  • 监控:设置复制延迟和连接状态的告警
  • 备份:定期从从节点备份数据
  • 测试:定期测试故障转移流程

部署拓扑

故障排除

从节点无法连接

复制延迟

提升失败

另请参阅