From 31b2751affd24ccff4633d09b88311587b39289a Mon Sep 17 00:00:00 2001 From: chrislu Date: Thu, 6 Jul 2023 00:32:58 -0700 Subject: [PATCH] clone volume locations in case they are changed fix https://github.com/seaweedfs/seaweedfs/issues/4642 --- weed/shell/command_ec_encode.go | 2 +- weed/shell/command_volume_tier_download.go | 2 +- weed/shell/command_volume_tier_move.go | 4 ++-- weed/shell/command_volume_tier_upload.go | 2 +- weed/wdclient/vid_map.go | 13 +++++++++++++ 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/weed/shell/command_ec_encode.go b/weed/shell/command_ec_encode.go index d5a344e3a..9fe7ad8e4 100644 --- a/weed/shell/command_ec_encode.go +++ b/weed/shell/command_ec_encode.go @@ -98,7 +98,7 @@ func doEcEncode(commandEnv *CommandEnv, collection string, vid needle.VolumeId, } // find volume location - locations, found := commandEnv.MasterClient.GetLocations(uint32(vid)) + locations, found := commandEnv.MasterClient.GetLocationsClone(uint32(vid)) if !found { return fmt.Errorf("volume %d not found", vid) } diff --git a/weed/shell/command_volume_tier_download.go b/weed/shell/command_volume_tier_download.go index bccbe8ccd..0dc34c071 100644 --- a/weed/shell/command_volume_tier_download.go +++ b/weed/shell/command_volume_tier_download.go @@ -105,7 +105,7 @@ func collectRemoteVolumes(topoInfo *master_pb.TopologyInfo, selectedCollection s func doVolumeTierDownload(commandEnv *CommandEnv, writer io.Writer, collection string, vid needle.VolumeId) (err error) { // find volume location - locations, found := commandEnv.MasterClient.GetLocations(uint32(vid)) + locations, found := commandEnv.MasterClient.GetLocationsClone(uint32(vid)) if !found { return fmt.Errorf("volume %d not found", vid) } diff --git a/weed/shell/command_volume_tier_move.go b/weed/shell/command_volume_tier_move.go index 90fef27c0..bf41c2ea0 100644 --- a/weed/shell/command_volume_tier_move.go +++ b/weed/shell/command_volume_tier_move.go @@ -116,7 +116,7 @@ func (c *commandVolumeTierMove) Do(args []string, commandEnv *CommandEnv, writer for job := range jobs { fmt.Fprintf(writer, "moving volume %d from %s to %s with disk type %s ...\n", job.vid, job.src, dst.dataNode.Id, toDiskType.ReadableString()) - locations, found := commandEnv.MasterClient.GetLocations(uint32(job.vid)) + locations, found := commandEnv.MasterClient.GetLocationsClone(uint32(job.vid)) if !found { fmt.Printf("volume %d not found", job.vid) continue @@ -186,7 +186,7 @@ func isOneOf(server string, locations []wdclient.Location) bool { func (c *commandVolumeTierMove) doVolumeTierMove(commandEnv *CommandEnv, writer io.Writer, vid needle.VolumeId, toDiskType types.DiskType, allLocations []location) (err error) { // find volume location - locations, found := commandEnv.MasterClient.GetLocations(uint32(vid)) + locations, found := commandEnv.MasterClient.GetLocationsClone(uint32(vid)) if !found { return fmt.Errorf("volume %d not found", vid) } diff --git a/weed/shell/command_volume_tier_upload.go b/weed/shell/command_volume_tier_upload.go index a8221bbe9..c109d59d8 100644 --- a/weed/shell/command_volume_tier_upload.go +++ b/weed/shell/command_volume_tier_upload.go @@ -97,7 +97,7 @@ func (c *commandVolumeTierUpload) Do(args []string, commandEnv *CommandEnv, writ func doVolumeTierUpload(commandEnv *CommandEnv, writer io.Writer, collection string, vid needle.VolumeId, dest string, keepLocalDatFile bool) (err error) { // find volume location - existingLocations, found := commandEnv.MasterClient.GetLocations(uint32(vid)) + existingLocations, found := commandEnv.MasterClient.GetLocationsClone(uint32(vid)) if !found { return fmt.Errorf("volume %d not found", vid) } diff --git a/weed/wdclient/vid_map.go b/weed/wdclient/vid_map.go index 5c3d167db..7a2a5bb92 100644 --- a/weed/wdclient/vid_map.go +++ b/weed/wdclient/vid_map.go @@ -141,6 +141,19 @@ func (vc *vidMap) GetLocations(vid uint32) (locations []Location, found bool) { return nil, false } +func (vc *vidMap) GetLocationsClone(vid uint32) (locations []Location, found bool) { + locations, found = vc.GetLocations(vid) + + if found { + // clone the locations in case the volume locations are changed below + existingLocations := make([]Location, len(locations)) + copy(existingLocations, locations) + return existingLocations, found + } + + return nil, false +} + func (vc *vidMap) getLocations(vid uint32) (locations []Location, found bool) { vc.RLock() defer vc.RUnlock()