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.

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