vid_map.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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) GetLocationsClone(vid uint32) (locations []Location, found bool) {
  126. locations, found = vc.GetLocations(vid)
  127. if found {
  128. // clone the locations in case the volume locations are changed below
  129. existingLocations := make([]Location, len(locations))
  130. copy(existingLocations, locations)
  131. return existingLocations, found
  132. }
  133. return nil, false
  134. }
  135. func (vc *vidMap) getLocations(vid uint32) (locations []Location, found bool) {
  136. vc.RLock()
  137. defer vc.RUnlock()
  138. locations, found = vc.vid2Locations[vid]
  139. if found && len(locations) > 0 {
  140. return
  141. }
  142. locations, found = vc.ecVid2Locations[vid]
  143. return
  144. }
  145. func (vc *vidMap) addLocation(vid uint32, location Location) {
  146. vc.Lock()
  147. defer vc.Unlock()
  148. glog.V(4).Infof("+ volume id %d: %+v", vid, location)
  149. locations, found := vc.vid2Locations[vid]
  150. if !found {
  151. vc.vid2Locations[vid] = []Location{location}
  152. return
  153. }
  154. for _, loc := range locations {
  155. if loc.Url == location.Url {
  156. return
  157. }
  158. }
  159. vc.vid2Locations[vid] = append(locations, location)
  160. }
  161. func (vc *vidMap) addEcLocation(vid uint32, location Location) {
  162. vc.Lock()
  163. defer vc.Unlock()
  164. glog.V(4).Infof("+ ec volume id %d: %+v", vid, location)
  165. locations, found := vc.ecVid2Locations[vid]
  166. if !found {
  167. vc.ecVid2Locations[vid] = []Location{location}
  168. return
  169. }
  170. for _, loc := range locations {
  171. if loc.Url == location.Url {
  172. return
  173. }
  174. }
  175. vc.ecVid2Locations[vid] = append(locations, location)
  176. }
  177. func (vc *vidMap) deleteLocation(vid uint32, location Location) {
  178. if vc.cache != nil {
  179. vc.cache.deleteLocation(vid, location)
  180. }
  181. vc.Lock()
  182. defer vc.Unlock()
  183. glog.V(4).Infof("- volume id %d: %+v", vid, location)
  184. locations, found := vc.vid2Locations[vid]
  185. if !found {
  186. return
  187. }
  188. for i, loc := range locations {
  189. if loc.Url == location.Url {
  190. vc.vid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  191. break
  192. }
  193. }
  194. }
  195. func (vc *vidMap) deleteEcLocation(vid uint32, location Location) {
  196. if vc.cache != nil {
  197. vc.cache.deleteLocation(vid, location)
  198. }
  199. vc.Lock()
  200. defer vc.Unlock()
  201. glog.V(4).Infof("- ec volume id %d: %+v", vid, location)
  202. locations, found := vc.ecVid2Locations[vid]
  203. if !found {
  204. return
  205. }
  206. for i, loc := range locations {
  207. if loc.Url == location.Url {
  208. vc.ecVid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  209. break
  210. }
  211. }
  212. }