2020-02-02 23:37:23 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-09-13 05:47:52 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
2020-02-02 23:37:23 +00:00
|
|
|
"io"
|
2021-11-11 09:01:47 +00:00
|
|
|
"path/filepath"
|
2020-02-02 23:37:23 +00:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/super_block"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-20 16:27:34 +00:00
|
|
|
This command changes a volume replication value. It should be followed by "volume.fix.replication".
|
2020-02-02 23:37:23 +00:00
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2021-11-11 07:08:41 +00:00
|
|
|
func (c *commandVolumeConfigureReplication) Do(args []string, commandEnv *CommandEnv, _ io.Writer) (err error) {
|
2020-04-23 20:37:31 +00:00
|
|
|
|
2020-02-02 23:37:23 +00: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 09:01:47 +00:00
|
|
|
collectionPattern := configureReplicationCommand.String("collectionPattern", "", "match with wildcard characters '*' and '?'")
|
2020-02-02 23:37:23 +00:00
|
|
|
if err = configureReplicationCommand.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 05:13:34 +00:00
|
|
|
if err = commandEnv.confirmIsLocked(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-02 23:37:23 +00: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 08:28:42 +00:00
|
|
|
// collect topology information
|
|
|
|
topologyInfo, _, err := collectTopologyInfo(commandEnv)
|
2020-02-02 23:37:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vid := needle.VolumeId(*volumeIdInt)
|
2021-11-11 09:01:47 +00:00
|
|
|
volumeFilter := getVolumeFilter(replicaPlacement, uint32(vid), *collectionPattern)
|
2020-02-02 23:37:23 +00:00
|
|
|
|
|
|
|
// find all data nodes with volumes that needs replication change
|
|
|
|
var allLocations []location
|
2021-02-22 08:28:42 +00:00
|
|
|
eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
|
2020-02-02 23:37:23 +00:00
|
|
|
loc := newLocation(dc, string(rack), dn)
|
2021-02-16 10:47:02 +00:00
|
|
|
for _, diskInfo := range dn.DiskInfos {
|
|
|
|
for _, v := range diskInfo.VolumeInfos {
|
2021-11-11 08:36:26 +00:00
|
|
|
if volumeFilter(v) {
|
2021-02-16 10:47:02 +00:00
|
|
|
allLocations = append(allLocations, loc)
|
|
|
|
continue
|
|
|
|
}
|
2020-02-02 23:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(allLocations) == 0 {
|
|
|
|
return fmt.Errorf("no volume needs change")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dst := range allLocations {
|
2021-09-13 05:47:52 +00:00
|
|
|
err := operation.WithVolumeServerClient(pb.NewServerAddressFromDataNode(dst.dataNode), commandEnv.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
2020-02-26 05:50:12 +00:00
|
|
|
resp, configureErr := volumeServerClient.VolumeConfigure(context.Background(), &volume_server_pb.VolumeConfigureRequest{
|
2020-02-02 23:37:23 +00:00
|
|
|
VolumeId: uint32(vid),
|
|
|
|
Replication: replicaPlacement.String(),
|
|
|
|
})
|
|
|
|
if configureErr != nil {
|
|
|
|
return configureErr
|
|
|
|
}
|
|
|
|
if resp.Error != "" {
|
|
|
|
return errors.New(resp.Error)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-11 08:36:26 +00:00
|
|
|
|
2021-11-11 09:01:47 +00:00
|
|
|
func getVolumeFilter(replicaPlacement *super_block.ReplicaPlacement, volumeId uint32, collectionPattern string) func(message *master_pb.VolumeInformationMessage) bool {
|
2021-11-11 08:36:26 +00:00
|
|
|
replicaPlacementInt32 := uint32(replicaPlacement.Byte())
|
2021-11-11 09:18:56 +00:00
|
|
|
if volumeId > 0 {
|
2021-11-11 09:01:47 +00:00
|
|
|
return func(v *master_pb.VolumeInformationMessage) bool {
|
2021-11-11 09:18:56 +00:00
|
|
|
return v.Id == volumeId && v.ReplicaPlacement != replicaPlacementInt32
|
2021-11-11 09:01:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-11 08:36:26 +00:00
|
|
|
return func(v *master_pb.VolumeInformationMessage) bool {
|
2021-11-11 09:18:56 +00:00
|
|
|
matched, err := filepath.Match(collectionPattern, v.Collection)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return matched
|
2021-11-11 08:36:26 +00:00
|
|
|
}
|
|
|
|
}
|