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.

236 lines
5.4 KiB

7 years ago
7 years ago
7 years ago
4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
7 years ago
7 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) LookupVolumeServerUrl(vid string) (serverUrls []string, err error) {
  55. id, err := strconv.Atoi(vid)
  56. if err != nil {
  57. glog.V(1).Infof("Unknown volume id %s", vid)
  58. return nil, err
  59. }
  60. locations, found := vc.GetLocations(uint32(id))
  61. if !found {
  62. return nil, fmt.Errorf("volume %d not found", id)
  63. }
  64. var sameDcServers, otherDcServers []string
  65. for _, loc := range locations {
  66. if vc.DataCenter == "" || loc.DataCenter == "" || vc.DataCenter != loc.DataCenter {
  67. otherDcServers = append(otherDcServers, loc.Url)
  68. } else {
  69. sameDcServers = append(sameDcServers, loc.Url)
  70. }
  71. }
  72. rand.Shuffle(len(sameDcServers), func(i, j int) {
  73. sameDcServers[i], sameDcServers[j] = sameDcServers[j], sameDcServers[i]
  74. })
  75. rand.Shuffle(len(otherDcServers), func(i, j int) {
  76. otherDcServers[i], otherDcServers[j] = otherDcServers[j], otherDcServers[i]
  77. })
  78. serverUrls = append(sameDcServers, otherDcServers...)
  79. return
  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. glog.V(4).Infof("~ lookup volume id %d: %+v ec:%+v", vid, vc.vid2Locations, vc.ecVid2Locations)
  109. locations, found = vc.getLocations(vid)
  110. if found && len(locations) > 0 {
  111. return locations, found
  112. }
  113. if vc.cache != nil {
  114. return vc.cache.GetLocations(vid)
  115. }
  116. return nil, false
  117. }
  118. func (vc *vidMap) getLocations(vid uint32) (locations []Location, found bool) {
  119. vc.RLock()
  120. defer vc.RUnlock()
  121. locations, found = vc.vid2Locations[vid]
  122. if found && len(locations) > 0 {
  123. return
  124. }
  125. locations, found = vc.ecVid2Locations[vid]
  126. return
  127. }
  128. func (vc *vidMap) addLocation(vid uint32, location Location) {
  129. vc.Lock()
  130. defer vc.Unlock()
  131. glog.V(4).Infof("+ volume id %d: %+v", vid, location)
  132. locations, found := vc.vid2Locations[vid]
  133. if !found {
  134. vc.vid2Locations[vid] = []Location{location}
  135. return
  136. }
  137. for _, loc := range locations {
  138. if loc.Url == location.Url {
  139. return
  140. }
  141. }
  142. vc.vid2Locations[vid] = append(locations, location)
  143. }
  144. func (vc *vidMap) addEcLocation(vid uint32, location Location) {
  145. vc.Lock()
  146. defer vc.Unlock()
  147. glog.V(4).Infof("+ ec volume id %d: %+v", vid, location)
  148. locations, found := vc.ecVid2Locations[vid]
  149. if !found {
  150. vc.ecVid2Locations[vid] = []Location{location}
  151. return
  152. }
  153. for _, loc := range locations {
  154. if loc.Url == location.Url {
  155. return
  156. }
  157. }
  158. vc.ecVid2Locations[vid] = append(locations, location)
  159. }
  160. func (vc *vidMap) deleteLocation(vid uint32, location Location) {
  161. if vc.cache != nil {
  162. vc.cache.deleteLocation(vid, location)
  163. }
  164. vc.Lock()
  165. defer vc.Unlock()
  166. glog.V(4).Infof("- volume id %d: %+v", vid, location)
  167. locations, found := vc.vid2Locations[vid]
  168. if !found {
  169. return
  170. }
  171. for i, loc := range locations {
  172. if loc.Url == location.Url {
  173. vc.vid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  174. break
  175. }
  176. }
  177. }
  178. func (vc *vidMap) deleteEcLocation(vid uint32, location Location) {
  179. if vc.cache != nil {
  180. vc.cache.deleteLocation(vid, location)
  181. }
  182. vc.Lock()
  183. defer vc.Unlock()
  184. glog.V(4).Infof("- ec volume id %d: %+v", vid, location)
  185. locations, found := vc.ecVid2Locations[vid]
  186. if !found {
  187. return
  188. }
  189. for i, loc := range locations {
  190. if loc.Url == location.Url {
  191. vc.ecVid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  192. break
  193. }
  194. }
  195. }