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;
}
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) {
final VDI dataDisk = VDI.getByUuid(conn, volumeTo.getPath());
if (dataDisk != null) {
@ -3364,25 +3364,33 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
}
}
if (volumeTo.getVolumeType() == Volume.Type.ROOT) {
final Map<VM, VM.Record> allVMs = VM.getAllRecords(conn);
// add size of memory snapshot vdi
if (allVMs != null && allVMs.size() > 0) {
for (final VM vmr : allVMs.keySet()) {
try {
final String vName = vmr.getNameLabel(conn);
if (vName != null && vName.contains(vmName) && vmr.getIsASnapshot(conn)) {
final VDI memoryVDI = vmr.getSuspendVDI(conn);
if (!isRefNull(memoryVDI)) {
size = size + memoryVDI.getPhysicalUtilisation(conn);
final VDI pMemoryVDI = memoryVDI.getParent(conn);
if (!isRefNull(pMemoryVDI)) {
size = size + pMemoryVDI.getPhysicalUtilisation(conn);
VM vm = getVM(conn, vmName);
if(vm != null){
Set<VM> vmSnapshots=vm.getSnapshots(conn);
if(vmSnapshots != null){
for(VM vmsnap: vmSnapshots){
try {
final String vmSnapName = vmsnap.getNameLabel(conn);
s_logger.debug("snapname " + vmSnapName);
if (vmSnapName != null && vmSnapName.contains(vmSnapshotName) && vmsnap.getIsASnapshot(conn)) {
s_logger.debug("snapname " + vmSnapName + "isASnapshot");
VDI memoryVDI = vmsnap.getSuspendVDI(conn);
if (!isRefNull(memoryVDI)) {
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.Set;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.cloudstack.storage.to.VolumeObjectTO;
import org.apache.log4j.Logger;
@ -142,8 +143,11 @@ public final class CitrixCreateVMSnapshotCommandWrapper extends CommandWrapper<C
}
// calculate used capacity for this VM snapshot
for (final VolumeObjectTO volumeTo : command.getVolumeTOs()) {
final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName());
volumeTo.setSize(size);
try {
final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName(), vmSnapshotName);
volumeTo.setSize(size);
} catch (final CloudRuntimeException cre) {
}
}
success = true;

View File

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