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.

127 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  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. }, nil
  46. }
  47. func (fs *FilerSink) fetchAndWrite(sourceChunk *filer_pb.FileChunk, dir string) (fileId string, err error) {
  48. filename, header, readCloser, err := fs.filerSource.ReadPart(sourceChunk.GetFileIdString())
  49. if err != nil {
  50. return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err)
  51. }
  52. defer readCloser.Close()
  53. var host string
  54. var auth security.EncodedJwt
  55. if err := fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  56. request := &filer_pb.AssignVolumeRequest{
  57. Count: 1,
  58. Replication: fs.replication,
  59. Collection: fs.collection,
  60. TtlSec: fs.ttlSec,
  61. DataCenter: fs.dataCenter,
  62. ParentPath: dir,
  63. }
  64. resp, err := client.AssignVolume(context.Background(), request)
  65. if err != nil {
  66. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  67. return err
  68. }
  69. if resp.Error != "" {
  70. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  71. }
  72. fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
  73. return nil
  74. }); err != nil {
  75. return "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  76. }
  77. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  78. glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
  79. uploadResult, err := operation.Upload(fileUrl, filename, readCloser,
  80. "gzip" == header.Get("Content-Encoding"), header.Get("Content-Type"), nil, auth)
  81. if err != nil {
  82. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  83. return "", fmt.Errorf("upload data: %v", err)
  84. }
  85. if uploadResult.Error != "" {
  86. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  87. return "", fmt.Errorf("upload result: %v", uploadResult.Error)
  88. }
  89. return
  90. }
  91. func (fs *FilerSink) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  92. return util.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  93. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  94. return fn(client)
  95. }, fs.grpcAddress, fs.grpcDialOption)
  96. }
  97. func volumeId(fileId string) string {
  98. lastCommaIndex := strings.LastIndex(fileId, ",")
  99. if lastCommaIndex > 0 {
  100. return fileId[:lastCommaIndex]
  101. }
  102. return fileId
  103. }