You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/operation"
  6. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  7. "github.com/chrislusf/seaweedfs/weed/remote_storage"
  8. "github.com/chrislusf/seaweedfs/weed/security"
  9. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  10. "github.com/chrislusf/seaweedfs/weed/storage/types"
  11. "sync"
  12. )
  13. func (vs *VolumeServer) FetchAndWriteNeedle(ctx context.Context, req *volume_server_pb.FetchAndWriteNeedleRequest) (resp *volume_server_pb.FetchAndWriteNeedleResponse, err error) {
  14. resp = &volume_server_pb.FetchAndWriteNeedleResponse{}
  15. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  16. if v == nil {
  17. return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
  18. }
  19. remoteConf := req.RemoteConf
  20. client, getClientErr := remote_storage.GetRemoteStorage(remoteConf)
  21. if getClientErr != nil {
  22. return nil, fmt.Errorf("get remote client: %v", getClientErr)
  23. }
  24. remoteStorageLocation := req.RemoteLocation
  25. data, ReadRemoteErr := client.ReadFile(remoteStorageLocation, req.Offset, req.Size)
  26. if ReadRemoteErr != nil {
  27. return nil, fmt.Errorf("read from remote %+v: %v", remoteStorageLocation, ReadRemoteErr)
  28. }
  29. var wg sync.WaitGroup
  30. wg.Add(1)
  31. go func() {
  32. defer wg.Done()
  33. n := new(needle.Needle)
  34. n.Id = types.NeedleId(req.NeedleId)
  35. n.Cookie = types.Cookie(req.Cookie)
  36. n.Data, n.DataSize = data, uint32(len(data))
  37. // copied from *Needle.prepareWriteBuffer()
  38. n.Size = 4 + types.Size(n.DataSize) + 1
  39. n.Checksum = needle.NewCRC(n.Data)
  40. if _, localWriteErr := vs.store.WriteVolumeNeedle(v.Id, n, true, false); localWriteErr != nil {
  41. if err == nil {
  42. err = fmt.Errorf("local write needle %d size %d: %v", req.NeedleId, req.Size, err)
  43. }
  44. }
  45. }()
  46. if len(req.Replicas)>0{
  47. fileId := needle.NewFileId(v.Id, req.NeedleId, req.Cookie)
  48. for _, replica := range req.Replicas {
  49. wg.Add(1)
  50. go func(targetVolumeServer string) {
  51. defer wg.Done()
  52. uploadOption := &operation.UploadOption{
  53. UploadUrl: fmt.Sprintf("http://%s/%s?type=replicate", targetVolumeServer, fileId.String()),
  54. Filename: "",
  55. Cipher: false,
  56. IsInputCompressed: false,
  57. MimeType: "",
  58. PairMap: nil,
  59. Jwt: security.EncodedJwt(req.Auth),
  60. }
  61. if _, replicaWriteErr := operation.UploadData(data, uploadOption); replicaWriteErr != nil {
  62. if err == nil {
  63. err = fmt.Errorf("remote write needle %d size %d: %v", req.NeedleId, req.Size, err)
  64. }
  65. }
  66. }(replica.Url)
  67. }
  68. }
  69. wg.Wait()
  70. return resp, err
  71. }