Browse Source

修复一些问题,增加TODO

gitlink
Sydonian 2 years ago
parent
commit
4fb87e062b
4 changed files with 41 additions and 61 deletions
  1. +1
    -1
      internal/config/config.go
  2. +13
    -15
      internal/services/cmd/storage.go
  3. +27
    -43
      internal/task/ec_read.go
  4. +0
    -2
      main.go

+ 1
- 1
internal/config/config.go View File

@@ -12,7 +12,7 @@ type Config struct {
ID int64 `json:"id"`
GRPCListenAddress string `json:"grpcListenAddress"`
GRPCPort int `json:"grpcPort"`
GRPCPacketSize int64 `json:"grpcPacketSize"`
ECPacketSize int64 `json:"ecPacketSize"`
LocalIP string `json:"localIP"`
ExternalIP string `json:"externalIP"`
StorageBaseDir string `json:"storageBaseDir"`


+ 13
- 15
internal/services/cmd/storage.go View File

@@ -37,19 +37,17 @@ func (service *Service) StartStorageMoveObject(msg *agtmsg.StartStorageMoveObjec

return ramsg.ReplyOK(agtmsg.NewStartStorageMoveObjectResp(taskID))

} else {
// TODO 处理其他备份类型
if repRed, ok := msg.Redundancy.(models.ECRedundancyData); ok {
taskID, err := service.moveEcObject(msg.FileSize, repRed, outFilePath)
if err != nil {
logger.Warnf("move ec object as %s failed, err: %s", outFilePath, err.Error())
return ramsg.ReplyFailed[agtmsg.StartStorageMoveObjectResp](errorcode.OperationFailed, "move ec object failed")
}

return ramsg.ReplyOK(agtmsg.NewStartStorageMoveObjectResp(taskID))
} else if repRed, ok := msg.Redundancy.(models.ECRedundancyData); ok {
taskID, err := service.moveEcObject(msg.ObjectID, msg.FileSize, repRed, outFilePath)
if err != nil {
logger.Warnf("move ec object as %s failed, err: %s", outFilePath, err.Error())
return ramsg.ReplyFailed[agtmsg.StartStorageMoveObjectResp](errorcode.OperationFailed, "move ec object failed")
}
return ramsg.ReplyFailed[agtmsg.StartStorageMoveObjectResp](errorcode.OperationFailed, "not rep or ec object???")

return ramsg.ReplyOK(agtmsg.NewStartStorageMoveObjectResp(taskID))
}

return ramsg.ReplyFailed[agtmsg.StartStorageMoveObjectResp](errorcode.OperationFailed, "not rep or ec object???")
}

func (svc *Service) moveRepObject(repData models.RepRedundancyData, outFilePath string) (string, error) {
@@ -57,15 +55,15 @@ func (svc *Service) moveRepObject(repData models.RepRedundancyData, outFilePath
return tsk.ID(), nil
}

func (svc *Service) moveEcObject(fileSize int64, ecData models.ECRedundancyData, outFilePath string) (string, error) {
func (svc *Service) moveEcObject(objID int64, fileSize int64, ecData models.ECRedundancyData, outFilePath string) (string, error) {
ecK := ecData.Ec.EcK
blockIDs := make([]int, ecK)
hashs := make([]string, ecK)
for i:=0 ; i<ecK; i++{
for i := 0; i < ecK; i++ {
blockIDs[i] = i
hashs[i] = ecData.Blocks[i].FileHash
hashs[i] = ecData.Blocks[i].FileHash
}
tsk := svc.taskManager.StartComparable(task.NewEcRead(fileSize, ecData.Ec, blockIDs, hashs, outFilePath))
tsk := svc.taskManager.StartComparable(task.NewEcRead(objID, fileSize, ecData.Ec, blockIDs, hashs, outFilePath))
return tsk.ID(), nil
}



+ 27
- 43
internal/task/ec_read.go View File

@@ -6,28 +6,30 @@ import (
"os"
"path/filepath"
"time"
"gitlink.org.cn/cloudream/ec"
"gitlink.org.cn/cloudream/common/pkg/logger"
"gitlink.org.cn/cloudream/common/models"

"gitlink.org.cn/cloudream/agent/internal/config"
"gitlink.org.cn/cloudream/common/models"
"gitlink.org.cn/cloudream/common/pkg/logger"
"gitlink.org.cn/cloudream/ec"
)

type EcRead struct {
FileSize int64
Ec models.EC
BlockIDs []int
BlockHashs []string
LocalPath string
objID int64
FileSize int64
Ec models.EC
BlockIDs []int
BlockHashs []string
LocalPath string
}

func NewEcRead(fileSize int64,ec models.EC, blockIDs []int, blockHashs []string, localPath string) *EcRead {
func NewEcRead(objID int64, fileSize int64, ec models.EC, blockIDs []int, blockHashs []string, localPath string) *EcRead {
return &EcRead{
FileSize: fileSize,
Ec: ec,
BlockIDs: blockIDs,
BlockHashs: blockHashs,
LocalPath: localPath,
objID: objID,
FileSize: fileSize,
Ec: ec,
BlockIDs: blockIDs,
BlockHashs: blockHashs,
LocalPath: localPath,
}
}

@@ -36,28 +38,8 @@ func (t *EcRead) Compare(other *Task) bool {
if !ok {
return false
}
checkBLockIDs := true
if len(t.BlockIDs) != len(tsk.BlockIDs){
checkBLockIDs = false
}else{
for i:=0; i<len(t.BlockIDs); i++{
if t.BlockIDs[i] != tsk.BlockIDs[i]{
checkBLockIDs = false
}
}
}

checkHashs := true
if len(t.BlockHashs) != len(tsk.BlockHashs){
checkHashs = false
}else{
for i:=0; i<len(t.BlockHashs); i++{
if t.BlockHashs[i] != tsk.BlockHashs[i]{
checkHashs = false
}
}
}
return t.FileSize == tsk.FileSize && t.Ec == tsk.Ec && checkBLockIDs && checkHashs &&t.LocalPath == tsk.LocalPath
return t.objID == tsk.objID && t.LocalPath == tsk.LocalPath
}

func (t *EcRead) Execute(ctx TaskContext, complete CompleteFn) {
@@ -119,8 +101,10 @@ func (t *EcRead) Execute(ctx TaskContext, complete CompleteFn) {
})
}

func readObject(ctx TaskContext, fileSize int64, ecK int, ecN int, blockIDs []int, hashs []string)(io.ReadCloser, error){
numPacket := (fileSize + int64(ecK)*config.Cfg().GRPCPacketSize - 1) / (int64(ecK) * config.Cfg().GRPCPacketSize)
func readObject(ctx TaskContext, fileSize int64, ecK int, ecN int, blockIDs []int, hashs []string) (io.ReadCloser, error) {
// TODO zkx 先使用同步方式实现读取多个block并解码数据的逻辑,做好错误处理

numPacket := (fileSize + int64(ecK)*config.Cfg().ECPacketSize - 1) / (int64(ecK) * config.Cfg().ECPacketSize)
getBufs := make([]chan []byte, ecN)
decodeBufs := make([]chan []byte, ecK)
for i := 0; i < ecN; i++ {
@@ -134,9 +118,9 @@ func readObject(ctx TaskContext, fileSize int64, ecK int, ecN int, blockIDs []in
}
//print(numPacket)
go decode(getBufs[:], decodeBufs[:], blockIDs, ecK, numPacket)
r,w := io.Pipe()
r, w := io.Pipe()
//这个就是persist函数
go func(){
go func() {
for i := 0; int64(i) < numPacket; i++ {
for j := 0; j < len(decodeBufs); j++ {
tmp := <-decodeBufs[j]
@@ -159,8 +143,8 @@ func get(ctx TaskContext, blockHash string, getBuf chan []byte, numPacket int64)
return fmt.Errorf("read ipfs block failed, err: %w", err)
}
defer reader.Close()
for i:=0; int64(i)<numPacket; i++{
buf := make([]byte, config.Cfg().GRPCPacketSize)
for i := 0; int64(i) < numPacket; i++ {
buf := make([]byte, config.Cfg().ECPacketSize)
_, err := io.ReadFull(reader, buf)
if err != nil {
return fmt.Errorf("read file falied, err:%w", err)
@@ -207,4 +191,4 @@ func decode(inBufs []chan []byte, outBufs []chan []byte, blockSeq []int, ecK int
for i := 0; i < len(outBufs); i++ {
close(outBufs[i])
}
}
}

+ 0
- 2
main.go View File

@@ -26,8 +26,6 @@ import (
var AgentIpList []string

func main() {
print("!!!!!!!!")
//logger.Warnf("worinimab")
// TODO 放到配置里读取
AgentIpList = []string{"pcm01", "pcm1", "pcm2"}



Loading…
Cancel
Save