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.

261 lines
8.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "github.com/chrislusf/seaweedfs/weed/storage/types"
  8. "io"
  9. "google.golang.org/grpc"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  13. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  14. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  15. )
  16. func init() {
  17. Commands = append(Commands, &commandEcDecode{})
  18. }
  19. type commandEcDecode struct {
  20. }
  21. func (c *commandEcDecode) Name() string {
  22. return "ec.decode"
  23. }
  24. func (c *commandEcDecode) Help() string {
  25. return `decode a erasure coded volume into a normal volume
  26. ec.decode [-collection=""] [-volumeId=<volume_id>]
  27. `
  28. }
  29. func (c *commandEcDecode) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  30. if err = commandEnv.confirmIsLocked(); err != nil {
  31. return
  32. }
  33. encodeCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  34. volumeId := encodeCommand.Int("volumeId", 0, "the volume id")
  35. collection := encodeCommand.String("collection", "", "the collection name")
  36. if err = encodeCommand.Parse(args); err != nil {
  37. return nil
  38. }
  39. vid := needle.VolumeId(*volumeId)
  40. // collect topology information
  41. topologyInfo, _, err := collectTopologyInfo(commandEnv)
  42. if err != nil {
  43. return err
  44. }
  45. // volumeId is provided
  46. if vid != 0 {
  47. return doEcDecode(commandEnv, topologyInfo, *collection, vid)
  48. }
  49. // apply to all volumes in the collection
  50. volumeIds := collectEcShardIds(topologyInfo, *collection)
  51. fmt.Printf("ec encode volumes: %v\n", volumeIds)
  52. for _, vid := range volumeIds {
  53. if err = doEcDecode(commandEnv, topologyInfo, *collection, vid); err != nil {
  54. return err
  55. }
  56. }
  57. return nil
  58. }
  59. func doEcDecode(commandEnv *CommandEnv, topoInfo *master_pb.TopologyInfo, collection string, vid needle.VolumeId) (err error) {
  60. // find volume location
  61. nodeToEcIndexBits := collectEcNodeShardBits(topoInfo, vid)
  62. fmt.Printf("ec volume %d shard locations: %+v\n", vid, nodeToEcIndexBits)
  63. // collect ec shards to the server with most space
  64. targetNodeLocation, err := collectEcShards(commandEnv, nodeToEcIndexBits, collection, vid)
  65. if err != nil {
  66. return fmt.Errorf("collectEcShards for volume %d: %v", vid, err)
  67. }
  68. // generate a normal volume
  69. err = generateNormalVolume(commandEnv.option.GrpcDialOption, vid, collection, targetNodeLocation)
  70. if err != nil {
  71. return fmt.Errorf("generate normal volume %d on %s: %v", vid, targetNodeLocation, err)
  72. }
  73. // delete the previous ec shards
  74. err = mountVolumeAndDeleteEcShards(commandEnv.option.GrpcDialOption, collection, targetNodeLocation, nodeToEcIndexBits, vid)
  75. if err != nil {
  76. return fmt.Errorf("delete ec shards for volume %d: %v", vid, err)
  77. }
  78. return nil
  79. }
  80. func mountVolumeAndDeleteEcShards(grpcDialOption grpc.DialOption, collection string, targetNodeLocation pb.ServerAddress, nodeToEcIndexBits map[pb.ServerAddress]erasure_coding.ShardBits, vid needle.VolumeId) error {
  81. // mount volume
  82. if err := operation.WithVolumeServerClient(targetNodeLocation, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  83. _, mountErr := volumeServerClient.VolumeMount(context.Background(), &volume_server_pb.VolumeMountRequest{
  84. VolumeId: uint32(vid),
  85. })
  86. return mountErr
  87. }); err != nil {
  88. return fmt.Errorf("mountVolumeAndDeleteEcShards mount volume %d on %s: %v", vid, targetNodeLocation, err)
  89. }
  90. // unmount ec shards
  91. for location, ecIndexBits := range nodeToEcIndexBits {
  92. fmt.Printf("unmount ec volume %d on %s has shards: %+v\n", vid, location, ecIndexBits.ShardIds())
  93. err := unmountEcShards(grpcDialOption, vid, location, ecIndexBits.ToUint32Slice())
  94. if err != nil {
  95. return fmt.Errorf("mountVolumeAndDeleteEcShards unmount ec volume %d on %s: %v", vid, location, err)
  96. }
  97. }
  98. // delete ec shards
  99. for location, ecIndexBits := range nodeToEcIndexBits {
  100. fmt.Printf("delete ec volume %d on %s has shards: %+v\n", vid, location, ecIndexBits.ShardIds())
  101. err := sourceServerDeleteEcShards(grpcDialOption, collection, vid, location, ecIndexBits.ToUint32Slice())
  102. if err != nil {
  103. return fmt.Errorf("mountVolumeAndDeleteEcShards delete ec volume %d on %s: %v", vid, location, err)
  104. }
  105. }
  106. return nil
  107. }
  108. func generateNormalVolume(grpcDialOption grpc.DialOption, vid needle.VolumeId, collection string, sourceVolumeServer pb.ServerAddress) error {
  109. fmt.Printf("generateNormalVolume from ec volume %d on %s\n", vid, sourceVolumeServer)
  110. err := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  111. _, genErr := volumeServerClient.VolumeEcShardsToVolume(context.Background(), &volume_server_pb.VolumeEcShardsToVolumeRequest{
  112. VolumeId: uint32(vid),
  113. Collection: collection,
  114. })
  115. return genErr
  116. })
  117. return err
  118. }
  119. func collectEcShards(commandEnv *CommandEnv, nodeToEcIndexBits map[pb.ServerAddress]erasure_coding.ShardBits, collection string, vid needle.VolumeId) (targetNodeLocation pb.ServerAddress, err error) {
  120. maxShardCount := 0
  121. var exisitngEcIndexBits erasure_coding.ShardBits
  122. for loc, ecIndexBits := range nodeToEcIndexBits {
  123. toBeCopiedShardCount := ecIndexBits.MinusParityShards().ShardIdCount()
  124. if toBeCopiedShardCount > maxShardCount {
  125. maxShardCount = toBeCopiedShardCount
  126. targetNodeLocation = loc
  127. exisitngEcIndexBits = ecIndexBits
  128. }
  129. }
  130. fmt.Printf("collectEcShards: ec volume %d collect shards to %s from: %+v\n", vid, targetNodeLocation, nodeToEcIndexBits)
  131. var copiedEcIndexBits erasure_coding.ShardBits
  132. for loc, ecIndexBits := range nodeToEcIndexBits {
  133. if loc == targetNodeLocation {
  134. continue
  135. }
  136. needToCopyEcIndexBits := ecIndexBits.Minus(exisitngEcIndexBits).MinusParityShards()
  137. if needToCopyEcIndexBits.ShardIdCount() == 0 {
  138. continue
  139. }
  140. err = operation.WithVolumeServerClient(targetNodeLocation, commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  141. fmt.Printf("copy %d.%v %s => %s\n", vid, needToCopyEcIndexBits.ShardIds(), loc, targetNodeLocation)
  142. _, copyErr := volumeServerClient.VolumeEcShardsCopy(context.Background(), &volume_server_pb.VolumeEcShardsCopyRequest{
  143. VolumeId: uint32(vid),
  144. Collection: collection,
  145. ShardIds: needToCopyEcIndexBits.ToUint32Slice(),
  146. CopyEcxFile: false,
  147. CopyEcjFile: true,
  148. CopyVifFile: true,
  149. SourceDataNode: string(loc),
  150. })
  151. if copyErr != nil {
  152. return fmt.Errorf("copy %d.%v %s => %s : %v\n", vid, needToCopyEcIndexBits.ShardIds(), loc, targetNodeLocation, copyErr)
  153. }
  154. return nil
  155. })
  156. if err != nil {
  157. break
  158. }
  159. copiedEcIndexBits = copiedEcIndexBits.Plus(needToCopyEcIndexBits)
  160. }
  161. nodeToEcIndexBits[targetNodeLocation] = exisitngEcIndexBits.Plus(copiedEcIndexBits)
  162. return targetNodeLocation, err
  163. }
  164. func collectTopologyInfo(commandEnv *CommandEnv) (topoInfo *master_pb.TopologyInfo, volumeSizeLimitMb uint64, err error) {
  165. var resp *master_pb.VolumeListResponse
  166. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  167. resp, err = client.VolumeList(context.Background(), &master_pb.VolumeListRequest{})
  168. return err
  169. })
  170. if err != nil {
  171. return
  172. }
  173. return resp.TopologyInfo, resp.VolumeSizeLimitMb, nil
  174. }
  175. func collectEcShardIds(topoInfo *master_pb.TopologyInfo, selectedCollection string) (vids []needle.VolumeId) {
  176. vidMap := make(map[uint32]bool)
  177. eachDataNode(topoInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  178. if diskInfo, found := dn.DiskInfos[string(types.HardDriveType)]; found {
  179. for _, v := range diskInfo.EcShardInfos {
  180. if v.Collection == selectedCollection {
  181. vidMap[v.Id] = true
  182. }
  183. }
  184. }
  185. })
  186. for vid := range vidMap {
  187. vids = append(vids, needle.VolumeId(vid))
  188. }
  189. return
  190. }
  191. func collectEcNodeShardBits(topoInfo *master_pb.TopologyInfo, vid needle.VolumeId) map[pb.ServerAddress]erasure_coding.ShardBits {
  192. nodeToEcIndexBits := make(map[pb.ServerAddress]erasure_coding.ShardBits)
  193. eachDataNode(topoInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  194. if diskInfo, found := dn.DiskInfos[string(types.HardDriveType)]; found {
  195. for _, v := range diskInfo.EcShardInfos {
  196. if v.Id == uint32(vid) {
  197. nodeToEcIndexBits[pb.NewServerAddressFromDataNode(dn)] = erasure_coding.ShardBits(v.EcIndexBits)
  198. }
  199. }
  200. }
  201. })
  202. return nodeToEcIndexBits
  203. }