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.

126 lines
3.5 KiB

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