2019-04-20 18:35:20 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-20 16:27:34 +00:00
|
|
|
"flag"
|
2019-04-20 18:35:20 +00:00
|
|
|
"fmt"
|
2021-09-13 05:47:52 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
2021-08-13 10:09:28 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/wdclient"
|
2019-04-20 18:35:20 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 08:30:24 +00:00
|
|
|
Commands = append(Commands, &commandVolumeMove{})
|
2019-04-20 18:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type commandVolumeMove struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeMove) Name() string {
|
|
|
|
return "volume.move"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeMove) Help() string {
|
2020-01-01 20:38:29 +00:00
|
|
|
return `move a live volume from one volume server to another volume server
|
2019-04-20 18:35:20 +00:00
|
|
|
|
2020-09-20 16:27:34 +00:00
|
|
|
volume.move -source <source volume server host:port> -target <target volume server host:port> -volumeId <volume id>
|
2021-02-22 10:03:12 +00:00
|
|
|
volume.move -source <source volume server host:port> -target <target volume server host:port> -volumeId <volume id> -disk [hdd|ssd|<tag>]
|
2019-04-20 18:35:20 +00:00
|
|
|
|
|
|
|
This command move a live volume from one volume server to another volume server. Here are the steps:
|
|
|
|
|
|
|
|
1. This command asks the target volume server to copy the source volume from source volume server, remember the last entry's timestamp.
|
|
|
|
2. This command asks the target volume server to mount the new volume
|
|
|
|
Now the master will mark this volume id as readonly.
|
|
|
|
3. This command asks the target volume server to tail the source volume for updates after the timestamp, for 1 minutes to drain the requests.
|
|
|
|
4. This command asks the source volume server to unmount the source volume
|
|
|
|
Now the master will mark this volume id as writable.
|
|
|
|
5. This command asks the source volume server to delete the source volume
|
|
|
|
|
2021-02-22 10:03:12 +00:00
|
|
|
The option "-disk [hdd|ssd|<tag>]" can be used to change the volume disk type.
|
2021-02-10 07:58:08 +00:00
|
|
|
|
2019-04-20 18:35:20 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2019-06-05 08:30:24 +00:00
|
|
|
func (c *commandVolumeMove) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-04-20 18:35:20 +00:00
|
|
|
|
2020-09-20 16:27:34 +00:00
|
|
|
volMoveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
volumeIdInt := volMoveCommand.Int("volumeId", 0, "the volume id")
|
|
|
|
sourceNodeStr := volMoveCommand.String("source", "", "the source volume server <host>:<port>")
|
|
|
|
targetNodeStr := volMoveCommand.String("target", "", "the target volume server <host>:<port>")
|
2021-02-22 10:03:12 +00:00
|
|
|
diskTypeStr := volMoveCommand.String("disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
|
2020-09-20 16:27:34 +00:00
|
|
|
if err = volMoveCommand.Parse(args); err != nil {
|
|
|
|
return nil
|
2019-04-20 18:35:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 05:13:34 +00:00
|
|
|
if err = commandEnv.confirmIsLocked(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
sourceVolumeServer, targetVolumeServer := pb.ServerAddress(*sourceNodeStr), pb.ServerAddress(*targetNodeStr)
|
2020-09-20 16:27:34 +00:00
|
|
|
|
|
|
|
volumeId := needle.VolumeId(*volumeIdInt)
|
2019-04-20 18:35:20 +00:00
|
|
|
|
|
|
|
if sourceVolumeServer == targetVolumeServer {
|
|
|
|
return fmt.Errorf("source and target volume servers are the same!")
|
|
|
|
}
|
|
|
|
|
2021-08-10 09:50:28 +00:00
|
|
|
return LiveMoveVolume(commandEnv.option.GrpcDialOption, writer, volumeId, sourceVolumeServer, targetVolumeServer, 5*time.Second, *diskTypeStr, false)
|
2019-04-20 19:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LiveMoveVolume moves one volume from one source volume server to one target volume server, with idleTimeout to drain the incoming requests.
|
2021-09-13 05:47:52 +00:00
|
|
|
func LiveMoveVolume(grpcDialOption grpc.DialOption, writer io.Writer, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer pb.ServerAddress, idleTimeout time.Duration, diskType string, skipTailError bool) (err error) {
|
2019-04-20 19:05:28 +00:00
|
|
|
|
2019-04-20 18:35:20 +00:00
|
|
|
log.Printf("copying volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
|
2021-02-10 07:58:08 +00:00
|
|
|
lastAppendAtNs, err := copyVolume(grpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer, diskType)
|
2019-04-20 18:35:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("copy volume %d from %s to %s: %v", volumeId, sourceVolumeServer, targetVolumeServer, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("tailing volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
|
2020-02-26 05:50:12 +00:00
|
|
|
if err = tailVolume(grpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer, lastAppendAtNs, idleTimeout); err != nil {
|
2021-08-10 09:50:28 +00:00
|
|
|
if skipTailError {
|
2021-08-10 10:25:18 +00:00
|
|
|
fmt.Fprintf(writer, "tail volume %d from %s to %s: %v\n", volumeId, sourceVolumeServer, targetVolumeServer, err)
|
2021-08-10 09:50:28 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("tail volume %d from %s to %s: %v", volumeId, sourceVolumeServer, targetVolumeServer, err)
|
|
|
|
}
|
2019-04-20 18:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("deleting volume %d from %s", volumeId, sourceVolumeServer)
|
2020-02-26 05:50:12 +00:00
|
|
|
if err = deleteVolume(grpcDialOption, volumeId, sourceVolumeServer); err != nil {
|
2019-04-20 18:35:20 +00:00
|
|
|
return fmt.Errorf("delete volume %d from %s: %v", volumeId, sourceVolumeServer, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("moved volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
func copyVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer pb.ServerAddress, diskType string) (lastAppendAtNs uint64, err error) {
|
2019-04-20 18:35:20 +00:00
|
|
|
|
2020-08-19 15:42:56 +00:00
|
|
|
// check to see if the volume is already read-only and if its not then we need
|
|
|
|
// to mark it as read-only and then before we return we need to undo what we
|
|
|
|
// did
|
|
|
|
var shouldMarkWritable bool
|
|
|
|
defer func() {
|
|
|
|
if !shouldMarkWritable {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clientErr := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
|
|
|
_, writableErr := volumeServerClient.VolumeMarkWritable(context.Background(), &volume_server_pb.VolumeMarkWritableRequest{
|
|
|
|
VolumeId: uint32(volumeId),
|
|
|
|
})
|
|
|
|
return writableErr
|
|
|
|
})
|
|
|
|
if clientErr != nil {
|
|
|
|
log.Printf("failed to mark volume %d as writable after copy from %s: %v", volumeId, sourceVolumeServer, clientErr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
|
|
|
resp, statusErr := volumeServerClient.VolumeStatus(context.Background(), &volume_server_pb.VolumeStatusRequest{
|
|
|
|
VolumeId: uint32(volumeId),
|
|
|
|
})
|
|
|
|
if statusErr == nil && !resp.IsReadOnly {
|
|
|
|
shouldMarkWritable = true
|
|
|
|
_, readonlyErr := volumeServerClient.VolumeMarkReadonly(context.Background(), &volume_server_pb.VolumeMarkReadonlyRequest{
|
|
|
|
VolumeId: uint32(volumeId),
|
|
|
|
})
|
|
|
|
return readonlyErr
|
|
|
|
}
|
|
|
|
return statusErr
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
err = operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
|
|
|
resp, replicateErr := volumeServerClient.VolumeCopy(context.Background(), &volume_server_pb.VolumeCopyRequest{
|
2019-04-20 18:35:20 +00:00
|
|
|
VolumeId: uint32(volumeId),
|
2021-09-13 05:47:52 +00:00
|
|
|
SourceDataNode: string(sourceVolumeServer),
|
2021-02-10 07:58:08 +00:00
|
|
|
DiskType: diskType,
|
2019-04-20 18:35:20 +00:00
|
|
|
})
|
|
|
|
if replicateErr == nil {
|
|
|
|
lastAppendAtNs = resp.LastAppendAtNs
|
|
|
|
}
|
|
|
|
return replicateErr
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
func tailVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer pb.ServerAddress, lastAppendAtNs uint64, idleTimeout time.Duration) (err error) {
|
2019-04-20 18:35:20 +00:00
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
return operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
|
|
|
_, replicateErr := volumeServerClient.VolumeTailReceiver(context.Background(), &volume_server_pb.VolumeTailReceiverRequest{
|
2019-04-20 18:35:20 +00:00
|
|
|
VolumeId: uint32(volumeId),
|
|
|
|
SinceNs: lastAppendAtNs,
|
2019-04-20 19:05:28 +00:00
|
|
|
IdleTimeoutSeconds: uint32(idleTimeout.Seconds()),
|
2021-09-13 05:47:52 +00:00
|
|
|
SourceVolumeServer: string(sourceVolumeServer),
|
2019-04-20 18:35:20 +00:00
|
|
|
})
|
|
|
|
return replicateErr
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
func deleteVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer pb.ServerAddress) (err error) {
|
2020-02-26 05:50:12 +00:00
|
|
|
return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
|
|
|
_, deleteErr := volumeServerClient.VolumeDelete(context.Background(), &volume_server_pb.VolumeDeleteRequest{
|
2019-04-20 18:35:20 +00:00
|
|
|
VolumeId: uint32(volumeId),
|
|
|
|
})
|
2019-04-21 03:31:35 +00:00
|
|
|
return deleteErr
|
2019-04-20 18:35:20 +00:00
|
|
|
})
|
|
|
|
}
|
2020-10-28 17:47:09 +00:00
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
func markVolumeWritable(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer pb.ServerAddress, writable bool) (err error) {
|
2020-10-28 17:47:09 +00:00
|
|
|
return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
|
|
|
if writable {
|
|
|
|
_, err = volumeServerClient.VolumeMarkWritable(context.Background(), &volume_server_pb.VolumeMarkWritableRequest{
|
|
|
|
VolumeId: uint32(volumeId),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
_, err = volumeServerClient.VolumeMarkReadonly(context.Background(), &volume_server_pb.VolumeMarkReadonlyRequest{
|
|
|
|
VolumeId: uint32(volumeId),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2021-08-13 10:09:28 +00:00
|
|
|
|
|
|
|
func markVolumeReplicasWritable(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, locations []wdclient.Location, writable bool) error {
|
|
|
|
for _, location := range locations {
|
|
|
|
fmt.Printf("markVolumeReadonly %d on %s ...\n", volumeId, location.Url)
|
2021-09-13 05:47:52 +00:00
|
|
|
if err := markVolumeWritable(grpcDialOption, volumeId, location.ServerAddress(), writable); err != nil {
|
2021-08-13 10:09:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|