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.

275 lines
8.4 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>] [-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 _, diskInfo := range rebuilder.info.DiskInfos {
  153. for _, ecShardInfo := range diskInfo.EcShardInfos {
  154. if ecShardInfo.Collection == collection && needle.VolumeId(ecShardInfo.Id) == volumeId {
  155. needEcxFile = false
  156. localShardBits = erasure_coding.ShardBits(ecShardInfo.EcIndexBits)
  157. }
  158. }
  159. }
  160. for shardId, ecNodes := range locations {
  161. if len(ecNodes) == 0 {
  162. fmt.Fprintf(writer, "missing shard %d.%d\n", volumeId, shardId)
  163. continue
  164. }
  165. if localShardBits.HasShardId(erasure_coding.ShardId(shardId)) {
  166. localShardIds = append(localShardIds, uint32(shardId))
  167. fmt.Fprintf(writer, "use existing shard %d.%d\n", volumeId, shardId)
  168. continue
  169. }
  170. var copyErr error
  171. if applyBalancing {
  172. copyErr = operation.WithVolumeServerClient(rebuilder.info.Id, commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  173. _, copyErr := volumeServerClient.VolumeEcShardsCopy(context.Background(), &volume_server_pb.VolumeEcShardsCopyRequest{
  174. VolumeId: uint32(volumeId),
  175. Collection: collection,
  176. ShardIds: []uint32{uint32(shardId)},
  177. CopyEcxFile: needEcxFile,
  178. CopyEcjFile: needEcxFile,
  179. CopyVifFile: needEcxFile,
  180. SourceDataNode: ecNodes[0].info.Id,
  181. })
  182. return copyErr
  183. })
  184. if copyErr == nil && needEcxFile {
  185. needEcxFile = false
  186. }
  187. }
  188. if copyErr != nil {
  189. fmt.Fprintf(writer, "%s failed to copy %d.%d from %s: %v\n", rebuilder.info.Id, volumeId, shardId, ecNodes[0].info.Id, copyErr)
  190. } else {
  191. fmt.Fprintf(writer, "%s copied %d.%d from %s\n", rebuilder.info.Id, volumeId, shardId, ecNodes[0].info.Id)
  192. copiedShardIds = append(copiedShardIds, uint32(shardId))
  193. }
  194. }
  195. if len(copiedShardIds)+len(localShardIds) >= erasure_coding.DataShardsCount {
  196. return copiedShardIds, localShardIds, nil
  197. }
  198. return nil, nil, fmt.Errorf("%d shards are not enough to recover volume %d", len(copiedShardIds)+len(localShardIds), volumeId)
  199. }
  200. type EcShardMap map[needle.VolumeId]EcShardLocations
  201. type EcShardLocations [][]*EcNode
  202. func (ecShardMap EcShardMap) registerEcNode(ecNode *EcNode, collection string) {
  203. for _, diskInfo := range ecNode.info.DiskInfos {
  204. for _, shardInfo := range diskInfo.EcShardInfos {
  205. if shardInfo.Collection == collection {
  206. existing, found := ecShardMap[needle.VolumeId(shardInfo.Id)]
  207. if !found {
  208. existing = make([][]*EcNode, erasure_coding.TotalShardsCount)
  209. ecShardMap[needle.VolumeId(shardInfo.Id)] = existing
  210. }
  211. for _, shardId := range erasure_coding.ShardBits(shardInfo.EcIndexBits).ShardIds() {
  212. existing[shardId] = append(existing[shardId], ecNode)
  213. }
  214. }
  215. }
  216. }
  217. }
  218. func (ecShardLocations EcShardLocations) shardCount() (count int) {
  219. for _, locations := range ecShardLocations {
  220. if len(locations) > 0 {
  221. count++
  222. }
  223. }
  224. return
  225. }