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.

153 lines
3.9 KiB

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