2020-02-03 07:37:23 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-11-11 17:01:47 +08:00
|
|
|
"path/filepath"
|
2020-02-03 07:37:23 +08:00
|
|
|
|
2024-11-19 22:33:18 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
2020-02-03 07:37:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Commands = append(Commands, &commandVolumeConfigureReplication{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type commandVolumeConfigureReplication struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeConfigureReplication) Name() string {
|
|
|
|
return "volume.configure.replication"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeConfigureReplication) Help() string {
|
|
|
|
return `change volume replication value
|
|
|
|
|
2020-09-21 00:27:34 +08:00
|
|
|
This command changes a volume replication value. It should be followed by "volume.fix.replication".
|
2020-02-03 07:37:23 +08:00
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2024-09-30 01:38:22 +08:00
|
|
|
func (c *commandVolumeConfigureReplication) HasTag(CommandTag) bool {
|
2024-09-29 11:22:57 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-11 15:08:41 +08:00
|
|
|
func (c *commandVolumeConfigureReplication) Do(args []string, commandEnv *CommandEnv, _ io.Writer) (err error) {
|
2020-04-24 04:37:31 +08:00
|
|
|
|
2020-02-03 07:37:23 +08:00
|
|
|
configureReplicationCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
volumeIdInt := configureReplicationCommand.Int("volumeId", 0, "the volume id")
|
|
|
|
replicationString := configureReplicationCommand.String("replication", "", "the intended replication value")
|
2021-11-11 17:01:47 +08:00
|
|
|
collectionPattern := configureReplicationCommand.String("collectionPattern", "", "match with wildcard characters '*' and '?'")
|
2020-02-03 07:37:23 +08:00
|
|
|
if err = configureReplicationCommand.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 05:24:38 +08:00
|
|
|
if err = commandEnv.confirmIsLocked(args); err != nil {
|
2021-09-14 13:13:34 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:37:23 +08:00
|
|
|
if *replicationString == "" {
|
|
|
|
return fmt.Errorf("empty replication value")
|
|
|
|
}
|
|
|
|
|
|
|
|
replicaPlacement, err := super_block.NewReplicaPlacementFromString(*replicationString)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("replication format: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:28:42 +08:00
|
|
|
// collect topology information
|
2022-02-08 16:53:55 +08:00
|
|
|
topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
|
2020-02-03 07:37:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vid := needle.VolumeId(*volumeIdInt)
|
2021-11-11 17:01:47 +08:00
|
|
|
volumeFilter := getVolumeFilter(replicaPlacement, uint32(vid), *collectionPattern)
|
2020-02-03 07:37:23 +08:00
|
|
|
|
|
|
|
// find all data nodes with volumes that needs replication change
|
2024-11-19 22:33:18 +08:00
|
|
|
eachDataNode(topologyInfo, func(dc DataCenterId, rack RackId, dn *master_pb.DataNodeInfo) {
|
2022-07-22 13:01:05 +08:00
|
|
|
var targetVolumeIds []uint32
|
2021-02-16 18:47:02 +08:00
|
|
|
for _, diskInfo := range dn.DiskInfos {
|
|
|
|
for _, v := range diskInfo.VolumeInfos {
|
2021-11-11 16:36:26 +08:00
|
|
|
if volumeFilter(v) {
|
2022-07-22 13:01:05 +08:00
|
|
|
targetVolumeIds = append(targetVolumeIds, v.Id)
|
2021-02-16 18:47:02 +08:00
|
|
|
}
|
2020-02-03 07:37:23 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-22 13:01:05 +08:00
|
|
|
if len(targetVolumeIds) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = operation.WithVolumeServerClient(false, pb.NewServerAddressFromDataNode(dn), commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
|
|
|
for _, targetVolumeId := range targetVolumeIds {
|
|
|
|
resp, configureErr := volumeServerClient.VolumeConfigure(context.Background(), &volume_server_pb.VolumeConfigureRequest{
|
|
|
|
VolumeId: targetVolumeId,
|
|
|
|
Replication: replicaPlacement.String(),
|
|
|
|
})
|
|
|
|
if configureErr != nil {
|
|
|
|
return configureErr
|
|
|
|
}
|
|
|
|
if resp.Error != "" {
|
|
|
|
return errors.New(resp.Error)
|
|
|
|
}
|
2020-02-03 07:37:23 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-07-22 13:01:05 +08:00
|
|
|
return
|
2020-02-03 07:37:23 +08:00
|
|
|
}
|
2022-07-22 13:01:05 +08:00
|
|
|
})
|
2020-02-03 07:37:23 +08:00
|
|
|
|
2022-07-22 13:01:05 +08:00
|
|
|
return err
|
2020-02-03 07:37:23 +08:00
|
|
|
}
|
2021-11-11 16:36:26 +08:00
|
|
|
|
2021-11-11 17:01:47 +08:00
|
|
|
func getVolumeFilter(replicaPlacement *super_block.ReplicaPlacement, volumeId uint32, collectionPattern string) func(message *master_pb.VolumeInformationMessage) bool {
|
2021-11-11 16:36:26 +08:00
|
|
|
replicaPlacementInt32 := uint32(replicaPlacement.Byte())
|
2021-11-11 17:18:56 +08:00
|
|
|
if volumeId > 0 {
|
2021-11-11 17:01:47 +08:00
|
|
|
return func(v *master_pb.VolumeInformationMessage) bool {
|
2021-11-11 17:18:56 +08:00
|
|
|
return v.Id == volumeId && v.ReplicaPlacement != replicaPlacementInt32
|
2021-11-11 17:01:47 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-11 16:36:26 +08:00
|
|
|
return func(v *master_pb.VolumeInformationMessage) bool {
|
2021-11-11 17:18:56 +08:00
|
|
|
matched, err := filepath.Match(collectionPattern, v.Collection)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return matched
|
2021-11-11 16:36:26 +08:00
|
|
|
}
|
|
|
|
}
|