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.

78 lines
2.5 KiB

  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func (wfs *WFS) saveDataAsChunk(fullPath util.FullPath, writeOnly bool) 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(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 := &filer_pb.Location{
  38. Url: resp.Url,
  39. PublicUrl: resp.PublicUrl,
  40. }
  41. host = wfs.AdjustedUrl(loc)
  42. collection, replication = resp.Collection, resp.Replication
  43. return nil
  44. })
  45. }); err != nil {
  46. return nil, "", "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  47. }
  48. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  49. if wfs.option.VolumeServerAccess == "filerProxy" {
  50. fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", wfs.option.FilerAddress, fileId)
  51. }
  52. uploadResult, err, data := operation.Upload(fileUrl, filename, wfs.option.Cipher, reader, false, "", nil, auth)
  53. if err != nil {
  54. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  55. return nil, "", "", fmt.Errorf("upload data: %v", err)
  56. }
  57. if uploadResult.Error != "" {
  58. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  59. return nil, "", "", fmt.Errorf("upload result: %v", uploadResult.Error)
  60. }
  61. if !writeOnly {
  62. wfs.chunkCache.SetChunk(fileId, data)
  63. }
  64. chunk = uploadResult.ToPbFileChunk(fileId, offset)
  65. return chunk, collection, replication, nil
  66. }
  67. }