filer_source.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. }
  23. func (fs *FilerSource) Initialize(configuration util.Configuration, prefix string) error {
  24. return fs.initialize(
  25. configuration.GetString(prefix+"grpcAddress"),
  26. configuration.GetString(prefix+"directory"),
  27. )
  28. }
  29. func (fs *FilerSource) initialize(grpcAddress string, dir string) (err error) {
  30. fs.grpcAddress = grpcAddress
  31. fs.Dir = dir
  32. fs.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
  33. return nil
  34. }
  35. func (fs *FilerSource) LookupFileId(part string) (fileUrl string, err error) {
  36. vid2Locations := make(map[string]*filer_pb.Locations)
  37. vid := volumeId(part)
  38. err = fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  39. glog.V(4).Infof("read lookup volume id locations: %v", vid)
  40. resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
  41. VolumeIds: []string{vid},
  42. })
  43. if err != nil {
  44. return err
  45. }
  46. vid2Locations = resp.LocationsMap
  47. return nil
  48. })
  49. if err != nil {
  50. glog.V(1).Infof("LookupFileId volume id %s: %v", vid, err)
  51. return "", fmt.Errorf("LookupFileId volume id %s: %v", vid, err)
  52. }
  53. locations := vid2Locations[vid]
  54. if locations == nil || len(locations.Locations) == 0 {
  55. glog.V(1).Infof("LookupFileId locate volume id %s: %v", vid, err)
  56. return "", fmt.Errorf("LookupFileId locate volume id %s: %v", vid, err)
  57. }
  58. fileUrl = fmt.Sprintf("http://%s/%s", locations.Locations[0].Url, part)
  59. return
  60. }
  61. func (fs *FilerSource) ReadPart(part string) (filename string, header http.Header, readCloser io.ReadCloser, err error) {
  62. fileUrl, err := fs.LookupFileId(part)
  63. if err != nil {
  64. return "", nil, nil, err
  65. }
  66. filename, header, readCloser, err = util.DownloadFile(fileUrl)
  67. return filename, header, readCloser, err
  68. }
  69. func (fs *FilerSource) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  70. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  71. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  72. return fn(client)
  73. }, fs.grpcAddress, fs.grpcDialOption)
  74. }
  75. func volumeId(fileId string) string {
  76. lastCommaIndex := strings.LastIndex(fileId, ",")
  77. if lastCommaIndex > 0 {
  78. return fileId[:lastCommaIndex]
  79. }
  80. return fileId
  81. }