CLOUDSTACK-8939: VM Snapshot size with memory correctly calculated in cloud.usage_event (XenServer) (#914)

Fixing the error  with snapshot size calculation  in a vm with memory
This commit is contained in:
subhash yedugundla 2017-09-01 14:30:55 +05:30 committed by Rohit Yadav
parent dfd01c99ef
commit 0d81e88601
3 changed files with 38 additions and 21 deletions

View File

@ -3333,7 +3333,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
return _instance; return _instance;
} }
public long getVMSnapshotChainSize(final Connection conn, final VolumeObjectTO volumeTo, final String vmName) throws BadServerResponse, XenAPIException, XmlRpcException { public long getVMSnapshotChainSize(final Connection conn, final VolumeObjectTO volumeTo, final String vmName, final String vmSnapshotName) throws BadServerResponse, XenAPIException, XmlRpcException {
if (volumeTo.getVolumeType() == Volume.Type.DATADISK) { if (volumeTo.getVolumeType() == Volume.Type.DATADISK) {
final VDI dataDisk = VDI.getByUuid(conn, volumeTo.getPath()); final VDI dataDisk = VDI.getByUuid(conn, volumeTo.getPath());
if (dataDisk != null) { if (dataDisk != null) {
@ -3364,25 +3364,33 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
} }
} }
if (volumeTo.getVolumeType() == Volume.Type.ROOT) { if (volumeTo.getVolumeType() == Volume.Type.ROOT) {
final Map<VM, VM.Record> allVMs = VM.getAllRecords(conn); VM vm = getVM(conn, vmName);
// add size of memory snapshot vdi if(vm != null){
if (allVMs != null && allVMs.size() > 0) { Set<VM> vmSnapshots=vm.getSnapshots(conn);
for (final VM vmr : allVMs.keySet()) { if(vmSnapshots != null){
try { for(VM vmsnap: vmSnapshots){
final String vName = vmr.getNameLabel(conn); try {
if (vName != null && vName.contains(vmName) && vmr.getIsASnapshot(conn)) { final String vmSnapName = vmsnap.getNameLabel(conn);
final VDI memoryVDI = vmr.getSuspendVDI(conn); s_logger.debug("snapname " + vmSnapName);
if (!isRefNull(memoryVDI)) { if (vmSnapName != null && vmSnapName.contains(vmSnapshotName) && vmsnap.getIsASnapshot(conn)) {
size = size + memoryVDI.getPhysicalUtilisation(conn); s_logger.debug("snapname " + vmSnapName + "isASnapshot");
final VDI pMemoryVDI = memoryVDI.getParent(conn); VDI memoryVDI = vmsnap.getSuspendVDI(conn);
if (!isRefNull(pMemoryVDI)) { if (!isRefNull(memoryVDI)) {
size = size + pMemoryVDI.getPhysicalUtilisation(conn); size = size + memoryVDI.getPhysicalUtilisation(conn);
s_logger.debug("memoryVDI size :"+size);
String parentUuid = memoryVDI.getSmConfig(conn).get("vhd-parent");
VDI pMemoryVDI = VDI.getByUuid(conn, parentUuid);
if (!isRefNull(pMemoryVDI)) {
size = size + pMemoryVDI.getPhysicalUtilisation(conn);
}
s_logger.debug("memoryVDI size+parent :"+size);
} }
} }
} catch (Exception e) {
s_logger.debug("Exception occurs when calculate snapshot capacity for memory: due to " + e.toString());
continue;
} }
} catch (final Exception e) {
s_logger.debug("Exception occurs when calculate snapshot capacity for memory: due to " + e.toString());
continue;
} }
} }
} }

View File

@ -23,6 +23,7 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.cloudstack.storage.to.VolumeObjectTO;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -142,8 +143,11 @@ public final class CitrixCreateVMSnapshotCommandWrapper extends CommandWrapper<C
} }
// calculate used capacity for this VM snapshot // calculate used capacity for this VM snapshot
for (final VolumeObjectTO volumeTo : command.getVolumeTOs()) { for (final VolumeObjectTO volumeTo : command.getVolumeTOs()) {
final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName()); try {
volumeTo.setSize(size); final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName(), vmSnapshotName);
volumeTo.setSize(size);
} catch (final CloudRuntimeException cre) {
}
} }
success = true; success = true;

View File

@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.cloudstack.storage.to.VolumeObjectTO;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -79,8 +80,12 @@ public final class CitrixDeleteVMSnapshotCommandWrapper extends CommandWrapper<D
} }
// re-calculate used capacify for this VM snapshot // re-calculate used capacify for this VM snapshot
for (final VolumeObjectTO volumeTo : command.getVolumeTOs()) { for (final VolumeObjectTO volumeTo : command.getVolumeTOs()) {
final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName()); try {
volumeTo.setSize(size); final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName(), snapshotName);
volumeTo.setSize(size);
} catch (final CloudRuntimeException cre) {
}
} }
return new DeleteVMSnapshotAnswer(command, command.getVolumeTOs()); return new DeleteVMSnapshotAnswer(command, command.getVolumeTOs());