From cea52a4faf2e2926a1573ffafcc7f6220a0304db Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 2 Apr 2020 15:36:55 -0700 Subject: [PATCH] volume copying adds cleaning up on error fix https://github.com/chrislusf/seaweedfs/issues/1253 --- weed/server/volume_grpc_copy.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/weed/server/volume_grpc_copy.go b/weed/server/volume_grpc_copy.go index 5cc75e74c..818123bf4 100644 --- a/weed/server/volume_grpc_copy.go +++ b/weed/server/volume_grpc_copy.go @@ -25,7 +25,20 @@ func (vs *VolumeServer) VolumeCopy(ctx context.Context, req *volume_server_pb.Vo v := vs.store.GetVolume(needle.VolumeId(req.VolumeId)) if v != nil { - return nil, fmt.Errorf("volume %d already exists", req.VolumeId) + + glog.V(0).Infof("volume %d already exists. deleted before copying...", req.VolumeId) + + err := vs.store.UnmountVolume(needle.VolumeId(req.VolumeId)) + if err != nil { + return nil, fmt.Errorf("failed to mount existing volume %d: %v", req.VolumeId, err) + } + + err = vs.store.DeleteVolume(needle.VolumeId(req.VolumeId)) + if err != nil { + return nil, fmt.Errorf("failed to delete existing volume %d: %v", req.VolumeId, err) + } + + glog.V(0).Infof("deleted exisitng volume %d before copying.", req.VolumeId) } location := vs.store.FindFreeLocation() @@ -73,10 +86,15 @@ func (vs *VolumeServer) VolumeCopy(ctx context.Context, req *volume_server_pb.Vo idxFileName = volumeFileName + ".idx" datFileName = volumeFileName + ".dat" + defer func() { + if err != nil && volumeFileName != "" { + os.Remove(idxFileName) + os.Remove(datFileName) + os.Remove(volumeFileName + ".vif") + } + }() + if err != nil && volumeFileName != "" { - os.Remove(idxFileName) - os.Remove(datFileName) - os.Remove(volumeFileName + ".vif") return nil, err }