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.

84 lines
2.6 KiB

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, collection, replication string, 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. collection, replication = resp.Collection, resp.Replication
  40. return nil
  41. })
  42. }); err != nil {
  43. return nil, "", "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  44. }
  45. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  46. if wfs.option.VolumeServerAccess == "filerProxy" {
  47. fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", wfs.getCurrentFiler(), fileId)
  48. }
  49. uploadOption := &operation.UploadOption{
  50. UploadUrl: fileUrl,
  51. Filename: filename,
  52. Cipher: wfs.option.Cipher,
  53. IsInputCompressed: false,
  54. MimeType: "",
  55. PairMap: nil,
  56. Jwt: auth,
  57. }
  58. uploadResult, err, data := operation.Upload(reader, uploadOption)
  59. if err != nil {
  60. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  61. return nil, "", "", fmt.Errorf("upload data: %v", err)
  62. }
  63. if uploadResult.Error != "" {
  64. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  65. return nil, "", "", fmt.Errorf("upload result: %v", uploadResult.Error)
  66. }
  67. if offset == 0 {
  68. wfs.chunkCache.SetChunk(fileId, data)
  69. }
  70. chunk = uploadResult.ToPbFileChunk(fileId, offset)
  71. return chunk, collection, replication, nil
  72. }
  73. }