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.

113 lines
3.1 KiB

6 years ago
  1. package operation
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "google.golang.org/grpc"
  7. "math/rand"
  8. "strings"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. )
  12. type Location struct {
  13. Url string `json:"url,omitempty"`
  14. PublicUrl string `json:"publicUrl,omitempty"`
  15. }
  16. type LookupResult struct {
  17. VolumeOrFileId string `json:"volumeOrFileId,omitempty"`
  18. Locations []Location `json:"locations,omitempty"`
  19. Jwt string `json:"jwt,omitempty"`
  20. Error string `json:"error,omitempty"`
  21. }
  22. func (lr *LookupResult) String() string {
  23. return fmt.Sprintf("VolumeOrFileId:%s, Locations:%v, Error:%s", lr.VolumeOrFileId, lr.Locations, lr.Error)
  24. }
  25. var (
  26. vc VidCache // caching of volume locations, re-check if after 10 minutes
  27. )
  28. func LookupFileId(masterFn GetMasterFn, grpcDialOption grpc.DialOption, fileId string) (fullUrl string, jwt string, err error) {
  29. parts := strings.Split(fileId, ",")
  30. if len(parts) != 2 {
  31. return "", jwt, errors.New("Invalid fileId " + fileId)
  32. }
  33. lookup, lookupError := LookupVolumeId(masterFn, grpcDialOption, parts[0])
  34. if lookupError != nil {
  35. return "", jwt, lookupError
  36. }
  37. if len(lookup.Locations) == 0 {
  38. return "", jwt, errors.New("File Not Found")
  39. }
  40. return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].Url + "/" + fileId, lookup.Jwt, nil
  41. }
  42. func LookupVolumeId(masterFn GetMasterFn, grpcDialOption grpc.DialOption, vid string) (*LookupResult, error) {
  43. results, err := LookupVolumeIds(masterFn, grpcDialOption, []string{vid})
  44. return results[vid], err
  45. }
  46. // LookupVolumeIds find volume locations by cache and actual lookup
  47. func LookupVolumeIds(masterFn GetMasterFn, grpcDialOption grpc.DialOption, vids []string) (map[string]*LookupResult, error) {
  48. ret := make(map[string]*LookupResult)
  49. var unknown_vids []string
  50. //check vid cache first
  51. for _, vid := range vids {
  52. locations, cacheErr := vc.Get(vid)
  53. if cacheErr == nil {
  54. ret[vid] = &LookupResult{VolumeOrFileId: vid, Locations: locations}
  55. } else {
  56. unknown_vids = append(unknown_vids, vid)
  57. }
  58. }
  59. //return success if all volume ids are known
  60. if len(unknown_vids) == 0 {
  61. return ret, nil
  62. }
  63. //only query unknown_vids
  64. err := WithMasterServerClient(masterFn(), grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  65. req := &master_pb.LookupVolumeRequest{
  66. VolumeOrFileIds: unknown_vids,
  67. }
  68. resp, grpcErr := masterClient.LookupVolume(context.Background(), req)
  69. if grpcErr != nil {
  70. return grpcErr
  71. }
  72. //set newly checked vids to cache
  73. for _, vidLocations := range resp.VolumeIdLocations {
  74. var locations []Location
  75. for _, loc := range vidLocations.Locations {
  76. locations = append(locations, Location{
  77. Url: loc.Url,
  78. PublicUrl: loc.PublicUrl,
  79. })
  80. }
  81. if vidLocations.Error != "" {
  82. vc.Set(vidLocations.VolumeOrFileId, locations, 10*time.Minute)
  83. }
  84. ret[vidLocations.VolumeOrFileId] = &LookupResult{
  85. VolumeOrFileId: vidLocations.VolumeOrFileId,
  86. Locations: locations,
  87. Jwt: vidLocations.Auth,
  88. Error: vidLocations.Error,
  89. }
  90. }
  91. return nil
  92. })
  93. if err != nil {
  94. return nil, err
  95. }
  96. return ret, nil
  97. }