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.

130 lines
3.8 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
5 years ago
5 years ago
5 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. }
  28. replicatedChunks[index] = replicatedChunk
  29. }(sourceChunk, chunkIndex)
  30. }
  31. wg.Wait()
  32. return
  33. }
  34. func (fs *FilerSink) replicateOneChunk(sourceChunk *filer_pb.FileChunk, path string) (*filer_pb.FileChunk, error) {
  35. fileId, err := fs.fetchAndWrite(sourceChunk, path)
  36. if err != nil {
  37. return nil, fmt.Errorf("copy %s: %v", sourceChunk.GetFileIdString(), err)
  38. }
  39. return &filer_pb.FileChunk{
  40. FileId: fileId,
  41. Offset: sourceChunk.Offset,
  42. Size: sourceChunk.Size,
  43. Mtime: sourceChunk.Mtime,
  44. ETag: sourceChunk.ETag,
  45. SourceFileId: sourceChunk.GetFileIdString(),
  46. CipherKey: sourceChunk.CipherKey,
  47. IsCompressed: sourceChunk.IsCompressed,
  48. }, nil
  49. }
  50. func (fs *FilerSink) fetchAndWrite(sourceChunk *filer_pb.FileChunk, path string) (fileId string, err error) {
  51. filename, header, resp, err := fs.filerSource.ReadPart(sourceChunk.GetFileIdString())
  52. if err != nil {
  53. return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err)
  54. }
  55. defer util.CloseResponse(resp)
  56. var host string
  57. var auth security.EncodedJwt
  58. if err := fs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  59. request := &filer_pb.AssignVolumeRequest{
  60. Count: 1,
  61. Replication: fs.replication,
  62. Collection: fs.collection,
  63. TtlSec: fs.ttlSec,
  64. DataCenter: fs.dataCenter,
  65. DiskType: fs.diskType,
  66. Path: path,
  67. }
  68. resp, err := client.AssignVolume(context.Background(), request)
  69. if err != nil {
  70. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  71. return err
  72. }
  73. if resp.Error != "" {
  74. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  75. }
  76. fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
  77. return nil
  78. }); err != nil {
  79. return "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  80. }
  81. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  82. glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
  83. // fetch data as is, regardless whether it is encrypted or not
  84. uploadResult, err, _ := operation.Upload(fileUrl, filename, false, resp.Body, "gzip" == header.Get("Content-Encoding"), header.Get("Content-Type"), nil, auth)
  85. if err != nil {
  86. glog.V(0).Infof("upload source data %v to %s: %v", sourceChunk.GetFileIdString(), fileUrl, err)
  87. return "", fmt.Errorf("upload data: %v", err)
  88. }
  89. if uploadResult.Error != "" {
  90. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  91. return "", fmt.Errorf("upload result: %v", uploadResult.Error)
  92. }
  93. return
  94. }
  95. var _ = filer_pb.FilerClient(&FilerSink{})
  96. func (fs *FilerSink) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  97. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  98. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  99. return fn(client)
  100. }, fs.grpcAddress, fs.grpcDialOption)
  101. }
  102. func (fs *FilerSink) AdjustedUrl(location *filer_pb.Location) string {
  103. return location.Url
  104. }