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.

244 lines
5.6 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
2 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/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. }