Browse Source

client增加http模式

gitlink
Sydonian 2 years ago
parent
commit
ceb9a39600
7 changed files with 41 additions and 37 deletions
  1. +11
    -11
      go.mod
  2. +2
    -2
      internal/services/cmd/agent.go
  3. +9
    -9
      internal/services/cmd/ipfs.go
  4. +2
    -2
      internal/services/cmd/object.go
  5. +11
    -10
      internal/services/cmd/storage.go
  6. +4
    -1
      internal/task/upload_rep_objects.go
  7. +2
    -2
      status_report.go

+ 11
- 11
go.mod View File

@@ -76,14 +76,14 @@ require (
go 1.18 go 1.18


// 运行go mod tidy时需要将下面几行取消注释 // 运行go mod tidy时需要将下面几行取消注释
replace gitlink.org.cn/cloudream/ec => ../ec
replace gitlink.org.cn/cloudream/proto => ../proto
replace gitlink.org.cn/cloudream/rabbitmq => ../rabbitmq
replace gitlink.org.cn/cloudream/common => ../common
replace gitlink.org.cn/cloudream/db => ../db
replace magefiles => ../magefiles
// replace gitlink.org.cn/cloudream/ec => ../ec
//
// replace gitlink.org.cn/cloudream/proto => ../proto
//
// replace gitlink.org.cn/cloudream/rabbitmq => ../rabbitmq
//
// replace gitlink.org.cn/cloudream/common => ../common
//
// replace gitlink.org.cn/cloudream/db => ../db
//
// replace magefiles => ../magefiles

+ 2
- 2
internal/services/cmd/agent.go View File

@@ -10,9 +10,9 @@ func (svc *Service) GetState(msg *agtmsg.GetState) (*agtmsg.GetStateResp, *ramsg
var ipfsState string var ipfsState string


if svc.ipfs.IsUp() { if svc.ipfs.IsUp() {
ipfsState = consts.IPFS_STATE_OK
ipfsState = consts.IPFSStateOK
} else { } else {
ipfsState = consts.IPFS_STATE_OK
ipfsState = consts.IPFSStateOK
} }


return ramsg.ReplyOK(agtmsg.NewGetStateRespBody(ipfsState)) return ramsg.ReplyOK(agtmsg.NewGetStateRespBody(ipfsState))


+ 9
- 9
internal/services/cmd/ipfs.go View File

@@ -17,7 +17,7 @@ func (svc *Service) CheckIPFS(msg *agtmsg.CheckIPFS) (*agtmsg.CheckIPFSResp, *ra
filesMap, err := svc.ipfs.GetPinnedFiles() filesMap, err := svc.ipfs.GetPinnedFiles()
if err != nil { if err != nil {
logger.Warnf("get pinned files from ipfs failed, err: %s", err.Error()) logger.Warnf("get pinned files from ipfs failed, err: %s", err.Error())
return ramsg.ReplyFailed[agtmsg.CheckIPFSResp](errorcode.OPERATION_FAILED, "get pinned files from ipfs failed")
return ramsg.ReplyFailed[agtmsg.CheckIPFSResp](errorcode.OperationFailed, "get pinned files from ipfs failed")
} }


// TODO 根据锁定清单过滤被锁定的文件的记录 // TODO 根据锁定清单过滤被锁定的文件的记录
@@ -33,9 +33,9 @@ func (svc *Service) checkIncrement(msg *agtmsg.CheckIPFS, filesMap map[string]sh
for _, cache := range msg.Caches { for _, cache := range msg.Caches {
_, ok := filesMap[cache.FileHash] _, ok := filesMap[cache.FileHash]
if ok { if ok {
if cache.State == consts.CACHE_STATE_PINNED {
if cache.State == consts.CacheStatePinned {
// 不处理 // 不处理
} else if cache.State == consts.CACHE_STATE_TEMP {
} else if cache.State == consts.CacheStateTemp {
logger.WithField("FileHash", cache.FileHash).Debugf("unpin for cache entry state is temp") logger.WithField("FileHash", cache.FileHash).Debugf("unpin for cache entry state is temp")
err := svc.ipfs.Unpin(cache.FileHash) err := svc.ipfs.Unpin(cache.FileHash)
if err != nil { if err != nil {
@@ -47,10 +47,10 @@ func (svc *Service) checkIncrement(msg *agtmsg.CheckIPFS, filesMap map[string]sh
delete(filesMap, cache.FileHash) delete(filesMap, cache.FileHash)


} else { } else {
if cache.State == consts.CACHE_STATE_PINNED {
if cache.State == consts.CacheStatePinned {
svc.taskManager.StartComparable(task.NewIPFSPin(cache.FileHash)) svc.taskManager.StartComparable(task.NewIPFSPin(cache.FileHash))


} else if cache.State == consts.CACHE_STATE_TEMP {
} else if cache.State == consts.CacheStateTemp {
if time.Since(cache.CacheTime) > time.Duration(config.Cfg().TempFileLifetime)*time.Second { if time.Since(cache.CacheTime) > time.Duration(config.Cfg().TempFileLifetime)*time.Second {
entries = append(entries, agtmsg.NewCheckIPFSRespEntry(cache.FileHash, agtmsg.CHECK_IPFS_RESP_OP_DELETE_TEMP)) entries = append(entries, agtmsg.NewCheckIPFSRespEntry(cache.FileHash, agtmsg.CHECK_IPFS_RESP_OP_DELETE_TEMP))
} }
@@ -68,9 +68,9 @@ func (svc *Service) checkComplete(msg *agtmsg.CheckIPFS, filesMap map[string]she
for _, cache := range msg.Caches { for _, cache := range msg.Caches {
_, ok := filesMap[cache.FileHash] _, ok := filesMap[cache.FileHash]
if ok { if ok {
if cache.State == consts.CACHE_STATE_PINNED {
if cache.State == consts.CacheStatePinned {
// 不处理 // 不处理
} else if cache.State == consts.CACHE_STATE_TEMP {
} else if cache.State == consts.CacheStateTemp {
logger.WithField("FileHash", cache.FileHash).Debugf("unpin for cache entry state is temp") logger.WithField("FileHash", cache.FileHash).Debugf("unpin for cache entry state is temp")
err := svc.ipfs.Unpin(cache.FileHash) err := svc.ipfs.Unpin(cache.FileHash)
if err != nil { if err != nil {
@@ -82,10 +82,10 @@ func (svc *Service) checkComplete(msg *agtmsg.CheckIPFS, filesMap map[string]she
delete(filesMap, cache.FileHash) delete(filesMap, cache.FileHash)


} else { } else {
if cache.State == consts.CACHE_STATE_PINNED {
if cache.State == consts.CacheStatePinned {
svc.taskManager.StartComparable(task.NewIPFSPin(cache.FileHash)) svc.taskManager.StartComparable(task.NewIPFSPin(cache.FileHash))


} else if cache.State == consts.CACHE_STATE_TEMP {
} else if cache.State == consts.CacheStateTemp {
if time.Since(cache.CacheTime) > time.Duration(config.Cfg().TempFileLifetime)*time.Second { if time.Since(cache.CacheTime) > time.Duration(config.Cfg().TempFileLifetime)*time.Second {
entries = append(entries, agtmsg.NewCheckIPFSRespEntry(cache.FileHash, agtmsg.CHECK_IPFS_RESP_OP_DELETE_TEMP)) entries = append(entries, agtmsg.NewCheckIPFSRespEntry(cache.FileHash, agtmsg.CHECK_IPFS_RESP_OP_DELETE_TEMP))
} }


+ 2
- 2
internal/services/cmd/object.go View File

@@ -18,7 +18,7 @@ func (svc *Service) StartPinningObject(msg *agtmsg.StartPinningObject) (*agtmsg.
if tsk.Error() != nil { if tsk.Error() != nil {
log.WithField("FileHash", msg.FileHash). log.WithField("FileHash", msg.FileHash).
Warnf("pin object failed, err: %s", tsk.Error().Error()) Warnf("pin object failed, err: %s", tsk.Error().Error())
return ramsg.ReplyFailed[agtmsg.StartPinningObjectResp](errorcode.OPERATION_FAILED, "pin object failed")
return ramsg.ReplyFailed[agtmsg.StartPinningObjectResp](errorcode.OperationFailed, "pin object failed")
} }


return ramsg.ReplyOK(agtmsg.NewStartPinningObjectResp(tsk.ID())) return ramsg.ReplyOK(agtmsg.NewStartPinningObjectResp(tsk.ID()))
@@ -29,7 +29,7 @@ func (svc *Service) WaitPinningObject(msg *agtmsg.WaitPinningObject) (*agtmsg.Wa


tsk := svc.taskManager.FindByID(msg.TaskID) tsk := svc.taskManager.FindByID(msg.TaskID)
if tsk == nil { if tsk == nil {
return ramsg.ReplyFailed[agtmsg.WaitPinningObjectResp](errorcode.TASK_NOT_FOUND, "task not found")
return ramsg.ReplyFailed[agtmsg.WaitPinningObjectResp](errorcode.TaskNotFound, "task not found")
} }


if msg.WaitTimeoutMs == 0 { if msg.WaitTimeoutMs == 0 {


+ 11
- 10
internal/services/cmd/storage.go View File

@@ -13,6 +13,7 @@ import (
"gitlink.org.cn/cloudream/agent/internal/config" "gitlink.org.cn/cloudream/agent/internal/config"
"gitlink.org.cn/cloudream/agent/internal/task" "gitlink.org.cn/cloudream/agent/internal/task"
"gitlink.org.cn/cloudream/common/consts" "gitlink.org.cn/cloudream/common/consts"
"gitlink.org.cn/cloudream/common/models"
"gitlink.org.cn/cloudream/common/pkg/logger" "gitlink.org.cn/cloudream/common/pkg/logger"
"gitlink.org.cn/cloudream/common/utils" "gitlink.org.cn/cloudream/common/utils"
"gitlink.org.cn/cloudream/ec" "gitlink.org.cn/cloudream/ec"
@@ -26,11 +27,11 @@ func (service *Service) StartStorageMoveObject(msg *agtmsg.StartStorageMoveObjec
outFileName := utils.MakeMoveOperationFileName(msg.ObjectID, msg.UserID) outFileName := utils.MakeMoveOperationFileName(msg.ObjectID, msg.UserID)
outFilePath := filepath.Join(config.Cfg().StorageBaseDir, msg.Directory, outFileName) outFilePath := filepath.Join(config.Cfg().StorageBaseDir, msg.Directory, outFileName)


if repRed, ok := msg.Redundancy.(ramsg.RepRedundancyData); ok {
if repRed, ok := msg.Redundancy.(models.RepRedundancyData); ok {
taskID, err := service.moveRepObject(repRed, outFilePath) taskID, err := service.moveRepObject(repRed, outFilePath)
if err != nil { if err != nil {
logger.Warnf("move rep object as %s failed, err: %s", outFilePath, err.Error()) logger.Warnf("move rep object as %s failed, err: %s", outFilePath, err.Error())
return ramsg.ReplyFailed[agtmsg.StartStorageMoveObjectResp](errorcode.OPERATION_FAILED, "move rep object failed")
return ramsg.ReplyFailed[agtmsg.StartStorageMoveObjectResp](errorcode.OperationFailed, "move rep object failed")
} }


return ramsg.ReplyOK(agtmsg.NewStartStorageMoveObjectResp(taskID)) return ramsg.ReplyOK(agtmsg.NewStartStorageMoveObjectResp(taskID))
@@ -38,11 +39,11 @@ func (service *Service) StartStorageMoveObject(msg *agtmsg.StartStorageMoveObjec
} else { } else {
// TODO 处理其他备份类型 // TODO 处理其他备份类型


return ramsg.ReplyFailed[agtmsg.StartStorageMoveObjectResp](errorcode.OPERATION_FAILED, "not implement yet!")
return ramsg.ReplyFailed[agtmsg.StartStorageMoveObjectResp](errorcode.OperationFailed, "not implement yet!")
} }
} }


func (svc *Service) moveRepObject(repData ramsg.RepRedundancyData, outFilePath string) (string, error) {
func (svc *Service) moveRepObject(repData models.RepRedundancyData, outFilePath string) (string, error) {
tsk := svc.taskManager.StartComparable(task.NewIPFSRead(repData.FileHash, outFilePath)) tsk := svc.taskManager.StartComparable(task.NewIPFSRead(repData.FileHash, outFilePath))
return tsk.ID(), nil return tsk.ID(), nil
} }
@@ -52,7 +53,7 @@ func (svc *Service) WaitStorageMoveObject(msg *agtmsg.WaitStorageMoveObject) (*a


tsk := svc.taskManager.FindByID(msg.TaskID) tsk := svc.taskManager.FindByID(msg.TaskID)
if tsk == nil { if tsk == nil {
return ramsg.ReplyFailed[agtmsg.WaitStorageMoveObjectResp](errorcode.TASK_NOT_FOUND, "task not found")
return ramsg.ReplyFailed[agtmsg.WaitStorageMoveObjectResp](errorcode.TaskNotFound, "task not found")
} }


if msg.WaitTimeoutMs == 0 { if msg.WaitTimeoutMs == 0 {
@@ -125,7 +126,7 @@ func (svc *Service) checkStorageIncrement(msg *agtmsg.StorageCheck, fileInfos []


// 增量情况下,不需要对infosMap中没检查的记录进行处理 // 增量情况下,不需要对infosMap中没检查的记录进行处理


return ramsg.ReplyOK(agtmsg.NewStorageCheckResp(consts.STORAGE_DIRECTORY_STATE_OK, entries))
return ramsg.ReplyOK(agtmsg.NewStorageCheckResp(consts.StorageDirectoryStateOK, entries))
} }


func (svc *Service) checkStorageComplete(msg *agtmsg.StorageCheck, fileInfos []fs.FileInfo) (*agtmsg.StorageCheckResp, *ramsg.CodeMessage) { func (svc *Service) checkStorageComplete(msg *agtmsg.StorageCheck, fileInfos []fs.FileInfo) (*agtmsg.StorageCheckResp, *ramsg.CodeMessage) {
@@ -153,7 +154,7 @@ func (svc *Service) checkStorageComplete(msg *agtmsg.StorageCheck, fileInfos []f


// Storage中多出来的文件不做处理 // Storage中多出来的文件不做处理


return ramsg.ReplyOK(agtmsg.NewStorageCheckResp(consts.STORAGE_DIRECTORY_STATE_OK, entries))
return ramsg.ReplyOK(agtmsg.NewStorageCheckResp(consts.StorageDirectoryStateOK, entries))
} }


/* /*
@@ -296,14 +297,14 @@ func (svc *Service) StartStorageUploadRepObject(msg *agtmsg.StartStorageUploadRe
file, err := os.Open(fullPath) file, err := os.Open(fullPath)
if err != nil { if err != nil {
logger.Warnf("opening file %s: %s", fullPath, err.Error()) logger.Warnf("opening file %s: %s", fullPath, err.Error())
return nil, ramsg.Failed(errorcode.OPERATION_FAILED, "open file failed")
return nil, ramsg.Failed(errorcode.OperationFailed, "open file failed")
} }


fileInfo, err := file.Stat() fileInfo, err := file.Stat()
if err != nil { if err != nil {
file.Close() file.Close()
logger.Warnf("getting file %s state: %s", fullPath, err.Error()) logger.Warnf("getting file %s state: %s", fullPath, err.Error())
return nil, ramsg.Failed(errorcode.OPERATION_FAILED, "get file info failed")
return nil, ramsg.Failed(errorcode.OperationFailed, "get file info failed")
} }
fileSize := fileInfo.Size() fileSize := fileInfo.Size()


@@ -322,7 +323,7 @@ func (svc *Service) StartStorageUploadRepObject(msg *agtmsg.StartStorageUploadRe
func (svc *Service) WaitStorageUploadRepObject(msg *agtmsg.WaitStorageUploadRepObject) (*agtmsg.WaitStorageUploadRepObjectResp, *ramsg.CodeMessage) { func (svc *Service) WaitStorageUploadRepObject(msg *agtmsg.WaitStorageUploadRepObject) (*agtmsg.WaitStorageUploadRepObjectResp, *ramsg.CodeMessage) {
tsk := svc.taskManager.FindByID(msg.TaskID) tsk := svc.taskManager.FindByID(msg.TaskID)
if tsk == nil { if tsk == nil {
return nil, ramsg.Failed(errorcode.TASK_NOT_FOUND, "task not found")
return nil, ramsg.Failed(errorcode.TaskNotFound, "task not found")
} }


if msg.WaitTimeoutMs == 0 { if msg.WaitTimeoutMs == 0 {


+ 4
- 1
internal/task/upload_rep_objects.go View File

@@ -10,6 +10,7 @@ import (
"gitlink.org.cn/cloudream/agent/internal/config" "gitlink.org.cn/cloudream/agent/internal/config"
"gitlink.org.cn/cloudream/common/pkg/distlock/reqbuilder" "gitlink.org.cn/cloudream/common/pkg/distlock/reqbuilder"
"gitlink.org.cn/cloudream/common/pkg/logger" "gitlink.org.cn/cloudream/common/pkg/logger"
"gitlink.org.cn/cloudream/common/utils"
mygrpc "gitlink.org.cn/cloudream/common/utils/grpc" mygrpc "gitlink.org.cn/cloudream/common/utils/grpc"
"gitlink.org.cn/cloudream/common/utils/ipfs" "gitlink.org.cn/cloudream/common/utils/ipfs"


@@ -207,8 +208,10 @@ func (t *UploadRepObjects) uploadSingleObject(ctx TaskContext, uploadObject Uplo
uploadedNodeIDs = append(uploadedNodeIDs, uploadNode.ID) uploadedNodeIDs = append(uploadedNodeIDs, uploadNode.ID)
} }


dirName := utils.GetDirectoryName(uploadObject.ObjectName)

// 记录写入的文件的Hash // 记录写入的文件的Hash
createResp, err := ctx.Coordinator.CreateRepObject(coormsg.NewCreateRepObject(t.bucketID, uploadObject.ObjectName, uploadObject.FileSize, t.repCount, t.userID, uploadedNodeIDs, fileHash))
createResp, err := ctx.Coordinator.CreateRepObject(coormsg.NewCreateRepObject(t.bucketID, uploadObject.ObjectName, uploadObject.FileSize, t.repCount, t.userID, uploadedNodeIDs, fileHash, dirName))
if err != nil { if err != nil {
return 0, "", fmt.Errorf("creating rep object: %w", err) return 0, "", fmt.Errorf("creating rep object: %w", err)
} }


+ 2
- 2
status_report.go View File

@@ -50,9 +50,9 @@ func reportStatus(wg *sync.WaitGroup) {
} }
waitG.Wait() waitG.Wait()
//TODO: 查看本地IPFS daemon是否正常,记录到ipfsStatus //TODO: 查看本地IPFS daemon是否正常,记录到ipfsStatus
ipfsStatus := consts.IPFS_STATE_OK
ipfsStatus := consts.IPFSStateOK
//TODO:访问自身资源目录(配置文件中获取路径),记录是否正常,记录到localDirStatus //TODO:访问自身资源目录(配置文件中获取路径),记录是否正常,记录到localDirStatus
localDirStatus := consts.STORAGE_DIRECTORY_STATE_OK
localDirStatus := consts.StorageDirectoryStateOK


//发送心跳 //发送心跳
// TODO 由于数据结构未定,暂时不发送真实数据 // TODO 由于数据结构未定,暂时不发送真实数据


Loading…
Cancel
Save