command_ec_decode.go 9.5 KB

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