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.

99 lines
1.9 KiB

6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package wdclient
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. )
  11. type Location struct {
  12. Url string `json:"url,omitempty"`
  13. PublicUrl string `json:"publicUrl,omitempty"`
  14. }
  15. type vidMap struct {
  16. sync.RWMutex
  17. vid2Locations map[uint32][]Location
  18. }
  19. func newVidMap() vidMap {
  20. return vidMap{
  21. vid2Locations: make(map[uint32][]Location),
  22. }
  23. }
  24. func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrl string, err error) {
  25. id, err := strconv.Atoi(vid)
  26. if err != nil {
  27. glog.V(1).Infof("Unknown volume id %s", vid)
  28. return "", err
  29. }
  30. locations := vc.GetLocations(uint32(id))
  31. if len(locations) == 0 {
  32. return "", fmt.Errorf("volume %d not found", id)
  33. }
  34. return locations[rand.Intn(len(locations))].Url, nil
  35. }
  36. func (vc *vidMap) LookupFileId(fileId string) (fullUrl string, err error) {
  37. parts := strings.Split(fileId, ",")
  38. if len(parts) != 2 {
  39. return "", errors.New("Invalid fileId " + fileId)
  40. }
  41. serverUrl, lookupError := vc.LookupVolumeServerUrl(parts[0])
  42. if lookupError != nil {
  43. return "", lookupError
  44. }
  45. return "http://" + serverUrl + "/" + fileId, nil
  46. }
  47. func (vc *vidMap) GetLocations(vid uint32) (locations []Location) {
  48. vc.RLock()
  49. defer vc.RUnlock()
  50. return vc.vid2Locations[vid]
  51. }
  52. func (vc *vidMap) addLocation(vid uint32, location Location) {
  53. vc.Lock()
  54. defer vc.Unlock()
  55. locations, found := vc.vid2Locations[vid]
  56. if !found {
  57. vc.vid2Locations[vid] = []Location{location}
  58. return
  59. }
  60. for _, loc := range locations {
  61. if loc.Url == location.Url {
  62. return
  63. }
  64. }
  65. vc.vid2Locations[vid] = append(locations, location)
  66. }
  67. func (vc *vidMap) deleteLocation(vid uint32, location Location) {
  68. vc.Lock()
  69. defer vc.Unlock()
  70. locations, found := vc.vid2Locations[vid]
  71. if !found {
  72. return
  73. }
  74. for i, loc := range locations {
  75. if loc.Url == location.Url {
  76. vc.vid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  77. }
  78. }
  79. }