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.

136 lines
2.8 KiB

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