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.

152 lines
3.3 KiB

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