lookup.go 3.5 KB

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