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.

83 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
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/operation"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/security"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. )
  13. func (wfs *WFS) saveDataAsChunk(fullPath util.FullPath) filer.SaveDataAsChunkFunctionType {
  14. return func(reader io.Reader, filename string, offset int64) (chunk *filer_pb.FileChunk, err error) {
  15. var fileId, host string
  16. var auth security.EncodedJwt
  17. if err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  18. return util.Retry("assignVolume", func() error {
  19. request := &filer_pb.AssignVolumeRequest{
  20. Count: 1,
  21. Replication: wfs.option.Replication,
  22. Collection: wfs.option.Collection,
  23. TtlSec: wfs.option.TtlSec,
  24. DiskType: string(wfs.option.DiskType),
  25. DataCenter: wfs.option.DataCenter,
  26. Path: string(fullPath),
  27. }
  28. resp, err := client.AssignVolume(context.Background(), request)
  29. if err != nil {
  30. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  31. return err
  32. }
  33. if resp.Error != "" {
  34. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  35. }
  36. fileId, auth = resp.FileId, security.EncodedJwt(resp.Auth)
  37. loc := resp.Location
  38. host = wfs.AdjustedUrl(loc)
  39. return nil
  40. })
  41. }); err != nil {
  42. return nil, fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  43. }
  44. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  45. if wfs.option.VolumeServerAccess == "filerProxy" {
  46. fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", wfs.getCurrentFiler(), fileId)
  47. }
  48. uploadOption := &operation.UploadOption{
  49. UploadUrl: fileUrl,
  50. Filename: filename,
  51. Cipher: wfs.option.Cipher,
  52. IsInputCompressed: false,
  53. MimeType: "",
  54. PairMap: nil,
  55. Jwt: auth,
  56. }
  57. uploadResult, err, data := operation.Upload(reader, uploadOption)
  58. if err != nil {
  59. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  60. return nil, fmt.Errorf("upload data: %v", err)
  61. }
  62. if uploadResult.Error != "" {
  63. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  64. return nil, fmt.Errorf("upload result: %v", uploadResult.Error)
  65. }
  66. if offset == 0 {
  67. wfs.chunkCache.SetChunk(fileId, data)
  68. }
  69. chunk = uploadResult.ToPbFileChunk(fileId, offset)
  70. return chunk, nil
  71. }
  72. }