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
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
5 years ago
5 years ago
  1. package filersink
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "google.golang.org/grpc"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. )
  13. func (fs *FilerSink) replicateChunks(sourceChunks []*filer_pb.FileChunk, dir string) (replicatedChunks []*filer_pb.FileChunk, err error) {
  14. if len(sourceChunks) == 0 {
  15. return
  16. }
  17. replicatedChunks = make([]*filer_pb.FileChunk, len(sourceChunks))
  18. var wg sync.WaitGroup
  19. for chunkIndex, sourceChunk := range sourceChunks {
  20. wg.Add(1)
  21. go func(chunk *filer_pb.FileChunk, index int) {
  22. defer wg.Done()
  23. replicatedChunk, e := fs.replicateOneChunk(chunk, dir)
  24. if e != nil {
  25. err = e
  26. }
  27. replicatedChunks[index] = replicatedChunk
  28. }(sourceChunk, chunkIndex)
  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. IsCompressed: sourceChunk.IsCompressed,
  47. }, nil
  48. }
  49. func (fs *FilerSink) fetchAndWrite(sourceChunk *filer_pb.FileChunk, dir string) (fileId string, err error) {
  50. filename, header, readCloser, err := fs.filerSource.ReadPart(sourceChunk.GetFileIdString())
  51. if err != nil {
  52. return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err)
  53. }
  54. defer readCloser.Close()
  55. var host string
  56. var auth security.EncodedJwt
  57. if err := fs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  58. request := &filer_pb.AssignVolumeRequest{
  59. Count: 1,
  60. Replication: fs.replication,
  61. Collection: fs.collection,
  62. TtlSec: fs.ttlSec,
  63. DataCenter: fs.dataCenter,
  64. ParentPath: dir,
  65. }
  66. resp, err := client.AssignVolume(context.Background(), request)
  67. if err != nil {
  68. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  69. return err
  70. }
  71. if resp.Error != "" {
  72. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  73. }
  74. fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
  75. return nil
  76. }); err != nil {
  77. return "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  78. }
  79. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  80. glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
  81. // fetch data as is, regardless whether it is encrypted or not
  82. uploadResult, err, _ := operation.Upload(fileUrl, filename, false, readCloser, "gzip" == header.Get("Content-Encoding"), header.Get("Content-Type"), nil, auth)
  83. if err != nil {
  84. glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
  85. return "", fmt.Errorf("upload data: %v", err)
  86. }
  87. if uploadResult.Error != "" {
  88. glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
  89. return "", fmt.Errorf("upload result: %v", uploadResult.Error)
  90. }
  91. return
  92. }
  93. var _ = filer_pb.FilerClient(&FilerSink{})
  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 (fs *FilerSink) AdjustedUrl(hostAndPort string) string {
  101. return hostAndPort
  102. }