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.

124 lines
2.6 KiB

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