vid_map.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package wdclient
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "math/rand"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "sync/atomic"
  11. "github.com/seaweedfs/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. ecVid2Locations map[uint32][]Location
  33. DataCenter string
  34. cursor int32
  35. cache *vidMap
  36. }
  37. func newVidMap(dataCenter string) *vidMap {
  38. return &vidMap{
  39. vid2Locations: make(map[uint32][]Location),
  40. ecVid2Locations: make(map[uint32][]Location),
  41. DataCenter: dataCenter,
  42. cursor: -1,
  43. }
  44. }
  45. func (vc *vidMap) getLocationIndex(length int) (int, error) {
  46. if length <= 0 {
  47. return 0, fmt.Errorf("invalid length: %d", length)
  48. }
  49. if atomic.LoadInt32(&vc.cursor) == maxCursorIndex {
  50. atomic.CompareAndSwapInt32(&vc.cursor, maxCursorIndex, -1)
  51. }
  52. return int(atomic.AddInt32(&vc.cursor, 1)) % length, nil
  53. }
  54. func (vc *vidMap) isSameDataCenter(loc *Location) bool {
  55. if vc.DataCenter == "" || loc.DataCenter == "" || vc.DataCenter != loc.DataCenter {
  56. return false
  57. }
  58. return true
  59. }
  60. func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrls []string, err error) {
  61. id, err := strconv.Atoi(vid)
  62. if err != nil {
  63. glog.V(1).Infof("Unknown volume id %s", vid)
  64. return nil, err
  65. }
  66. locations, found := vc.GetLocations(uint32(id))
  67. if !found {
  68. return nil, fmt.Errorf("volume %d not found", id)
  69. }
  70. var sameDcServers, otherDcServers []string
  71. for _, loc := range locations {
  72. if vc.isSameDataCenter(&loc) {
  73. sameDcServers = append(sameDcServers, loc.Url)
  74. } else {
  75. otherDcServers = append(otherDcServers, loc.Url)
  76. }
  77. }
  78. rand.Shuffle(len(sameDcServers), func(i, j int) {
  79. sameDcServers[i], sameDcServers[j] = sameDcServers[j], sameDcServers[i]
  80. })
  81. rand.Shuffle(len(otherDcServers), func(i, j int) {
  82. otherDcServers[i], otherDcServers[j] = otherDcServers[j], otherDcServers[i]
  83. })
  84. // Prefer same data center
  85. serverUrls = append(sameDcServers, otherDcServers...)
  86. return
  87. }
  88. func (vc *vidMap) LookupFileId(fileId string) (fullUrls []string, err error) {
  89. parts := strings.Split(fileId, ",")
  90. if len(parts) != 2 {
  91. return nil, errors.New("Invalid fileId " + fileId)
  92. }
  93. serverUrls, lookupError := vc.LookupVolumeServerUrl(parts[0])
  94. if lookupError != nil {
  95. return nil, lookupError
  96. }
  97. for _, serverUrl := range serverUrls {
  98. fullUrls = append(fullUrls, "http://"+serverUrl+"/"+fileId)
  99. }
  100. return
  101. }
  102. func (vc *vidMap) GetVidLocations(vid string) (locations []Location, err error) {
  103. id, err := strconv.Atoi(vid)
  104. if err != nil {
  105. glog.V(1).Infof("Unknown volume id %s", vid)
  106. return nil, fmt.Errorf("Unknown volume id %s", vid)
  107. }
  108. foundLocations, found := vc.GetLocations(uint32(id))
  109. if found {
  110. return foundLocations, nil
  111. }
  112. return nil, fmt.Errorf("volume id %s not found", vid)
  113. }
  114. func (vc *vidMap) GetLocations(vid uint32) (locations []Location, found bool) {
  115. // glog.V(4).Infof("~ lookup volume id %d: %+v ec:%+v", vid, vc.vid2Locations, vc.ecVid2Locations)
  116. locations, found = vc.getLocations(vid)
  117. if found && len(locations) > 0 {
  118. return locations, found
  119. }
  120. if vc.cache != nil {
  121. return vc.cache.GetLocations(vid)
  122. }
  123. return nil, false
  124. }
  125. func (vc *vidMap) getLocations(vid uint32) (locations []Location, found bool) {
  126. vc.RLock()
  127. defer vc.RUnlock()
  128. locations, found = vc.vid2Locations[vid]
  129. if found && len(locations) > 0 {
  130. return
  131. }
  132. locations, found = vc.ecVid2Locations[vid]
  133. return
  134. }
  135. func (vc *vidMap) addLocation(vid uint32, location Location) {
  136. vc.Lock()
  137. defer vc.Unlock()
  138. glog.V(4).Infof("+ volume id %d: %+v", vid, location)
  139. locations, found := vc.vid2Locations[vid]
  140. if !found {
  141. vc.vid2Locations[vid] = []Location{location}
  142. return
  143. }
  144. for _, loc := range locations {
  145. if loc.Url == location.Url {
  146. return
  147. }
  148. }
  149. vc.vid2Locations[vid] = append(locations, location)
  150. }
  151. func (vc *vidMap) addEcLocation(vid uint32, location Location) {
  152. vc.Lock()
  153. defer vc.Unlock()
  154. glog.V(4).Infof("+ ec volume id %d: %+v", vid, location)
  155. locations, found := vc.ecVid2Locations[vid]
  156. if !found {
  157. vc.ecVid2Locations[vid] = []Location{location}
  158. return
  159. }
  160. for _, loc := range locations {
  161. if loc.Url == location.Url {
  162. return
  163. }
  164. }
  165. vc.ecVid2Locations[vid] = append(locations, location)
  166. }
  167. func (vc *vidMap) deleteLocation(vid uint32, location Location) {
  168. if vc.cache != nil {
  169. vc.cache.deleteLocation(vid, location)
  170. }
  171. vc.Lock()
  172. defer vc.Unlock()
  173. glog.V(4).Infof("- volume id %d: %+v", vid, location)
  174. locations, found := vc.vid2Locations[vid]
  175. if !found {
  176. return
  177. }
  178. for i, loc := range locations {
  179. if loc.Url == location.Url {
  180. vc.vid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  181. break
  182. }
  183. }
  184. }
  185. func (vc *vidMap) deleteEcLocation(vid uint32, location Location) {
  186. if vc.cache != nil {
  187. vc.cache.deleteLocation(vid, location)
  188. }
  189. vc.Lock()
  190. defer vc.Unlock()
  191. glog.V(4).Infof("- ec volume id %d: %+v", vid, location)
  192. locations, found := vc.ecVid2Locations[vid]
  193. if !found {
  194. return
  195. }
  196. for i, loc := range locations {
  197. if loc.Url == location.Url {
  198. vc.ecVid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  199. break
  200. }
  201. }
  202. }