From d0dd69fca40fd4b9871a4929fef7592ce2b84b43 Mon Sep 17 00:00:00 2001 From: bizhiyuan Date: Mon, 20 Oct 2025 10:19:09 +0800 Subject: [PATCH] Modify the interface data for DeleteHeartbeat function --- controllers/heatbeat.go | 15 +++++++++ models/heartbeat.go | 69 +++++++++++++++++++++++++++++++---------- routers/router.go | 2 ++ 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/controllers/heatbeat.go b/controllers/heatbeat.go index eaadbf3..9c4d800 100644 --- a/controllers/heatbeat.go +++ b/controllers/heatbeat.go @@ -17,6 +17,21 @@ type HeartBeatController struct { web.Controller } +func (lhbc *LocalHeartBeatController) Delete() { + var reqData models.HeartbeatRequest + var result utils.GeneralResponse + if err := json.Unmarshal(lhbc.Ctx.Input.RequestBody, &reqData); err != nil { + result.Action = false + result.Error = gettext.Gettext("invalid input data") + lhbc.Data["json"] = &result + lhbc.ServeJSON() + } + + result = models.DeleteHeartbeat(reqData) + lhbc.Data["json"] = &result + lhbc.ServeJSON() +} + func (hbc *HeartBeatController) Get() { result := map[string]interface{}{} diff --git a/models/heartbeat.go b/models/heartbeat.go index c94a141..43dd8f1 100644 --- a/models/heartbeat.go +++ b/models/heartbeat.go @@ -358,40 +358,66 @@ func isValidIPv4(ip string) bool { return net.ParseIP(ip) != nil && strings.Contains(ip, ".") } -func DeleteHeartbeat(hbInfo HBInfo) utils.GeneralResponse { +// 承接编辑心跳、删除心跳请求数据 +type HeartbeatRequest struct { + ClusterName string `json:"cluster_name"` + Data []HeartData `json:"data"` +} + +type HeartData struct { + Name string `json:"name"` + RingData []RingDetail `json:"ring_data"` // 包含 ring_name 和 ip +} + +type RingDetail struct { + RingName string `json:"ring_name"` + IP string `json:"ip"` +} + +func DeleteHeartbeat(hbInfo HeartbeatRequest) utils.GeneralResponse { hbData := hbInfo.Data + if len(hbData) == 0 { + return utils.GeneralResponse{ + Action: false, + Error: gettext.Gettext("input validation failed"), + } + } + minLinkNum := 1 - hbInfoList, _ := ExtractHbInfo(hbData) - status := GetClusterStatus() currentLocalHbIds := GetCurrentLinkIds() - var linkId int - var linkIdSet []string - if len(currentLocalHbIds) <= minLinkNum || len(currentLocalHbIds) <= len(hbInfoList) { + ringData := hbData[0].RingData + // 至少要保留一路心跳 + if len(currentLocalHbIds) <= minLinkNum || len(currentLocalHbIds) <= len(ringData) { return utils.GeneralResponse{ Action: false, Error: gettext.Gettext("At least one heartbeat needs to be preserved and cannot be deleted further."), } } + var linkId int + var linkIdSet []string - for _, hbInfo := range hbInfoList { - ip := utils.Values[string, string](hbInfo) + status := GetClusterStatus() + for _, ring := range ringData { + ip := ring.IP if status == 0 { - linkId, _ = GetRingIdFromIPOnline(ip[0]) + linkId, _ = GetRingIdFromIPOnline(ip) + if linkId == -1 { + linkId, _ = GetRingIdFromIPOffline(ip) + } } else { - linkId, _ = GetRingIdFromIPOnline(ip[0]) + linkId, _ = GetRingIdFromIPOffline(ip) } if linkId == -1 { - linkId, _ = GetRingIdFromIPOnline(ip[0]) - if linkId == -1 { - return utils.GeneralResponse{ - Action: false, - Error: gettext.Gettext("Exception occurred while deleting the heartbeat. Please check the logs and the original heartbeat status."), - } + return utils.GeneralResponse{ + Action: false, + Error: gettext.Gettext("Exception occurred while deleting the heartbeat. Please check the logs and the original heartbeat status."), } } + linkIdSet = append(linkIdSet, strconv.Itoa(linkId)) } + linkIdSetStr := strings.Join(linkIdSet, " ") cmd := fmt.Sprintf(utils.CmdDeleteLinks, linkIdSetStr) if _, err := utils.RunCommand(cmd); err != nil { @@ -401,9 +427,18 @@ func DeleteHeartbeat(hbInfo HBInfo) utils.GeneralResponse { } } + //更新并同步配置文件 + localCluster := LocalClusterInfo() + if err := UpdateClusterConfFile(localCluster); err != nil { + return utils.GeneralResponse{ + Action: false, + Info: gettext.Gettext("Failed to synchronize Cluster file"), + } + } + return utils.GeneralResponse{ Action: true, - Error: gettext.Gettext("Delete heartbeat success"), + Info: gettext.Gettext("Delete heartbeat success"), } } diff --git a/routers/router.go b/routers/router.go index b1e1d81..112c4ed 100644 --- a/routers/router.go +++ b/routers/router.go @@ -38,6 +38,7 @@ func init() { web.NSRouter("/managec/add_nodes", &controllers.AddNodesController{}), web.NSRouter("/managec/local_cluster_info", &controllers.LocalClusterInfoController{}), web.NSRouter("/managec/is_cluster_exist", &controllers.IsClusterExistController{}), + web.NSRouter("/managec/edit_hb_info", &controllers.HeartBeatController{}), web.NSRouter("/:cluster_name/1/resources", &controllers.ResourceController{}), web.NSRouter("/:cluster_name/1/resources/:rscID/:action", &controllers.ResourceActionController{}), @@ -78,6 +79,7 @@ func init() { nr := web.NewNamespace("/remote/api/v1", web.NSRouter("/sync_config", &controllers.Sync_configController{}), web.NSRouter("/nodes/add_nodes", &controllers.LocalAddNodesController{}), + web.NSRouter("/nodes/edit_hb_info", &controllers.LocalHeartBeatController{}), web.NSRouter("/managec/local_cluster_info", &controllers.LocalClusterInfoController{}), web.NSRouter("/destroy_cluster", &controllers.LocalClusterDestroyController{}), -- Gitee