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.

124 lines
3.6 KiB

3 months ago
4 years ago
4 years ago
  1. package shell
  2. import (
  3. "context"
  4. "errors"
  5. "flag"
  6. "fmt"
  7. "github.com/seaweedfs/seaweedfs/weed/pb"
  8. "io"
  9. "path/filepath"
  10. "github.com/seaweedfs/seaweedfs/weed/operation"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  14. "github.com/seaweedfs/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) HasTag(CommandTag) bool {
  30. return false
  31. }
  32. func (c *commandVolumeConfigureReplication) Do(args []string, commandEnv *CommandEnv, _ io.Writer) (err error) {
  33. configureReplicationCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  34. volumeIdInt := configureReplicationCommand.Int("volumeId", 0, "the volume id")
  35. replicationString := configureReplicationCommand.String("replication", "", "the intended replication value")
  36. collectionPattern := configureReplicationCommand.String("collectionPattern", "", "match with wildcard characters '*' and '?'")
  37. if err = configureReplicationCommand.Parse(args); err != nil {
  38. return nil
  39. }
  40. if err = commandEnv.confirmIsLocked(args); err != nil {
  41. return
  42. }
  43. if *replicationString == "" {
  44. return fmt.Errorf("empty replication value")
  45. }
  46. replicaPlacement, err := super_block.NewReplicaPlacementFromString(*replicationString)
  47. if err != nil {
  48. return fmt.Errorf("replication format: %v", err)
  49. }
  50. // collect topology information
  51. topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
  52. if err != nil {
  53. return err
  54. }
  55. vid := needle.VolumeId(*volumeIdInt)
  56. volumeFilter := getVolumeFilter(replicaPlacement, uint32(vid), *collectionPattern)
  57. // find all data nodes with volumes that needs replication change
  58. eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  59. var targetVolumeIds []uint32
  60. for _, diskInfo := range dn.DiskInfos {
  61. for _, v := range diskInfo.VolumeInfos {
  62. if volumeFilter(v) {
  63. targetVolumeIds = append(targetVolumeIds, v.Id)
  64. }
  65. }
  66. }
  67. if len(targetVolumeIds) == 0 {
  68. return
  69. }
  70. err = operation.WithVolumeServerClient(false, pb.NewServerAddressFromDataNode(dn), commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  71. for _, targetVolumeId := range targetVolumeIds {
  72. resp, configureErr := volumeServerClient.VolumeConfigure(context.Background(), &volume_server_pb.VolumeConfigureRequest{
  73. VolumeId: targetVolumeId,
  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. }
  83. return nil
  84. })
  85. if err != nil {
  86. return
  87. }
  88. })
  89. return err
  90. }
  91. func getVolumeFilter(replicaPlacement *super_block.ReplicaPlacement, volumeId uint32, collectionPattern string) func(message *master_pb.VolumeInformationMessage) bool {
  92. replicaPlacementInt32 := uint32(replicaPlacement.Byte())
  93. if volumeId > 0 {
  94. return func(v *master_pb.VolumeInformationMessage) bool {
  95. return v.Id == volumeId && v.ReplicaPlacement != replicaPlacementInt32
  96. }
  97. }
  98. return func(v *master_pb.VolumeInformationMessage) bool {
  99. matched, err := filepath.Match(collectionPattern, v.Collection)
  100. if err != nil {
  101. return false
  102. }
  103. return matched
  104. }
  105. }