fetch_write.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package filersink
  2. import (
  3. "fmt"
  4. "github.com/schollz/progressbar/v3"
  5. "github.com/seaweedfs/seaweedfs/weed/util"
  6. "os"
  7. "path/filepath"
  8. "sync"
  9. "google.golang.org/grpc"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/operation"
  12. "github.com/seaweedfs/seaweedfs/weed/pb"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  14. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  15. )
  16. func (fs *FilerSink) replicateChunks(sourceChunks []*filer_pb.FileChunk, path string) (replicatedChunks []*filer_pb.FileChunk, err error) {
  17. if len(sourceChunks) == 0 {
  18. return
  19. }
  20. // a simple progress bar. Not ideal. Fix me.
  21. var bar *progressbar.ProgressBar
  22. if len(sourceChunks) > 1 {
  23. name := filepath.Base(path)
  24. bar = progressbar.NewOptions64(int64(len(sourceChunks)),
  25. progressbar.OptionClearOnFinish(),
  26. progressbar.OptionOnCompletion(func() {
  27. fmt.Fprint(os.Stderr, "\n")
  28. }),
  29. progressbar.OptionFullWidth(),
  30. progressbar.OptionSetDescription(name),
  31. )
  32. }
  33. replicatedChunks = make([]*filer_pb.FileChunk, len(sourceChunks))
  34. var wg sync.WaitGroup
  35. for chunkIndex, sourceChunk := range sourceChunks {
  36. wg.Add(1)
  37. index, source := chunkIndex, sourceChunk
  38. fs.executor.Execute(func() {
  39. defer wg.Done()
  40. util.Retry("replicate chunks", func() error {
  41. replicatedChunk, e := fs.replicateOneChunk(source, path)
  42. if e != nil {
  43. err = e
  44. return e
  45. }
  46. replicatedChunks[index] = replicatedChunk
  47. if bar != nil {
  48. bar.Add(1)
  49. }
  50. err = nil
  51. return nil
  52. })
  53. })
  54. }
  55. wg.Wait()
  56. return
  57. }
  58. func (fs *FilerSink) replicateOneChunk(sourceChunk *filer_pb.FileChunk, path string) (*filer_pb.FileChunk, error) {
  59. fileId, err := fs.fetchAndWrite(sourceChunk, path)
  60. if err != nil {
  61. return nil, fmt.Errorf("copy %s: %v", sourceChunk.GetFileIdString(), err)
  62. }
  63. return &filer_pb.FileChunk{
  64. FileId: fileId,
  65. Offset: sourceChunk.Offset,
  66. Size: sourceChunk.Size,
  67. ModifiedTsNs: sourceChunk.ModifiedTsNs,
  68. ETag: sourceChunk.ETag,
  69. SourceFileId: sourceChunk.GetFileIdString(),
  70. CipherKey: sourceChunk.CipherKey,
  71. IsCompressed: sourceChunk.IsCompressed,
  72. }, nil
  73. }
  74. func (fs *FilerSink) fetchAndWrite(sourceChunk *filer_pb.FileChunk, path string) (fileId string, err error) {
  75. filename, header, resp, err := fs.filerSource.ReadPart(sourceChunk.GetFileIdString())
  76. if err != nil {
  77. return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err)
  78. }
  79. defer util_http.CloseResponse(resp)
  80. uploader, err := operation.NewUploader()
  81. if err != nil {
  82. glog.V(0).Infof("upload source data %v: %v", sourceChunk.GetFileIdString(), err)
  83. return "", fmt.Errorf("upload data: %v", err)
  84. }
  85. fileId, uploadResult, err, _ := uploader.UploadWithRetry(
  86. fs,
  87. &filer_pb.AssignVolumeRequest{
  88. Count: 1,
  89. Replication: fs.replication,
  90. Collection: fs.collection,
  91. TtlSec: fs.ttlSec,
  92. DataCenter: fs.dataCenter,
  93. DiskType: fs.diskType,
  94. Path: path,
  95. },
  96. &operation.UploadOption{
  97. Filename: filename,
  98. Cipher: false,
  99. IsInputCompressed: "gzip" == header.Get("Content-Encoding"),
  100. MimeType: header.Get("Content-Type"),
  101. PairMap: nil,
  102. },
  103. func(host, fileId string) string {
  104. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  105. if fs.writeChunkByFiler {
  106. fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", fs.address, fileId)
  107. }
  108. glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
  109. return fileUrl
  110. },
  111. resp.Body,
  112. )
  113. if err != nil {
  114. glog.V(0).Infof("upload source data %v: %v", sourceChunk.GetFileIdString(), err)
  115. return "", fmt.Errorf("upload data: %v", err)
  116. }
  117. if uploadResult.Error != "" {
  118. glog.V(0).Infof("upload failure %v: %v", filename, err)
  119. return "", fmt.Errorf("upload result: %v", uploadResult.Error)
  120. }
  121. return
  122. }
  123. var _ = filer_pb.FilerClient(&FilerSink{})
  124. func (fs *FilerSink) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
  125. return pb.WithGrpcClient(streamingMode, fs.signature, func(grpcConnection *grpc.ClientConn) error {
  126. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  127. return fn(client)
  128. }, fs.grpcAddress, false, fs.grpcDialOption)
  129. }
  130. func (fs *FilerSink) AdjustedUrl(location *filer_pb.Location) string {
  131. return location.Url
  132. }
  133. func (fs *FilerSink) GetDataCenter() string {
  134. return fs.dataCenter
  135. }