command_volume_configure_replication.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if err = commandEnv.confirmIsLocked(); err != nil {
  29. return
  30. }
  31. configureReplicationCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  32. volumeIdInt := configureReplicationCommand.Int("volumeId", 0, "the volume id")
  33. replicationString := configureReplicationCommand.String("replication", "", "the intended replication value")
  34. if err = configureReplicationCommand.Parse(args); err != nil {
  35. return nil
  36. }
  37. if *replicationString == "" {
  38. return fmt.Errorf("empty replication value")
  39. }
  40. replicaPlacement, err := super_block.NewReplicaPlacementFromString(*replicationString)
  41. if err != nil {
  42. return fmt.Errorf("replication format: %v", err)
  43. }
  44. replicaPlacementInt32 := uint32(replicaPlacement.Byte())
  45. var resp *master_pb.VolumeListResponse
  46. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  47. resp, err = client.VolumeList(context.Background(), &master_pb.VolumeListRequest{})
  48. return err
  49. })
  50. if err != nil {
  51. return err
  52. }
  53. vid := needle.VolumeId(*volumeIdInt)
  54. // find all data nodes with volumes that needs replication change
  55. var allLocations []location
  56. eachDataNode(resp.TopologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  57. loc := newLocation(dc, string(rack), dn)
  58. for _, v := range dn.VolumeInfos {
  59. if v.Id == uint32(vid) && v.ReplicaPlacement != replicaPlacementInt32 {
  60. allLocations = append(allLocations, loc)
  61. continue
  62. }
  63. }
  64. })
  65. if len(allLocations) == 0 {
  66. return fmt.Errorf("no volume needs change")
  67. }
  68. for _, dst := range allLocations {
  69. err := operation.WithVolumeServerClient(dst.dataNode.Id, commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  70. resp, configureErr := volumeServerClient.VolumeConfigure(context.Background(), &volume_server_pb.VolumeConfigureRequest{
  71. VolumeId: uint32(vid),
  72. Replication: replicaPlacement.String(),
  73. })
  74. if configureErr != nil {
  75. return configureErr
  76. }
  77. if resp.Error != "" {
  78. return errors.New(resp.Error)
  79. }
  80. return nil
  81. })
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return nil
  87. }