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.

272 lines
8.5 KiB

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