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.

285 lines
8.8 KiB

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