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.

137 lines
3.3 KiB

6 years ago
6 years ago
5 years ago
6 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. 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. for _, loc := range locations.Locations {
  68. fileUrls = append(fileUrls, fmt.Sprintf("http://%s/%s", loc.Url, part))
  69. }
  70. return
  71. }
  72. func (fs *FilerSource) ReadPart(fileId string) (filename string, header http.Header, resp *http.Response, err error) {
  73. if fs.proxyByFiler {
  74. return util.DownloadFile("http://" + fs.address + "/?proxyChunkId=" + fileId)
  75. }
  76. fileUrls, err := fs.LookupFileId(fileId)
  77. if err != nil {
  78. return "", nil, nil, err
  79. }
  80. for _, fileUrl := range fileUrls {
  81. filename, header, resp, err = util.DownloadFile(fileUrl)
  82. if err != nil {
  83. glog.V(1).Infof("fail to read from %s: %v", fileUrl, err)
  84. } else {
  85. break
  86. }
  87. }
  88. return filename, header, resp, err
  89. }
  90. var _ = filer_pb.FilerClient(&FilerSource{})
  91. func (fs *FilerSource) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  92. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  93. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  94. return fn(client)
  95. }, fs.grpcAddress, fs.grpcDialOption)
  96. }
  97. func (fs *FilerSource) AdjustedUrl(location *filer_pb.Location) string {
  98. return location.Url
  99. }
  100. func volumeId(fileId string) string {
  101. lastCommaIndex := strings.LastIndex(fileId, ",")
  102. if lastCommaIndex > 0 {
  103. return fileId[:lastCommaIndex]
  104. }
  105. return fileId
  106. }