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.

123 lines
3.4 KiB

  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) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. volumeVacuumCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  27. growCount := volumeVacuumCommand.Uint("count", 2, "")
  28. collection := volumeVacuumCommand.String("collection", "", "grow this collection")
  29. dataCenter := volumeVacuumCommand.String("dataCenter", "", "grow volumes only from the specified data center")
  30. rack := volumeVacuumCommand.String("rack", "", "grow volumes only from the specified rack")
  31. dataNode := volumeVacuumCommand.String("dataNode", "", "grow volumes only from the specified data node")
  32. diskType := volumeVacuumCommand.String("diskType", "", "grow volumes only from the specified disk type")
  33. if err = volumeVacuumCommand.Parse(args); err != nil {
  34. return nil
  35. }
  36. if *collection == "" {
  37. return fmt.Errorf("collection option is required")
  38. }
  39. t, _, err := collectTopologyInfo(commandEnv, 0)
  40. if err != nil {
  41. return err
  42. }
  43. volumeGrowRequest := &master_pb.VolumeGrowRequest{
  44. Collection: *collection,
  45. DataCenter: *dataCenter,
  46. Rack: *rack,
  47. DataNode: *dataNode,
  48. WritableVolumeCount: uint32(*growCount),
  49. }
  50. collectionFound := false
  51. dataCenterFound := *dataCenter == ""
  52. rackFound := *rack == ""
  53. dataNodeFound := *dataNode == ""
  54. diskTypeFound := *diskType == ""
  55. for _, dc := range t.DataCenterInfos {
  56. if dc.Id == *dataCenter {
  57. dataCenterFound = true
  58. }
  59. for _, r := range dc.RackInfos {
  60. if r.Id == *rack {
  61. rackFound = true
  62. }
  63. for _, dn := range r.DataNodeInfos {
  64. if dn.Id == *dataNode {
  65. dataNodeFound = true
  66. }
  67. for _, di := range dn.DiskInfos {
  68. if !diskTypeFound && di.Type == types.ToDiskType(*diskType).String() {
  69. diskTypeFound = true
  70. }
  71. for _, vi := range di.VolumeInfos {
  72. if !collectionFound && vi.Collection == *collection {
  73. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(vi.ReplicaPlacement))
  74. volumeGrowRequest.Ttl = needle.LoadTTLFromUint32(vi.Ttl).String()
  75. volumeGrowRequest.DiskType = vi.DiskType
  76. volumeGrowRequest.Replication = replicaPlacement.String()
  77. collectionFound = true
  78. }
  79. if collectionFound && dataCenterFound && rackFound && dataNodeFound && diskTypeFound {
  80. break
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. if !dataCenterFound {
  88. return fmt.Errorf("data center not found")
  89. }
  90. if !rackFound {
  91. return fmt.Errorf("rack not found")
  92. }
  93. if !dataNodeFound {
  94. return fmt.Errorf("data node not found")
  95. }
  96. if !diskTypeFound {
  97. return fmt.Errorf("disk type not found")
  98. }
  99. if !collectionFound {
  100. return fmt.Errorf("collection not found")
  101. }
  102. if err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  103. if _, err := client.VolumeGrow(context.Background(), volumeGrowRequest); err != nil {
  104. return err
  105. }
  106. return nil
  107. }); err != nil {
  108. return
  109. }
  110. return nil
  111. }