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.

108 lines
2.9 KiB

4 years ago
4 years ago
  1. package shell
  2. import (
  3. "context"
  4. "errors"
  5. "flag"
  6. "fmt"
  7. "github.com/chrislusf/seaweedfs/weed/pb"
  8. "io"
  9. "github.com/chrislusf/seaweedfs/weed/operation"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  12. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  13. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  14. )
  15. func init() {
  16. Commands = append(Commands, &commandVolumeConfigureReplication{})
  17. }
  18. type commandVolumeConfigureReplication struct {
  19. }
  20. func (c *commandVolumeConfigureReplication) Name() string {
  21. return "volume.configure.replication"
  22. }
  23. func (c *commandVolumeConfigureReplication) Help() string {
  24. return `change volume replication value
  25. This command changes a volume replication value. It should be followed by "volume.fix.replication".
  26. `
  27. }
  28. func (c *commandVolumeConfigureReplication) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  29. configureReplicationCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  30. volumeIdInt := configureReplicationCommand.Int("volumeId", 0, "the volume id")
  31. replicationString := configureReplicationCommand.String("replication", "", "the intended replication value")
  32. if err = configureReplicationCommand.Parse(args); err != nil {
  33. return nil
  34. }
  35. if err = commandEnv.confirmIsLocked(); err != nil {
  36. return
  37. }
  38. if *replicationString == "" {
  39. return fmt.Errorf("empty replication value")
  40. }
  41. replicaPlacement, err := super_block.NewReplicaPlacementFromString(*replicationString)
  42. if err != nil {
  43. return fmt.Errorf("replication format: %v", err)
  44. }
  45. replicaPlacementInt32 := uint32(replicaPlacement.Byte())
  46. // collect topology information
  47. topologyInfo, _, err := collectTopologyInfo(commandEnv)
  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(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  55. loc := newLocation(dc, string(rack), dn)
  56. for _, diskInfo := range dn.DiskInfos {
  57. for _, v := range diskInfo.VolumeInfos {
  58. if v.Id == uint32(vid) && v.ReplicaPlacement != replicaPlacementInt32 {
  59. allLocations = append(allLocations, loc)
  60. continue
  61. }
  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(pb.NewServerAddressFromDataNode(dst.dataNode), 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. }