You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.0 KiB

6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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.DoInitialize(
  25. configuration.GetString(prefix+"grpcAddress"),
  26. configuration.GetString(prefix+"directory"),
  27. )
  28. }
  29. func (fs *FilerSource) DoInitialize(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) (fileUrls []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 nil, 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 nil, fmt.Errorf("LookupFileId locate volume id %s: %v", vid, err)
  57. }
  58. for _, loc := range locations.Locations {
  59. fileUrls = append(fileUrls, fmt.Sprintf("http://%s/%s", loc.Url, part))
  60. }
  61. return
  62. }
  63. func (fs *FilerSource) ReadPart(part string) (filename string, header http.Header, resp *http.Response, err error) {
  64. fileUrls, err := fs.LookupFileId(part)
  65. if err != nil {
  66. return "", nil, nil, err
  67. }
  68. for _, fileUrl := range fileUrls {
  69. filename, header, resp, err = util.DownloadFile(fileUrl)
  70. if err != nil {
  71. glog.V(1).Infof("fail to read from %s: %v", fileUrl, err)
  72. } else {
  73. break
  74. }
  75. }
  76. return filename, header, resp, err
  77. }
  78. var _ = filer_pb.FilerClient(&FilerSource{})
  79. func (fs *FilerSource) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  80. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  81. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  82. return fn(client)
  83. }, fs.grpcAddress, fs.grpcDialOption)
  84. }
  85. func (fs *FilerSource) AdjustedUrl(location *filer_pb.Location) string {
  86. return location.Url
  87. }
  88. func volumeId(fileId string) string {
  89. lastCommaIndex := strings.LastIndex(fileId, ",")
  90. if lastCommaIndex > 0 {
  91. return fileId[:lastCommaIndex]
  92. }
  93. return fileId
  94. }