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.

315 lines
9.1 KiB

6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 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 finds all under-replicated volumes, and finds 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 quickly 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. overReplicatedVolumeLocations := make(map[uint32][]location)
  68. for vid, locations := range replicatedVolumeLocations {
  69. volumeInfo := replicatedVolumeInfo[vid]
  70. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(volumeInfo.ReplicaPlacement))
  71. if replicaPlacement.GetCopyCount() > len(locations) {
  72. underReplicatedVolumeLocations[vid] = locations
  73. } else if replicaPlacement.GetCopyCount() < len(locations) {
  74. overReplicatedVolumeLocations[vid] = locations
  75. fmt.Fprintf(writer, "volume %d replication %s, but over replicated:%+v\n", volumeInfo.Id, replicaPlacement, locations)
  76. }
  77. }
  78. if len(underReplicatedVolumeLocations) == 0 {
  79. return fmt.Errorf("no under replicated volumes")
  80. }
  81. if len(allLocations) == 0 {
  82. return fmt.Errorf("no data nodes at all")
  83. }
  84. // find the most under populated data nodes
  85. keepDataNodesSorted(allLocations)
  86. return c.fixUnderReplicatedVolumes(commandEnv, writer, takeAction, underReplicatedVolumeLocations, replicatedVolumeInfo, allLocations)
  87. }
  88. func (c *commandVolumeFixReplication) fixUnderReplicatedVolumes(commandEnv *CommandEnv, writer io.Writer, takeAction bool, underReplicatedVolumeLocations map[uint32][]location, replicatedVolumeInfo map[uint32]*master_pb.VolumeInformationMessage, allLocations []location) error {
  89. for vid, locations := range underReplicatedVolumeLocations {
  90. volumeInfo := replicatedVolumeInfo[vid]
  91. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(volumeInfo.ReplicaPlacement))
  92. foundNewLocation := false
  93. for _, dst := range allLocations {
  94. // check whether data nodes satisfy the constraints
  95. if dst.dataNode.FreeVolumeCount > 0 && satisfyReplicaPlacement(replicaPlacement, locations, dst) {
  96. // ask the volume server to replicate the volume
  97. sourceNodes := underReplicatedVolumeLocations[vid]
  98. sourceNode := sourceNodes[rand.Intn(len(sourceNodes))]
  99. foundNewLocation = true
  100. fmt.Fprintf(writer, "replicating volume %d %s from %s to dataNode %s ...\n", volumeInfo.Id, replicaPlacement, sourceNode.dataNode.Id, dst.dataNode.Id)
  101. if !takeAction {
  102. break
  103. }
  104. err := operation.WithVolumeServerClient(dst.dataNode.Id, commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  105. _, replicateErr := volumeServerClient.VolumeCopy(context.Background(), &volume_server_pb.VolumeCopyRequest{
  106. VolumeId: volumeInfo.Id,
  107. SourceDataNode: sourceNode.dataNode.Id,
  108. })
  109. if replicateErr != nil {
  110. return fmt.Errorf("copying from %s => %s : %v", sourceNode.dataNode.Id, dst.dataNode.Id, replicateErr)
  111. }
  112. return nil
  113. })
  114. if err != nil {
  115. return err
  116. }
  117. // adjust free volume count
  118. dst.dataNode.FreeVolumeCount--
  119. keepDataNodesSorted(allLocations)
  120. break
  121. }
  122. }
  123. if !foundNewLocation {
  124. fmt.Fprintf(writer, "failed to place volume %d replica as %s, existing:%+v\n", volumeInfo.Id, replicaPlacement, locations)
  125. }
  126. }
  127. return nil
  128. }
  129. func keepDataNodesSorted(dataNodes []location) {
  130. sort.Slice(dataNodes, func(i, j int) bool {
  131. return dataNodes[i].dataNode.FreeVolumeCount > dataNodes[j].dataNode.FreeVolumeCount
  132. })
  133. }
  134. /*
  135. if on an existing data node {
  136. return false
  137. }
  138. if different from existing dcs {
  139. if lack on different dcs {
  140. return true
  141. }else{
  142. return false
  143. }
  144. }
  145. if not on primary dc {
  146. return false
  147. }
  148. if different from existing racks {
  149. if lack on different racks {
  150. return true
  151. }else{
  152. return false
  153. }
  154. }
  155. if not on primary rack {
  156. return false
  157. }
  158. if lacks on same rack {
  159. return true
  160. } else {
  161. return false
  162. }
  163. */
  164. func satisfyReplicaPlacement(replicaPlacement *super_block.ReplicaPlacement, existingLocations []location, possibleLocation location) bool {
  165. existingDataNodes := make(map[string]int)
  166. for _, loc := range existingLocations {
  167. existingDataNodes[loc.String()] += 1
  168. }
  169. sameDataNodeCount := existingDataNodes[possibleLocation.String()]
  170. // avoid duplicated volume on the same data node
  171. if sameDataNodeCount > 0 {
  172. return false
  173. }
  174. existingDataCenters := make(map[string]int)
  175. for _, loc := range existingLocations {
  176. existingDataCenters[loc.DataCenter()] += 1
  177. }
  178. primaryDataCenters, _ := findTopKeys(existingDataCenters)
  179. // ensure data center count is within limit
  180. if _, found := existingDataCenters[possibleLocation.DataCenter()]; !found {
  181. // different from existing dcs
  182. if len(existingDataCenters) < replicaPlacement.DiffDataCenterCount+1 {
  183. // lack on different dcs
  184. return true
  185. } else {
  186. // adding this would go over the different dcs limit
  187. return false
  188. }
  189. }
  190. // now this is same as one of the existing data center
  191. if !isAmong(possibleLocation.DataCenter(), primaryDataCenters) {
  192. // not on one of the primary dcs
  193. return false
  194. }
  195. // now this is one of the primary dcs
  196. existingRacks := make(map[string]int)
  197. for _, loc := range existingLocations {
  198. if loc.DataCenter() != possibleLocation.DataCenter() {
  199. continue
  200. }
  201. existingRacks[loc.Rack()] += 1
  202. }
  203. primaryRacks, _ := findTopKeys(existingRacks)
  204. sameRackCount := existingRacks[possibleLocation.Rack()]
  205. // ensure rack count is within limit
  206. if _, found := existingRacks[possibleLocation.Rack()]; !found {
  207. // different from existing racks
  208. if len(existingRacks) < replicaPlacement.DiffRackCount+1 {
  209. // lack on different racks
  210. return true
  211. } else {
  212. // adding this would go over the different racks limit
  213. return false
  214. }
  215. }
  216. // now this is same as one of the existing racks
  217. if !isAmong(possibleLocation.Rack(), primaryRacks) {
  218. // not on the primary rack
  219. return false
  220. }
  221. // now this is on the primary rack
  222. // different from existing data nodes
  223. if sameRackCount < replicaPlacement.SameRackCount+1 {
  224. // lack on same rack
  225. return true
  226. } else {
  227. // adding this would go over the same data node limit
  228. return false
  229. }
  230. }
  231. func findTopKeys(m map[string]int) (topKeys []string, max int) {
  232. for k, c := range m {
  233. if max < c {
  234. topKeys = topKeys[:0]
  235. topKeys = append(topKeys, k)
  236. max = c
  237. } else if max == c {
  238. topKeys = append(topKeys, k)
  239. }
  240. }
  241. return
  242. }
  243. func isAmong(key string, keys []string) bool {
  244. for _, k := range keys {
  245. if k == key {
  246. return true
  247. }
  248. }
  249. return false
  250. }
  251. type location struct {
  252. dc string
  253. rack string
  254. dataNode *master_pb.DataNodeInfo
  255. }
  256. func newLocation(dc, rack string, dataNode *master_pb.DataNodeInfo) location {
  257. return location{
  258. dc: dc,
  259. rack: rack,
  260. dataNode: dataNode,
  261. }
  262. }
  263. func (l location) String() string {
  264. return fmt.Sprintf("%s %s %s", l.dc, l.rack, l.dataNode.Id)
  265. }
  266. func (l location) Rack() string {
  267. return fmt.Sprintf("%s %s", l.dc, l.rack)
  268. }
  269. func (l location) DataCenter() string {
  270. return l.dc
  271. }