command_ec_rebuild.go 8.2 KB

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