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.

152 lines
3.8 KiB

6 years ago
6 years ago
3 years ago
5 years ago
  1. package source
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "google.golang.org/grpc"
  9. "github.com/seaweedfs/seaweedfs/weed/pb"
  10. "github.com/seaweedfs/seaweedfs/weed/security"
  11. "github.com/seaweedfs/seaweedfs/weed/glog"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/util"
  14. )
  15. type ReplicationSource interface {
  16. ReadPart(part string) io.ReadCloser
  17. }
  18. type FilerSource struct {
  19. grpcAddress string
  20. grpcDialOption grpc.DialOption
  21. Dir string
  22. address string
  23. proxyByFiler bool
  24. dataCenter string
  25. }
  26. func (fs *FilerSource) Initialize(configuration util.Configuration, prefix string) error {
  27. fs.dataCenter = configuration.GetString(prefix + "dataCenter")
  28. return fs.DoInitialize(
  29. "",
  30. configuration.GetString(prefix+"grpcAddress"),
  31. configuration.GetString(prefix+"directory"),
  32. false,
  33. )
  34. }
  35. func (fs *FilerSource) DoInitialize(address, grpcAddress string, dir string, readChunkFromFiler bool) (err error) {
  36. fs.address = address
  37. if fs.address == "" {
  38. fs.address = pb.GrpcAddressToServerAddress(grpcAddress)
  39. }
  40. fs.grpcAddress = grpcAddress
  41. fs.Dir = dir
  42. fs.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
  43. fs.proxyByFiler = readChunkFromFiler
  44. return nil
  45. }
  46. func (fs *FilerSource) LookupFileId(part string) (fileUrls []string, err error) {
  47. vid2Locations := make(map[string]*filer_pb.Locations)
  48. vid := volumeId(part)
  49. err = fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  50. resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
  51. VolumeIds: []string{vid},
  52. })
  53. if err != nil {
  54. return err
  55. }
  56. vid2Locations = resp.LocationsMap
  57. return nil
  58. })
  59. if err != nil {
  60. glog.V(1).Infof("LookupFileId volume id %s: %v", vid, err)
  61. return nil, fmt.Errorf("LookupFileId volume id %s: %v", vid, err)
  62. }
  63. locations := vid2Locations[vid]
  64. if locations == nil || len(locations.Locations) == 0 {
  65. glog.V(1).Infof("LookupFileId locate volume id %s: %v", vid, err)
  66. return nil, fmt.Errorf("LookupFileId locate volume id %s: %v", vid, err)
  67. }
  68. if !fs.proxyByFiler {
  69. for _, loc := range locations.Locations {
  70. fileUrl := fmt.Sprintf("http://%s/%s?readDeleted=true", loc.Url, part)
  71. // Prefer same data center
  72. if fs.dataCenter != "" && fs.dataCenter == loc.DataCenter {
  73. fileUrls = append([]string{fileUrl}, fileUrls...)
  74. } else {
  75. fileUrls = append(fileUrls, fileUrl)
  76. }
  77. }
  78. } else {
  79. fileUrls = append(fileUrls, fmt.Sprintf("http://%s/?proxyChunkId=%s", fs.address, part))
  80. }
  81. return
  82. }
  83. func (fs *FilerSource) ReadPart(fileId string) (filename string, header http.Header, resp *http.Response, err error) {
  84. if fs.proxyByFiler {
  85. return util.DownloadFile("http://"+fs.address+"/?proxyChunkId="+fileId, "")
  86. }
  87. fileUrls, err := fs.LookupFileId(fileId)
  88. if err != nil {
  89. return "", nil, nil, err
  90. }
  91. for _, fileUrl := range fileUrls {
  92. filename, header, resp, err = util.DownloadFile(fileUrl, "")
  93. if err != nil {
  94. glog.V(1).Infof("fail to read from %s: %v", fileUrl, err)
  95. } else {
  96. break
  97. }
  98. }
  99. return filename, header, resp, err
  100. }
  101. var _ = filer_pb.FilerClient(&FilerSource{})
  102. func (fs *FilerSource) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
  103. return pb.WithGrpcClient(streamingMode, func(grpcConnection *grpc.ClientConn) error {
  104. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  105. return fn(client)
  106. }, fs.grpcAddress, false, fs.grpcDialOption)
  107. }
  108. func (fs *FilerSource) AdjustedUrl(location *filer_pb.Location) string {
  109. return location.Url
  110. }
  111. func (fs *FilerSource) GetDataCenter() string {
  112. return fs.dataCenter
  113. }
  114. func volumeId(fileId string) string {
  115. lastCommaIndex := strings.LastIndex(fileId, ",")
  116. if lastCommaIndex > 0 {
  117. return fileId[:lastCommaIndex]
  118. }
  119. return fileId
  120. }