From 638b8d383a23934f559450c558e055d5f2449ac3 Mon Sep 17 00:00:00 2001 From: songjc <969378911@qq.com> Date: Thu, 24 Aug 2023 15:39:47 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/cmdline/package.go | 16 ++++++++-------- internal/http/package.go | 36 ++++++++++++++++++------------------ internal/http/server.go | 4 ++-- internal/services/package.go | 12 ++++++------ 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/internal/cmdline/package.go b/internal/cmdline/package.go index 5411d05..7ba0876 100644 --- a/internal/cmdline/package.go +++ b/internal/cmdline/package.go @@ -273,21 +273,21 @@ func PackageDeletePackage(ctx CommandContext, packageID int64) error { return nil } -func PackageGetCacheNodesByPackage(ctx CommandContext, packageID int64, userID int64) error { - nodeIDs, redunancyType, err := ctx.Cmdline.Svc.PackageSvc().GetCacheNodesByPackage(userID, packageID) +func PackageGetCachedNodes(ctx CommandContext, packageID int64, userID int64) error { + nodeIDs, redunancyType, err := ctx.Cmdline.Svc.PackageSvc().GetCachedNodes(userID, packageID) fmt.Printf("nodeIDs: %v\n", nodeIDs) fmt.Printf("redunancyType: %v\n", redunancyType) if err != nil { - return fmt.Errorf("get cache nodes by packageID %d failed, err: %w", packageID, err) + return fmt.Errorf("get package %d cached nodes failed, err: %w", packageID, err) } return nil } -func PackageGetStorageNodesByPackage(ctx CommandContext, packageID int64, userID int64) error { - nodeIDs, err := ctx.Cmdline.Svc.PackageSvc().GetStorageNodesByPackage(userID, packageID) +func PackageGetLoadedNodes(ctx CommandContext, packageID int64, userID int64) error { + nodeIDs, err := ctx.Cmdline.Svc.PackageSvc().GetLoadedNodes(userID, packageID) fmt.Printf("nodeIDs: %v\n", nodeIDs) if err != nil { - return fmt.Errorf("get storage nodes by packageID %d failed, err: %w", packageID, err) + return fmt.Errorf("get package %d loaded nodes failed, err: %w", packageID, err) } return nil } @@ -307,7 +307,7 @@ func init() { commands.MustAdd(PackageDeletePackage, "pkg", "delete") - commands.MustAdd(PackageGetCacheNodesByPackage, "pkg", "cache", "nodes") + commands.MustAdd(PackageGetCachedNodes, "pkg", "cached") - commands.MustAdd(PackageGetStorageNodesByPackage, "pkg", "storage", "nodes") + commands.MustAdd(PackageGetLoadedNodes, "pkg", "loaded") } diff --git a/internal/http/package.go b/internal/http/package.go index 1edd9cb..4cd3d18 100644 --- a/internal/http/package.go +++ b/internal/http/package.go @@ -172,65 +172,65 @@ func (s *PackageService) Delete(ctx *gin.Context) { ctx.JSON(http.StatusOK, OK(nil)) } -type PackageGetCacheNodeIDs struct { +type GetCachedNodesReq struct { UserID *int64 `json:"userID" binding:"required"` PackageID *int64 `json:"packageID" binding:"required"` } -type GetCacheNodesByPackageResp struct { +type GetCachedNodesResp struct { NodeIDs []int64 `json:"nodeIDs"` RedunancyType string `json:"redunancyType,string"` } -func (s *PackageService) GetCacheNodeIDs(ctx *gin.Context) { - log := logger.WithField("HTTP", "Package.GetCacheNodeIDs") +func (s *PackageService) GetCachedNodes(ctx *gin.Context) { + log := logger.WithField("HTTP", "Package.GetCachedNodes") - var req PackageGetCacheNodeIDs + var req GetCachedNodesReq if err := ctx.ShouldBindJSON(&req); err != nil { log.Warnf("binding body: %s", err.Error()) ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument")) return } - nodeIDs, redunancyType, err := s.svc.PackageSvc().GetCacheNodesByPackage(*req.UserID, *req.PackageID) + nodeIDs, redunancyType, err := s.svc.PackageSvc().GetCachedNodes(*req.UserID, *req.PackageID) if err != nil { - log.Warnf("get cache nodes by packageID failed: %s", err.Error()) - ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get cache nodes by packageID failed")) + log.Warnf("get package cached nodes failed: %s", err.Error()) + ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get package cached nodes failed")) return } - ctx.JSON(http.StatusOK, OK(GetCacheNodesByPackageResp{ + ctx.JSON(http.StatusOK, OK(GetCachedNodesResp{ NodeIDs: nodeIDs, RedunancyType: redunancyType, })) } -type PackageGetStorageNodeIDs struct { +type GetLoadedNodesReq struct { UserID *int64 `json:"userID" binding:"required"` PackageID *int64 `json:"packageID" binding:"required"` } -type GetStorageNodesByPackageResp struct { +type GetLoadedNodesResp struct { NodeIDs []int64 `json:"nodeIDs"` } -func (s *PackageService) GetStorageNodeIDs(ctx *gin.Context) { - log := logger.WithField("HTTP", "Package.GetStorageNodeIDs") +func (s *PackageService) GetLoadedNodes(ctx *gin.Context) { + log := logger.WithField("HTTP", "Package.GetLoadedNodes") - var req PackageGetStorageNodeIDs + var req GetLoadedNodesReq if err := ctx.ShouldBindJSON(&req); err != nil { log.Warnf("binding body: %s", err.Error()) ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument")) return } - nodeIDs, err := s.svc.PackageSvc().GetStorageNodesByPackage(*req.UserID, *req.PackageID) + nodeIDs, err := s.svc.PackageSvc().GetLoadedNodes(*req.UserID, *req.PackageID) if err != nil { - log.Warnf("get storage nodes by packageID failed: %s", err.Error()) - ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get storage nodes by packageID failed")) + log.Warnf("get package loaded nodes failed: %s", err.Error()) + ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get package loaded nodes failed")) return } - ctx.JSON(http.StatusOK, OK(GetStorageNodesByPackageResp{ + ctx.JSON(http.StatusOK, OK(GetLoadedNodesResp{ NodeIDs: nodeIDs, })) } diff --git a/internal/http/server.go b/internal/http/server.go index 953915d..465bde8 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -42,8 +42,8 @@ func (s *Server) initRouters() { s.engine.POST("/package/upload", s.PackageSvc().Upload) s.engine.POST("/package/delete", s.PackageSvc().Delete) - s.engine.GET("/package/getCacheNodeIDs", s.PackageSvc().GetCacheNodeIDs) - s.engine.GET("/package/getStorageNodeIDs", s.PackageSvc().GetStorageNodeIDs) + s.engine.GET("/package/getCachedNodes", s.PackageSvc().GetCachedNodes) + s.engine.GET("/package/getLoadedNodes", s.PackageSvc().GetLoadedNodes) s.engine.POST("/storage/loadPackage", s.StorageSvc().LoadPackage) s.engine.POST("/storage/createPackage", s.StorageSvc().CreatePackage) diff --git a/internal/services/package.go b/internal/services/package.go index 3ab031f..166d374 100644 --- a/internal/services/package.go +++ b/internal/services/package.go @@ -219,30 +219,30 @@ func (svc *PackageService) DeletePackage(userID int64, packageID int64) error { return nil } -func (svc *PackageService) GetCacheNodesByPackage(userID int64, packageID int64) ([]int64, string, error) { +func (svc *PackageService) GetCachedNodes(userID int64, packageID int64) ([]int64, string, error) { coorCli, err := globals.CoordinatorMQPool.Acquire() if err != nil { return nil, "", fmt.Errorf("new coordinator client: %w", err) } defer coorCli.Close() - resp, err := coorCli.GetCacheNodesByPackage(coormq.NewGetCacheNodesByPackage(userID, packageID)) + resp, err := coorCli.GetPackageCachedNodes(coormq.NewGetPackageCachedNodes(userID, packageID)) if err != nil { - return nil, "", fmt.Errorf("get node by package: %w", err) + return nil, "", fmt.Errorf("get package cached nodes: %w", err) } return resp.NodeIDs, resp.RedundancyType, nil } -func (svc *PackageService) GetStorageNodesByPackage(userID int64, packageID int64) ([]int64, error) { +func (svc *PackageService) GetLoadedNodes(userID int64, packageID int64) ([]int64, error) { coorCli, err := globals.CoordinatorMQPool.Acquire() if err != nil { return nil, fmt.Errorf("new coordinator client: %w", err) } defer coorCli.Close() - resp, err := coorCli.GetStorageNodesByPackage(coormq.NewGetStorageNodesByPackage(userID, packageID)) + resp, err := coorCli.GetPackageLoadedNodes(coormq.NewGetPackageLoadedNodes(userID, packageID)) if err != nil { - return nil, fmt.Errorf("get node by package: %w", err) + return nil, fmt.Errorf("get package loaded nodes: %w", err) } return resp.NodeIDs, nil } From 449967d72dc3f206ba65b60c7010270577f154e1 Mon Sep 17 00:00:00 2001 From: songjc <969378911@qq.com> Date: Fri, 25 Aug 2023 09:28:25 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/cmdline/package.go | 5 ++--- internal/http/package.go | 10 +++------- internal/services/package.go | 14 ++++++++++---- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/internal/cmdline/package.go b/internal/cmdline/package.go index 7ba0876..2db388d 100644 --- a/internal/cmdline/package.go +++ b/internal/cmdline/package.go @@ -274,9 +274,8 @@ func PackageDeletePackage(ctx CommandContext, packageID int64) error { } func PackageGetCachedNodes(ctx CommandContext, packageID int64, userID int64) error { - nodeIDs, redunancyType, err := ctx.Cmdline.Svc.PackageSvc().GetCachedNodes(userID, packageID) - fmt.Printf("nodeIDs: %v\n", nodeIDs) - fmt.Printf("redunancyType: %v\n", redunancyType) + resp, err := ctx.Cmdline.Svc.PackageSvc().GetCachedNodes(userID, packageID) + fmt.Printf("resp: %v\n", resp) if err != nil { return fmt.Errorf("get package %d cached nodes failed, err: %w", packageID, err) } diff --git a/internal/http/package.go b/internal/http/package.go index 4cd3d18..f473384 100644 --- a/internal/http/package.go +++ b/internal/http/package.go @@ -177,8 +177,7 @@ type GetCachedNodesReq struct { PackageID *int64 `json:"packageID" binding:"required"` } type GetCachedNodesResp struct { - NodeIDs []int64 `json:"nodeIDs"` - RedunancyType string `json:"redunancyType,string"` + models.PackageCachingInfo } func (s *PackageService) GetCachedNodes(ctx *gin.Context) { @@ -191,17 +190,14 @@ func (s *PackageService) GetCachedNodes(ctx *gin.Context) { return } - nodeIDs, redunancyType, err := s.svc.PackageSvc().GetCachedNodes(*req.UserID, *req.PackageID) + resp, err := s.svc.PackageSvc().GetCachedNodes(*req.UserID, *req.PackageID) if err != nil { log.Warnf("get package cached nodes failed: %s", err.Error()) ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get package cached nodes failed")) return } - ctx.JSON(http.StatusOK, OK(GetCachedNodesResp{ - NodeIDs: nodeIDs, - RedunancyType: redunancyType, - })) + ctx.JSON(http.StatusOK, OK(resp)) } type GetLoadedNodesReq struct { diff --git a/internal/services/package.go b/internal/services/package.go index 166d374..89c6678 100644 --- a/internal/services/package.go +++ b/internal/services/package.go @@ -219,18 +219,24 @@ func (svc *PackageService) DeletePackage(userID int64, packageID int64) error { return nil } -func (svc *PackageService) GetCachedNodes(userID int64, packageID int64) ([]int64, string, error) { +func (svc *PackageService) GetCachedNodes(userID int64, packageID int64) (models.PackageCachingInfo, error) { coorCli, err := globals.CoordinatorMQPool.Acquire() if err != nil { - return nil, "", fmt.Errorf("new coordinator client: %w", err) + return models.PackageCachingInfo{}, fmt.Errorf("new coordinator client: %w", err) } defer coorCli.Close() resp, err := coorCli.GetPackageCachedNodes(coormq.NewGetPackageCachedNodes(userID, packageID)) if err != nil { - return nil, "", fmt.Errorf("get package cached nodes: %w", err) + return models.PackageCachingInfo{}, fmt.Errorf("get package cached nodes: %w", err) } - return resp.NodeIDs, resp.RedundancyType, nil + + tmp := models.PackageCachingInfo{ + NodeInfos: resp.NodeInfos, + PackageSize: resp.PackageSize, + RedunancyType: resp.RedunancyType, + } + return tmp, nil } func (svc *PackageService) GetLoadedNodes(userID int64, packageID int64) ([]int64, error) { From d9e2bdf82c04021dff77fcce7597b3411f07a0d4 Mon Sep 17 00:00:00 2001 From: songjc <969378911@qq.com> Date: Fri, 25 Aug 2023 09:51:13 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/http/package.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/http/package.go b/internal/http/package.go index f473384..2980901 100644 --- a/internal/http/package.go +++ b/internal/http/package.go @@ -197,7 +197,7 @@ func (s *PackageService) GetCachedNodes(ctx *gin.Context) { return } - ctx.JSON(http.StatusOK, OK(resp)) + ctx.JSON(http.StatusOK, OK(GetCachedNodesResp{resp})) } type GetLoadedNodesReq struct {