command_ec_rebuild.go 8.3 KB

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