filer_source.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
  49. VolumeIds: []string{vid},
  50. })
  51. if err != nil {
  52. return err
  53. }
  54. vid2Locations = resp.LocationsMap
  55. return nil
  56. })
  57. if err != nil {
  58. glog.V(1).Infof("LookupFileId volume id %s: %v", vid, err)
  59. return nil, fmt.Errorf("LookupFileId volume id %s: %v", vid, err)
  60. }
  61. locations := vid2Locations[vid]
  62. if locations == nil || len(locations.Locations) == 0 {
  63. glog.V(1).Infof("LookupFileId locate volume id %s: %v", vid, err)
  64. return nil, fmt.Errorf("LookupFileId locate volume id %s: %v", vid, err)
  65. }
  66. if !fs.proxyByFiler {
  67. for _, loc := range locations.Locations {
  68. fileUrls = append(fileUrls, fmt.Sprintf("http://%s/%s?readDeleted=true", loc.Url, part))
  69. }
  70. } else {
  71. fileUrls = append(fileUrls, fmt.Sprintf("http://%s/?proxyChunkId=%s", fs.address, part))
  72. }
  73. return
  74. }
  75. func (fs *FilerSource) ReadPart(fileId string) (filename string, header http.Header, resp *http.Response, err error) {
  76. if fs.proxyByFiler {
  77. return util.DownloadFile("http://" + fs.address + "/?proxyChunkId=" + fileId)
  78. }
  79. fileUrls, err := fs.LookupFileId(fileId)
  80. if err != nil {
  81. return "", nil, nil, err
  82. }
  83. for _, fileUrl := range fileUrls {
  84. filename, header, resp, err = util.DownloadFile(fileUrl)
  85. if err != nil {
  86. glog.V(1).Infof("fail to read from %s: %v", fileUrl, err)
  87. } else {
  88. break
  89. }
  90. }
  91. return filename, header, resp, err
  92. }
  93. var _ = filer_pb.FilerClient(&FilerSource{})
  94. func (fs *FilerSource) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  95. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  96. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  97. return fn(client)
  98. }, fs.grpcAddress, fs.grpcDialOption)
  99. }
  100. func (fs *FilerSource) AdjustedUrl(location *filer_pb.Location) string {
  101. return location.Url
  102. }
  103. func volumeId(fileId string) string {
  104. lastCommaIndex := strings.LastIndex(fileId, ",")
  105. if lastCommaIndex > 0 {
  106. return fileId[:lastCommaIndex]
  107. }
  108. return fileId
  109. }