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.

145 lines
3.7 KiB

6 years ago
6 years ago
  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. func LookupVolumeId(masterFn GetMasterFn, grpcDialOption grpc.DialOption, vid string) (*LookupResult, error) {
  74. results, err := LookupVolumeIds(masterFn, grpcDialOption, []string{vid})
  75. return results[vid], err
  76. }
  77. // LookupVolumeIds find volume locations by cache and actual lookup
  78. func LookupVolumeIds(masterFn GetMasterFn, grpcDialOption grpc.DialOption, vids []string) (map[string]*LookupResult, error) {
  79. ret := make(map[string]*LookupResult)
  80. var unknown_vids []string
  81. //check vid cache first
  82. for _, vid := range vids {
  83. locations, cacheErr := vc.Get(vid)
  84. if cacheErr == nil {
  85. ret[vid] = &LookupResult{VolumeId: vid, Locations: locations}
  86. } else {
  87. unknown_vids = append(unknown_vids, vid)
  88. }
  89. }
  90. //return success if all volume ids are known
  91. if len(unknown_vids) == 0 {
  92. return ret, nil
  93. }
  94. //only query unknown_vids
  95. err := WithMasterServerClient(masterFn(), grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  96. req := &master_pb.LookupVolumeRequest{
  97. VolumeIds: unknown_vids,
  98. }
  99. resp, grpcErr := masterClient.LookupVolume(context.Background(), req)
  100. if grpcErr != nil {
  101. return grpcErr
  102. }
  103. //set newly checked vids to cache
  104. for _, vidLocations := range resp.VolumeIdLocations {
  105. var locations []Location
  106. for _, loc := range vidLocations.Locations {
  107. locations = append(locations, Location{
  108. Url: loc.Url,
  109. PublicUrl: loc.PublicUrl,
  110. })
  111. }
  112. if vidLocations.Error != "" {
  113. vc.Set(vidLocations.VolumeId, locations, 10*time.Minute)
  114. }
  115. ret[vidLocations.VolumeId] = &LookupResult{
  116. VolumeId: vidLocations.VolumeId,
  117. Locations: locations,
  118. Error: vidLocations.Error,
  119. }
  120. }
  121. return nil
  122. })
  123. if err != nil {
  124. return nil, err
  125. }
  126. return ret, nil
  127. }