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.

120 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/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) 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, 0)
  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. eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  56. var targetVolumeIds []uint32
  57. for _, diskInfo := range dn.DiskInfos {
  58. for _, v := range diskInfo.VolumeInfos {
  59. if volumeFilter(v) {
  60. targetVolumeIds = append(targetVolumeIds, v.Id)
  61. }
  62. }
  63. }
  64. if len(targetVolumeIds) == 0 {
  65. return
  66. }
  67. err = operation.WithVolumeServerClient(false, pb.NewServerAddressFromDataNode(dn), commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  68. for _, targetVolumeId := range targetVolumeIds {
  69. resp, configureErr := volumeServerClient.VolumeConfigure(context.Background(), &volume_server_pb.VolumeConfigureRequest{
  70. VolumeId: targetVolumeId,
  71. Replication: replicaPlacement.String(),
  72. })
  73. if configureErr != nil {
  74. return configureErr
  75. }
  76. if resp.Error != "" {
  77. return errors.New(resp.Error)
  78. }
  79. }
  80. return nil
  81. })
  82. if err != nil {
  83. return
  84. }
  85. })
  86. return err
  87. }
  88. func getVolumeFilter(replicaPlacement *super_block.ReplicaPlacement, volumeId uint32, collectionPattern string) func(message *master_pb.VolumeInformationMessage) bool {
  89. replicaPlacementInt32 := uint32(replicaPlacement.Byte())
  90. if volumeId > 0 {
  91. return func(v *master_pb.VolumeInformationMessage) bool {
  92. return v.Id == volumeId && v.ReplicaPlacement != replicaPlacementInt32
  93. }
  94. }
  95. return func(v *master_pb.VolumeInformationMessage) bool {
  96. matched, err := filepath.Match(collectionPattern, v.Collection)
  97. if err != nil {
  98. return false
  99. }
  100. return matched
  101. }
  102. }