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.

137 lines
2.8 KiB

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