volume copying adds cleaning up on error

fix https://github.com/chrislusf/seaweedfs/issues/1253
This commit is contained in:
Chris Lu 2020-04-02 15:36:55 -07:00
parent c981975627
commit cea52a4faf

View file

@ -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
}