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.

142 lines
3.1 KiB

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