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.

288 lines
6.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
3 years ago
4 years ago
7 years ago
7 years ago
6 years ago
  1. package wdclient
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "sync/atomic"
  10. "github.com/seaweedfs/seaweedfs/weed/pb"
  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. DataInRemote bool `json:"dataInRemote,omitempty"`
  26. }
  27. func (l Location) ServerAddress() pb.ServerAddress {
  28. return pb.NewServerAddressWithGrpcPort(l.Url, l.GrpcPort)
  29. }
  30. type vidMap struct {
  31. sync.RWMutex
  32. vid2Locations map[uint32][]Location
  33. ecVid2Locations map[uint32][]Location
  34. DataCenter string
  35. cursor int32
  36. cache *vidMap
  37. }
  38. func newVidMap(dataCenter string) *vidMap {
  39. return &vidMap{
  40. vid2Locations: make(map[uint32][]Location),
  41. ecVid2Locations: make(map[uint32][]Location),
  42. DataCenter: dataCenter,
  43. cursor: -1,
  44. }
  45. }
  46. func (vc *vidMap) getLocationIndex(length int) (int, error) {
  47. if length <= 0 {
  48. return 0, fmt.Errorf("invalid length: %d", length)
  49. }
  50. if atomic.LoadInt32(&vc.cursor) == maxCursorIndex {
  51. atomic.CompareAndSwapInt32(&vc.cursor, maxCursorIndex, -1)
  52. }
  53. return int(atomic.AddInt32(&vc.cursor, 1)) % length, nil
  54. }
  55. func (vc *vidMap) isSameDataCenter(loc *Location) bool {
  56. if vc.DataCenter == "" || loc.DataCenter == "" || vc.DataCenter != loc.DataCenter {
  57. return false
  58. }
  59. return true
  60. }
  61. func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrls []string, err error) {
  62. id, err := strconv.Atoi(vid)
  63. if err != nil {
  64. glog.V(1).Infof("Unknown volume id %s", vid)
  65. return nil, err
  66. }
  67. locations, found := vc.GetLocations(uint32(id))
  68. if !found {
  69. return nil, fmt.Errorf("volume %d not found", id)
  70. }
  71. var sameDcServers, otherDcServers []string
  72. localUrls := make(map[string]bool)
  73. for _, loc := range locations {
  74. glog.V(4).Infof("lookup %s => %s, data in remote storage tier: %v", vid, loc.Url, loc.DataInRemote)
  75. if !loc.DataInRemote {
  76. localUrls[loc.Url] = true
  77. }
  78. if vc.isSameDataCenter(&loc) {
  79. sameDcServers = append(sameDcServers, loc.Url)
  80. } else {
  81. otherDcServers = append(otherDcServers, loc.Url)
  82. }
  83. }
  84. rand.Shuffle(len(sameDcServers), func(i, j int) {
  85. sameDcServers[i], sameDcServers[j] = sameDcServers[j], sameDcServers[i]
  86. })
  87. if len(localUrls) > 0 && len(localUrls) != len(sameDcServers) {
  88. for idx, url := range sameDcServers {
  89. // move local url to the front of the list
  90. if _, found := localUrls[url]; found {
  91. if idx != 0 {
  92. sameDcServers[idx], sameDcServers[0] = sameDcServers[0], sameDcServers[idx]
  93. }
  94. break
  95. }
  96. }
  97. }
  98. rand.Shuffle(len(otherDcServers), func(i, j int) {
  99. otherDcServers[i], otherDcServers[j] = otherDcServers[j], otherDcServers[i]
  100. })
  101. if len(localUrls) > 0 && len(localUrls) != len(otherDcServers) {
  102. for idx, url := range otherDcServers {
  103. if _, found := localUrls[url]; found {
  104. if idx != 0 {
  105. otherDcServers[idx], otherDcServers[0] = otherDcServers[0], otherDcServers[idx]
  106. }
  107. break
  108. }
  109. }
  110. }
  111. // Prefer same data center
  112. serverUrls = append(sameDcServers, otherDcServers...)
  113. return
  114. }
  115. func (vc *vidMap) LookupFileId(fileId string) (fullUrls []string, err error) {
  116. parts := strings.Split(fileId, ",")
  117. if len(parts) != 2 {
  118. return nil, errors.New("Invalid fileId " + fileId)
  119. }
  120. serverUrls, lookupError := vc.LookupVolumeServerUrl(parts[0])
  121. if lookupError != nil {
  122. return nil, lookupError
  123. }
  124. for _, serverUrl := range serverUrls {
  125. fullUrls = append(fullUrls, "http://"+serverUrl+"/"+fileId)
  126. }
  127. return
  128. }
  129. func (vc *vidMap) GetVidLocations(vid string) (locations []Location, err error) {
  130. id, err := strconv.Atoi(vid)
  131. if err != nil {
  132. glog.V(1).Infof("Unknown volume id %s", vid)
  133. return nil, fmt.Errorf("Unknown volume id %s", vid)
  134. }
  135. foundLocations, found := vc.GetLocations(uint32(id))
  136. if found {
  137. return foundLocations, nil
  138. }
  139. return nil, fmt.Errorf("volume id %s not found", vid)
  140. }
  141. func (vc *vidMap) GetLocations(vid uint32) (locations []Location, found bool) {
  142. // glog.V(4).Infof("~ lookup volume id %d: %+v ec:%+v", vid, vc.vid2Locations, vc.ecVid2Locations)
  143. locations, found = vc.getLocations(vid)
  144. if found && len(locations) > 0 {
  145. return locations, found
  146. }
  147. if vc.cache != nil {
  148. return vc.cache.GetLocations(vid)
  149. }
  150. return nil, false
  151. }
  152. func (vc *vidMap) GetLocationsClone(vid uint32) (locations []Location, found bool) {
  153. locations, found = vc.GetLocations(vid)
  154. if found {
  155. // clone the locations in case the volume locations are changed below
  156. existingLocations := make([]Location, len(locations))
  157. copy(existingLocations, locations)
  158. return existingLocations, found
  159. }
  160. return nil, false
  161. }
  162. func (vc *vidMap) getLocations(vid uint32) (locations []Location, found bool) {
  163. vc.RLock()
  164. defer vc.RUnlock()
  165. locations, found = vc.vid2Locations[vid]
  166. if found && len(locations) > 0 {
  167. return
  168. }
  169. locations, found = vc.ecVid2Locations[vid]
  170. return
  171. }
  172. func (vc *vidMap) addLocation(vid uint32, location Location) {
  173. vc.Lock()
  174. defer vc.Unlock()
  175. glog.V(4).Infof("+ volume id %d: %+v", vid, location)
  176. locations, found := vc.vid2Locations[vid]
  177. if !found {
  178. vc.vid2Locations[vid] = []Location{location}
  179. return
  180. }
  181. for _, loc := range locations {
  182. if loc.Url == location.Url {
  183. return
  184. }
  185. }
  186. vc.vid2Locations[vid] = append(locations, location)
  187. }
  188. func (vc *vidMap) addEcLocation(vid uint32, location Location) {
  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. vc.ecVid2Locations[vid] = []Location{location}
  195. return
  196. }
  197. for _, loc := range locations {
  198. if loc.Url == location.Url {
  199. return
  200. }
  201. }
  202. vc.ecVid2Locations[vid] = append(locations, location)
  203. }
  204. func (vc *vidMap) deleteLocation(vid uint32, location Location) {
  205. if vc.cache != nil {
  206. vc.cache.deleteLocation(vid, location)
  207. }
  208. vc.Lock()
  209. defer vc.Unlock()
  210. glog.V(4).Infof("- volume id %d: %+v", vid, location)
  211. locations, found := vc.vid2Locations[vid]
  212. if !found {
  213. return
  214. }
  215. for i, loc := range locations {
  216. if loc.Url == location.Url {
  217. vc.vid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  218. break
  219. }
  220. }
  221. }
  222. func (vc *vidMap) deleteEcLocation(vid uint32, location Location) {
  223. if vc.cache != nil {
  224. vc.cache.deleteLocation(vid, location)
  225. }
  226. vc.Lock()
  227. defer vc.Unlock()
  228. glog.V(4).Infof("- ec volume id %d: %+v", vid, location)
  229. locations, found := vc.ecVid2Locations[vid]
  230. if !found {
  231. return
  232. }
  233. for i, loc := range locations {
  234. if loc.Url == location.Url {
  235. vc.ecVid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  236. break
  237. }
  238. }
  239. }