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.

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