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.7 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
6 years ago
6 years ago
6 years ago
5 years ago
  1. package filersink
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  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, dir 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, dir)
  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, dir string) (*filer_pb.FileChunk, error) {
  35. fileId, err := fs.fetchAndWrite(sourceChunk, dir)
  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. IsGzipped: sourceChunk.IsGzipped,
  48. }, nil
  49. }
  50. func (fs *FilerSink) fetchAndWrite(sourceChunk *filer_pb.FileChunk, dir string) (fileId string, err error) {
  51. filename, header, readCloser, err := fs.filerSource.ReadPart(sourceChunk.GetFileIdString())
  52. if err != nil {
  53. return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err)
  54. }
  55. defer readCloser.Close()
  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. ParentPath: dir,
  66. }
  67. resp, err := client.AssignVolume(context.Background(), request)
  68. if err != nil {
  69. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  70. return err
  71. }
  72. if resp.Error != "" {
  73. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  74. }
  75. fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
  76. return nil
  77. }); err != nil {
  78. return "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  79. }
  80. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  81. glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
  82. // fetch data as is, regardless whether it is encrypted or not
  83. uploadResult, err := operation.Upload(fileUrl, filename, false, readCloser, "gzip" == header.Get("Content-Encoding"), header.Get("Content-Type"), nil, auth)
  84. if err != nil {
  85. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  86. return "", fmt.Errorf("upload data: %v", err)
  87. }
  88. if uploadResult.Error != "" {
  89. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  90. return "", fmt.Errorf("upload result: %v", uploadResult.Error)
  91. }
  92. return
  93. }
  94. func (fs *FilerSink) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  95. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  96. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  97. return fn(client)
  98. }, fs.grpcAddress, fs.grpcDialOption)
  99. }
  100. func volumeId(fileId string) string {
  101. lastCommaIndex := strings.LastIndex(fileId, ",")
  102. if lastCommaIndex > 0 {
  103. return fileId[:lastCommaIndex]
  104. }
  105. return fileId
  106. }