vid_map.go 3.9 KB

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