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.

133 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. 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(func(client filer_pb.SeaweedFilerClient) error {
  60. request := &filer_pb.AssignVolumeRequest{
  61. Count: 1,
  62. Replication: fs.replication,
  63. Collection: fs.collection,
  64. TtlSec: fs.ttlSec,
  65. DataCenter: fs.dataCenter,
  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. if fs.writeChunkByFiler {
  83. fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", fs.address, fileId)
  84. }
  85. glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
  86. // fetch data as is, regardless whether it is encrypted or not
  87. uploadResult, err, _ := operation.Upload(fileUrl, filename, false, resp.Body, "gzip" == header.Get("Content-Encoding"), header.Get("Content-Type"), nil, auth)
  88. if err != nil {
  89. glog.V(0).Infof("upload source data %v to %s: %v", sourceChunk.GetFileIdString(), fileUrl, err)
  90. return "", fmt.Errorf("upload data: %v", err)
  91. }
  92. if uploadResult.Error != "" {
  93. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, 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(fn func(filer_pb.SeaweedFilerClient) error) error {
  100. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  101. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  102. return fn(client)
  103. }, fs.grpcAddress, fs.grpcDialOption)
  104. }
  105. func (fs *FilerSink) AdjustedUrl(location *filer_pb.Location) string {
  106. return location.Url
  107. }