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.

144 lines
4.2 KiB

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