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
  1. package operation
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "math/rand"
  8. "net/url"
  9. "strings"
  10. "time"
  11. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. type Location struct {
  15. Url string `json:"url,omitempty"`
  16. PublicUrl string `json:"publicUrl,omitempty"`
  17. }
  18. type LookupResult struct {
  19. VolumeId string `json:"volumeId,omitempty"`
  20. Locations []Location `json:"locations,omitempty"`
  21. Error string `json:"error,omitempty"`
  22. }
  23. func (lr *LookupResult) String() string {
  24. return fmt.Sprintf("VolumeId:%s, Locations:%v, Error:%s", lr.VolumeId, lr.Locations, lr.Error)
  25. }
  26. var (
  27. vc VidCache // caching of volume locations, re-check if after 10 minutes
  28. )
  29. func Lookup(server string, vid string) (ret *LookupResult, err error) {
  30. locations, cache_err := vc.Get(vid)
  31. if cache_err != nil {
  32. if ret, err = do_lookup(server, vid); err == nil {
  33. vc.Set(vid, ret.Locations, 10*time.Minute)
  34. }
  35. } else {
  36. ret = &LookupResult{VolumeId: vid, Locations: locations}
  37. }
  38. return
  39. }
  40. func do_lookup(server string, vid string) (*LookupResult, error) {
  41. values := make(url.Values)
  42. values.Add("volumeId", vid)
  43. jsonBlob, err := util.Post("http://"+server+"/dir/lookup", values)
  44. if err != nil {
  45. return nil, err
  46. }
  47. var ret LookupResult
  48. err = json.Unmarshal(jsonBlob, &ret)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if ret.Error != "" {
  53. return nil, errors.New(ret.Error)
  54. }
  55. return &ret, nil
  56. }
  57. func LookupFileId(server string, fileId string) (fullUrl string, err error) {
  58. parts := strings.Split(fileId, ",")
  59. if len(parts) != 2 {
  60. return "", errors.New("Invalid fileId " + fileId)
  61. }
  62. lookup, lookupError := Lookup(server, parts[0])
  63. if lookupError != nil {
  64. return "", lookupError
  65. }
  66. if len(lookup.Locations) == 0 {
  67. return "", errors.New("File Not Found")
  68. }
  69. return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].Url + "/" + fileId, nil
  70. }
  71. // LookupVolumeIds find volume locations by cache and actual lookup
  72. func LookupVolumeIds(server string, vids []string) (map[string]LookupResult, error) {
  73. ret := make(map[string]LookupResult)
  74. var unknown_vids []string
  75. //check vid cache first
  76. for _, vid := range vids {
  77. locations, cache_err := vc.Get(vid)
  78. if cache_err == nil {
  79. ret[vid] = LookupResult{VolumeId: vid, Locations: locations}
  80. } else {
  81. unknown_vids = append(unknown_vids, vid)
  82. }
  83. }
  84. //return success if all volume ids are known
  85. if len(unknown_vids) == 0 {
  86. return ret, nil
  87. }
  88. //only query unknown_vids
  89. err := withMasterServerClient(server, func(masterClient master_pb.SeaweedClient) error {
  90. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
  91. defer cancel()
  92. req := &master_pb.LookupVolumeRequest{
  93. VolumeIds: unknown_vids,
  94. }
  95. resp, grpcErr := masterClient.LookupVolume(ctx, req)
  96. if grpcErr != nil {
  97. return grpcErr
  98. }
  99. //set newly checked vids to cache
  100. for _, vidLocations := range resp.VolumeIdLocations {
  101. var locations []Location
  102. for _, loc := range vidLocations.Locations {
  103. locations = append(locations, Location{
  104. Url: loc.Url,
  105. PublicUrl: loc.PublicUrl,
  106. })
  107. }
  108. if vidLocations.Error != "" {
  109. vc.Set(vidLocations.VolumeId, locations, 10*time.Minute)
  110. }
  111. ret[vidLocations.VolumeId] = LookupResult{
  112. VolumeId: vidLocations.VolumeId,
  113. Locations: locations,
  114. Error: vidLocations.Error,
  115. }
  116. }
  117. return nil
  118. })
  119. if err != nil {
  120. return nil, err
  121. }
  122. return ret, nil
  123. }