bug CS-15389: Added prepareStop to Virtualmachine manager and moved network usage collection into prepareStop of VR manager

This commit is contained in:
kishan 2012-06-28 18:06:30 -07:00
parent 2a95258fe9
commit 468f2720eb
19 changed files with 183 additions and 175 deletions

View File

@ -352,12 +352,12 @@ public class MockVmManagerImpl implements MockVmManager {
_mockAgentMgr.handleSystemVMStop(vm.getId());
}
return new StopAnswer(cmd, null, new Integer(0), new Long(100), new Long(200));
return new StopAnswer(cmd, null, new Integer(0), true);
}
@Override
public Answer rebootVM(RebootCommand cmd) {
return new RebootAnswer(cmd, "Rebooted "+cmd.getVmName());
return new RebootAnswer(cmd, "Rebooted "+cmd.getVmName(), false);
}
@Override

View File

@ -233,10 +233,10 @@ public class AgentRoutingResource extends AgentStorageResource {
Answer result = _simMgr.simulate(cmd, hostGuid);
if (!result.getResult()) {
return new StopAnswer(cmd, result.getDetails());
return new StopAnswer(cmd, result.getDetails(), false);
}
answer = new StopAnswer(cmd, null, 0, new Long(100), new Long(200));
answer = new StopAnswer(cmd, null, 0, true);
Pair<Long, Long> data = _runningVms.get(vmName);
if (data != null) {
this.usedCpu -= data.first();

View File

@ -443,8 +443,6 @@ public class FakeComputingResource extends ServerResourceBase implements
String vmName = cmd.getVmName();
Integer port = vmMgr.getVncPort(vmName);
Long bytesReceived = null;
Long bytesSent = null;
State state = null;
synchronized (_vms) {
@ -462,17 +460,16 @@ public class FakeComputingResource extends ServerResourceBase implements
s_logger.warn("Couldn't stop " + vmName);
if (result != null) {
return new StopAnswer(cmd, result);
return new StopAnswer(cmd, result, false);
}
}
answer = new StopAnswer(cmd, null, port, bytesSent, bytesReceived);
answer = new StopAnswer(cmd, null, port, true);
String result2 = vmMgr.cleanupVnet(cmd.getVnet());
if (result2 != null) {
result = result2 + (result != null ? ("\n" + result) : "");
answer = new StopAnswer(cmd, result, port, bytesSent,
bytesReceived);
answer = new StopAnswer(cmd, result, port, true);
}
_dhcpSnooper.cleanup(vmName, null);
@ -498,7 +495,7 @@ public class FakeComputingResource extends ServerResourceBase implements
protected Answer execute(RebootCommand cmd) {
VmMgr vmMgr = getVmManager();
vmMgr.rebootVM(cmd.getVmName());
return new RebootAnswer(cmd, "success", 0L, 0L);
return new RebootAnswer(cmd, "success", true);
}
private Answer execute(PingTestCommand cmd) {

View File

@ -2270,8 +2270,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements
}
private Answer execute(RebootCommand cmd) {
Long bytesReceived = null;
Long bytesSent = null;
synchronized (_vms) {
_vms.put(cmd.getVmName(), State.Starting);
@ -2288,13 +2286,12 @@ public class LibvirtComputingResource extends ServerResourceBase implements
}
get_rule_logs_for_vms();
return new RebootAnswer(cmd, null, bytesSent, bytesReceived,
vncPort);
return new RebootAnswer(cmd, null, vncPort);
} else {
return new RebootAnswer(cmd, result);
return new RebootAnswer(cmd, result, false);
}
} catch (LibvirtException e) {
return new RebootAnswer(cmd, e.getMessage());
return new RebootAnswer(cmd, e.getMessage(), false);
} finally {
synchronized (_vms) {
_vms.put(cmd.getVmName(), State.Running);
@ -2303,16 +2300,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements
}
protected Answer execute(RebootRouterCommand cmd) {
Long bytesSent = 0L;
Long bytesRcvd = 0L;
if (VirtualMachineName.isValidRouterName(cmd.getVmName())) {
long[] stats = getNetworkStats(cmd.getPrivateIpAddress());
bytesSent = stats[0];
bytesRcvd = stats[1];
}
RebootAnswer answer = (RebootAnswer) execute((RebootCommand) cmd);
answer.setBytesSent(bytesSent);
answer.setBytesReceived(bytesRcvd);
String result = _virtRouterResource.connect(cmd.getPrivateIpAddress());
if (result == null) {
networkUsage(cmd.getPrivateIpAddress(), "create", null);
@ -2345,9 +2333,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements
protected Answer execute(StopCommand cmd) {
final String vmName = cmd.getVmName();
Long bytesReceived = new Long(0);
Long bytesSent = new Long(0);
State state = null;
synchronized (_vms) {
state = _vms.get(vmName);
@ -2373,9 +2358,9 @@ public class LibvirtComputingResource extends ServerResourceBase implements
result = result2 + result;
}
state = State.Stopped;
return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
return new StopAnswer(cmd, result, 0, true);
} catch (LibvirtException e) {
return new StopAnswer(cmd, e.getMessage());
return new StopAnswer(cmd, e.getMessage(), false);
} finally {
synchronized (_vms) {
if (state != null) {

View File

@ -17,52 +17,25 @@
package com.cloud.agent.api;
public class RebootAnswer extends Answer {
Long bytesSent;
Long bytesReceived;
Integer vncPort;
protected RebootAnswer() {
}
public RebootAnswer(RebootCommand cmd, String details, Long bytesSent, Long bytesReceived, Integer vncport) {
public RebootAnswer(RebootCommand cmd, String details, Integer vncport) {
super(cmd, true, details);
this.bytesReceived = bytesReceived;
this.bytesSent = bytesSent;
this.vncPort = vncport;
}
public RebootAnswer(RebootCommand cmd, String details, Long bytesSent, Long bytesReceived) {
super(cmd, true, details);
this.bytesReceived = bytesReceived;
this.bytesSent = bytesSent;
public RebootAnswer(RebootCommand cmd, String details, boolean success) {
super(cmd, success, details);
this.vncPort = null;
}
public RebootAnswer(RebootCommand cmd, String details) {
super(cmd, false, details);
bytesSent = null;
bytesReceived = null;
}
public RebootAnswer(RebootCommand cmd, Exception e) {
super(cmd, e);
}
public void setBytesReceived(Long bytesReceived) {
this.bytesReceived = bytesReceived;
}
public Long getBytesReceived() {
return bytesReceived;
}
public void setBytesSent(Long bytesSent) {
this.bytesSent = bytesSent;
}
public Long getBytesSent() {
return bytesSent;
}
public Integer getVncPort() {
return vncPort;
}

View File

@ -22,13 +22,13 @@ public class StopAnswer extends RebootAnswer {
protected StopAnswer() {
}
public StopAnswer(StopCommand cmd, String details, Integer vncPort, Long bytesSent, Long bytesReceived) {
super(cmd, details, bytesSent, bytesReceived);
public StopAnswer(StopCommand cmd, String details, Integer vncPort, boolean success) {
super(cmd, details, success);
this.vncPort = vncPort;
}
public StopAnswer(StopCommand cmd, String details) {
super(cmd, details);
public StopAnswer(StopCommand cmd, String details, boolean success) {
super(cmd, details, success);
vncPort = null;
}

View File

@ -23,7 +23,6 @@ public class StopCommand extends RebootCommand {
private boolean isProxy=false;
private String urlPort=null;
private String publicConsoleProxyIpAddress=null;
private String privateRouterIpAddress=null;
protected StopCommand() {
}
@ -45,12 +44,6 @@ public class StopCommand extends RebootCommand {
this.vnet = vnet;
}
public StopCommand(VirtualMachine vm, String vmName, String vnet, String privateRouterIpAddress) {
super(vmName);
this.vnet = vnet;
this.privateRouterIpAddress = privateRouterIpAddress;
}
public StopCommand(String vmName) {
super(vmName);
}
@ -76,7 +69,4 @@ public class StopCommand extends RebootCommand {
return this.publicConsoleProxyIpAddress;
}
public String getPrivateRouterIpAddress() {
return privateRouterIpAddress;
}
}

View File

@ -716,7 +716,7 @@ public class OvmResourceBase implements ServerResource, HypervisorResource {
vm = OvmVm.getDetails(_conn, vmName);
} catch (XmlRpcException e) {
s_logger.debug("Unable to get details of vm: " + vmName + ", treating it as stopped", e);
return new StopAnswer(cmd, "success", 0, 0L, 0L);
return new StopAnswer(cmd, "success", 0, true);
}
deleteAllNetworkRulesForVm(vmName);
@ -724,10 +724,10 @@ public class OvmResourceBase implements ServerResource, HypervisorResource {
cleanup(vm);
state = State.Stopped;
return new StopAnswer(cmd, "success", 0, 0L, 0L);
return new StopAnswer(cmd, "success", 0, true);
} catch (Exception e) {
s_logger.debug("Stop " + vmName + "failed", e);
return new StopAnswer(cmd, e.getMessage());
return new StopAnswer(cmd, e.getMessage(), false);
} finally {
synchronized(_vms) {
if (state != null) {
@ -749,10 +749,10 @@ public class OvmResourceBase implements ServerResource, HypervisorResource {
try {
Map<String, String> res = OvmVm.reboot(_conn, vmName);
Integer vncPort = Integer.parseInt(res.get("vncPort"));
return new RebootAnswer(cmd, null, null, null, vncPort);
return new RebootAnswer(cmd, null, vncPort);
} catch (Exception e) {
s_logger.debug("Reboot " + vmName + " failed", e);
return new RebootAnswer(cmd, e.getMessage());
return new RebootAnswer(cmd, e.getMessage(), false);
} finally {
synchronized(_vms) {
_vms.put(cmd.getVmName(), State.Running);

View File

@ -2027,16 +2027,6 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, "0");
if (getVmState(vmMo) != State.Stopped) {
Long bytesSent = 0L;
Long bytesRcvd = 0L;
if (VirtualMachineName.isValidRouterName(cmd.getVmName())) {
if (cmd.getPrivateRouterIpAddress() != null) {
long[] stats = getNetworkStats(cmd.getPrivateRouterIpAddress());
bytesSent = stats[0];
bytesRcvd = stats[1];
}
}
// before we stop VM, remove all possible snapshots on the VM to let
// disk chain be collapsed
@ -2044,11 +2034,11 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
vmMo.removeAllSnapshots();
if (vmMo.safePowerOff(_shutdown_waitMs)) {
state = State.Stopped;
return new StopAnswer(cmd, "Stop VM " + cmd.getVmName() + " Succeed", 0, bytesSent, bytesRcvd);
return new StopAnswer(cmd, "Stop VM " + cmd.getVmName() + " Succeed", 0, true);
} else {
String msg = "Have problem in powering off VM " + cmd.getVmName() + ", let the process continue";
s_logger.warn(msg);
return new StopAnswer(cmd, msg, 0, 0L, 0L);
return new StopAnswer(cmd, msg, 0, true);
}
} else {
state = State.Stopped;
@ -2056,7 +2046,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
String msg = "VM " + cmd.getVmName() + " is already in stopped state";
s_logger.info(msg);
return new StopAnswer(cmd, msg, 0, 0L, 0L);
return new StopAnswer(cmd, msg, 0, true);
} finally {
synchronized (_vms) {
_vms.put(cmd.getVmName(), state);
@ -2069,7 +2059,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
String msg = "VM " + cmd.getVmName() + " is no longer in vSphere";
s_logger.info(msg);
return new StopAnswer(cmd, msg, 0, 0L, 0L);
return new StopAnswer(cmd, msg, 0, true);
}
} catch (Exception e) {
if (e instanceof RemoteException) {
@ -2079,7 +2069,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
String msg = "StopCommand failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg);
return new StopAnswer(cmd, msg);
return new StopAnswer(cmd, msg, false);
}
}
@ -2088,17 +2078,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
s_logger.info("Executing resource RebootRouterCommand: " + _gson.toJson(cmd));
}
Long bytesSent = 0L;
Long bytesRcvd = 0L;
if (VirtualMachineName.isValidRouterName(cmd.getVmName())) {
long[] stats = getNetworkStats(cmd.getPrivateIpAddress());
bytesSent = stats[0];
bytesRcvd = stats[1];
}
RebootAnswer answer = (RebootAnswer) execute((RebootCommand) cmd);
answer.setBytesSent(bytesSent);
answer.setBytesReceived(bytesRcvd);
if (answer.getResult()) {
String connectResult = connect(cmd.getVmName(), cmd.getPrivateIpAddress());
@ -2124,7 +2104,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
if (vmMo != null) {
try {
vmMo.rebootGuest();
return new RebootAnswer(cmd, "reboot succeeded", null, null);
return new RebootAnswer(cmd, "reboot succeeded", true);
} catch(ToolsUnavailable e) {
s_logger.warn("VMware tools is not installed at guest OS, we will perform hard reset for reboot");
} catch(Exception e) {
@ -2133,16 +2113,16 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
// continue to try with hard-reset
if (vmMo.reset()) {
return new RebootAnswer(cmd, "reboot succeeded", null, null);
return new RebootAnswer(cmd, "reboot succeeded", true);
}
String msg = "Reboot failed in vSphere. vm: " + cmd.getVmName();
s_logger.warn(msg);
return new RebootAnswer(cmd, msg);
return new RebootAnswer(cmd, msg, false);
} else {
String msg = "Unable to find the VM in vSphere to reboot. vm: " + cmd.getVmName();
s_logger.warn(msg);
return new RebootAnswer(cmd, msg);
return new RebootAnswer(cmd, msg, false);
}
} catch (Exception e) {
if (e instanceof RemoteException) {
@ -2152,7 +2132,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
String msg = "RebootCommand failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg);
return new RebootAnswer(cmd, msg);
return new RebootAnswer(cmd, msg, false);
}
}

View File

@ -201,7 +201,6 @@ import com.cloud.utils.net.NetUtils;
import com.cloud.vm.DiskProfile;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachineName;
import com.trilead.ssh2.SCPClient;
import com.xensource.xenapi.Bond;
import com.xensource.xenapi.Connection;
@ -2823,10 +2822,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
vms = VM.getByNameLabel(conn, cmd.getVmName());
} catch (XenAPIException e0) {
s_logger.debug("getByNameLabel failed " + e0.toString());
return new RebootAnswer(cmd, "getByNameLabel failed " + e0.toString());
return new RebootAnswer(cmd, "getByNameLabel failed " + e0.toString(), false);
} catch (Exception e0) {
s_logger.debug("getByNameLabel failed " + e0.getMessage());
return new RebootAnswer(cmd, "getByNameLabel failed");
return new RebootAnswer(cmd, "getByNameLabel failed", false);
}
for (VM vm : vms) {
try {
@ -2834,10 +2833,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
} catch (Exception e) {
String msg = e.toString();
s_logger.warn(msg, e);
return new RebootAnswer(cmd, msg);
return new RebootAnswer(cmd, msg, false);
}
}
return new RebootAnswer(cmd, "reboot succeeded", null, null);
return new RebootAnswer(cmd, "reboot succeeded", true);
} finally {
synchronized (_cluster.intern()) {
s_vms.put(_cluster, _name, cmd.getVmName(), State.Running);
@ -2848,16 +2847,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
protected Answer execute(RebootRouterCommand cmd) {
Connection conn = getConnection();
Long bytesSent = 0L;
Long bytesRcvd = 0L;
if (VirtualMachineName.isValidRouterName(cmd.getVmName())) {
long[] stats = getNetworkStats(conn, cmd.getPrivateIpAddress());
bytesSent = stats[0];
bytesRcvd = stats[1];
}
RebootAnswer answer = execute((RebootCommand) cmd);
answer.setBytesSent(bytesSent);
answer.setBytesReceived(bytesRcvd);
if (answer.getResult()) {
String cnct = connect(conn, cmd.getVmName(), cmd.getPrivateIpAddress());
networkUsage(conn, cmd.getPrivateIpAddress(), "create", null);
@ -3320,23 +3310,21 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
s_logger.info("VM does not exist on XenServer" + _host.uuid);
s_vms.remove(_cluster, _name, vmName);
}
return new StopAnswer(cmd, "VM does not exist", 0 , 0L, 0L);
return new StopAnswer(cmd, "VM does not exist", 0 , true);
}
Long bytesSent = 0L;
Long bytesRcvd = 0L;
for (VM vm : vms) {
VM.Record vmr = vm.getRecord(conn);
if (vmr.isControlDomain) {
String msg = "Tring to Shutdown control domain";
s_logger.warn(msg);
return new StopAnswer(cmd, msg);
return new StopAnswer(cmd, msg, false);
}
if (vmr.powerState == VmPowerState.RUNNING && !isRefNull(vmr.residentOn) && !vmr.residentOn.getUuid(conn).equals(_host.uuid)) {
String msg = "Stop Vm " + vmName + " failed due to this vm is not running on this host: " + _host.uuid + " but host:" + vmr.residentOn.getUuid(conn);
s_logger.warn(msg);
return new StopAnswer(cmd, msg);
return new StopAnswer(cmd, msg, false);
}
State state = s_vms.getState(_cluster, vmName);
@ -3350,13 +3338,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
if (vmr.powerState == VmPowerState.RUNNING) {
/* when stop a vm, set affinity to current xenserver */
vm.setAffinity(conn, vm.getResidentOn(conn));
if (VirtualMachineName.isValidRouterName(vmName)) {
if (cmd.getPrivateRouterIpAddress() != null) {
long[] stats = getNetworkStats(conn, cmd.getPrivateRouterIpAddress());
bytesSent = stats[0];
bytesRcvd = stats[1];
}
}
if (_canBridgeFirewall) {
String result = callHostPlugin(conn, "vmops", "destroy_network_rules_for_vm", "vmName", cmd
.getVmName());
@ -3371,7 +3353,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
} catch (Exception e) {
String msg = "Catch exception " + e.getClass().getName() + " when stop VM:" + cmd.getVmName() + " due to " + e.toString();
s_logger.debug(msg);
return new StopAnswer(cmd, msg);
return new StopAnswer(cmd, msg, false);
} finally {
try {
@ -3396,7 +3378,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
disableVlanNetwork(conn, network);
}
}
return new StopAnswer(cmd, "Stop VM " + vmName + " Succeed", 0, bytesSent, bytesRcvd);
return new StopAnswer(cmd, "Stop VM " + vmName + " Succeed", 0, true);
}
} catch (XenAPIException e) {
String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e.toString();
@ -3416,16 +3398,16 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
} catch (XenAPIException e) {
String msg = "Stop Vm " + vmName + " fail due to " + e.toString();
s_logger.warn(msg, e);
return new StopAnswer(cmd, msg);
return new StopAnswer(cmd, msg, false);
} catch (XmlRpcException e) {
String msg = "Stop Vm " + vmName + " fail due to " + e.getMessage();
s_logger.warn(msg, e);
return new StopAnswer(cmd, msg);
return new StopAnswer(cmd, msg, false);
} catch (Exception e) {
s_logger.warn("Unable to stop " + vmName + " due to ", e);
return new StopAnswer(cmd, e);
}
return new StopAnswer(cmd, "Stop VM failed");
return new StopAnswer(cmd, "Stop VM failed", false);
}
private List<VDI> getVdis(Connection conn, VM vm) {

View File

@ -996,4 +996,9 @@ public class ElasticLoadBalancerManagerImpl implements
// TODO Auto-generated method stub
return false;
}
@Override
public void prepareStop(VirtualMachineProfile<DomainRouterVO> profile) {
}
}

View File

@ -428,10 +428,10 @@ public class BareMetalResourceBase implements ServerResource {
protected RebootAnswer execute(final RebootCommand cmd) {
if (!doScript(_rebootCommand)) {
return new RebootAnswer(cmd, "IPMI reboot failed");
return new RebootAnswer(cmd, "IPMI reboot failed", false);
}
return new RebootAnswer(cmd, "reboot succeeded", null, null);
return new RebootAnswer(cmd, "reboot succeeded", true);
}
protected StopAnswer execute(final StopCommand cmd) {
@ -466,7 +466,7 @@ public class BareMetalResourceBase implements ServerResource {
count++;
}
return success ? new StopAnswer(cmd, "Success", null, Long.valueOf(0), Long.valueOf(0)) : new StopAnswer(cmd, "IPMI power off failed");
return success ? new StopAnswer(cmd, "Success", null, true) : new StopAnswer(cmd, "IPMI power off failed", false);
}
protected StartAnswer execute(StartCommand cmd) {

View File

@ -353,4 +353,8 @@ public class AgentBasedConsoleProxyManager implements ConsoleProxyManager, Virtu
// TODO Auto-generated method stub
return false;
}
@Override
public void prepareStop(VirtualMachineProfile<ConsoleProxyVO> profile) {
}
}

View File

@ -2014,4 +2014,8 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx
// TODO Auto-generated method stub
return false;
}
@Override
public void prepareStop(VirtualMachineProfile<ConsoleProxyVO> profile) {
}
}

View File

@ -506,34 +506,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
txn.start();
final UserStatisticsVO userStats = _userStatsDao.lock(router.getAccountId(), router.getDataCenterIdToDeployIn(), router.getNetworkId(), null, router.getId(), router.getType().toString());
if (userStats != null) {
final RebootAnswer sa = (RebootAnswer) answer;
final Long received = sa.getBytesReceived();
long netBytes = 0;
if (received != null) {
if (received.longValue() >= userStats.getCurrentBytesReceived()) {
netBytes = received.longValue();
} else {
netBytes = userStats.getCurrentBytesReceived() + received;
}
} else {
netBytes = userStats.getCurrentBytesReceived();
}
final long currentBytesRcvd = userStats.getCurrentBytesReceived();
userStats.setCurrentBytesReceived(0);
userStats.setNetBytesReceived(userStats.getNetBytesReceived() + netBytes);
userStats.setNetBytesReceived(userStats.getNetBytesReceived() + currentBytesRcvd);
final Long sent = sa.getBytesSent();
if (sent != null) {
if (sent.longValue() >= userStats.getCurrentBytesSent()) {
netBytes = sent.longValue();
} else {
netBytes = userStats.getCurrentBytesSent() + sent;
}
} else {
netBytes = userStats.getCurrentBytesSent();
}
userStats.setNetBytesSent(userStats.getNetBytesSent() + netBytes);
final long currentBytesSent = userStats.getCurrentBytesSent();
userStats.setCurrentBytesSent(0);
userStats.setNetBytesSent(userStats.getNetBytesSent() + currentBytesSent);
_userStatsDao.update(userStats.getId(), userStats);
s_logger.debug("Successfully updated user statistics as a part of domR " + router + " reboot/stop");
} else {
@ -542,7 +521,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
txn.commit();
} catch (final Exception e) {
txn.rollback();
throw new CloudRuntimeException("Problem getting stats after reboot/stop ", e);
throw new CloudRuntimeException("Problem updating stats after reboot/stop ", e);
}
}
@ -2985,4 +2964,99 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
return false;
}
@Override
public void prepareStop(VirtualMachineProfile<DomainRouterVO> profile){
//Collect network usage before stopping Vm
VMInstanceVO vm = profile.getVirtualMachine();
DomainRouterVO router = _routerDao.findById(vm.getId());
if(router == null){
return;
}
/*String privateIP = router.getPrivateIpAddress();
if (privateIP != null) {
List<Long> routerGuestNtwkIds = _routerDao.getRouterNetworks(router.getId());
for (Long guestNtwkId : routerGuestNtwkIds) {
boolean forVpc = router.getVpcId() != null;
Network guestNtwk = _networkMgr.getNetwork(guestNtwkId);
Nic guestNic = _nicDao.findByInstanceIdAndNetworkId(guestNtwk.getId(), router.getId());
NicProfile guestNicProfile = new NicProfile(guestNic, guestNtwk, guestNic.getBroadcastUri(),
guestNic.getIsolationUri(), _networkMgr.getNetworkRate(guestNtwk.getId(), router.getId()),
_networkMgr.isSecurityGroupSupportedInNetwork(guestNtwk),
_networkMgr.getNetworkTag(router.getHypervisorType(), guestNtwk));
final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(),
forVpc, _itMgr.toNicTO(guestNicProfile, router.getHypervisorType()));
UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(),
router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString());
NetworkUsageAnswer answer = null;
try {
answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), usageCmd);
} catch (Exception e) {
s_logger.warn("Error while collecting network stats from router: "+router.getInstanceName()+" from host: "+router.getHostId(), e);
continue;
}
if (answer != null) {
if (!answer.getResult()) {
s_logger.warn("Error while collecting network stats from router: "+router.getInstanceName()+" from host: "+router.getHostId() + "; details: " + answer.getDetails());
continue;
}
Transaction txn = Transaction.open(Transaction.CLOUD_DB);
try {
if ((answer.getBytesReceived() == 0) && (answer.getBytesSent() == 0)) {
s_logger.debug("Recieved and Sent bytes are both 0. Not updating user_statistics");
continue;
}
txn.start();
UserStatisticsVO stats = _statsDao.lock(router.getAccountId(),
router.getDataCenterIdToDeployIn(), guestNtwkId, null, router.getId(), router.getType().toString());
if (stats == null) {
s_logger.warn("unable to find stats for account: " + router.getAccountId());
continue;
}
if(previousStats != null
&& ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived())
|| (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))){
s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " +
"Ignoring current answer. Router: "+answer.getRouterName()+" Rcvd: " +
answer.getBytesReceived()+ "Sent: " +answer.getBytesSent());
continue;
}
if (stats.getCurrentBytesReceived() > answer.getBytesReceived()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Received # of bytes that's less than the last one. " +
"Assuming something went wrong and persisting it. Router: " +
answer.getRouterName()+" Reported: " + answer.getBytesReceived()
+ " Stored: " + stats.getCurrentBytesReceived());
}
stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived());
}
stats.setCurrentBytesReceived(answer.getBytesReceived());
if (stats.getCurrentBytesSent() > answer.getBytesSent()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Received # of bytes that's less than the last one. " +
"Assuming something went wrong and persisting it. Router: " +
answer.getRouterName()+" Reported: " + answer.getBytesSent()
+ " Stored: " + stats.getCurrentBytesSent());
}
stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent());
}
stats.setCurrentBytesSent(answer.getBytesSent());
_statsDao.update(stats.getId(), stats);
txn.commit();
} catch (Exception e) {
txn.rollback();
s_logger.warn("Unable to update user statistics for account: " + router.getAccountId()
+ " Rx: " + answer.getBytesReceived() + "; Tx: " + answer.getBytesSent());
} finally {
txn.close();
}
}
}
}*/
}
}

View File

@ -1457,5 +1457,10 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V
Commands cmds, ReservationContext context) {
// TODO Auto-generated method stub
return false;
}
@Override
public void prepareStop(VirtualMachineProfile<SecondaryStorageVmVO> profile) {
}
}

View File

@ -3630,5 +3630,8 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
return vm;
}
@Override
public void prepareStop(VirtualMachineProfile<UserVmVO> profile) {
}
}

View File

@ -74,4 +74,11 @@ public interface VirtualMachineGuru<T extends VirtualMachine> {
* @return id if the handler works for this vm and can parse id. null if not.
*/
Long convertToId(String vmName);
/**
* Prepare Vm for Stop
* @param profile
* @return
*/
void prepareStop(VirtualMachineProfile<T> profile);
}

View File

@ -1070,11 +1070,10 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene
if (vm.getState() != State.Stopping) {
throw new CloudRuntimeException("We cannot proceed with stop VM " + vm + " since it is not in 'Stopping' state, current state: " + vm.getState());
}
String routerPrivateIp = null;
if (vm.getType() == VirtualMachine.Type.DomainRouter) {
routerPrivateIp = vm.getPrivateIpAddress();
}
StopCommand stop = new StopCommand(vm, vm.getInstanceName(), null, routerPrivateIp);
vmGuru.prepareStop(profile);
StopCommand stop = new StopCommand(vm, vm.getInstanceName(), null);
boolean stopped = false;
StopAnswer answer = null;
try {