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.

277 lines
8.6 KiB

  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "io"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  10. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "google.golang.org/grpc"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandEcRebuild{})
  16. }
  17. type commandEcRebuild struct {
  18. }
  19. func (c *commandEcRebuild) Name() string {
  20. return "ec.rebuild"
  21. }
  22. func (c *commandEcRebuild) Help() string {
  23. return `find and rebuild missing ec shards among volume servers
  24. ec.rebuild [-c EACH_COLLECTION|<collection_name>] [-force]
  25. Algorithm:
  26. For each type of volume server (different max volume count limit){
  27. for each collection {
  28. rebuildEcVolumes()
  29. }
  30. }
  31. func rebuildEcVolumes(){
  32. idealWritableVolumes = totalWritableVolumes / numVolumeServers
  33. for {
  34. sort all volume servers ordered by the number of local writable volumes
  35. pick the volume server A with the lowest number of writable volumes x
  36. pick the volume server B with the highest number of writable volumes y
  37. if y > idealWritableVolumes and x +1 <= idealWritableVolumes {
  38. if B has a writable volume id v that A does not have {
  39. move writable volume v from A to B
  40. }
  41. }
  42. }
  43. }
  44. `
  45. }
  46. func (c *commandEcRebuild) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  47. fixCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  48. collection := fixCommand.String("collection", "EACH_COLLECTION", "collection name, or \"EACH_COLLECTION\" for each collection")
  49. applyChanges := fixCommand.Bool("force", false, "apply the changes")
  50. if err = fixCommand.Parse(args); err != nil {
  51. return nil
  52. }
  53. infoAboutSimulationMode(writer, *applyChanges, "-force")
  54. if err = commandEnv.confirmIsLocked(args); err != nil {
  55. return
  56. }
  57. // collect all ec nodes
  58. allEcNodes, _, err := collectEcNodes(commandEnv, "")
  59. if err != nil {
  60. return err
  61. }
  62. if *collection == "EACH_COLLECTION" {
  63. collections, err := ListCollectionNames(commandEnv, false, true)
  64. if err != nil {
  65. return err
  66. }
  67. fmt.Printf("rebuildEcVolumes collections %+v\n", len(collections))
  68. for _, c := range collections {
  69. fmt.Printf("rebuildEcVolumes collection %+v\n", c)
  70. if err = rebuildEcVolumes(commandEnv, allEcNodes, c, writer, *applyChanges); err != nil {
  71. return err
  72. }
  73. }
  74. } else {
  75. if err = rebuildEcVolumes(commandEnv, allEcNodes, *collection, writer, *applyChanges); err != nil {
  76. return err
  77. }
  78. }
  79. return nil
  80. }
  81. func rebuildEcVolumes(commandEnv *CommandEnv, allEcNodes []*EcNode, collection string, writer io.Writer, applyChanges bool) error {
  82. fmt.Printf("rebuildEcVolumes %s\n", collection)
  83. // collect vid => each shard locations, similar to ecShardMap in topology.go
  84. ecShardMap := make(EcShardMap)
  85. for _, ecNode := range allEcNodes {
  86. ecShardMap.registerEcNode(ecNode, collection)
  87. }
  88. for vid, locations := range ecShardMap {
  89. shardCount := locations.shardCount()
  90. if shardCount == erasure_coding.TotalShardsCount {
  91. continue
  92. }
  93. if shardCount < erasure_coding.DataShardsCount {
  94. return fmt.Errorf("ec volume %d is unrepairable with %d shards\n", vid, shardCount)
  95. }
  96. sortEcNodesByFreeslotsDecending(allEcNodes)
  97. if allEcNodes[0].freeEcSlot < erasure_coding.TotalShardsCount {
  98. return fmt.Errorf("disk space is not enough")
  99. }
  100. if err := rebuildOneEcVolume(commandEnv, allEcNodes[0], collection, vid, locations, writer, applyChanges); err != nil {
  101. return err
  102. }
  103. }
  104. return nil
  105. }
  106. func rebuildOneEcVolume(commandEnv *CommandEnv, rebuilder *EcNode, collection string, volumeId needle.VolumeId, locations EcShardLocations, writer io.Writer, applyChanges bool) error {
  107. fmt.Printf("rebuildOneEcVolume %s %d\n", collection, volumeId)
  108. // collect shard files to rebuilder local disk
  109. var generatedShardIds []uint32
  110. copiedShardIds, _, err := prepareDataToRecover(commandEnv, rebuilder, collection, volumeId, locations, writer, applyChanges)
  111. if err != nil {
  112. return err
  113. }
  114. defer func() {
  115. // clean up working files
  116. // ask the rebuilder to delete the copied shards
  117. err = sourceServerDeleteEcShards(commandEnv.option.GrpcDialOption, collection, volumeId, pb.NewServerAddressFromDataNode(rebuilder.info), copiedShardIds)
  118. if err != nil {
  119. fmt.Fprintf(writer, "%s delete copied ec shards %s %d.%v\n", rebuilder.info.Id, collection, volumeId, copiedShardIds)
  120. }
  121. }()
  122. if !applyChanges {
  123. return nil
  124. }
  125. // generate ec shards, and maybe ecx file
  126. generatedShardIds, err = generateMissingShards(commandEnv.option.GrpcDialOption, collection, volumeId, pb.NewServerAddressFromDataNode(rebuilder.info))
  127. if err != nil {
  128. return err
  129. }
  130. // mount the generated shards
  131. err = mountEcShards(commandEnv.option.GrpcDialOption, collection, volumeId, pb.NewServerAddressFromDataNode(rebuilder.info), generatedShardIds)
  132. if err != nil {
  133. return err
  134. }
  135. rebuilder.addEcVolumeShards(volumeId, collection, generatedShardIds)
  136. return nil
  137. }
  138. func generateMissingShards(grpcDialOption grpc.DialOption, collection string, volumeId needle.VolumeId, sourceLocation pb.ServerAddress) (rebuiltShardIds []uint32, err error) {
  139. err = operation.WithVolumeServerClient(false, sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  140. resp, rebultErr := volumeServerClient.VolumeEcShardsRebuild(context.Background(), &volume_server_pb.VolumeEcShardsRebuildRequest{
  141. VolumeId: uint32(volumeId),
  142. Collection: collection,
  143. })
  144. if rebultErr == nil {
  145. rebuiltShardIds = resp.RebuiltShardIds
  146. }
  147. return rebultErr
  148. })
  149. return
  150. }
  151. func prepareDataToRecover(commandEnv *CommandEnv, rebuilder *EcNode, collection string, volumeId needle.VolumeId, locations EcShardLocations, writer io.Writer, applyBalancing bool) (copiedShardIds []uint32, localShardIds []uint32, err error) {
  152. needEcxFile := true
  153. var localShardBits erasure_coding.ShardBits
  154. for _, diskInfo := range rebuilder.info.DiskInfos {
  155. for _, ecShardInfo := range diskInfo.EcShardInfos {
  156. if ecShardInfo.Collection == collection && needle.VolumeId(ecShardInfo.Id) == volumeId {
  157. needEcxFile = false
  158. localShardBits = erasure_coding.ShardBits(ecShardInfo.EcIndexBits)
  159. }
  160. }
  161. }
  162. for shardId, ecNodes := range locations {
  163. if len(ecNodes) == 0 {
  164. fmt.Fprintf(writer, "missing shard %d.%d\n", volumeId, shardId)
  165. continue
  166. }
  167. if localShardBits.HasShardId(erasure_coding.ShardId(shardId)) {
  168. localShardIds = append(localShardIds, uint32(shardId))
  169. fmt.Fprintf(writer, "use existing shard %d.%d\n", volumeId, shardId)
  170. continue
  171. }
  172. var copyErr error
  173. if applyBalancing {
  174. copyErr = operation.WithVolumeServerClient(false, pb.NewServerAddressFromDataNode(rebuilder.info), commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  175. _, copyErr := volumeServerClient.VolumeEcShardsCopy(context.Background(), &volume_server_pb.VolumeEcShardsCopyRequest{
  176. VolumeId: uint32(volumeId),
  177. Collection: collection,
  178. ShardIds: []uint32{uint32(shardId)},
  179. CopyEcxFile: needEcxFile,
  180. CopyEcjFile: needEcxFile,
  181. CopyVifFile: needEcxFile,
  182. SourceDataNode: ecNodes[0].info.Id,
  183. })
  184. return copyErr
  185. })
  186. if copyErr == nil && needEcxFile {
  187. needEcxFile = false
  188. }
  189. }
  190. if copyErr != nil {
  191. fmt.Fprintf(writer, "%s failed to copy %d.%d from %s: %v\n", rebuilder.info.Id, volumeId, shardId, ecNodes[0].info.Id, copyErr)
  192. } else {
  193. fmt.Fprintf(writer, "%s copied %d.%d from %s\n", rebuilder.info.Id, volumeId, shardId, ecNodes[0].info.Id)
  194. copiedShardIds = append(copiedShardIds, uint32(shardId))
  195. }
  196. }
  197. if len(copiedShardIds)+len(localShardIds) >= erasure_coding.DataShardsCount {
  198. return copiedShardIds, localShardIds, nil
  199. }
  200. return nil, nil, fmt.Errorf("%d shards are not enough to recover volume %d", len(copiedShardIds)+len(localShardIds), volumeId)
  201. }
  202. type EcShardMap map[needle.VolumeId]EcShardLocations
  203. type EcShardLocations [][]*EcNode
  204. func (ecShardMap EcShardMap) registerEcNode(ecNode *EcNode, collection string) {
  205. for _, diskInfo := range ecNode.info.DiskInfos {
  206. for _, shardInfo := range diskInfo.EcShardInfos {
  207. if shardInfo.Collection == collection {
  208. existing, found := ecShardMap[needle.VolumeId(shardInfo.Id)]
  209. if !found {
  210. existing = make([][]*EcNode, erasure_coding.TotalShardsCount)
  211. ecShardMap[needle.VolumeId(shardInfo.Id)] = existing
  212. }
  213. for _, shardId := range erasure_coding.ShardBits(shardInfo.EcIndexBits).ShardIds() {
  214. existing[shardId] = append(existing[shardId], ecNode)
  215. }
  216. }
  217. }
  218. }
  219. }
  220. func (ecShardLocations EcShardLocations) shardCount() (count int) {
  221. for _, locations := range ecShardLocations {
  222. if len(locations) > 0 {
  223. count++
  224. }
  225. }
  226. return
  227. }