filer_source.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. }