fix(server): 显式 target_hosts 时主机指向用户指定的主节点

- 显式列表校验通过后令 host = nodes[0],task.TargetHost/IP、台账、
  CloudDM 注册与凭据地址不再指向此前默认挑选的空闲主机
- 显式列表解析抽为纯函数 resolvePinnedPrimaryReplicaNodes,
  行为与原内联逻辑一致(校验失败终态/单机上限 defer 重试)
- 新增测试覆盖主节点非池首空闲节点及数量/池外/重复/占满场景
This commit is contained in:
Hungerdream
2026-07-31 18:49:34 +08:00
parent 454df86021
commit 95c3e98611
2 changed files with 72 additions and 26 deletions
+43 -26
View File
@@ -335,6 +335,40 @@ func hostTaskCount(occupied []string, name string) int {
return count
}
// resolvePinnedPrimaryReplicaNodes 按用户显式指定的有序 target_hosts 解析 primary_replica 节点列表,
// 首节点为主节点。返回值:节点列表、参数校验失败原因(终态)、延后调度错误(defer: 前缀,可重试)。
func resolvePinnedPrimaryReplicaNodes(hosts []targetHost, requested []string, occupied []string, limit int, nodeCount int64, pinnedPrimary string) ([]*targetHost, string, error) {
if int64(len(requested)) != nodeCount {
return nil, fmt.Sprintf("primary_replica requires %d target_hosts, got %d", nodeCount, len(requested)), nil
}
nodes := make([]*targetHost, 0, len(requested))
seen := map[string]struct{}{}
for index, requestedHost := range requested {
var selected *targetHost
for i := range hosts {
if hosts[i].Name == requestedHost {
selected = &hosts[i]
break
}
}
if selected == nil {
return nil, fmt.Sprintf("target_hosts[%d] %q is not in the candidate host pool", index, requestedHost), nil
}
if _, exists := seen[selected.Name]; exists {
return nil, fmt.Sprintf("target_hosts contains duplicate host %q", selected.Name), nil
}
if hostTaskCount(occupied, selected.Name) >= max(limit, 1) {
return nil, "", fmt.Errorf("defer: pinned host %s reached the per-host instance limit %d", selected.Name, max(limit, 1))
}
seen[selected.Name] = struct{}{}
nodes = append(nodes, selected)
}
if pinnedPrimary != "" && nodes[0].Name != pinnedPrimary {
return nil, "target_host must match the first primary_replica target_hosts entry", nil
}
return nodes, "", nil
}
func topologyNodeCount(input MySQLDeliveryInput) int64 {
if input.Topology == "primary_replica" {
replicas := input.ReplicaCount
@@ -1757,34 +1791,17 @@ func (s *DeliveryService) claimAndReserve(ctx context.Context) (*model.DeliveryT
nodes := []*targetHost{host}
if nodeCount > 1 {
if len(payload.TargetHosts) > 0 {
if int64(len(payload.TargetHosts)) != nodeCount {
return s.failInTransaction(tx, &task, model.TaskValidationFailed, fmt.Sprintf("primary_replica requires %d target_hosts, got %d", nodeCount, len(payload.TargetHosts)))
pinned, validationMsg, deferErr := resolvePinnedPrimaryReplicaNodes(meta.Hosts, payload.TargetHosts, occupied, limit, nodeCount, payload.TargetHost)
if deferErr != nil {
return deferErr
}
nodes = nodes[:0]
seen := map[string]struct{}{}
for index, requestedHost := range payload.TargetHosts {
var selected *targetHost
for i := range meta.Hosts {
if meta.Hosts[i].Name == requestedHost {
selected = &meta.Hosts[i]
break
}
}
if selected == nil {
return s.failInTransaction(tx, &task, model.TaskValidationFailed, fmt.Sprintf("target_hosts[%d] %q is not in the candidate host pool", index, requestedHost))
}
if _, exists := seen[selected.Name]; exists {
return s.failInTransaction(tx, &task, model.TaskValidationFailed, fmt.Sprintf("target_hosts contains duplicate host %q", selected.Name))
}
if hostTaskCount(occupied, selected.Name) >= max(limit, 1) {
return fmt.Errorf("defer: pinned host %s reached the per-host instance limit %d", selected.Name, max(limit, 1))
}
seen[selected.Name] = struct{}{}
nodes = append(nodes, selected)
}
if payload.TargetHost != "" && nodes[0].Name != payload.TargetHost {
return s.failInTransaction(tx, &task, model.TaskValidationFailed, "target_host must match the first primary_replica target_hosts entry")
if validationMsg != "" {
return s.failInTransaction(tx, &task, model.TaskValidationFailed, validationMsg)
}
// 显式列表以首节点为主节点:覆盖此前默认挑选的空闲主机,
// 避免 task.TargetHost/IP 与台账、凭据指向未部署节点。
nodes = pinned
host = nodes[0]
} else {
for i := range meta.Hosts {
candidate := &meta.Hosts[i]