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.

304 lines
8.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "math/rand"
  7. "sort"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  11. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandVolumeFixReplication{})
  15. }
  16. type commandVolumeFixReplication struct {
  17. }
  18. func (c *commandVolumeFixReplication) Name() string {
  19. return "volume.fix.replication"
  20. }
  21. func (c *commandVolumeFixReplication) Help() string {
  22. return `add replicas to volumes that are missing replicas
  23. This command file all under-replicated volumes, and find volume servers with free slots.
  24. If the free slots satisfy the replication requirement, the volume content is copied over and mounted.
  25. volume.fix.replication -n # do not take action
  26. volume.fix.replication # actually copying the volume files and mount the volume
  27. Note:
  28. * each time this will only add back one replica for one volume id. If there are multiple replicas
  29. are missing, e.g. multiple volume servers are new, you may need to run this multiple times.
  30. * do not run this too quick within seconds, since the new volume replica may take a few seconds
  31. to register itself to the master.
  32. `
  33. }
  34. func (c *commandVolumeFixReplication) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  35. if err = commandEnv.confirmIsLocked(); err != nil {
  36. return
  37. }
  38. takeAction := true
  39. if len(args) > 0 && args[0] == "-n" {
  40. takeAction = false
  41. }
  42. var resp *master_pb.VolumeListResponse
  43. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  44. resp, err = client.VolumeList(context.Background(), &master_pb.VolumeListRequest{})
  45. return err
  46. })
  47. if err != nil {
  48. return err
  49. }
  50. // find all volumes that needs replication
  51. // collect all data nodes
  52. replicatedVolumeLocations := make(map[uint32][]location)
  53. replicatedVolumeInfo := make(map[uint32]*master_pb.VolumeInformationMessage)
  54. var allLocations []location
  55. eachDataNode(resp.TopologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  56. loc := newLocation(dc, string(rack), dn)
  57. for _, v := range dn.VolumeInfos {
  58. if v.ReplicaPlacement > 0 {
  59. replicatedVolumeLocations[v.Id] = append(replicatedVolumeLocations[v.Id], loc)
  60. replicatedVolumeInfo[v.Id] = v
  61. }
  62. }
  63. allLocations = append(allLocations, loc)
  64. })
  65. // find all under replicated volumes
  66. underReplicatedVolumeLocations := make(map[uint32][]location)
  67. for vid, locations := range replicatedVolumeLocations {
  68. volumeInfo := replicatedVolumeInfo[vid]
  69. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(volumeInfo.ReplicaPlacement))
  70. if replicaPlacement.GetCopyCount() > len(locations) {
  71. underReplicatedVolumeLocations[vid] = locations
  72. }
  73. }
  74. if len(underReplicatedVolumeLocations) == 0 {
  75. return fmt.Errorf("no under replicated volumes")
  76. }
  77. if len(allLocations) == 0 {
  78. return fmt.Errorf("no data nodes at all")
  79. }
  80. // find the most under populated data nodes
  81. keepDataNodesSorted(allLocations)
  82. for vid, locations := range underReplicatedVolumeLocations {
  83. volumeInfo := replicatedVolumeInfo[vid]
  84. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(volumeInfo.ReplicaPlacement))
  85. foundNewLocation := false
  86. for _, dst := range allLocations {
  87. // check whether data nodes satisfy the constraints
  88. if dst.dataNode.FreeVolumeCount > 0 && satisfyReplicaPlacement(replicaPlacement, locations, dst) {
  89. // ask the volume server to replicate the volume
  90. sourceNodes := underReplicatedVolumeLocations[vid]
  91. sourceNode := sourceNodes[rand.Intn(len(sourceNodes))]
  92. foundNewLocation = true
  93. fmt.Fprintf(writer, "replicating volume %d %s from %s to dataNode %s ...\n", volumeInfo.Id, replicaPlacement, sourceNode.dataNode.Id, dst.dataNode.Id)
  94. if !takeAction {
  95. break
  96. }
  97. err := operation.WithVolumeServerClient(dst.dataNode.Id, commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  98. _, replicateErr := volumeServerClient.VolumeCopy(context.Background(), &volume_server_pb.VolumeCopyRequest{
  99. VolumeId: volumeInfo.Id,
  100. SourceDataNode: sourceNode.dataNode.Id,
  101. })
  102. return fmt.Errorf("copying from %s => %s : %v", sourceNode.dataNode.Id, dst.dataNode.Id, replicateErr)
  103. })
  104. if err != nil {
  105. return err
  106. }
  107. // adjust free volume count
  108. dst.dataNode.FreeVolumeCount--
  109. keepDataNodesSorted(allLocations)
  110. break
  111. }
  112. }
  113. if !foundNewLocation {
  114. fmt.Fprintf(writer, "failed to place volume %d replica as %s, existing:%+v\n", volumeInfo.Id, replicaPlacement, locations)
  115. }
  116. }
  117. return nil
  118. }
  119. func keepDataNodesSorted(dataNodes []location) {
  120. sort.Slice(dataNodes, func(i, j int) bool {
  121. return dataNodes[i].dataNode.FreeVolumeCount > dataNodes[j].dataNode.FreeVolumeCount
  122. })
  123. }
  124. /*
  125. if on an existing data node {
  126. return false
  127. }
  128. if different from existing dcs {
  129. if lack on different dcs {
  130. return true
  131. }else{
  132. return false
  133. }
  134. }
  135. if not on primary dc {
  136. return false
  137. }
  138. if different from existing racks {
  139. if lack on different racks {
  140. return true
  141. }else{
  142. return false
  143. }
  144. }
  145. if not on primary rack {
  146. return false
  147. }
  148. if lacks on same rack {
  149. return true
  150. } else {
  151. return false
  152. }
  153. */
  154. func satisfyReplicaPlacement(replicaPlacement *super_block.ReplicaPlacement, existingLocations []location, possibleLocation location) bool {
  155. existingDataNodes := make(map[string]int)
  156. for _, loc := range existingLocations {
  157. existingDataNodes[loc.String()] += 1
  158. }
  159. sameDataNodeCount := existingDataNodes[possibleLocation.String()]
  160. // avoid duplicated volume on the same data node
  161. if sameDataNodeCount > 0 {
  162. return false
  163. }
  164. existingDataCenters := make(map[string]int)
  165. for _, loc := range existingLocations {
  166. existingDataCenters[loc.DataCenter()] += 1
  167. }
  168. primaryDataCenters, _ := findTopKeys(existingDataCenters)
  169. // ensure data center count is within limit
  170. if _, found := existingDataCenters[possibleLocation.DataCenter()]; !found {
  171. // different from existing dcs
  172. if len(existingDataCenters) < replicaPlacement.DiffDataCenterCount+1 {
  173. // lack on different dcs
  174. return true
  175. } else {
  176. // adding this would go over the different dcs limit
  177. return false
  178. }
  179. }
  180. // now this is same as one of the existing data center
  181. if !isAmong(possibleLocation.DataCenter(), primaryDataCenters) {
  182. // not on one of the primary dcs
  183. return false
  184. }
  185. // now this is one of the primary dcs
  186. existingRacks := make(map[string]int)
  187. for _, loc := range existingLocations {
  188. if loc.DataCenter() != possibleLocation.DataCenter() {
  189. continue
  190. }
  191. existingRacks[loc.Rack()] += 1
  192. }
  193. primaryRacks, _ := findTopKeys(existingRacks)
  194. sameRackCount := existingRacks[possibleLocation.Rack()]
  195. // ensure rack count is within limit
  196. if _, found := existingRacks[possibleLocation.Rack()]; !found {
  197. // different from existing racks
  198. if len(existingRacks) < replicaPlacement.DiffRackCount+1 {
  199. // lack on different racks
  200. return true
  201. } else {
  202. // adding this would go over the different racks limit
  203. return false
  204. }
  205. }
  206. // now this is same as one of the existing racks
  207. if !isAmong(possibleLocation.Rack(), primaryRacks) {
  208. // not on the primary rack
  209. return false
  210. }
  211. // now this is on the primary rack
  212. // different from existing data nodes
  213. if sameRackCount < replicaPlacement.SameRackCount+1 {
  214. // lack on same rack
  215. return true
  216. } else {
  217. // adding this would go over the same data node limit
  218. return false
  219. }
  220. }
  221. func findTopKeys(m map[string]int) (topKeys []string, max int) {
  222. for k, c := range m {
  223. if max < c {
  224. topKeys = topKeys[:0]
  225. topKeys = append(topKeys, k)
  226. max = c
  227. } else if max == c {
  228. topKeys = append(topKeys, k)
  229. }
  230. }
  231. return
  232. }
  233. func isAmong(key string, keys []string) bool {
  234. for _, k := range keys {
  235. if k == key {
  236. return true
  237. }
  238. }
  239. return false
  240. }
  241. type location struct {
  242. dc string
  243. rack string
  244. dataNode *master_pb.DataNodeInfo
  245. }
  246. func newLocation(dc, rack string, dataNode *master_pb.DataNodeInfo) location {
  247. return location{
  248. dc: dc,
  249. rack: rack,
  250. dataNode: dataNode,
  251. }
  252. }
  253. func (l location) String() string {
  254. return fmt.Sprintf("%s %s %s", l.dc, l.rack, l.dataNode.Id)
  255. }
  256. func (l location) Rack() string {
  257. return fmt.Sprintf("%s %s", l.dc, l.rack)
  258. }
  259. func (l location) DataCenter() string {
  260. return l.dc
  261. }