command_ec_rebuild.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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) 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. sortEcNodesByFreeslotsDescending(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. if !commandEnv.isLocked() {
  108. return fmt.Errorf("lock is lost")
  109. }
  110. fmt.Printf("rebuildOneEcVolume %s %d\n", collection, volumeId)
  111. // collect shard files to rebuilder local disk
  112. var generatedShardIds []uint32
  113. copiedShardIds, _, err := prepareDataToRecover(commandEnv, rebuilder, collection, volumeId, locations, writer, applyChanges)
  114. if err != nil {
  115. return err
  116. }
  117. defer func() {
  118. // clean up working files
  119. // ask the rebuilder to delete the copied shards
  120. err = sourceServerDeleteEcShards(commandEnv.option.GrpcDialOption, collection, volumeId, pb.NewServerAddressFromDataNode(rebuilder.info), copiedShardIds)
  121. if err != nil {
  122. fmt.Fprintf(writer, "%s delete copied ec shards %s %d.%v\n", rebuilder.info.Id, collection, volumeId, copiedShardIds)
  123. }
  124. }()
  125. if !applyChanges {
  126. return nil
  127. }
  128. // generate ec shards, and maybe ecx file
  129. generatedShardIds, err = generateMissingShards(commandEnv.option.GrpcDialOption, collection, volumeId, pb.NewServerAddressFromDataNode(rebuilder.info))
  130. if err != nil {
  131. return err
  132. }
  133. // mount the generated shards
  134. err = mountEcShards(commandEnv.option.GrpcDialOption, collection, volumeId, pb.NewServerAddressFromDataNode(rebuilder.info), generatedShardIds)
  135. if err != nil {
  136. return err
  137. }
  138. rebuilder.addEcVolumeShards(volumeId, collection, generatedShardIds)
  139. return nil
  140. }
  141. func generateMissingShards(grpcDialOption grpc.DialOption, collection string, volumeId needle.VolumeId, sourceLocation pb.ServerAddress) (rebuiltShardIds []uint32, err error) {
  142. err = operation.WithVolumeServerClient(false, sourceLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  143. resp, rebuildErr := volumeServerClient.VolumeEcShardsRebuild(context.Background(), &volume_server_pb.VolumeEcShardsRebuildRequest{
  144. VolumeId: uint32(volumeId),
  145. Collection: collection,
  146. })
  147. if rebuildErr == nil {
  148. rebuiltShardIds = resp.RebuiltShardIds
  149. }
  150. return rebuildErr
  151. })
  152. return
  153. }
  154. func prepareDataToRecover(commandEnv *CommandEnv, rebuilder *EcNode, collection string, volumeId needle.VolumeId, locations EcShardLocations, writer io.Writer, applyBalancing bool) (copiedShardIds []uint32, localShardIds []uint32, err error) {
  155. needEcxFile := true
  156. var localShardBits erasure_coding.ShardBits
  157. for _, diskInfo := range rebuilder.info.DiskInfos {
  158. for _, ecShardInfo := range diskInfo.EcShardInfos {
  159. if ecShardInfo.Collection == collection && needle.VolumeId(ecShardInfo.Id) == volumeId {
  160. needEcxFile = false
  161. localShardBits = erasure_coding.ShardBits(ecShardInfo.EcIndexBits)
  162. }
  163. }
  164. }
  165. for shardId, ecNodes := range locations {
  166. if len(ecNodes) == 0 {
  167. fmt.Fprintf(writer, "missing shard %d.%d\n", volumeId, shardId)
  168. continue
  169. }
  170. if localShardBits.HasShardId(erasure_coding.ShardId(shardId)) {
  171. localShardIds = append(localShardIds, uint32(shardId))
  172. fmt.Fprintf(writer, "use existing shard %d.%d\n", volumeId, shardId)
  173. continue
  174. }
  175. var copyErr error
  176. if applyBalancing {
  177. copyErr = operation.WithVolumeServerClient(false, pb.NewServerAddressFromDataNode(rebuilder.info), commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  178. _, copyErr := volumeServerClient.VolumeEcShardsCopy(context.Background(), &volume_server_pb.VolumeEcShardsCopyRequest{
  179. VolumeId: uint32(volumeId),
  180. Collection: collection,
  181. ShardIds: []uint32{uint32(shardId)},
  182. CopyEcxFile: needEcxFile,
  183. CopyEcjFile: needEcxFile,
  184. CopyVifFile: needEcxFile,
  185. SourceDataNode: ecNodes[0].info.Id,
  186. })
  187. return copyErr
  188. })
  189. if copyErr == nil && needEcxFile {
  190. needEcxFile = false
  191. }
  192. }
  193. if copyErr != nil {
  194. fmt.Fprintf(writer, "%s failed to copy %d.%d from %s: %v\n", rebuilder.info.Id, volumeId, shardId, ecNodes[0].info.Id, copyErr)
  195. } else {
  196. fmt.Fprintf(writer, "%s copied %d.%d from %s\n", rebuilder.info.Id, volumeId, shardId, ecNodes[0].info.Id)
  197. copiedShardIds = append(copiedShardIds, uint32(shardId))
  198. }
  199. }
  200. if len(copiedShardIds)+len(localShardIds) >= erasure_coding.DataShardsCount {
  201. return copiedShardIds, localShardIds, nil
  202. }
  203. return nil, nil, fmt.Errorf("%d shards are not enough to recover volume %d", len(copiedShardIds)+len(localShardIds), volumeId)
  204. }
  205. type EcShardMap map[needle.VolumeId]EcShardLocations
  206. type EcShardLocations [][]*EcNode
  207. func (ecShardMap EcShardMap) registerEcNode(ecNode *EcNode, collection string) {
  208. for _, diskInfo := range ecNode.info.DiskInfos {
  209. for _, shardInfo := range diskInfo.EcShardInfos {
  210. if shardInfo.Collection == collection {
  211. existing, found := ecShardMap[needle.VolumeId(shardInfo.Id)]
  212. if !found {
  213. existing = make([][]*EcNode, erasure_coding.TotalShardsCount)
  214. ecShardMap[needle.VolumeId(shardInfo.Id)] = existing
  215. }
  216. for _, shardId := range erasure_coding.ShardBits(shardInfo.EcIndexBits).ShardIds() {
  217. existing[shardId] = append(existing[shardId], ecNode)
  218. }
  219. }
  220. }
  221. }
  222. }
  223. func (ecShardLocations EcShardLocations) shardCount() (count int) {
  224. for _, locations := range ecShardLocations {
  225. if len(locations) > 0 {
  226. count++
  227. }
  228. }
  229. return
  230. }