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
  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. req := &master_pb.LookupVolumeRequest{
  91. VolumeIds: unknown_vids,
  92. }
  93. resp, grpcErr := masterClient.LookupVolume(context.Background(), req)
  94. if grpcErr != nil {
  95. return grpcErr
  96. }
  97. //set newly checked vids to cache
  98. for _, vidLocations := range resp.VolumeIdLocations {
  99. var locations []Location
  100. for _, loc := range vidLocations.Locations {
  101. locations = append(locations, Location{
  102. Url: loc.Url,
  103. PublicUrl: loc.PublicUrl,
  104. })
  105. }
  106. if vidLocations.Error != "" {
  107. vc.Set(vidLocations.VolumeId, locations, 10*time.Minute)
  108. }
  109. ret[vidLocations.VolumeId] = LookupResult{
  110. VolumeId: vidLocations.VolumeId,
  111. Locations: locations,
  112. Error: vidLocations.Error,
  113. }
  114. }
  115. return nil
  116. })
  117. if err != nil {
  118. return nil, err
  119. }
  120. return ret, nil
  121. }