filer_source.go 3.8 KB

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