command_volume_configure_replication.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package shell
  2. import (
  3. "context"
  4. "errors"
  5. "flag"
  6. "fmt"
  7. "io"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandVolumeConfigureReplication{})
  16. }
  17. type commandVolumeConfigureReplication struct {
  18. }
  19. func (c *commandVolumeConfigureReplication) Name() string {
  20. return "volume.configure.replication"
  21. }
  22. func (c *commandVolumeConfigureReplication) Help() string {
  23. return `change volume replication value
  24. This command changes a volume replication value. It should be followed by volume.fix.replication.
  25. `
  26. }
  27. func (c *commandVolumeConfigureReplication) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  28. configureReplicationCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  29. volumeIdInt := configureReplicationCommand.Int("volumeId", 0, "the volume id")
  30. replicationString := configureReplicationCommand.String("replication", "", "the intended replication value")
  31. if err = configureReplicationCommand.Parse(args); err != nil {
  32. return nil
  33. }
  34. if *replicationString == "" {
  35. return fmt.Errorf("empty replication value")
  36. }
  37. replicaPlacement, err := super_block.NewReplicaPlacementFromString(*replicationString)
  38. if err != nil {
  39. return fmt.Errorf("replication format: %v", err)
  40. }
  41. replicaPlacementInt32 := uint32(replicaPlacement.Byte())
  42. var resp *master_pb.VolumeListResponse
  43. ctx := context.Background()
  44. err = commandEnv.MasterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  45. resp, err = client.VolumeList(ctx, &master_pb.VolumeListRequest{})
  46. return err
  47. })
  48. if err != nil {
  49. return err
  50. }
  51. vid := needle.VolumeId(*volumeIdInt)
  52. // find all data nodes with volumes that needs replication change
  53. var allLocations []location
  54. eachDataNode(resp.TopologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  55. loc := newLocation(dc, string(rack), dn)
  56. for _, v := range dn.VolumeInfos {
  57. if v.Id == uint32(vid) && v.ReplicaPlacement != replicaPlacementInt32 {
  58. allLocations = append(allLocations, loc)
  59. continue
  60. }
  61. }
  62. })
  63. if len(allLocations) == 0 {
  64. return fmt.Errorf("no volume needs change")
  65. }
  66. for _, dst := range allLocations {
  67. err := operation.WithVolumeServerClient(dst.dataNode.Id, commandEnv.option.GrpcDialOption, func(ctx context.Context, volumeServerClient volume_server_pb.VolumeServerClient) error {
  68. resp, configureErr := volumeServerClient.VolumeConfigure(ctx, &volume_server_pb.VolumeConfigureRequest{
  69. VolumeId: uint32(vid),
  70. Replication: replicaPlacement.String(),
  71. })
  72. if configureErr != nil {
  73. return configureErr
  74. }
  75. if resp.Error != "" {
  76. return errors.New(resp.Error)
  77. }
  78. return nil
  79. })
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }