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.

155 lines
3.3 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. "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) (serverUrl 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 "", err
  43. }
  44. return vc.GetRandomLocation(uint32(id))
  45. }
  46. func (vc *vidMap) LookupFileId(fileId string) (fullUrl string, err error) {
  47. parts := strings.Split(fileId, ",")
  48. if len(parts) != 2 {
  49. return "", errors.New("Invalid fileId " + fileId)
  50. }
  51. serverUrl, lookupError := vc.LookupVolumeServerUrl(parts[0])
  52. if lookupError != nil {
  53. return "", lookupError
  54. }
  55. return "http://" + serverUrl + "/" + fileId, nil
  56. }
  57. func (vc *vidMap) LookupVolumeServer(fileId string) (volumeServer string, err error) {
  58. parts := strings.Split(fileId, ",")
  59. if len(parts) != 2 {
  60. return "", errors.New("Invalid fileId " + fileId)
  61. }
  62. serverUrl, lookupError := vc.LookupVolumeServerUrl(parts[0])
  63. if lookupError != nil {
  64. return "", lookupError
  65. }
  66. return serverUrl, nil
  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) GetRandomLocation(vid uint32) (serverUrl string, err error) {
  87. vc.RLock()
  88. defer vc.RUnlock()
  89. locations := vc.vid2Locations[vid]
  90. if len(locations) == 0 {
  91. return "", fmt.Errorf("volume %d not found", vid)
  92. }
  93. index, err := vc.getLocationIndex(len(locations))
  94. if err != nil {
  95. return "", fmt.Errorf("volume %d: %v", vid, err)
  96. }
  97. return locations[index].Url, nil
  98. }
  99. func (vc *vidMap) addLocation(vid uint32, location Location) {
  100. vc.Lock()
  101. defer vc.Unlock()
  102. locations, found := vc.vid2Locations[vid]
  103. if !found {
  104. vc.vid2Locations[vid] = []Location{location}
  105. return
  106. }
  107. for _, loc := range locations {
  108. if loc.Url == location.Url {
  109. return
  110. }
  111. }
  112. vc.vid2Locations[vid] = append(locations, location)
  113. }
  114. func (vc *vidMap) deleteLocation(vid uint32, location Location) {
  115. vc.Lock()
  116. defer vc.Unlock()
  117. locations, found := vc.vid2Locations[vid]
  118. if !found {
  119. return
  120. }
  121. for i, loc := range locations {
  122. if loc.Url == location.Url {
  123. vc.vid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  124. break
  125. }
  126. }
  127. }