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.

107 lines
2.7 KiB

6 years ago
6 years ago
6 years ago
  1. package source
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/security"
  6. "github.com/spf13/viper"
  7. "google.golang.org/grpc"
  8. "io"
  9. "net/http"
  10. "strings"
  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) error {
  24. return fs.initialize(
  25. configuration.GetString("grpcAddress"),
  26. configuration.GetString("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(viper.Sub("grpc"), "client")
  33. return nil
  34. }
  35. func (fs *FilerSource) LookupFileId(ctx context.Context, part string) (fileUrl string, err error) {
  36. vid2Locations := make(map[string]*filer_pb.Locations)
  37. vid := volumeId(part)
  38. err = fs.withFilerClient(ctx, fs.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  39. glog.V(4).Infof("read lookup volume id locations: %v", vid)
  40. resp, err := client.LookupVolume(ctx, &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(ctx context.Context, part string) (filename string, header http.Header, readCloser io.ReadCloser, err error) {
  62. fileUrl, err := fs.LookupFileId(ctx, 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(ctx context.Context, grpcDialOption grpc.DialOption, fn func(filer_pb.SeaweedFilerClient) error) error {
  70. return util.WithCachedGrpcClient(ctx, 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. }