deleteservicelogic.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // udeServer: DeleteService 走事务 —— 同一事务内先删该服务下的 t_directory,
  2. // 再删 t_service。任一步失败全部回滚,不会残留孤儿目录数据。
  3. package logic
  4. import (
  5. "context"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "path"
  11. "strings"
  12. "udeServer/internal/model"
  13. "udeServer/internal/svc"
  14. "udeServer/internal/types"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. "github.com/zeromicro/go-zero/core/stores/sqlx"
  17. )
  18. type DeleteServiceLogic struct {
  19. logx.Logger
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. }
  23. func NewDeleteServiceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteServiceLogic {
  24. return &DeleteServiceLogic{
  25. Logger: logx.WithContext(ctx),
  26. ctx: ctx,
  27. svcCtx: svcCtx,
  28. }
  29. }
  30. func (l *DeleteServiceLogic) DeleteService(req *types.ServiceIdReq) (*types.DeleteResp, error) {
  31. if err := checkId(req.Id); err != nil {
  32. return nil, err
  33. }
  34. // 先查出服务实体:删库前要拿 ip/port 去通知外挂程序卸载,删了就查不到了
  35. svc, err := findService(l.ctx, l.svcCtx.ServiceModel, req.Id)
  36. if err != nil {
  37. return nil, err
  38. }
  39. // P1 联动卸载:先卸载后删库。卸载失败软处理(不阻塞删除,外挂机器可能已下线),
  40. // 聚合成 unmountWarning 由前端弹黄色警告。
  41. unmountWarning := l.notifyFuseUnmount(svc)
  42. err = l.svcCtx.DB.Transact(func(session sqlx.Session) error {
  43. dirModel := l.svcCtx.DirectoryModel.WithSession(session)
  44. svcModel := l.svcCtx.ServiceModel.WithSession(session)
  45. if err := dirModel.DeleteByServiceId(l.ctx, req.Id); err != nil {
  46. return fmt.Errorf("delete t_directory of service %d: %w", req.Id, err)
  47. }
  48. if err := svcModel.Delete(l.ctx, req.Id); err != nil {
  49. return fmt.Errorf("delete t_service %d: %w", req.Id, err)
  50. }
  51. return nil
  52. })
  53. if err != nil {
  54. l.Errorf("delete service tx failed: %v", err)
  55. return nil, err
  56. }
  57. resp := &types.DeleteResp{Ok: true}
  58. if unmountWarning != "" {
  59. resp.UnmountWarning = unmountWarning
  60. }
  61. return resp, nil
  62. }
  63. // notifyFuseUnmount 删库前回调外挂程序(svc.Ip:svc.Port)的 GET /api/fuseUnMount 接口,
  64. // 逐目录卸载该服务全部 FUSE 挂载。
  65. // 软处理:单个目录失败只记录并聚合,绝不阻塞删除流程(外挂机器可能已关机,
  66. // 服务记录必须还能删)。返回空串 = 全部卸载成功或无需卸载。
  67. func (l *DeleteServiceLogic) notifyFuseUnmount(svc *model.TService) string {
  68. ip, port := svc.Ip.String, svc.Port // port 已是 int64
  69. if ip == "" || port == 0 {
  70. return "外挂服务 IP/端口为空,无法联动卸载,目录可能仍处于挂载状态"
  71. }
  72. dirs, err := l.svcCtx.DirectoryModel.FindDirsByServiceId(l.ctx, svc.Id)
  73. if err != nil {
  74. return fmt.Sprintf("查询保护目录失败,无法联动卸载: %v", err)
  75. }
  76. if len(dirs) == 0 {
  77. return "" // 没配保护目录,无需卸载
  78. }
  79. baseURL := fmt.Sprintf("http://%s:%d/api/fuseUnMount", ip, port) // %d:port 是 int64
  80. var msgs []string
  81. for _, mount := range dirs {
  82. // path.Clean(非 filepath!):该路径发给远程 Linux 外挂程序,必须保持 / 分隔符
  83. cleanMount := path.Clean(mount)
  84. reqURL, uerr := url.Parse(baseURL)
  85. if uerr != nil {
  86. msgs = append(msgs, fmt.Sprintf("解析 URL 失败: %v", uerr))
  87. continue
  88. }
  89. // q.Encode() 自动做 URL 编码(/ → %2F),外挂侧 Query().Get 自动解码还原
  90. q := reqURL.Query()
  91. q.Set("mount", cleanMount)
  92. reqURL.RawQuery = q.Encode()
  93. l.Infof("fuse unmount forward: GET %s", reqURL.String()) // 打出转发 URL 便于排查
  94. resp, rerr := fuseMountClient.Get(reqURL.String()) // 复用挂载逻辑里的 HTTP 客户端(同包变量)
  95. if rerr != nil {
  96. msgs = append(msgs, fmt.Sprintf("卸载目录 %s 失败(外挂程序未启动?): %v", cleanMount, rerr))
  97. continue
  98. }
  99. if resp.StatusCode >= http.StatusBadRequest {
  100. snippet, _ := io.ReadAll(io.LimitReader(resp.Body, 256)) // 带出外挂错误内容(前 256 字节)
  101. resp.Body.Close()
  102. msgs = append(msgs, fmt.Sprintf("卸载目录 %s 失败,外挂程序返回 HTTP %d: %s", cleanMount, resp.StatusCode, string(snippet)))
  103. continue
  104. }
  105. _, _ = io.Copy(io.Discard, resp.Body) // 读入"黑洞"保证 TCP 连接复用 Keep-Alive
  106. resp.Body.Close()
  107. l.Infof("fuse unmount ok: %s", cleanMount)
  108. }
  109. if len(msgs) == 0 {
  110. return ""
  111. }
  112. return "服务已删除,但卸载挂载失败: " + strings.Join(msgs, "; ")
  113. }