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.

126 lines
3.6 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. "path/filepath"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  15. )
  16. func init() {
  17. Commands = append(Commands, &commandVolumeConfigureReplication{})
  18. }
  19. type commandVolumeConfigureReplication struct {
  20. }
  21. func (c *commandVolumeConfigureReplication) Name() string {
  22. return "volume.configure.replication"
  23. }
  24. func (c *commandVolumeConfigureReplication) Help() string {
  25. return `change volume replication value
  26. This command changes a volume replication value. It should be followed by "volume.fix.replication".
  27. `
  28. }
  29. func (c *commandVolumeConfigureReplication) Do(args []string, commandEnv *CommandEnv, _ io.Writer) (err error) {
  30. configureReplicationCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  31. volumeIdInt := configureReplicationCommand.Int("volumeId", 0, "the volume id")
  32. replicationString := configureReplicationCommand.String("replication", "", "the intended replication value")
  33. collectionPattern := configureReplicationCommand.String("collectionPattern", "", "match with wildcard characters '*' and '?'")
  34. if err = configureReplicationCommand.Parse(args); err != nil {
  35. return nil
  36. }
  37. if err = commandEnv.confirmIsLocked(args); err != nil {
  38. return
  39. }
  40. if *replicationString == "" {
  41. return fmt.Errorf("empty replication value")
  42. }
  43. replicaPlacement, err := super_block.NewReplicaPlacementFromString(*replicationString)
  44. if err != nil {
  45. return fmt.Errorf("replication format: %v", err)
  46. }
  47. // collect topology information
  48. topologyInfo, _, err := collectTopologyInfo(commandEnv)
  49. if err != nil {
  50. return err
  51. }
  52. vid := needle.VolumeId(*volumeIdInt)
  53. volumeFilter := getVolumeFilter(replicaPlacement, uint32(vid), *collectionPattern)
  54. // find all data nodes with volumes that needs replication change
  55. var allLocations []location
  56. eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  57. loc := newLocation(dc, string(rack), dn)
  58. for _, diskInfo := range dn.DiskInfos {
  59. for _, v := range diskInfo.VolumeInfos {
  60. if volumeFilter(v) {
  61. allLocations = append(allLocations, loc)
  62. continue
  63. }
  64. }
  65. }
  66. })
  67. if len(allLocations) == 0 {
  68. return fmt.Errorf("no volume needs change")
  69. }
  70. for _, dst := range allLocations {
  71. err := operation.WithVolumeServerClient(false, pb.NewServerAddressFromDataNode(dst.dataNode), commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  72. resp, configureErr := volumeServerClient.VolumeConfigure(context.Background(), &volume_server_pb.VolumeConfigureRequest{
  73. VolumeId: uint32(vid),
  74. Replication: replicaPlacement.String(),
  75. })
  76. if configureErr != nil {
  77. return configureErr
  78. }
  79. if resp.Error != "" {
  80. return errors.New(resp.Error)
  81. }
  82. return nil
  83. })
  84. if err != nil {
  85. return err
  86. }
  87. }
  88. return nil
  89. }
  90. func getVolumeFilter(replicaPlacement *super_block.ReplicaPlacement, volumeId uint32, collectionPattern string) func(message *master_pb.VolumeInformationMessage) bool {
  91. replicaPlacementInt32 := uint32(replicaPlacement.Byte())
  92. if volumeId > 0 {
  93. return func(v *master_pb.VolumeInformationMessage) bool {
  94. return v.Id == volumeId && v.ReplicaPlacement != replicaPlacementInt32
  95. }
  96. }
  97. return func(v *master_pb.VolumeInformationMessage) bool {
  98. matched, err := filepath.Match(collectionPattern, v.Collection)
  99. if err != nil {
  100. return false
  101. }
  102. return matched
  103. }
  104. }