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.

158 lines
4.3 KiB

6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
2 years ago
2 years ago
6 years ago
6 years ago
6 years ago
5 years ago
  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. }