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.

217 lines
5.0 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/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) LookupFileId(fileId string) (fullUrls []string, err error) {
  81. parts := strings.Split(fileId, ",")
  82. if len(parts) != 2 {
  83. return nil, errors.New("Invalid fileId " + fileId)
  84. }
  85. serverUrls, lookupError := vc.LookupVolumeServerUrl(parts[0])
  86. if lookupError != nil {
  87. return nil, lookupError
  88. }
  89. for _, serverUrl := range serverUrls {
  90. fullUrls = append(fullUrls, "http://"+serverUrl+"/"+fileId)
  91. }
  92. return
  93. }
  94. func (vc *vidMap) GetVidLocations(vid string) (locations []Location, err error) {
  95. id, err := strconv.Atoi(vid)
  96. if err != nil {
  97. glog.V(1).Infof("Unknown volume id %s", vid)
  98. return nil, fmt.Errorf("Unknown volume id %s", vid)
  99. }
  100. foundLocations, found := vc.GetLocations(uint32(id))
  101. if found {
  102. return foundLocations, nil
  103. }
  104. return nil, fmt.Errorf("volume id %s not found", vid)
  105. }
  106. func (vc *vidMap) GetLocations(vid uint32) (locations []Location, found bool) {
  107. vc.RLock()
  108. defer vc.RUnlock()
  109. glog.V(4).Infof("~ lookup volume id %d: %+v ec:%+v", vid, vc.vid2Locations, vc.ecVid2Locations)
  110. locations, found = vc.vid2Locations[vid]
  111. if found && len(locations) > 0 {
  112. return
  113. }
  114. locations, found = vc.ecVid2Locations[vid]
  115. return locations, found && len(locations) > 0
  116. }
  117. func (vc *vidMap) addLocation(vid uint32, location Location) {
  118. vc.Lock()
  119. defer vc.Unlock()
  120. glog.V(4).Infof("+ volume id %d: %+v", vid, location)
  121. locations, found := vc.vid2Locations[vid]
  122. if !found {
  123. vc.vid2Locations[vid] = []Location{location}
  124. return
  125. }
  126. for _, loc := range locations {
  127. if loc.Url == location.Url {
  128. return
  129. }
  130. }
  131. vc.vid2Locations[vid] = append(locations, location)
  132. }
  133. func (vc *vidMap) addEcLocation(vid uint32, location Location) {
  134. vc.Lock()
  135. defer vc.Unlock()
  136. glog.V(4).Infof("+ ec volume id %d: %+v", vid, location)
  137. locations, found := vc.ecVid2Locations[vid]
  138. if !found {
  139. vc.ecVid2Locations[vid] = []Location{location}
  140. return
  141. }
  142. for _, loc := range locations {
  143. if loc.Url == location.Url {
  144. return
  145. }
  146. }
  147. vc.ecVid2Locations[vid] = append(locations, location)
  148. }
  149. func (vc *vidMap) deleteLocation(vid uint32, location Location) {
  150. vc.Lock()
  151. defer vc.Unlock()
  152. glog.V(4).Infof("- volume id %d: %+v", vid, location)
  153. locations, found := vc.vid2Locations[vid]
  154. if !found {
  155. return
  156. }
  157. for i, loc := range locations {
  158. if loc.Url == location.Url {
  159. vc.vid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  160. break
  161. }
  162. }
  163. }
  164. func (vc *vidMap) deleteEcLocation(vid uint32, location Location) {
  165. vc.Lock()
  166. defer vc.Unlock()
  167. glog.V(4).Infof("- ec volume id %d: %+v", vid, location)
  168. locations, found := vc.ecVid2Locations[vid]
  169. if !found {
  170. return
  171. }
  172. for i, loc := range locations {
  173. if loc.Url == location.Url {
  174. vc.ecVid2Locations[vid] = append(locations[0:i], locations[i+1:]...)
  175. break
  176. }
  177. }
  178. }