修复节点更新问题

This commit is contained in:
2025-09-11 00:08:11 +08:00
parent 1900f5e82d
commit 140235d069
2 changed files with 17 additions and 14 deletions

View File

@@ -73,7 +73,8 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
// 更新网关配置 // 更新网关配置
var newConfigs = make(map[model.Gateway][]ConfigInfo) var newConfigs = make(map[model.Gateway][]ConfigInfo)
var changes []model.Change var citiesUpdate []model.City
var changesCreate []model.Change
for _, city := range cities { for _, city := range cities {
@@ -85,7 +86,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{ newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{
Change: false, Change: false,
Remote: jd.EdgeInfo{ Remote: jd.EdgeInfo{
Mac: oldConfig.Macaddr, Mac: oldConfig.EdgeMac,
City: oldConfig.Cityhash, City: oldConfig.Cityhash,
}, },
}) })
@@ -94,18 +95,17 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
} }
} }
count := len(configsUpdate) count := len(configsUpdate)
if len(configsUpdate) == 0 { if count == 0 {
continue continue
} }
// 如果有需要变更的节点,获取足量新节点 // 如果有需要变更的节点,获取足量新节点
offset := city.Offset
if count > city.EdgesCount { if count > city.EdgesCount {
slog.Warn(fmt.Sprintf("城市节点数量不足,跳过本次更新,城市:%s节点数%d网关数%d", city.Name, city.EdgesCount, count)) slog.Warn(fmt.Sprintf("城市节点数量不足,跳过本次更新,城市:%s节点数%d网关数%d", city.Name, city.EdgesCount, count))
continue continue
} }
edges, err := SliceActiveEdges(tx, city.Id, offset, count) edges, err := SliceActiveEdges(tx, city.Id, city.Offset, count)
if err != nil { if err != nil {
return fmt.Errorf("查询城市 %s 可用节点失败:%w", city.Name, err) return fmt.Errorf("查询城市 %s 可用节点失败:%w", city.Name, err)
} }
@@ -117,7 +117,10 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
} }
} }
city.Offset = edges[len(edges)-1].Id citiesUpdate = append(citiesUpdate, model.City{
Id: city.Id,
Offset: edges[len(edges)-1].Id,
})
// 分配新节点 // 分配新节点
for i, oldConfig := range configsUpdate { for i, oldConfig := range configsUpdate {
@@ -125,7 +128,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
gateway := findGateway[oldConfig.GatewayMac] gateway := findGateway[oldConfig.GatewayMac]
edge := edges[i] edge := edges[i]
slog.Debug(fmt.Sprintf("网关配置变更,网关:%s旧节点%s新节点%s", gateway.Macaddr, oldConfig.Macaddr, edge.Macaddr)) slog.Debug(fmt.Sprintf("网关配置变更,网关:%s旧节点%s新节点%s", gateway.Macaddr, oldConfig.EdgeMac, edge.Macaddr))
newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{ newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{
Change: true, Change: true,
@@ -135,16 +138,16 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
}, },
Config: model.ConfigUpdate{ Config: model.ConfigUpdate{
Id: oldConfig.Id, Id: oldConfig.Id,
Macaddr: u.P(edge.Macaddr), EdgeMac: u.P(edge.Macaddr),
IsChange: u.P(0), IsChange: u.P(0),
IsOnline: u.P(1), IsOnline: u.P(1),
}, },
}) })
changes = append(changes, model.Change{ changesCreate = append(changesCreate, model.Change{
Time: now, Time: now,
CityId: city.Id, CityId: city.Id,
Gateway: gateway.Macaddr, Gateway: gateway.Macaddr,
OldEdge: oldConfig.Macaddr, OldEdge: oldConfig.EdgeMac,
NewEdge: edge.Macaddr, NewEdge: edge.Macaddr,
}) })
} }
@@ -154,7 +157,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
step = time.Now() step = time.Now()
// 更新城市偏移量 // 更新城市偏移量
err = UpdateCitiesOffset(tx, cities) err = UpdateCitiesOffset(tx, citiesUpdate)
if err != nil { if err != nil {
return fmt.Errorf("更新城市偏移量失败:%w", err) return fmt.Errorf("更新城市偏移量失败:%w", err)
} }
@@ -163,7 +166,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
step = time.Now() step = time.Now()
// 记录节点变更 // 记录节点变更
err = RecordChanges(tx, changes) err = RecordChanges(tx, changesCreate)
if err != nil { if err != nil {
return fmt.Errorf("记录节点变更失败:%w", err) return fmt.Errorf("记录节点变更失败:%w", err)
} }

View File

@@ -4,7 +4,7 @@ type Config struct {
Id int `gorm:"column:id;primaryKey"` Id int `gorm:"column:id;primaryKey"`
GatewayMac string `gorm:"column:macaddr"` GatewayMac string `gorm:"column:macaddr"`
Table string `gorm:"column:table"` Table string `gorm:"column:table"`
Macaddr string `gorm:"column:edge"` EdgeMac string `gorm:"column:edge"`
Network string `gorm:"column:network"` Network string `gorm:"column:network"`
Cityhash string `gorm:"column:cityhash"` Cityhash string `gorm:"column:cityhash"`
CityLabel string `gorm:"column:label"` CityLabel string `gorm:"column:label"`
@@ -25,7 +25,7 @@ type ConfigUpdate struct {
Id int `gorm:"column:id;primaryKey"` Id int `gorm:"column:id;primaryKey"`
GatewayMac *string `gorm:"column:macaddr"` GatewayMac *string `gorm:"column:macaddr"`
Table *string `gorm:"column:table"` Table *string `gorm:"column:table"`
Macaddr *string `gorm:"column:edge"` EdgeMac *string `gorm:"column:edge"`
Network *string `gorm:"column:network"` Network *string `gorm:"column:network"`
Cityhash *string `gorm:"column:cityhash"` Cityhash *string `gorm:"column:cityhash"`
CityLabel *string `gorm:"column:label"` CityLabel *string `gorm:"column:label"`