filer_source.go 3.4 KB

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