filer_source.go 3.9 KB

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