package main import ( "context" "io" "os" "fmt" "encoding/json" "path/filepath" "sync" "strconv" "rabbitmq" "time" agentcaller "proto" //"github.com/pborman/uuid" //"github.com/streadway/amqp" "google.golang.org/grpc" _ "google.golang.org/grpc/balancer/grpclb" ) const ( port = ":5010" packetSizeInBytes=10 ) func Move(bucketName string, objectName string, destination string){ //将bucketName, objectName, destination发给协调端 fmt.Println("move "+bucketName+"/"+objectName+" to "+destination) //获取块hash,ip,序号,编码参数等 //发送写请求,分配写入节点Ip userId:=0 command1:= rabbitmq.MoveCommand{ BucketName: bucketName, ObjectName: objectName, UserId: userId, Destination: destination, } c1,_:=json.Marshal(command1) b1:=append([]byte("05"),c1...) fmt.Println(string(b1)) rabbit1 := rabbitmq.NewRabbitMQSimple("coorQueue") rabbit1.PublishSimple(b1) //接收消息,赋值给ip, repHash, fileSizeInBytes var res1 rabbitmq.MoveRes var redundancy string var hashs []string var fileSizeInBytes int64 var ecName string var ids []int queueName := "coorClientQueue"+strconv.Itoa(userId) rabbit2 := rabbitmq.NewRabbitMQSimple(queueName) msgs:=rabbit2.ConsumeSimple(time.Millisecond, true) wg := sync.WaitGroup{} wg.Add(1) go func() { for d := range msgs { _ = json.Unmarshal(d.Body, &res1) redundancy=res1.Redundancy ids=res1.Ids hashs=res1.Hashs fileSizeInBytes=res1.FileSizeInBytes ecName=res1.EcName wg.Done() } }() wg.Wait() fmt.Println(redundancy) fmt.Println(hashs) fmt.Println(ids) fmt.Println(fileSizeInBytes) fmt.Println(ecName) //根据redundancy调用repMove和ecMove rabbit3 := rabbitmq.NewRabbitMQSimple("agentQueue"+destination) var b2 []byte switch redundancy { case "rep": command2:= rabbitmq.RepMoveCommand{ Hashs: hashs, BucketName: bucketName, ObjectName: objectName, UserId: userId, } c2,_:=json.Marshal(command2) b2=append([]byte("00"),c2...) case "ec": command2:= rabbitmq.EcMoveCommand{ Hashs: hashs, Ids: ids, EcName: ecName, BucketName: bucketName, ObjectName: objectName, UserId: userId, } c2,_:=json.Marshal(command2) b2=append([]byte("01"),c2...) } fmt.Println(b2) rabbit3.PublishSimple(b2) //接受调度成功与否的消息 //接受第二轮通讯结果 var res2 rabbitmq.AgentMoveRes queueName = "agentClientQueue"+strconv.Itoa(userId) rabbit4 := rabbitmq.NewRabbitMQSimple(queueName) msgs=rabbit4.ConsumeSimple(time.Millisecond, true) wg.Add(1) go func() { for d := range msgs { _ = json.Unmarshal(d.Body, &res2) if(res2.MoveCode==0){ wg.Done() fmt.Println("Move Success") } } }() wg.Wait() rabbit1.Destroy() rabbit2.Destroy() rabbit3.Destroy() rabbit4.Destroy() } func Read(localFilePath string, bucketName string, objectName string){ fmt.Println("read "+bucketName+"/"+objectName+" to "+localFilePath) //获取块hash,ip,序号,编码参数等 //发送写请求,分配写入节点Ip userId:=0 command1:= rabbitmq.ReadCommand{ BucketName: bucketName, ObjectName: objectName, UserId: userId, } c1,_:=json.Marshal(command1) b1:=append([]byte("02"),c1...) fmt.Println(b1) rabbit1 := rabbitmq.NewRabbitMQSimple("coorQueue") rabbit1.PublishSimple(b1) //接收消息,赋值给ip, repHash, fileSizeInBytes var res1 rabbitmq.ReadRes var hashs []string var ips []string var fileSizeInBytes int64 var ecName string var ids []int var redundancy string queueName := "coorClientQueue"+strconv.Itoa(userId) rabbit2 := rabbitmq.NewRabbitMQSimple(queueName) msgs:=rabbit2.ConsumeSimple(time.Millisecond, true) wg := sync.WaitGroup{} wg.Add(1) go func() { for d := range msgs { _ = json.Unmarshal(d.Body, &res1) ips=res1.Ips hashs=res1.Hashs ids=res1.BlockIds ecName=res1.EcName fileSizeInBytes=res1.FileSizeInBytes redundancy=res1.Redundancy wg.Done() } }() wg.Wait() fmt.Println(redundancy) fmt.Println(ips) fmt.Println(hashs) fmt.Println(ids) fmt.Println(ecName) fmt.Println(fileSizeInBytes) rabbit1.Destroy() rabbit2.Destroy() switch redundancy { case "rep": repRead(fileSizeInBytes, ips[0], hashs[0], localFilePath) case "ec": ecRead(fileSizeInBytes, ips, hashs, ids, ecName, localFilePath) } } func repRead(fileSizeInBytes int64, ip string, repHash string, localFilePath string){ numPacket := (fileSizeInBytes+packetSizeInBytes-1)/(packetSizeInBytes) fmt.Println(numPacket) //rpc相关 conn, err := grpc.Dial(ip+port, grpc.WithInsecure()) if err != nil { panic(err) } client := agentcaller.NewTranBlockOrReplicaClient(conn) fDir, err := os.Executable() if err != nil { panic(err) } fURL := filepath.Join(filepath.Dir(fDir), "assets") _, err = os.Stat(fURL) if os.IsNotExist(err) { os.MkdirAll(fURL, os.ModePerm) } file, err := os.Create(filepath.Join(fURL, localFilePath)) if err != nil { return } stream, _ := client.GetBlockOrReplica(context.Background(), &agentcaller.GetReq{ BlockOrReplicaHash: repHash, }) fmt.Println(numPacket) for i:=0;int64(i)0 { numPacket++ } //发送写请求,分配写入节点Ip command1:= rabbitmq.RepWriteCommand{ BucketName: bucketName, ObjectName: objectName, FileSizeInBytes: fileSizeInBytes, NumRep: numRep, UserId: userId, } c1,_:=json.Marshal(command1) b1:=append([]byte("03"),c1...) fmt.Println(b1) rabbit1 := rabbitmq.NewRabbitMQSimple("coorQueue") rabbit1.PublishSimple(b1) //接收消息,赋值给ips var res1 rabbitmq.WriteRes var ips []string queueName := "coorClientQueue"+strconv.Itoa(userId) rabbit2 := rabbitmq.NewRabbitMQSimple(queueName) msgs:=rabbit2.ConsumeSimple(time.Millisecond, true) wg := sync.WaitGroup{} wg.Add(1) go func() { for d := range msgs { _ = json.Unmarshal(d.Body, &res1) ips=res1.Ips wg.Done() } }() wg.Wait() //创建channel loadDistributeBufs:=make([]chan []byte,numRep) for i := 0; i < numRep; i++ { loadDistributeBufs[i] = make(chan []byte) } //正式开始写入 hashs:= make([]string, numRep) go loadDistribute(localFilePath, loadDistributeBufs[:], numWholePacket, lastPacketInBytes)//从本地文件系统加载数据 wg.Add(numRep) for i:=0;i0 { buf := make([]byte, lastPacketInBytes) file.Read(buf) for j:=0;j