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.

140 lines
3.4 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. 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. }