修复混拨配置无法更新的问题

This commit is contained in:
2025-09-11 16:06:14 +08:00
parent b71c41424b
commit d70bdaae16
3 changed files with 59 additions and 40 deletions

View File

@@ -63,43 +63,40 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
if err != nil {
return fmt.Errorf("获取所有配置失败:%w", err)
}
var findConfig = make(map[string]model.Config)
for _, config := range configs {
findConfig[config.Cityhash+":"+config.GatewayMac] = config
}
println(fmt.Sprintf("获取配置:%v", time.Since(step)))
step = time.Now()
// 查找需要更新的配置
oldConfigs := make(map[model.City][]model.Config)
newConfigs := make(map[model.Gateway][]ConfigInfo)
for _, config := range configs {
city := findCity[config.Cityhash]
gateway := findGateway[config.GatewayMac]
if config.IsChange == 1 {
oldConfigs[city] = append(oldConfigs[city], config)
} else {
newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{
Change: false,
Remote: jd.EdgeInfo{
Mac: config.EdgeMac,
City: config.Cityhash,
Network: config.Network,
},
})
}
}
println(fmt.Sprintf("查找更新:%v", time.Since(step)))
step = time.Now()
// 更新网关配置
var newConfigs = make(map[model.Gateway][]ConfigInfo)
var citiesUpdate []model.City
var changesCreate []model.Change
for _, city := range cities {
// 先处理不变更的节点
configsUpdate := make([]model.Config, 0)
for _, gateway := range gateways {
oldConfig, exists := findConfig[city.Hash+":"+gateway.Macaddr]
if exists && oldConfig.IsChange != 1 {
newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{
Change: false,
Remote: jd.EdgeInfo{
Mac: oldConfig.EdgeMac,
City: oldConfig.Cityhash,
},
})
} else {
configsUpdate = append(configsUpdate, oldConfig)
}
}
count := len(configsUpdate)
if count == 0 {
continue
}
for city, configs := range oldConfigs {
// 如果有需要变更的节点,获取足量新节点
count := len(configs)
if count > city.EdgesCount {
slog.Warn(fmt.Sprintf("城市节点数量不足,跳过本次更新,城市:%s节点数%d网关数%d", city.Name, city.EdgesCount, count))
continue
@@ -123,21 +120,21 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
})
// 分配新节点
for i, oldConfig := range configsUpdate {
gateway := findGateway[oldConfig.GatewayMac]
for i, config := range configs {
gateway := findGateway[config.GatewayMac]
edge := edges[i]
slog.Debug(fmt.Sprintf("网关配置变更,网关:%s旧节点%s新节点%s", gateway.Macaddr, oldConfig.EdgeMac, edge.Macaddr))
slog.Debug(fmt.Sprintf("网关配置变更,网关:%s旧节点%s新节点%s", gateway.Macaddr, config.EdgeMac, edge.Macaddr))
newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{
Change: true,
Remote: jd.EdgeInfo{
City: city.Hash,
Mac: edge.Macaddr,
Mac: edge.Macaddr,
City: city.Hash,
Network: config.Network,
},
Config: model.ConfigUpdate{
Id: oldConfig.Id,
Id: config.Id,
EdgeMac: u.P(edge.Macaddr),
IsChange: u.P(0),
IsOnline: u.P(1),
@@ -147,13 +144,13 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
Time: now,
CityId: city.Id,
Gateway: gateway.Macaddr,
OldEdge: oldConfig.EdgeMac,
OldEdge: config.EdgeMac,
NewEdge: edge.Macaddr,
})
}
}
println(fmt.Sprintf("循环分配%v", time.Since(step)))
println(fmt.Sprintf("更新配置%v", time.Since(step)))
step = time.Now()
// 更新城市偏移量
@@ -214,6 +211,8 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
println(fmt.Sprintf("提交配置:%v", time.Since(step)))
println(fmt.Sprintf("总耗时:%v", time.Since(now)))
println(fmt.Sprint("更新总数:", len(changesCreate)))
return nil
}