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.

86 lines
2.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
  1. package filer
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/operation"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. func (f *Filer) appendToFile(targetFile string, data []byte) error {
  12. assignResult, uploadResult, err2 := f.assignAndUpload(targetFile, data)
  13. if err2 != nil {
  14. return err2
  15. }
  16. // find out existing entry
  17. fullpath := util.FullPath(targetFile)
  18. entry, err := f.FindEntry(context.Background(), fullpath)
  19. var offset int64 = 0
  20. if err == filer_pb.ErrNotFound {
  21. entry = &Entry{
  22. FullPath: fullpath,
  23. Attr: Attr{
  24. Crtime: time.Now(),
  25. Mtime: time.Now(),
  26. Mode: os.FileMode(0644),
  27. Uid: OS_UID,
  28. Gid: OS_GID,
  29. },
  30. }
  31. } else if err != nil {
  32. return fmt.Errorf("find %s: %v", fullpath, err)
  33. } else {
  34. offset = int64(TotalSize(entry.Chunks))
  35. }
  36. // append to existing chunks
  37. entry.Chunks = append(entry.Chunks, uploadResult.ToPbFileChunk(assignResult.Fid, offset))
  38. // update the entry
  39. err = f.CreateEntry(context.Background(), entry, false, false, nil)
  40. return err
  41. }
  42. func (f *Filer) assignAndUpload(targetFile string, data []byte) (*operation.AssignResult, *operation.UploadResult, error) {
  43. // assign a volume location
  44. rule := f.FilerConf.MatchStorageRule(targetFile)
  45. assignRequest := &operation.VolumeAssignRequest{
  46. Count: 1,
  47. Collection: util.Nvl(f.metaLogCollection, rule.Collection),
  48. Replication: util.Nvl(f.metaLogReplication, rule.Replication),
  49. WritableVolumeCount: rule.VolumeGrowthCount,
  50. }
  51. assignResult, err := operation.Assign(f.GetMaster, f.GrpcDialOption, assignRequest)
  52. if err != nil {
  53. return nil, nil, fmt.Errorf("AssignVolume: %v", err)
  54. }
  55. if assignResult.Error != "" {
  56. return nil, nil, fmt.Errorf("AssignVolume error: %v", assignResult.Error)
  57. }
  58. // upload data
  59. targetUrl := "http://" + assignResult.Url + "/" + assignResult.Fid
  60. uploadOption := &operation.UploadOption{
  61. UploadUrl: targetUrl,
  62. Filename: "",
  63. Cipher: f.Cipher,
  64. IsInputCompressed: false,
  65. MimeType: "",
  66. PairMap: nil,
  67. Jwt: assignResult.Auth,
  68. }
  69. uploadResult, err := operation.UploadData(data, uploadOption)
  70. if err != nil {
  71. return nil, nil, fmt.Errorf("upload data %s: %v", targetUrl, err)
  72. }
  73. // println("uploaded to", targetUrl)
  74. return assignResult, uploadResult, nil
  75. }