command_volume_configure_replication.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  44. resp, err = client.VolumeList(context.Background(), &master_pb.VolumeListRequest{})
  45. return err
  46. })
  47. if err != nil {
  48. return err
  49. }
  50. vid := needle.VolumeId(*volumeIdInt)
  51. // find all data nodes with volumes that needs replication change
  52. var allLocations []location
  53. eachDataNode(resp.TopologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  54. loc := newLocation(dc, string(rack), dn)
  55. for _, v := range dn.VolumeInfos {
  56. if v.Id == uint32(vid) && v.ReplicaPlacement != replicaPlacementInt32 {
  57. allLocations = append(allLocations, loc)
  58. continue
  59. }
  60. }
  61. })
  62. if len(allLocations) == 0 {
  63. return fmt.Errorf("no volume needs change")
  64. }
  65. for _, dst := range allLocations {
  66. err := operation.WithVolumeServerClient(dst.dataNode.Id, commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  67. resp, configureErr := volumeServerClient.VolumeConfigure(context.Background(), &volume_server_pb.VolumeConfigureRequest{
  68. VolumeId: uint32(vid),
  69. Replication: replicaPlacement.String(),
  70. })
  71. if configureErr != nil {
  72. return configureErr
  73. }
  74. if resp.Error != "" {
  75. return errors.New(resp.Error)
  76. }
  77. return nil
  78. })
  79. if err != nil {
  80. return err
  81. }
  82. }
  83. return nil
  84. }