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.

132 lines
3.6 KiB

6 years ago
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. "sync"
  5. "github.com/seaweedfs/seaweedfs/weed/util"
  6. "google.golang.org/grpc"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/operation"
  9. "github.com/seaweedfs/seaweedfs/weed/pb"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. )
  12. func (fs *FilerSink) replicateChunks(sourceChunks []*filer_pb.FileChunk, path string) (replicatedChunks []*filer_pb.FileChunk, err error) {
  13. if len(sourceChunks) == 0 {
  14. return
  15. }
  16. replicatedChunks = make([]*filer_pb.FileChunk, len(sourceChunks))
  17. var wg sync.WaitGroup
  18. for chunkIndex, sourceChunk := range sourceChunks {
  19. wg.Add(1)
  20. index, source := chunkIndex, sourceChunk
  21. fs.executor.Execute(func() {
  22. defer wg.Done()
  23. util.Retry("replicate chunks", func() error {
  24. replicatedChunk, e := fs.replicateOneChunk(source, path)
  25. if e != nil {
  26. err = e
  27. return e
  28. }
  29. replicatedChunks[index] = replicatedChunk
  30. err = nil
  31. return nil
  32. })
  33. })
  34. }
  35. wg.Wait()
  36. return
  37. }
  38. func (fs *FilerSink) replicateOneChunk(sourceChunk *filer_pb.FileChunk, path string) (*filer_pb.FileChunk, error) {
  39. fileId, err := fs.fetchAndWrite(sourceChunk, path)
  40. if err != nil {
  41. return nil, fmt.Errorf("copy %s: %v", sourceChunk.GetFileIdString(), err)
  42. }
  43. return &filer_pb.FileChunk{
  44. FileId: fileId,
  45. Offset: sourceChunk.Offset,
  46. Size: sourceChunk.Size,
  47. ModifiedTsNs: sourceChunk.ModifiedTsNs,
  48. ETag: sourceChunk.ETag,
  49. SourceFileId: sourceChunk.GetFileIdString(),
  50. CipherKey: sourceChunk.CipherKey,
  51. IsCompressed: sourceChunk.IsCompressed,
  52. }, nil
  53. }
  54. func (fs *FilerSink) fetchAndWrite(sourceChunk *filer_pb.FileChunk, path string) (fileId string, err error) {
  55. filename, header, resp, err := fs.filerSource.ReadPart(sourceChunk.GetFileIdString())
  56. if err != nil {
  57. return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err)
  58. }
  59. defer util.CloseResponse(resp)
  60. fileId, uploadResult, err, _ := operation.UploadWithRetry(
  61. fs,
  62. &filer_pb.AssignVolumeRequest{
  63. Count: 1,
  64. Replication: fs.replication,
  65. Collection: fs.collection,
  66. TtlSec: fs.ttlSec,
  67. DataCenter: fs.dataCenter,
  68. DiskType: fs.diskType,
  69. Path: path,
  70. },
  71. &operation.UploadOption{
  72. Filename: filename,
  73. Cipher: false,
  74. IsInputCompressed: "gzip" == header.Get("Content-Encoding"),
  75. MimeType: header.Get("Content-Type"),
  76. PairMap: nil,
  77. },
  78. func(host, fileId string) string {
  79. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  80. if fs.writeChunkByFiler {
  81. fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", fs.address, fileId)
  82. }
  83. glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
  84. return fileUrl
  85. },
  86. resp.Body,
  87. )
  88. if err != nil {
  89. glog.V(0).Infof("upload source data %v: %v", sourceChunk.GetFileIdString(), err)
  90. return "", fmt.Errorf("upload data: %v", err)
  91. }
  92. if uploadResult.Error != "" {
  93. glog.V(0).Infof("upload failure %v: %v", filename, err)
  94. return "", fmt.Errorf("upload result: %v", uploadResult.Error)
  95. }
  96. return
  97. }
  98. var _ = filer_pb.FilerClient(&FilerSink{})
  99. func (fs *FilerSink) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
  100. return pb.WithGrpcClient(streamingMode, func(grpcConnection *grpc.ClientConn) error {
  101. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  102. return fn(client)
  103. }, fs.grpcAddress, false, fs.grpcDialOption)
  104. }
  105. func (fs *FilerSink) AdjustedUrl(location *filer_pb.Location) string {
  106. return location.Url
  107. }
  108. func (fs *FilerSink) GetDataCenter() string {
  109. return fs.dataCenter
  110. }