command_ec_rebuild.go 8.4 KB

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