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.

128 lines
3.6 KiB

6 years ago
6 years ago
6 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. var wg sync.WaitGroup
  19. for _, sourceChunk := range sourceChunks {
  20. wg.Add(1)
  21. go func(chunk *filer_pb.FileChunk) {
  22. defer wg.Done()
  23. replicatedChunk, e := fs.replicateOneChunk(chunk, dir)
  24. if e != nil {
  25. err = e
  26. }
  27. replicatedChunks = append(replicatedChunks, replicatedChunk)
  28. }(sourceChunk)
  29. }
  30. wg.Wait()
  31. return
  32. }
  33. func (fs *FilerSink) replicateOneChunk(sourceChunk *filer_pb.FileChunk, dir string) (*filer_pb.FileChunk, error) {
  34. fileId, err := fs.fetchAndWrite(sourceChunk, dir)
  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. }, nil
  47. }
  48. func (fs *FilerSink) fetchAndWrite(sourceChunk *filer_pb.FileChunk, dir string) (fileId string, err error) {
  49. filename, header, readCloser, err := fs.filerSource.ReadPart(sourceChunk.GetFileIdString())
  50. if err != nil {
  51. return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err)
  52. }
  53. defer readCloser.Close()
  54. var host string
  55. var auth security.EncodedJwt
  56. if err := fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  57. request := &filer_pb.AssignVolumeRequest{
  58. Count: 1,
  59. Replication: fs.replication,
  60. Collection: fs.collection,
  61. TtlSec: fs.ttlSec,
  62. DataCenter: fs.dataCenter,
  63. ParentPath: dir,
  64. }
  65. resp, err := client.AssignVolume(context.Background(), request)
  66. if err != nil {
  67. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  68. return err
  69. }
  70. if resp.Error != "" {
  71. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  72. }
  73. fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
  74. return nil
  75. }); err != nil {
  76. return "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  77. }
  78. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  79. glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
  80. // fetch data as is, regardless whether it is encrypted or not
  81. uploadResult, err := operation.Upload(fileUrl, filename, false, readCloser, "gzip" == header.Get("Content-Encoding"), header.Get("Content-Type"), nil, auth)
  82. if err != nil {
  83. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  84. return "", fmt.Errorf("upload data: %v", err)
  85. }
  86. if uploadResult.Error != "" {
  87. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  88. return "", fmt.Errorf("upload result: %v", uploadResult.Error)
  89. }
  90. return
  91. }
  92. func (fs *FilerSink) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  93. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  94. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  95. return fn(client)
  96. }, fs.grpcAddress, fs.grpcDialOption)
  97. }
  98. func volumeId(fileId string) string {
  99. lastCommaIndex := strings.LastIndex(fileId, ",")
  100. if lastCommaIndex > 0 {
  101. return fileId[:lastCommaIndex]
  102. }
  103. return fileId
  104. }