command_volume_grow.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  8. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  10. "io"
  11. )
  12. func init() {
  13. Commands = append(Commands, &commandGrow{})
  14. }
  15. type commandGrow struct {
  16. }
  17. func (c *commandGrow) Name() string {
  18. return "volume.grow"
  19. }
  20. func (c *commandGrow) Help() string {
  21. return `grow volumes
  22. volume.grow [-collection=<collection name>] [-dataCenter=<data center name>]
  23. `
  24. }
  25. func (c *commandGrow) HasTag(CommandTag) bool {
  26. return false
  27. }
  28. func (c *commandGrow) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  29. volumeVacuumCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  30. growCount := volumeVacuumCommand.Uint("count", 2, "")
  31. collection := volumeVacuumCommand.String("collection", "", "grow this collection")
  32. dataCenter := volumeVacuumCommand.String("dataCenter", "", "grow volumes only from the specified data center")
  33. rack := volumeVacuumCommand.String("rack", "", "grow volumes only from the specified rack")
  34. dataNode := volumeVacuumCommand.String("dataNode", "", "grow volumes only from the specified data node")
  35. diskType := volumeVacuumCommand.String("diskType", "", "grow volumes only from the specified disk type")
  36. if err = volumeVacuumCommand.Parse(args); err != nil {
  37. return nil
  38. }
  39. if *collection == "" {
  40. return fmt.Errorf("collection option is required")
  41. }
  42. t, _, err := collectTopologyInfo(commandEnv, 0)
  43. if err != nil {
  44. return err
  45. }
  46. volumeGrowRequest := &master_pb.VolumeGrowRequest{
  47. Collection: *collection,
  48. DataCenter: *dataCenter,
  49. Rack: *rack,
  50. DataNode: *dataNode,
  51. WritableVolumeCount: uint32(*growCount),
  52. }
  53. collectionFound := false
  54. dataCenterFound := *dataCenter == ""
  55. rackFound := *rack == ""
  56. dataNodeFound := *dataNode == ""
  57. diskTypeFound := *diskType == ""
  58. for _, dc := range t.DataCenterInfos {
  59. if dc.Id == *dataCenter {
  60. dataCenterFound = true
  61. }
  62. for _, r := range dc.RackInfos {
  63. if r.Id == *rack {
  64. rackFound = true
  65. }
  66. for _, dn := range r.DataNodeInfos {
  67. if dn.Id == *dataNode {
  68. dataNodeFound = true
  69. }
  70. for _, di := range dn.DiskInfos {
  71. if !diskTypeFound && di.Type == types.ToDiskType(*diskType).String() {
  72. diskTypeFound = true
  73. }
  74. for _, vi := range di.VolumeInfos {
  75. if !collectionFound && vi.Collection == *collection {
  76. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(vi.ReplicaPlacement))
  77. volumeGrowRequest.Ttl = needle.LoadTTLFromUint32(vi.Ttl).String()
  78. volumeGrowRequest.DiskType = vi.DiskType
  79. volumeGrowRequest.Replication = replicaPlacement.String()
  80. collectionFound = true
  81. }
  82. if collectionFound && dataCenterFound && rackFound && dataNodeFound && diskTypeFound {
  83. break
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. if !dataCenterFound {
  91. return fmt.Errorf("data center not found")
  92. }
  93. if !rackFound {
  94. return fmt.Errorf("rack not found")
  95. }
  96. if !dataNodeFound {
  97. return fmt.Errorf("data node not found")
  98. }
  99. if !diskTypeFound {
  100. return fmt.Errorf("disk type not found")
  101. }
  102. if !collectionFound {
  103. return fmt.Errorf("collection not found")
  104. }
  105. if err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  106. if _, err := client.VolumeGrow(context.Background(), volumeGrowRequest); err != nil {
  107. return err
  108. }
  109. return nil
  110. }); err != nil {
  111. return
  112. }
  113. return nil
  114. }